forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
315 additions
and
8 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...entelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/events.py
This file contains 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,69 @@ | ||
# 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. | ||
|
||
""" | ||
Factories for event types described in | ||
https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-events.md#system-event. | ||
Hopefully this code can be autogenerated by Weaver once Gen AI semantic conventions are | ||
schematized in YAML and the Weaver tool supports it. | ||
""" | ||
|
||
from opentelemetry._events import Event | ||
from opentelemetry.semconv._incubating.attributes import gen_ai_attributes | ||
from opentelemetry.util.types import AnyValue | ||
|
||
|
||
def user_event( | ||
*, | ||
role: str = "user", | ||
content: AnyValue = None, | ||
) -> Event: | ||
"""Creates a User event | ||
https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/gen-ai/gen-ai-events.md#user-event | ||
""" | ||
body: dict[str, AnyValue] = { | ||
"role": role, | ||
} | ||
if content is not None: | ||
body["content"] = content | ||
return Event( | ||
name="gen_ai.user.message", | ||
attributes={ | ||
gen_ai_attributes.GEN_AI_SYSTEM: gen_ai_attributes.GenAiSystemValues.VERTEX_AI.value, | ||
}, | ||
body=body, | ||
) | ||
|
||
|
||
def assistant_event( | ||
*, | ||
role: str = "model", | ||
content: AnyValue = None, | ||
) -> Event: | ||
"""Creates an Assistant event | ||
https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/gen-ai/gen-ai-events.md#assistant-event | ||
""" | ||
# TODO: add tool_calls once instrumentation supports it | ||
|
||
return Event( | ||
name="gen_ai.assistant.message", | ||
attributes={ | ||
gen_ai_attributes.GEN_AI_SYSTEM: gen_ai_attributes.GenAiSystemValues.VERTEX_AI.value, | ||
}, | ||
body={ | ||
"role": role, | ||
"content": content, | ||
}, | ||
) |
This file contains 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 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
56 changes: 56 additions & 0 deletions
56
...elemetry-instrumentation-vertexai/tests/cassettes/test_generate_content_invalid_role.yaml
This file contains 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,56 @@ | ||
interactions: | ||
- request: | ||
body: |- | ||
{ | ||
"contents": [ | ||
{ | ||
"role": "invalid_role", | ||
"parts": [ | ||
{ | ||
"text": "Say this is a test" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '149' | ||
Content-Type: | ||
- application/json | ||
User-Agent: | ||
- python-requests/2.32.3 | ||
method: POST | ||
uri: https://us-central1-aiplatform.googleapis.com/v1/projects/fake-project/locations/us-central1/publishers/google/models/gemini-1.5-flash-002:generateContent?%24alt=json%3Benum-encoding%3Dint | ||
response: | ||
body: | ||
string: |- | ||
{ | ||
"error": { | ||
"code": 400, | ||
"message": "Please use a valid role: user, model.", | ||
"status": "INVALID_ARGUMENT", | ||
"details": [] | ||
} | ||
} | ||
headers: | ||
Content-Type: | ||
- application/json; charset=UTF-8 | ||
Transfer-Encoding: | ||
- chunked | ||
Vary: | ||
- Origin | ||
- X-Origin | ||
- Referer | ||
content-length: | ||
- '416' | ||
status: | ||
code: 400 | ||
message: Bad Request | ||
version: 1 |
70 changes: 70 additions & 0 deletions
70
...emetry-instrumentation-vertexai/tests/cassettes/test_generate_content_without_events.yaml
This file contains 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,70 @@ | ||
interactions: | ||
- request: | ||
body: |- | ||
{ | ||
"contents": [ | ||
{ | ||
"role": "user", | ||
"parts": [ | ||
{ | ||
"text": "Say this is a test" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '141' | ||
Content-Type: | ||
- application/json | ||
User-Agent: | ||
- python-requests/2.32.3 | ||
method: POST | ||
uri: https://us-central1-aiplatform.googleapis.com/v1/projects/fake-project/locations/us-central1/publishers/google/models/gemini-1.5-flash-002:generateContent?%24alt=json%3Benum-encoding%3Dint | ||
response: | ||
body: | ||
string: |- | ||
{ | ||
"candidates": [ | ||
{ | ||
"content": { | ||
"role": "model", | ||
"parts": [ | ||
{ | ||
"text": "Okay, I understand. I'm ready for your test. Please proceed.\n" | ||
} | ||
] | ||
}, | ||
"finishReason": 1, | ||
"avgLogprobs": -0.005519990466142956 | ||
} | ||
], | ||
"usageMetadata": { | ||
"promptTokenCount": 5, | ||
"candidatesTokenCount": 19, | ||
"totalTokenCount": 24 | ||
}, | ||
"modelVersion": "gemini-1.5-flash-002" | ||
} | ||
headers: | ||
Content-Type: | ||
- application/json; charset=UTF-8 | ||
Transfer-Encoding: | ||
- chunked | ||
Vary: | ||
- Origin | ||
- X-Origin | ||
- Referer | ||
content-length: | ||
- '453' | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
Oops, something went wrong.