Skip to content

Commit ddba8fc

Browse files
wrisazhirafovod
authored andcommitted
added tool support and modified llm accordingly
1 parent 4c2eb23 commit ddba8fc

File tree

16 files changed

+1033
-159
lines changed

16 files changed

+1033
-159
lines changed

instrumentation-genai/opentelemetry-genai-sdk/pyproject.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ classifiers = [
2525
"Programming Language :: Python :: 3.13",
2626
]
2727
dependencies = [
28-
"opentelemetry-api ~= 1.30",
29-
"opentelemetry-instrumentation ~= 0.51b0",
30-
"opentelemetry-semantic-conventions ~= 0.51b0",
31-
"opentelemetry-api>=1.31.0",
32-
"opentelemetry-sdk>=1.31.0",
28+
"opentelemetry-api ~= 1.36.0",
29+
"opentelemetry-instrumentation ~= 0.57b0",
30+
"opentelemetry-semantic-conventions ~= 0.57b0",
3331
]
3432

3533
[project.optional-dependencies]

instrumentation-genai/opentelemetry-genai-sdk/src/opentelemetry/genai/sdk/api.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
from typing import List, Optional
1818
from uuid import UUID
1919

20-
from .types import LLMInvocation
20+
from .types import LLMInvocation, ToolInvocation
2121
from .exporters import SpanMetricEventExporter, SpanMetricExporter
22-
from .data import Message, ChatGeneration, Error
22+
from .data import Message, ChatGeneration, Error, ToolOutput, ToolFunction
2323

2424
from opentelemetry.instrumentation.langchain.version import __version__
2525
from opentelemetry.metrics import get_meter
@@ -56,29 +56,52 @@ def __init__(self, exporter_type_full: bool = True, **kwargs):
5656
)
5757

5858
self._llm_registry: dict[UUID, LLMInvocation] = {}
59+
self._tool_registry: dict[UUID, ToolInvocation] = {}
5960
self._lock = Lock()
6061

61-
def start_llm(self, prompts: List[Message], run_id: UUID, parent_run_id: Optional[UUID] = None, **attributes):
62-
invocation = LLMInvocation(messages=prompts , run_id=run_id, parent_run_id=parent_run_id, attributes=attributes)
62+
def start_llm(self, prompts: List[Message], tool_functions: List[ToolFunction], run_id: UUID, parent_run_id: Optional[UUID] = None, **attributes):
63+
invocation = LLMInvocation(messages=prompts , tool_functions=tool_functions, run_id=run_id, parent_run_id=parent_run_id, attributes=attributes)
6364
with self._lock:
6465
self._llm_registry[invocation.run_id] = invocation
65-
self._exporter.init(invocation)
66+
self._exporter.init_llm(invocation)
6667

6768
def stop_llm(self, run_id: UUID, chat_generations: List[ChatGeneration], **attributes) -> LLMInvocation:
6869
with self._lock:
6970
invocation = self._llm_registry.pop(run_id)
7071
invocation.end_time = time.time()
7172
invocation.chat_generations = chat_generations
7273
invocation.attributes.update(attributes)
73-
self._exporter.export(invocation)
74+
self._exporter.export_llm(invocation)
7475
return invocation
7576

7677
def fail_llm(self, run_id: UUID, error: Error, **attributes) -> LLMInvocation:
7778
with self._lock:
7879
invocation = self._llm_registry.pop(run_id)
7980
invocation.end_time = time.time()
8081
invocation.attributes.update(**attributes)
81-
self._exporter.error(error, invocation)
82+
self._exporter.error_llm(error, invocation)
83+
return invocation
84+
85+
def start_tool(self, input_str: str, run_id: UUID, parent_run_id: Optional[UUID] = None, **attributes):
86+
invocation = ToolInvocation(input_str=input_str , run_id=run_id, parent_run_id=parent_run_id, attributes=attributes)
87+
with self._lock:
88+
self._tool_registry[invocation.run_id] = invocation
89+
self._exporter.init_tool(invocation)
90+
91+
def stop_tool(self, run_id: UUID, output: ToolOutput, **attributes) -> ToolInvocation:
92+
with self._lock:
93+
invocation = self._tool_registry.pop(run_id)
94+
invocation.end_time = time.time()
95+
invocation.output = output
96+
self._exporter.export_tool(invocation)
97+
return invocation
98+
99+
def fail_tool(self, run_id: UUID, error: Error, **attributes) -> ToolInvocation:
100+
with self._lock:
101+
invocation = self._tool_registry.pop(run_id)
102+
invocation.end_time = time.time()
103+
invocation.attributes.update(**attributes)
104+
self._exporter.error_tool(error, invocation)
82105
return invocation
83106

84107
# Singleton accessor

instrumentation-genai/opentelemetry-genai-sdk/src/opentelemetry/genai/sdk/data.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
from dataclasses import dataclass
1+
from dataclasses import dataclass, field
2+
from typing import List
23

34

5+
@dataclass
6+
class ToolOutput:
7+
tool_call_id: str
8+
content: str
9+
10+
@dataclass
11+
class ToolFunction:
12+
name: str
13+
description: str
14+
parameters: str
15+
16+
@dataclass
17+
class ToolFunctionCall:
18+
id: str
19+
name: str
20+
arguments: str
21+
type: str
22+
423
@dataclass
524
class Message:
625
content: str
726
type: str
27+
name: str
28+
tool_call_id: str
29+
tool_function_calls: List[ToolFunctionCall] = field(default_factory=list)
830

931
@dataclass
1032
class ChatGeneration:
1133
content: str
1234
type: str
1335
finish_reason: str = None
36+
tool_function_calls: List[ToolFunctionCall] = field(default_factory=list)
1437

1538
@dataclass
1639
class Error:

0 commit comments

Comments
 (0)