Skip to content
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

feat(ollama): tool calling #2059

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import os
import json
from typing import Collection
from opentelemetry.instrumentation.ollama.config import Config
from opentelemetry.instrumentation.ollama.utils import dont_throw
Expand Down Expand Up @@ -57,6 +58,61 @@ def _set_span_attribute(span, name, value):
return


def _set_prompts(span, messages):
if not span.is_recording() or messages is None:
return
for i, msg in enumerate(messages):
prefix = f"{SpanAttributes.LLM_PROMPTS}.{i}"

_set_span_attribute(span, f"{prefix}.role", msg.get("role"))
if msg.get("content"):
content = msg.get("content")
if isinstance(content, list):
content = json.dumps(content)
_set_span_attribute(span, f"{prefix}.content", content)
if msg.get("tool_call_id"):
_set_span_attribute(span, f"{prefix}.tool_call_id", msg.get("tool_call_id"))
tool_calls = msg.get("tool_calls")
if tool_calls:
for i, tool_call in enumerate(tool_calls):
function = tool_call.get("function")
_set_span_attribute(
span,
f"{prefix}.tool_calls.{i}.id",
tool_call.get("id"),
)
_set_span_attribute(
span,
f"{prefix}.tool_calls.{i}.name",
function.get("name"),
)
_set_span_attribute(
span,
f"{prefix}.tool_calls.{i}.arguments",
function.get("arguments"),
)

if function.get("arguments"):
function["arguments"] = json.loads(function.get("arguments"))


def set_tools_attributes(span, tools):
if not tools:
return

for i, tool in enumerate(tools):
function = tool.get("function")
if not function:
continue

prefix = f"{SpanAttributes.LLM_REQUEST_FUNCTIONS}.{i}"
_set_span_attribute(span, f"{prefix}.name", function.get("name"))
_set_span_attribute(span, f"{prefix}.description", function.get("description"))
_set_span_attribute(
span, f"{prefix}.parameters", json.dumps(function.get("parameters"))
)


@dont_throw
def _set_input_attributes(span, llm_request_type, kwargs):
_set_span_attribute(span, SpanAttributes.LLM_REQUEST_MODEL, kwargs.get("model"))
Expand All @@ -78,6 +134,9 @@ def _set_input_attributes(span, llm_request_type, kwargs):
f"{SpanAttributes.LLM_PROMPTS}.{index}.role",
message.get("role"),
)
_set_prompts(span, kwargs.get("messages"))
if kwargs.get("tools"):
set_tools_attributes(span, kwargs.get("tools"))
else:
_set_span_attribute(span, f"{SpanAttributes.LLM_PROMPTS}.0.role", "user")
_set_span_attribute(
Expand Down
Loading