-
Notifications
You must be signed in to change notification settings - Fork 798
Added Vertex AI spans for request parameters #3192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
816212b
Added Vertex AI spans for request parameters
aabmass 5dfc282
small fixes, get CI passing
aabmass 302c2a4
Use standard OTel tracing error handling
aabmass 66ed1de
move nested util
aabmass 3512ca3
Actually use GAPIC client since thats what we use under the hood
aabmass dd57389
Comment out seed for now
aabmass fb0b4ae
Merge branch 'main' into vertex-0-impl
aabmass c84c532
Remove unnecessary dict.get() calls
aabmass 874098f
Typing improvements to check that we support both v1 and v1beta1
aabmass 5bed336
Add more teest cases for error conditions and fix span name bug
aabmass baed486
Merge branch 'main' into vertex-0-impl
aabmass 18e8fc2
fix typing
aabmass 043b47f
Merge branch 'main' into vertex-0-impl
aabmass 28d6ffd
Add todos for error.type
aabmass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...pentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# 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. | ||
|
||
from __future__ import annotations | ||
|
||
import re | ||
from dataclasses import dataclass | ||
from os import environ | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Mapping, | ||
Sequence, | ||
) | ||
|
||
from opentelemetry.semconv._incubating.attributes import ( | ||
gen_ai_attributes as GenAIAttributes, | ||
) | ||
from opentelemetry.util.types import AttributeValue | ||
|
||
if TYPE_CHECKING: | ||
from google.cloud.aiplatform_v1.types import content, tool | ||
from google.cloud.aiplatform_v1beta1.types import ( | ||
content as content_v1beta1, | ||
) | ||
from google.cloud.aiplatform_v1beta1.types import ( | ||
tool as tool_v1beta1, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class GenerateContentParams: | ||
model: str | ||
contents: ( | ||
Sequence[content.Content] | Sequence[content_v1beta1.Content] | None | ||
) = None | ||
system_instruction: content.Content | content_v1beta1.Content | None = None | ||
tools: Sequence[tool.Tool] | Sequence[tool_v1beta1.Tool] | None = None | ||
tool_config: tool.ToolConfig | tool_v1beta1.ToolConfig | None = None | ||
labels: Mapping[str, str] | None = None | ||
safety_settings: ( | ||
Sequence[content.SafetySetting] | ||
| Sequence[content_v1beta1.SafetySetting] | ||
| None | ||
) = None | ||
generation_config: ( | ||
content.GenerationConfig | content_v1beta1.GenerationConfig | None | ||
) = None | ||
|
||
|
||
def get_genai_request_attributes( | ||
params: GenerateContentParams, | ||
operation_name: GenAIAttributes.GenAiOperationNameValues = GenAIAttributes.GenAiOperationNameValues.CHAT, | ||
): | ||
model = _get_model_name(params.model) | ||
generation_config = params.generation_config | ||
attributes: dict[str, AttributeValue] = { | ||
aabmass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GenAIAttributes.GEN_AI_OPERATION_NAME: operation_name.value, | ||
GenAIAttributes.GEN_AI_SYSTEM: GenAIAttributes.GenAiSystemValues.VERTEX_AI.value, | ||
GenAIAttributes.GEN_AI_REQUEST_MODEL: model, | ||
} | ||
|
||
if not generation_config: | ||
return attributes | ||
|
||
# Check for optional fields | ||
# https://proto-plus-python.readthedocs.io/en/stable/fields.html#optional-fields | ||
if "temperature" in generation_config: | ||
attributes[GenAIAttributes.GEN_AI_REQUEST_TEMPERATURE] = ( | ||
generation_config.temperature | ||
) | ||
if "top_p" in generation_config: | ||
attributes[GenAIAttributes.GEN_AI_REQUEST_TOP_P] = ( | ||
generation_config.top_p | ||
) | ||
if "max_output_tokens" in generation_config: | ||
attributes[GenAIAttributes.GEN_AI_REQUEST_MAX_TOKENS] = ( | ||
generation_config.max_output_tokens | ||
) | ||
if "presence_penalty" in generation_config: | ||
attributes[GenAIAttributes.GEN_AI_REQUEST_PRESENCE_PENALTY] = ( | ||
generation_config.presence_penalty | ||
) | ||
if "frequency_penalty" in generation_config: | ||
attributes[GenAIAttributes.GEN_AI_REQUEST_FREQUENCY_PENALTY] = ( | ||
generation_config.frequency_penalty | ||
) | ||
# Uncomment once GEN_AI_REQUEST_SEED is released in 1.30 | ||
# https://github.com/open-telemetry/semantic-conventions/pull/1710 | ||
# if "seed" in generation_config: | ||
# attributes[GenAIAttributes.GEN_AI_REQUEST_SEED] = ( | ||
aabmass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# generation_config.seed | ||
# ) | ||
if "stop_sequences" in generation_config: | ||
attributes[GenAIAttributes.GEN_AI_REQUEST_STOP_SEQUENCES] = ( | ||
generation_config.stop_sequences | ||
) | ||
|
||
return attributes | ||
|
||
|
||
_MODEL_STRIP_RE = re.compile( | ||
r"^projects/(.*)/locations/(.*)/publishers/google/models/" | ||
) | ||
|
||
|
||
def _get_model_name(model: str) -> str: | ||
return _MODEL_STRIP_RE.sub("", model) | ||
|
||
|
||
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = ( | ||
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT" | ||
) | ||
|
||
|
||
def is_content_enabled() -> bool: | ||
capture_content = environ.get( | ||
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false" | ||
) | ||
|
||
return capture_content.lower() == "true" | ||
|
||
|
||
def get_span_name(span_attributes: Mapping[str, AttributeValue]): | ||
name = span_attributes[GenAIAttributes.GEN_AI_OPERATION_NAME] | ||
model = span_attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL] | ||
return f"{name} {model}" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.