-
Notifications
You must be signed in to change notification settings - Fork 710
VertexAI emit user, system, and assistant events #3203
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 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f39fc60
VertexAI emit user events
aabmass 23b063d
Emit system and assistant events
aabmass 1760c68
Fix for python 3.8
aabmass 8f52750
Merge branch 'main' into vertex-1-events
aabmass 75a043e
Record events regardless of span recording
aabmass 96e024f
fix tests
aabmass 1988ef3
Merge branch 'main' into vertex-1-events
aabmass feb45d1
Apply suggestions from code review
aabmass 31a3b40
Merge branch 'main' into vertex-1-events
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
91 changes: 91 additions & 0 deletions
91
...entelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/events.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,91 @@ | ||
# 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 = "assistant", | ||
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#assistant-event | ||
""" | ||
body: dict[str, AnyValue] = { | ||
"role": role, | ||
} | ||
if content is not None: | ||
body["content"] = content | ||
return Event( | ||
name="gen_ai.assistant.message", | ||
attributes={ | ||
gen_ai_attributes.GEN_AI_SYSTEM: gen_ai_attributes.GenAiSystemValues.VERTEX_AI.value, | ||
}, | ||
body=body, | ||
) | ||
|
||
|
||
def system_event( | ||
*, | ||
role: str = "system", | ||
content: AnyValue = None, | ||
) -> Event: | ||
"""Creates a User event | ||
aabmass marked this conversation as resolved.
Show resolved
Hide resolved
|
||
https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/gen-ai/gen-ai-events.md#system-event | ||
""" | ||
body: dict[str, AnyValue] = { | ||
"role": role, | ||
} | ||
if content is not None: | ||
body["content"] = content | ||
return Event( | ||
name="gen_ai.system.message", | ||
attributes={ | ||
gen_ai_attributes.GEN_AI_SYSTEM: gen_ai_attributes.GenAiSystemValues.VERTEX_AI.value, | ||
}, | ||
body=body, | ||
) |
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
94 changes: 94 additions & 0 deletions
94
...etry-instrumentation-vertexai/tests/cassettes/test_generate_content_all_input_events.yaml
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,94 @@ | ||
interactions: | ||
- request: | ||
body: |- | ||
{ | ||
"contents": [ | ||
{ | ||
"role": "user", | ||
"parts": [ | ||
{ | ||
"text": "My name is OpenTelemetry" | ||
} | ||
] | ||
}, | ||
{ | ||
"role": "model", | ||
"parts": [ | ||
{ | ||
"text": "Hello OpenTelemetry!" | ||
} | ||
] | ||
}, | ||
{ | ||
"role": "user", | ||
"parts": [ | ||
{ | ||
"text": "Address me by name and say this is a test" | ||
} | ||
] | ||
} | ||
], | ||
"systemInstruction": { | ||
"role": "user", | ||
"parts": [ | ||
{ | ||
"text": "You are a clever language model" | ||
} | ||
] | ||
} | ||
} | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '548' | ||
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": "OpenTelemetry, this is a test.\n" | ||
} | ||
] | ||
}, | ||
"finishReason": 1, | ||
"avgLogprobs": -1.1655389850299496e-06 | ||
} | ||
], | ||
"usageMetadata": { | ||
"promptTokenCount": 25, | ||
"candidatesTokenCount": 9, | ||
"totalTokenCount": 34 | ||
}, | ||
"modelVersion": "gemini-1.5-flash-002" | ||
} | ||
headers: | ||
Content-Type: | ||
- application/json; charset=UTF-8 | ||
Transfer-Encoding: | ||
- chunked | ||
Vary: | ||
- Origin | ||
- X-Origin | ||
- Referer | ||
content-length: | ||
- '422' | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
56 changes: 56 additions & 0 deletions
56
...elemetry-instrumentation-vertexai/tests/cassettes/test_generate_content_invalid_role.yaml
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,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 |
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.