mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-27 03:34:10 +00:00
fix - otel set tools attribute
This commit is contained in:
parent
638c4acbb2
commit
21ac0efaae
1 changed files with 38 additions and 11 deletions
|
@ -244,6 +244,31 @@ class OpenTelemetry(CustomLogger):
|
||||||
self.set_attributes(span, kwargs, response_obj)
|
self.set_attributes(span, kwargs, response_obj)
|
||||||
span.end(end_time=self._to_ns(end_time))
|
span.end(end_time=self._to_ns(end_time))
|
||||||
|
|
||||||
|
def set_tools_attributes(self, span: Span, tools):
|
||||||
|
from opentelemetry.semconv.ai import SpanAttributes
|
||||||
|
import json
|
||||||
|
|
||||||
|
if not tools:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
for i, tool in enumerate(tools):
|
||||||
|
function = tool.get("function")
|
||||||
|
if not function:
|
||||||
|
continue
|
||||||
|
|
||||||
|
prefix = f"{SpanAttributes.LLM_REQUEST_FUNCTIONS}.{i}"
|
||||||
|
span.set_attribute(f"{prefix}.name", function.get("name"))
|
||||||
|
span.set_attribute(f"{prefix}.description", function.get("description"))
|
||||||
|
span.set_attribute(
|
||||||
|
f"{prefix}.parameters", json.dumps(function.get("parameters"))
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
verbose_logger.error(
|
||||||
|
"OpenTelemetry: Error setting tools attributes: %s", str(e)
|
||||||
|
)
|
||||||
|
pass
|
||||||
|
|
||||||
def set_attributes(self, span: Span, kwargs, response_obj):
|
def set_attributes(self, span: Span, kwargs, response_obj):
|
||||||
from opentelemetry.semconv.ai import SpanAttributes
|
from opentelemetry.semconv.ai import SpanAttributes
|
||||||
|
|
||||||
|
@ -291,11 +316,8 @@ class OpenTelemetry(CustomLogger):
|
||||||
)
|
)
|
||||||
|
|
||||||
if optional_params.get("tools"):
|
if optional_params.get("tools"):
|
||||||
# cast to str - since OTEL only accepts string values
|
tools = optional_params["tools"]
|
||||||
_tools = str(optional_params.get("tools"))
|
self.set_tools_attributes(span, tools)
|
||||||
span.set_attribute(
|
|
||||||
SpanAttributes.LLM_REQUEST_FUNCTIONS, optional_params.get("tools")
|
|
||||||
)
|
|
||||||
|
|
||||||
if optional_params.get("user"):
|
if optional_params.get("user"):
|
||||||
span.set_attribute(SpanAttributes.LLM_USER, optional_params.get("user"))
|
span.set_attribute(SpanAttributes.LLM_USER, optional_params.get("user"))
|
||||||
|
@ -341,13 +363,18 @@ class OpenTelemetry(CustomLogger):
|
||||||
choice.get("message").get("content"),
|
choice.get("message").get("content"),
|
||||||
)
|
)
|
||||||
|
|
||||||
if choice.get("message").get("tool_calls"):
|
message = choice.get("message")
|
||||||
_tool_calls = choice.get("message").get("tool_calls")
|
if not isinstance(message, dict):
|
||||||
if not isinstance(_tool_calls, str):
|
message = message.dict()
|
||||||
_tool_calls = str(_tool_calls)
|
tool_calls = message.get("tool_calls")
|
||||||
|
if tool_calls:
|
||||||
span.set_attribute(
|
span.set_attribute(
|
||||||
f"{SpanAttributes.LLM_COMPLETIONS}.{idx}.tool_calls",
|
f"{SpanAttributes.LLM_COMPLETIONS}.{idx}.function_call.name",
|
||||||
_tool_calls,
|
tool_calls[0].get("function").get("name"),
|
||||||
|
)
|
||||||
|
span.set_attribute(
|
||||||
|
f"{SpanAttributes.LLM_COMPLETIONS}.{idx}.function_call.arguments",
|
||||||
|
tool_calls[0].get("function").get("arguments"),
|
||||||
)
|
)
|
||||||
|
|
||||||
# The unique identifier for the completion.
|
# The unique identifier for the completion.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue