|
| 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 | +""" |
| 16 | +Factories for event types described in |
| 17 | +https://github.com/open-telemetry/semantic-conventions/blob/main/docs/gen-ai/gen-ai-events.md#system-event. |
| 18 | +
|
| 19 | +Hopefully this code can be autogenerated by Weaver once Gen AI semantic conventions are |
| 20 | +schematized in YAML and the Weaver tool supports it. |
| 21 | +""" |
| 22 | + |
| 23 | +from opentelemetry._events import Event |
| 24 | +from opentelemetry.semconv._incubating.attributes import gen_ai_attributes |
| 25 | +from opentelemetry.util.types import AnyValue |
| 26 | + |
| 27 | + |
| 28 | +def user_event( |
| 29 | + *, |
| 30 | + role: str = "user", |
| 31 | + content: AnyValue = None, |
| 32 | +) -> Event: |
| 33 | + """Creates a User event |
| 34 | + https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/gen-ai/gen-ai-events.md#user-event |
| 35 | + """ |
| 36 | + body: dict[str, AnyValue] = { |
| 37 | + "role": role, |
| 38 | + } |
| 39 | + if content is not None: |
| 40 | + body["content"] = content |
| 41 | + return Event( |
| 42 | + name="gen_ai.user.message", |
| 43 | + attributes={ |
| 44 | + gen_ai_attributes.GEN_AI_SYSTEM: gen_ai_attributes.GenAiSystemValues.VERTEX_AI.value, |
| 45 | + }, |
| 46 | + body=body, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def assistant_event( |
| 51 | + *, |
| 52 | + role: str = "assistant", |
| 53 | + content: AnyValue = None, |
| 54 | +) -> Event: |
| 55 | + """Creates an Assistant event |
| 56 | + https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/gen-ai/gen-ai-events.md#assistant-event |
| 57 | + """ |
| 58 | + body: dict[str, AnyValue] = { |
| 59 | + "role": role, |
| 60 | + } |
| 61 | + if content is not None: |
| 62 | + body["content"] = content |
| 63 | + return Event( |
| 64 | + name="gen_ai.assistant.message", |
| 65 | + attributes={ |
| 66 | + gen_ai_attributes.GEN_AI_SYSTEM: gen_ai_attributes.GenAiSystemValues.VERTEX_AI.value, |
| 67 | + }, |
| 68 | + body=body, |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def system_event( |
| 73 | + *, |
| 74 | + role: str = "system", |
| 75 | + content: AnyValue = None, |
| 76 | +) -> Event: |
| 77 | + """Creates a System event |
| 78 | + https://github.com/open-telemetry/semantic-conventions/blob/v1.28.0/docs/gen-ai/gen-ai-events.md#system-event |
| 79 | + """ |
| 80 | + body: dict[str, AnyValue] = { |
| 81 | + "role": role, |
| 82 | + } |
| 83 | + if content is not None: |
| 84 | + body["content"] = content |
| 85 | + return Event( |
| 86 | + name="gen_ai.system.message", |
| 87 | + attributes={ |
| 88 | + gen_ai_attributes.GEN_AI_SYSTEM: gen_ai_attributes.GenAiSystemValues.VERTEX_AI.value, |
| 89 | + }, |
| 90 | + body=body, |
| 91 | + ) |
0 commit comments