forked from open-telemetry/opentelemetry-python-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
GenAI Utils Inference #2
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
Draft
keith-decker
wants to merge
9
commits into
main
Choose a base branch
from
util-genai-inference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
00c2091
WIP initial code import
keith-decker 40e6c48
remove references to tool types
keith-decker 43526d6
add a simple unit test
keith-decker 76dbd57
rename exporter to emitter.
keith-decker 567e0a4
rename api file to client
keith-decker fd41dfb
Merge branch 'main' into util-genai-inference
keith-decker 4bd72aa
WIP gen_ai chat refactor
keith-decker 59414fa
Add provider.name, rename client to handler
keith-decker 5127c39
add message to log functions
keith-decker 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
160 changes: 160 additions & 0 deletions
160
util/opentelemetry-util-genai/src/opentelemetry/util/genai/client.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,160 @@ | ||
# 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. | ||
|
||
import time | ||
from threading import Lock | ||
from typing import List, Optional | ||
from uuid import UUID | ||
|
||
from opentelemetry._events import get_event_logger | ||
from opentelemetry.metrics import get_meter | ||
from opentelemetry.semconv.schemas import Schemas | ||
from opentelemetry.trace import get_tracer | ||
|
||
from .data import ChatGeneration, Error, Message | ||
from .emitters import SpanMetricEmitter, SpanMetricEventEmitter | ||
from .types import LLMInvocation | ||
|
||
# TODO: Get the tool version for emitting spans, use GenAI Utils for now | ||
from .version import __version__ | ||
|
||
|
||
class TelemetryClient: | ||
""" | ||
High-level client managing GenAI invocation lifecycles and emitting | ||
them as spans, metrics, and events. | ||
""" | ||
|
||
def __init__(self, emitter_type_full: bool = True, **kwargs): | ||
tracer_provider = kwargs.get("tracer_provider") | ||
self._tracer = get_tracer( | ||
__name__, | ||
__version__, | ||
tracer_provider, | ||
schema_url=Schemas.V1_28_0.value, | ||
) | ||
|
||
meter_provider = kwargs.get("meter_provider") | ||
self._meter = get_meter( | ||
__name__, | ||
__version__, | ||
meter_provider, | ||
schema_url=Schemas.V1_28_0.value, | ||
keith-decker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
|
||
event_logger_provider = kwargs.get("event_logger_provider") | ||
self._event_logger = get_event_logger( | ||
__name__, | ||
__version__, | ||
event_logger_provider=event_logger_provider, | ||
schema_url=Schemas.V1_28_0.value, | ||
) | ||
|
||
self._emitter = ( | ||
SpanMetricEventEmitter( | ||
tracer=self._tracer, | ||
meter=self._meter, | ||
event_logger=self._event_logger, | ||
) | ||
if emitter_type_full | ||
else SpanMetricEmitter(tracer=self._tracer, meter=self._meter) | ||
) | ||
|
||
self._llm_registry: dict[UUID, LLMInvocation] = {} | ||
self._lock = Lock() | ||
|
||
def start_llm( | ||
self, | ||
prompts: List[Message], | ||
run_id: UUID, | ||
parent_run_id: Optional[UUID] = None, | ||
**attributes, | ||
): | ||
invocation = LLMInvocation( | ||
messages=prompts, | ||
run_id=run_id, | ||
parent_run_id=parent_run_id, | ||
attributes=attributes, | ||
) | ||
with self._lock: | ||
self._llm_registry[invocation.run_id] = invocation | ||
self._emitter.init(invocation) | ||
|
||
def stop_llm( | ||
self, | ||
run_id: UUID, | ||
chat_generations: List[ChatGeneration], | ||
**attributes, | ||
) -> LLMInvocation: | ||
with self._lock: | ||
invocation = self._llm_registry.pop(run_id) | ||
invocation.end_time = time.time() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this isn't thread safe all operations should be within the lock. The same for emitter.emit and same for emitter.init |
||
invocation.chat_generations = chat_generations | ||
invocation.attributes.update(attributes) | ||
self._emitter.emit(invocation) | ||
return invocation | ||
|
||
def fail_llm( | ||
self, run_id: UUID, error: Error, **attributes | ||
) -> LLMInvocation: | ||
with self._lock: | ||
invocation = self._llm_registry.pop(run_id) | ||
invocation.end_time = time.time() | ||
invocation.attributes.update(**attributes) | ||
self._emitter.error(error, invocation) | ||
return invocation | ||
|
||
|
||
# Singleton accessor | ||
_default_client: TelemetryClient | None = None | ||
keith-decker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
|
||
def get_telemetry_client( | ||
emitter_type_full: bool = True, **kwargs | ||
) -> TelemetryClient: | ||
global _default_client | ||
if _default_client is None: | ||
_default_client = TelemetryClient( | ||
emitter_type_full=emitter_type_full, **kwargs | ||
) | ||
return _default_client | ||
|
||
|
||
# Module‐level convenience functions | ||
def llm_start( | ||
prompts: List[Message], | ||
run_id: UUID, | ||
parent_run_id: Optional[UUID] = None, | ||
**attributes, | ||
): | ||
return get_telemetry_client().start_llm( | ||
prompts=prompts, | ||
run_id=run_id, | ||
parent_run_id=parent_run_id, | ||
**attributes, | ||
) | ||
|
||
|
||
def llm_stop( | ||
run_id: UUID, chat_generations: List[ChatGeneration], **attributes | ||
) -> LLMInvocation: | ||
return get_telemetry_client().stop_llm( | ||
run_id=run_id, chat_generations=chat_generations, **attributes | ||
) | ||
|
||
|
||
def llm_fail(run_id: UUID, error: Error, **attributes) -> LLMInvocation: | ||
return get_telemetry_client().fail_llm( | ||
run_id=run_id, error=error, **attributes | ||
) |
21 changes: 21 additions & 0 deletions
21
util/opentelemetry-util-genai/src/opentelemetry/util/genai/data.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,21 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Message: | ||
content: str | ||
type: str | ||
name: str | ||
|
||
|
||
@dataclass | ||
class ChatGeneration: | ||
content: str | ||
type: str | ||
finish_reason: str = None | ||
|
||
|
||
@dataclass | ||
class Error: | ||
message: str | ||
type: type[BaseException] |
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.