-
Notifications
You must be signed in to change notification settings - Fork 678
/
Copy pathgenerate_content.py
697 lines (617 loc) · 24.5 KB
/
generate_content.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import functools
import json
import logging
import os
import time
from typing import Any, AsyncIterator, Awaitable, Iterator, Optional, Union
from google.genai.models import AsyncModels, Models
from google.genai.types import (
BlockedReason,
Candidate,
Content,
ContentListUnion,
ContentListUnionDict,
ContentUnion,
ContentUnionDict,
GenerateContentConfigOrDict,
GenerateContentResponse,
)
from opentelemetry import trace
from opentelemetry.semconv._incubating.attributes import (
code_attributes,
gen_ai_attributes,
)
from opentelemetry.semconv.attributes import error_attributes
from .flags import is_content_recording_enabled
from .otel_wrapper import OTelWrapper
_logger = logging.getLogger(__name__)
# Constant used to make the absence of content more understandable.
_CONTENT_ELIDED = "<elided>"
# Constant used for the value of 'gen_ai.operation.name".
_GENERATE_CONTENT_OP_NAME = "generate_content"
class _MethodsSnapshot:
def __init__(self):
self._original_generate_content = Models.generate_content
self._original_generate_content_stream = Models.generate_content_stream
self._original_async_generate_content = AsyncModels.generate_content
self._original_async_generate_content_stream = (
AsyncModels.generate_content_stream
)
@property
def generate_content(self):
return self._original_generate_content
@property
def generate_content_stream(self):
return self._original_generate_content_stream
@property
def async_generate_content(self):
return self._original_async_generate_content
@property
def async_generate_content_stream(self):
return self._original_async_generate_content_stream
def restore(self):
Models.generate_content = self._original_generate_content
Models.generate_content_stream = self._original_generate_content_stream
AsyncModels.generate_content = self._original_async_generate_content
AsyncModels.generate_content_stream = (
self._original_async_generate_content_stream
)
def _get_vertexai_system_name():
return gen_ai_attributes.GenAiSystemValues.VERTEX_AI.name.lower()
def _get_gemini_system_name():
return gen_ai_attributes.GenAiSystemValues.GEMINI.name.lower()
def _guess_genai_system_from_env():
if os.environ.get("GOOGLE_GENAI_USE_VERTEXAI", "0").lower() in [
"true",
"1",
]:
return _get_vertexai_system_name()
return _get_gemini_system_name()
def _get_is_vertexai(models_object: Union[Models, AsyncModels]):
# Since commit 8e561de04965bb8766db87ad8eea7c57c1040442 of "googleapis/python-genai",
# it is possible to obtain the information using a documented property.
if hasattr(models_object, "vertexai"):
vertexai_attr = getattr(models_object, "vertexai")
if vertexai_attr is not None:
return vertexai_attr
# For earlier revisions, it is necessary to deeply inspect the internals.
if hasattr(models_object, "_api_client"):
client = getattr(models_object, "_api_client")
if not client:
return None
if hasattr(client, "vertexai"):
return getattr(client, "vertexai")
return None
def _determine_genai_system(models_object: Union[Models, AsyncModels]):
vertexai_attr = _get_is_vertexai(models_object)
if vertexai_attr is None:
return _guess_genai_system_from_env()
if vertexai_attr:
return _get_vertexai_system_name()
return _get_gemini_system_name()
def _get_config_property(
config: Optional[GenerateContentConfigOrDict], path: str
) -> Any:
if config is None:
return None
path_segments = path.split(".")
current_context: Any = config
for path_segment in path_segments:
if current_context is None:
return None
if isinstance(current_context, dict):
current_context = current_context.get(path_segment)
else:
current_context = getattr(current_context, path_segment)
return current_context
def _get_response_property(response: GenerateContentResponse, path: str):
path_segments = path.split(".")
current_context = response
for path_segment in path_segments:
if current_context is None:
return None
if isinstance(current_context, dict):
current_context = current_context.get(path_segment)
else:
current_context = getattr(current_context, path_segment)
return current_context
def _get_temperature(config: Optional[GenerateContentConfigOrDict]):
return _get_config_property(config, "temperature")
def _get_top_k(config: Optional[GenerateContentConfigOrDict]):
return _get_config_property(config, "top_k")
def _get_top_p(config: Optional[GenerateContentConfigOrDict]):
return _get_config_property(config, "top_p")
# A map from define attributes to the function that can obtain
# the relevant information from the request object.
#
# TODO: expand this to cover a larger set of the available
# span attributes from GenAI semantic conventions.
#
# TODO: define semantic conventions for attributes that
# are relevant for the Google GenAI SDK which are not
# currently covered by the existing semantic conventions.
#
# See also: TODOS.md
_SPAN_ATTRIBUTE_TO_CONFIG_EXTRACTOR = {
gen_ai_attributes.GEN_AI_REQUEST_TEMPERATURE: _get_temperature,
gen_ai_attributes.GEN_AI_REQUEST_TOP_K: _get_top_k,
gen_ai_attributes.GEN_AI_REQUEST_TOP_P: _get_top_p,
}
def _to_dict(value: object):
if isinstance(value, dict):
return value
if hasattr(value, "model_dump"):
return value.model_dump()
return json.loads(json.dumps(value))
class _GenerateContentInstrumentationHelper:
def __init__(
self,
models_object: Union[Models, AsyncModels],
otel_wrapper: OTelWrapper,
model: str,
):
self._start_time = time.time_ns()
self._otel_wrapper = otel_wrapper
self._genai_system = _determine_genai_system(models_object)
self._genai_request_model = model
self._finish_reasons_set = set()
self._error_type = None
self._input_tokens = 0
self._output_tokens = 0
self._content_recording_enabled = is_content_recording_enabled()
self._response_index = 0
self._candidate_index = 0
def start_span_as_current_span(
self, model_name, function_name, end_on_exit=True
):
return self._otel_wrapper.start_as_current_span(
f"{_GENERATE_CONTENT_OP_NAME} {model_name}",
start_time=self._start_time,
attributes={
code_attributes.CODE_FUNCTION_NAME: function_name,
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
},
end_on_exit=end_on_exit,
)
def process_request(
self,
contents: Union[ContentListUnion, ContentListUnionDict],
config: Optional[GenerateContentConfigOrDict],
):
span = trace.get_current_span()
for (
attribute_key,
extractor,
) in _SPAN_ATTRIBUTE_TO_CONFIG_EXTRACTOR.items():
attribute_value = extractor(config)
if attribute_value is not None:
span.set_attribute(attribute_key, attribute_value)
self._maybe_log_system_instruction(config=config)
self._maybe_log_user_prompt(contents)
def process_response(self, response: GenerateContentResponse):
# TODO: Determine if there are other response properties that
# need to be reflected back into the span attributes.
#
# See also: TODOS.md.
self._maybe_update_token_counts(response)
self._maybe_update_error_type(response)
self._maybe_log_response(response)
self._response_index += 1
def process_error(self, e: Exception):
self._error_type = str(e.__class__.__name__)
def finalize_processing(self):
span = trace.get_current_span()
span.set_attribute(
gen_ai_attributes.GEN_AI_USAGE_INPUT_TOKENS, self._input_tokens
)
span.set_attribute(
gen_ai_attributes.GEN_AI_USAGE_OUTPUT_TOKENS, self._output_tokens
)
span.set_attribute(
gen_ai_attributes.GEN_AI_RESPONSE_FINISH_REASONS,
sorted(self._finish_reasons_set),
)
self._record_token_usage_metric()
self._record_duration_metric()
def _maybe_update_token_counts(self, response: GenerateContentResponse):
input_tokens = _get_response_property(
response, "usage_metadata.prompt_token_count"
)
output_tokens = _get_response_property(
response, "usage_metadata.candidates_token_count"
)
if input_tokens and isinstance(input_tokens, int):
self._input_tokens += input_tokens
if output_tokens and isinstance(output_tokens, int):
self._output_tokens += output_tokens
def _maybe_update_error_type(self, response: GenerateContentResponse):
if response.candidates:
return
if (
(not response.prompt_feedback)
or (not response.prompt_feedback.block_reason)
or (
response.prompt_feedback.block_reason
== BlockedReason.BLOCKED_REASON_UNSPECIFIED
)
):
self._error_type = "NO_CANDIDATES"
return
# TODO: in the case where there are no candidate responses due to
# safety settings like this, it might make sense to emit an event
# that contains more details regarding the safety settings, their
# thresholds, etc. However, this requires defining an associated
# semantic convention to capture this. Follow up with SemConv to
# establish appropriate data modelling to capture these details,
# and then emit those details accordingly. (For the time being,
# we use the defined 'error.type' semantic convention to relay
# just the minimum amount of error information here).
#
# See also: "TODOS.md"
block_reason = response.prompt_feedback.block_reason.name.upper()
self._error_type = f"BLOCKED_{block_reason}"
def _maybe_log_system_instruction(
self, config: Optional[GenerateContentConfigOrDict] = None
):
system_instruction = _get_config_property(config, "system_instruction")
if not system_instruction:
return
attributes = {
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
}
# TODO: determine if "role" should be reported here or not. It is unclear
# since the caller does not supply a "role" and since this comes through
# a property named "system_instruction" which would seem to align with
# the default "role" that is allowed to be omitted by default.
#
# See also: "TODOS.md"
body = {}
if self._content_recording_enabled:
body["content"] = _to_dict(system_instruction)
else:
body["content"] = _CONTENT_ELIDED
self._otel_wrapper.log_system_prompt(
attributes=attributes,
body=body,
)
def _maybe_log_user_prompt(
self, contents: Union[ContentListUnion, ContentListUnionDict]
):
if isinstance(contents, list):
total = len(contents)
index = 0
for entry in contents:
self._maybe_log_single_user_prompt(
entry, index=index, total=total
)
index += 1
else:
self._maybe_log_single_user_prompt(contents)
def _maybe_log_single_user_prompt(
self, contents: Union[ContentUnion, ContentUnionDict], index=0, total=1
):
# TODO: figure out how to report the index in a manner that is
# aligned with the OTel semantic conventions.
attributes = {
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
}
# TODO: determine if "role" should be reported here or not and, if so,
# what the value ought to be. It is not clear whether there is always
# a role supplied (and it looks like there could be cases where there
# is more than one role present in the supplied contents)?
#
# See also: "TODOS.md"
body = {}
if self._content_recording_enabled:
logged_contents = contents
if isinstance(contents, list):
logged_contents = Content(parts=contents)
body["content"] = _to_dict(logged_contents)
else:
body["content"] = _CONTENT_ELIDED
self._otel_wrapper.log_user_prompt(
attributes=attributes,
body=body,
)
def _maybe_log_response_stats(self, response: GenerateContentResponse):
# TODO: Determine if there is a way that we can log a summary
# of the overall response in a manner that is aligned with
# Semantic Conventions. For example, it would be natural
# to report an event that looks something like:
#
# gen_ai.response.stats {
# response_index: 0,
# candidate_count: 3,
# parts_per_candidate: [
# 3,
# 1,
# 5
# ]
# }
#
pass
def _maybe_log_response_safety_ratings(
self, response: GenerateContentResponse
):
# TODO: Determine if there is a way that we can log
# the "prompt_feedback". This would be especially useful
# in the case where the response is blocked.
pass
def _maybe_log_response(self, response: GenerateContentResponse):
self._maybe_log_response_stats(response)
self._maybe_log_response_safety_ratings(response)
if not response.candidates:
return
candidate_in_response_index = 0
for candidate in response.candidates:
self._maybe_log_response_candidate(
candidate,
flat_candidate_index=self._candidate_index,
candidate_in_response_index=candidate_in_response_index,
response_index=self._response_index,
)
self._candidate_index += 1
candidate_in_response_index += 1
def _maybe_log_response_candidate(
self,
candidate: Candidate,
flat_candidate_index: int,
candidate_in_response_index: int,
response_index: int,
):
# TODO: Determine if there might be a way to report the
# response index and candidate response index.
attributes = {
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
}
# TODO: determine if "role" should be reported here or not and, if so,
# what the value ought to be.
#
# TODO: extract tool information into a separate tool message.
#
# TODO: determine if/when we need to emit a 'gen_ai.assistant.message' event.
#
# TODO: determine how to report other relevant details in the candidate that
# are not presently captured by Semantic Conventions. For example, the
# "citation_metadata", "grounding_metadata", "logprobs_result", etc.
#
# See also: "TODOS.md"
body = {
"index": flat_candidate_index,
}
if self._content_recording_enabled:
if candidate.content:
body["content"] = _to_dict(candidate.content)
else:
body["content"] = _CONTENT_ELIDED
if candidate.finish_reason is not None:
body["finish_reason"] = candidate.finish_reason.name
self._otel_wrapper.log_response_content(
attributes=attributes,
body=body,
)
def _record_token_usage_metric(self):
self._otel_wrapper.token_usage_metric.record(
self._input_tokens,
attributes={
gen_ai_attributes.GEN_AI_TOKEN_TYPE: "input",
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
},
)
self._otel_wrapper.token_usage_metric.record(
self._output_tokens,
attributes={
gen_ai_attributes.GEN_AI_TOKEN_TYPE: "output",
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
},
)
def _record_duration_metric(self):
attributes = {
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
}
if self._error_type is not None:
attributes[error_attributes.ERROR_TYPE] = self._error_type
duration_nanos = time.time_ns() - self._start_time
duration_seconds = duration_nanos / 1e9
self._otel_wrapper.operation_duration_metric.record(
duration_seconds,
attributes=attributes,
)
def _create_instrumented_generate_content(
snapshot: _MethodsSnapshot, otel_wrapper: OTelWrapper
):
wrapped_func = snapshot.generate_content
@functools.wraps(wrapped_func)
def instrumented_generate_content(
self: Models,
*,
model: str,
contents: Union[ContentListUnion, ContentListUnionDict],
config: Optional[GenerateContentConfigOrDict] = None,
**kwargs: Any,
) -> GenerateContentResponse:
helper = _GenerateContentInstrumentationHelper(
self, otel_wrapper, model
)
with helper.start_span_as_current_span(
model, "google.genai.Models.generate_content"
):
helper.process_request(contents, config)
try:
response = wrapped_func(
self,
model=model,
contents=contents,
config=config,
**kwargs,
)
helper.process_response(response)
return response
except Exception as error:
helper.process_error(error)
raise
finally:
helper.finalize_processing()
return instrumented_generate_content
def _create_instrumented_generate_content_stream(
snapshot: _MethodsSnapshot, otel_wrapper: OTelWrapper
):
wrapped_func = snapshot.generate_content_stream
@functools.wraps(wrapped_func)
def instrumented_generate_content_stream(
self: Models,
*,
model: str,
contents: Union[ContentListUnion, ContentListUnionDict],
config: Optional[GenerateContentConfigOrDict] = None,
**kwargs: Any,
) -> Iterator[GenerateContentResponse]:
helper = _GenerateContentInstrumentationHelper(
self, otel_wrapper, model
)
with helper.start_span_as_current_span(
model, "google.genai.Models.generate_content_stream"
):
helper.process_request(contents, config)
try:
for response in wrapped_func(
self,
model=model,
contents=contents,
config=config,
**kwargs,
):
helper.process_response(response)
yield response
except Exception as error:
helper.process_error(error)
raise
finally:
helper.finalize_processing()
return instrumented_generate_content_stream
def _create_instrumented_async_generate_content(
snapshot: _MethodsSnapshot, otel_wrapper: OTelWrapper
):
wrapped_func = snapshot.async_generate_content
@functools.wraps(wrapped_func)
async def instrumented_generate_content(
self: AsyncModels,
*,
model: str,
contents: Union[ContentListUnion, ContentListUnionDict],
config: Optional[GenerateContentConfigOrDict] = None,
**kwargs: Any,
) -> GenerateContentResponse:
helper = _GenerateContentInstrumentationHelper(
self, otel_wrapper, model
)
with helper.start_span_as_current_span(
model, "google.genai.AsyncModels.generate_content"
):
helper.process_request(contents, config)
try:
response = await wrapped_func(
self,
model=model,
contents=contents,
config=config,
**kwargs,
)
helper.process_response(response)
return response
except Exception as error:
helper.process_error(error)
raise
finally:
helper.finalize_processing()
return instrumented_generate_content
# Disabling type checking because this is not yet implemented and tested fully.
def _create_instrumented_async_generate_content_stream( # type: ignore
snapshot: _MethodsSnapshot, otel_wrapper: OTelWrapper
):
wrapped_func = snapshot.async_generate_content_stream
@functools.wraps(wrapped_func)
async def instrumented_generate_content_stream(
self: AsyncModels,
*,
model: str,
contents: Union[ContentListUnion, ContentListUnionDict],
config: Optional[GenerateContentConfigOrDict] = None,
**kwargs: Any,
) -> Awaitable[AsyncIterator[GenerateContentResponse]]: # type: ignore
helper = _GenerateContentInstrumentationHelper(
self, otel_wrapper, model
)
with helper.start_span_as_current_span(
model,
"google.genai.AsyncModels.generate_content_stream",
end_on_exit=False,
) as span:
helper.process_request(contents, config)
try:
response_async_generator = await wrapped_func(
self,
model=model,
contents=contents,
config=config,
**kwargs,
)
except Exception as error: # pylint: disable=broad-exception-caught
helper.process_error(error)
helper.finalize_processing()
with trace.use_span(span, end_on_exit=True):
raise
async def _response_async_generator_wrapper():
with trace.use_span(span, end_on_exit=True):
try:
async for response in response_async_generator:
helper.process_response(response)
yield response
except Exception as error:
helper.process_error(error)
raise
finally:
helper.finalize_processing()
return _response_async_generator_wrapper()
return instrumented_generate_content_stream
def uninstrument_generate_content(snapshot: object):
assert isinstance(snapshot, _MethodsSnapshot)
snapshot.restore()
def instrument_generate_content(otel_wrapper: OTelWrapper) -> object:
snapshot = _MethodsSnapshot()
Models.generate_content = _create_instrumented_generate_content(
snapshot, otel_wrapper
)
Models.generate_content_stream = (
_create_instrumented_generate_content_stream(snapshot, otel_wrapper)
)
AsyncModels.generate_content = _create_instrumented_async_generate_content(
snapshot, otel_wrapper
)
AsyncModels.generate_content_stream = (
_create_instrumented_async_generate_content_stream(
snapshot, otel_wrapper
)
)
return snapshot