|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +from dataclasses import dataclass |
| 18 | +from os import environ |
| 19 | +from typing import ( |
| 20 | + TYPE_CHECKING, |
| 21 | + Dict, |
| 22 | + List, |
| 23 | + Mapping, |
| 24 | + Optional, |
| 25 | + TypedDict, |
| 26 | + cast, |
| 27 | +) |
| 28 | + |
| 29 | +from opentelemetry.semconv._incubating.attributes import ( |
| 30 | + gen_ai_attributes as GenAIAttributes, |
| 31 | +) |
| 32 | +from opentelemetry.semconv.attributes import ( |
| 33 | + error_attributes as ErrorAttributes, |
| 34 | +) |
| 35 | +from opentelemetry.trace import Span |
| 36 | +from opentelemetry.trace.status import Status, StatusCode |
| 37 | +from opentelemetry.util.types import AttributeValue |
| 38 | + |
| 39 | +if TYPE_CHECKING: |
| 40 | + from vertexai.generative_models import Tool, ToolConfig |
| 41 | + from vertexai.generative_models._generative_models import ( |
| 42 | + ContentsType, |
| 43 | + GenerationConfigType, |
| 44 | + SafetySettingsType, |
| 45 | + _GenerativeModel, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@dataclass(frozen=True) |
| 50 | +class GenerateContentParams: |
| 51 | + contents: ContentsType |
| 52 | + generation_config: Optional[GenerationConfigType] |
| 53 | + safety_settings: Optional[SafetySettingsType] |
| 54 | + tools: Optional[List["Tool"]] |
| 55 | + tool_config: Optional["ToolConfig"] |
| 56 | + labels: Optional[Dict[str, str]] |
| 57 | + stream: bool |
| 58 | + |
| 59 | + |
| 60 | +class GenerationConfigDict(TypedDict, total=False): |
| 61 | + temperature: Optional[float] |
| 62 | + top_p: Optional[float] |
| 63 | + top_k: Optional[int] |
| 64 | + max_output_tokens: Optional[int] |
| 65 | + stop_sequences: Optional[List[str]] |
| 66 | + presence_penalty: Optional[float] |
| 67 | + frequency_penalty: Optional[float] |
| 68 | + seed: Optional[int] |
| 69 | + # And more fields which aren't needed yet |
| 70 | + |
| 71 | + |
| 72 | +def get_genai_request_attributes( |
| 73 | + # TODO: use types |
| 74 | + instance: _GenerativeModel, |
| 75 | + params: GenerateContentParams, |
| 76 | + operation_name: GenAIAttributes.GenAiOperationNameValues = GenAIAttributes.GenAiOperationNameValues.CHAT, |
| 77 | +): |
| 78 | + model = _get_model_name(instance) |
| 79 | + generation_config = _get_generation_config(instance, params) |
| 80 | + attributes = { |
| 81 | + GenAIAttributes.GEN_AI_OPERATION_NAME: operation_name.value, |
| 82 | + GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.VERTEX_AI.value, |
| 83 | + GenAIAttributes.GEN_AI_REQUEST_MODEL: model, |
| 84 | + GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE: generation_config.get( |
| 85 | + "temperature" |
| 86 | + ), |
| 87 | + GenAIAttributes.GEN_AI_REQUEST_TOP_P: generation_config.get("top_p"), |
| 88 | + GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS: generation_config.get( |
| 89 | + "max_output_tokens" |
| 90 | + ), |
| 91 | + GenAIAttributes.GEN_AI_REQUEST_PRESENCE_PENALTY: generation_config.get( |
| 92 | + "presence_penalty" |
| 93 | + ), |
| 94 | + GenAIAttributes.GEN_AI_REQUEST_FREQUENCY_PENALTY: generation_config.get( |
| 95 | + "frequency_penalty" |
| 96 | + ), |
| 97 | + GenAIAttributes.GEN_AI_OPENAI_REQUEST_SEED: generation_config.get( |
| 98 | + "seed" |
| 99 | + ), |
| 100 | + GenAIAttributes.GEN_AI_REQUEST_STOP_SEQUENCES: generation_config.get( |
| 101 | + "stop_sequences" |
| 102 | + ), |
| 103 | + } |
| 104 | + |
| 105 | + # filter out None values |
| 106 | + return {k: v for k, v in attributes.items() if v is not None} |
| 107 | + |
| 108 | + |
| 109 | +def _get_generation_config( |
| 110 | + instance: _GenerativeModel, |
| 111 | + params: GenerateContentParams, |
| 112 | +) -> GenerationConfigDict: |
| 113 | + generation_config = params.generation_config or instance._generation_config |
| 114 | + if generation_config is None: |
| 115 | + return {} |
| 116 | + if isinstance(generation_config, dict): |
| 117 | + return cast(GenerationConfigDict, generation_config) |
| 118 | + return cast(GenerationConfigDict, generation_config.to_dict()) |
| 119 | + |
| 120 | + |
| 121 | +_RESOURCE_PREFIX = "publishers/google/models/" |
| 122 | + |
| 123 | + |
| 124 | +def _get_model_name(instance: _GenerativeModel) -> str: |
| 125 | + model_name = instance._model_name |
| 126 | + |
| 127 | + # Can use str.removeprefix() once 3.8 is dropped |
| 128 | + if model_name.startswith(_RESOURCE_PREFIX): |
| 129 | + model_name = model_name[len(_RESOURCE_PREFIX) :] |
| 130 | + return model_name |
| 131 | + |
| 132 | + |
| 133 | +# TODO: Everything below here should be replaced with |
| 134 | +# opentelemetry.instrumentation.genai_utils instead once it is released. |
| 135 | +# https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3191 |
| 136 | + |
| 137 | +OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = ( |
| 138 | + "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT" |
| 139 | +) |
| 140 | + |
| 141 | + |
| 142 | +def is_content_enabled() -> bool: |
| 143 | + capture_content = environ.get( |
| 144 | + OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false" |
| 145 | + ) |
| 146 | + |
| 147 | + return capture_content.lower() == "true" |
| 148 | + |
| 149 | + |
| 150 | +def get_span_name(span_attributes: Mapping[str, AttributeValue]): |
| 151 | + name = span_attributes.get(GenAIAttributes.GEN_AI_OPERATION_NAME, "") |
| 152 | + model = span_attributes.get(GenAIAttributes.GEN_AI_REQUEST_MODEL, "") |
| 153 | + return f"{name} {model}" |
| 154 | + |
| 155 | + |
| 156 | +def handle_span_exception(span: Span, error: Exception): |
| 157 | + span.set_status(Status(StatusCode.ERROR, str(error))) |
| 158 | + if span.is_recording(): |
| 159 | + span.set_attribute( |
| 160 | + ErrorAttributes.ERROR_TYPE, type(error).__qualname__ |
| 161 | + ) |
| 162 | + span.end() |
0 commit comments