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

fix(smolagents): use correct smolagents tool schema extraction function #1236

Merged
merged 3 commits into from
Jan 29, 2025
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 @@ -34,7 +34,7 @@ dependencies = [

[project.optional-dependencies]
instruments = [
"smolagents>=1.2.2",
"smolagents>=1.6.0",
]
test = [
"smolagents>=1.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,25 +228,16 @@ def _llm_invocation_parameters(


def _llm_tools(tools_to_call_from: list[Any]) -> Iterator[Tuple[str, Any]]:
from smolagents import Tool, __version__

major_version_string, minor_version_string, *_ = __version__.split(".")
major_version = int(major_version_string)
minor_version = int(minor_version_string)
if (major_version, minor_version) >= (1, 5):
from smolagents._function_type_hints_utils import ( # type: ignore[import-untyped]
get_json_schema,
)
else:
from smolagents.models import get_json_schema # type: ignore[import-untyped]
from smolagents import Tool
from smolagents.models import get_tool_json_schema # type: ignore[import-untyped]

if not isinstance(tools_to_call_from, list):
return
for tool_index, tool in enumerate(tools_to_call_from):
if isinstance(tool, Tool):
yield (
f"{LLM_TOOLS}.{tool_index}.{TOOL_JSON_SCHEMA}",
safe_json_dumps(get_json_schema(tool)),
safe_json_dumps(get_tool_json_schema(tool)),
)


Expand Down Expand Up @@ -297,7 +288,6 @@ def __call__(
span.set_attribute(
LLM_TOKEN_COUNT_TOTAL, model.last_input_token_count + model.last_output_token_count
)
span.set_attribute(OUTPUT_VALUE, output_message)
span.set_attributes(dict(_llm_output_messages(output_message)))
span.set_attributes(dict(_llm_tools(arguments.get("tools_to_call_from", []))))
span.set_attributes(dict(_output_value_and_mime_type(output_message)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Any, Generator, Optional

import pytest
from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall
from opentelemetry import trace as trace_api
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
Expand All @@ -15,6 +14,11 @@
ManagedAgent,
ToolCallingAgent,
)
from smolagents.models import ( # type: ignore[import-untyped]
ChatMessage,
ChatMessageToolCall,
ChatMessageToolCallDefinition,
)

from openinference.instrumentation.smolagents import SmolagentsInstrumentor
from openinference.semconv.trace import (
Expand Down Expand Up @@ -137,7 +141,7 @@ def test_openai_server_model_has_expected_attributes(
assert isinstance(json.loads(output_value), dict)
assert attributes.pop(LLM_MODEL_NAME) == "gpt-4o"
assert isinstance(inv_params := attributes.pop(LLM_INVOCATION_PARAMETERS), str)
assert json.loads(inv_params) == {}
assert json.loads(inv_params) == {"max_tokens": 4096}
assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_ROLE}") == "user"
assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_CONTENT}") == input_message_content
assert isinstance(attributes.pop(LLM_TOKEN_COUNT_PROMPT), int)
Expand Down Expand Up @@ -191,10 +195,9 @@ def forward(self, location: str) -> str:
assert output_message_content is None
tool_calls = output_message.tool_calls
assert len(tool_calls) == 1
assert isinstance(tool_call := tool_calls[0], ChatCompletionMessageToolCall)
assert isinstance(tool_call := tool_calls[0], ChatMessageToolCall)
assert tool_call.function.name == "get_weather"
assert isinstance(tool_call_arguments := tool_call.function.arguments, str)
assert json.loads(tool_call_arguments) == {"location": "Paris"}
assert tool_call.function.arguments == {"location": "Paris"}

spans = in_memory_span_exporter.get_finished_spans()
assert len(spans) == 1
Expand All @@ -212,7 +215,7 @@ def forward(self, location: str) -> str:
assert isinstance(json.loads(output_value), dict)
assert attributes.pop(LLM_MODEL_NAME) == "gpt-4o"
assert isinstance(inv_params := attributes.pop(LLM_INVOCATION_PARAMETERS), str)
assert json.loads(inv_params) == {}
assert json.loads(inv_params) == {"max_tokens": 4096}
assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_ROLE}") == "user"
assert attributes.pop(f"{LLM_INPUT_MESSAGES}.0.{MESSAGE_CONTENT}") == input_message_content
assert isinstance(
Expand Down Expand Up @@ -262,12 +265,6 @@ def forward(self, location: str) -> str:
class TestRun:
@pytest.mark.xfail
def test_multiagents(self) -> None:
from smolagents.models import ( # type: ignore[import-untyped]
ChatMessage,
ChatMessageToolCall,
ChatMessageToolCallDefinition,
)

class FakeModelMultiagentsManagerAgent:
def __call__(
self,
Expand Down
Loading