|
17 | 17 | from typing import List, Optional |
18 | 18 | from uuid import UUID |
19 | 19 |
|
20 | | -from .types import LLMInvocation |
| 20 | +from .types import LLMInvocation, ToolInvocation |
21 | 21 | from .exporters import SpanMetricEventExporter, SpanMetricExporter |
22 | | -from .data import Message, ChatGeneration, Error |
| 22 | +from .data import Message, ChatGeneration, Error, ToolOutput, ToolFunction |
23 | 23 |
|
24 | 24 | from opentelemetry.instrumentation.langchain.version import __version__ |
25 | 25 | from opentelemetry.metrics import get_meter |
@@ -56,29 +56,52 @@ def __init__(self, exporter_type_full: bool = True, **kwargs): |
56 | 56 | ) |
57 | 57 |
|
58 | 58 | self._llm_registry: dict[UUID, LLMInvocation] = {} |
| 59 | + self._tool_registry: dict[UUID, ToolInvocation] = {} |
59 | 60 | self._lock = Lock() |
60 | 61 |
|
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) |
63 | 64 | with self._lock: |
64 | 65 | self._llm_registry[invocation.run_id] = invocation |
65 | | - self._exporter.init(invocation) |
| 66 | + self._exporter.init_llm(invocation) |
66 | 67 |
|
67 | 68 | def stop_llm(self, run_id: UUID, chat_generations: List[ChatGeneration], **attributes) -> LLMInvocation: |
68 | 69 | with self._lock: |
69 | 70 | invocation = self._llm_registry.pop(run_id) |
70 | 71 | invocation.end_time = time.time() |
71 | 72 | invocation.chat_generations = chat_generations |
72 | 73 | invocation.attributes.update(attributes) |
73 | | - self._exporter.export(invocation) |
| 74 | + self._exporter.export_llm(invocation) |
74 | 75 | return invocation |
75 | 76 |
|
76 | 77 | def fail_llm(self, run_id: UUID, error: Error, **attributes) -> LLMInvocation: |
77 | 78 | with self._lock: |
78 | 79 | invocation = self._llm_registry.pop(run_id) |
79 | 80 | invocation.end_time = time.time() |
80 | 81 | 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) |
82 | 105 | return invocation |
83 | 106 |
|
84 | 107 | # Singleton accessor |
|
0 commit comments