Skip to content

Commit 3ee24a8

Browse files
authored
feat(tinyagent): Updates to use AnyLLM class API. (#900)
* feat(tinyagent): Updates to use AnyLLM class API. Allows to reuse the clients which is specially important when interacting with any-llm.ai to reduce the overhead of re-authenticating (which involves solving challenge, etc) * tests: Add missing `mock_verify_api_key`.
1 parent 480fc0c commit 3ee24a8

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/any_agent/frameworks/tinyagent.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import json
66
from typing import TYPE_CHECKING, Any
77

8-
from any_llm import AnyLLM, LLMProvider, acompletion
8+
from any_llm import AnyLLM, LLMProvider
99
from mcp.types import CallToolResult, TextContent
1010

1111
from any_agent.config import AgentConfig, AgentFramework
@@ -95,24 +95,28 @@ def __init__(self, config: AgentConfig) -> None:
9595
super().__init__(config)
9696
self.clients: dict[str, ToolExecutor] = {}
9797

98-
self.completion_params = {
99-
"model": self.config.model_id,
100-
"tools": [],
101-
"tool_choice": "required",
102-
**(self.config.model_args or {}),
103-
}
104-
10598
provider_name, model_id = AnyLLM.split_model_provider(self.config.model_id)
10699
if provider_name == "gateway":
107100
provider_name, model_id = AnyLLM.split_model_provider(model_id)
108101
self.uses_openai = provider_name == LLMProvider.OPENAI
109-
if not self.uses_openai and self.completion_params["tool_choice"] == "required":
110-
self.config.tools.append(final_answer)
111102

103+
# Create the LLM instance using the AnyLLM class pattern
104+
llm_kwargs: dict[str, Any] = {}
112105
if self.config.api_key:
113-
self.completion_params["api_key"] = self.config.api_key
106+
llm_kwargs["api_key"] = self.config.api_key
114107
if self.config.api_base:
115-
self.completion_params["api_base"] = self.config.api_base
108+
llm_kwargs["api_base"] = self.config.api_base
109+
self.llm = AnyLLM.create(provider_name, **llm_kwargs)
110+
111+
self.completion_params: dict[str, Any] = {
112+
"model": model_id,
113+
"tools": [],
114+
"tool_choice": "required",
115+
**(self.config.model_args or {}),
116+
}
117+
118+
if not self.uses_openai and self.completion_params["tool_choice"] == "required":
119+
self.config.tools.append(final_answer)
116120

117121
async def _load_agent(self) -> None:
118122
"""Load the agent and its tools."""
@@ -288,7 +292,7 @@ async def _return_output_type(
288292
)
289293

290294
async def call_model(self, **completion_params: dict[str, Any]) -> ChatCompletion:
291-
return await acompletion(**completion_params) # type: ignore[return-value, arg-type]
295+
return await self.llm.acompletion(**completion_params) # type: ignore[return-value, arg-type]
292296

293297
async def update_output_type_async(
294298
self, output_type: type[BaseModel] | None

src/any_agent/testing/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
LLM_IMPORT_PATHS = {
1515
AgentFramework.GOOGLE: "any_agent.frameworks.google.acompletion",
1616
AgentFramework.LANGCHAIN: "any_agent.frameworks.langchain.acompletion",
17-
AgentFramework.TINYAGENT: "any_agent.frameworks.tinyagent.acompletion",
17+
AgentFramework.TINYAGENT: "any_llm.AnyLLM.acompletion",
1818
AgentFramework.AGNO: "any_agent.frameworks.agno.acompletion",
1919
AgentFramework.OPENAI: "any_llm.AnyLLM.acompletion",
2020
AgentFramework.SMOLAGENTS: "any_llm.completion",

tests/unit/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import os
22
from collections.abc import Generator
3+
from unittest.mock import patch
34

45
import pytest
56

67
from any_agent.config import AgentFramework
78

89

10+
@pytest.fixture(autouse=True)
11+
def mock_verify_api_key() -> Generator[None, None, None]:
12+
"""Mock AnyLLM._verify_and_set_api_key to skip API key validation in unit tests."""
13+
with patch("any_llm.AnyLLM._verify_and_set_api_key"):
14+
yield
15+
16+
917
@pytest.fixture(autouse=True)
1018
def mock_api_keys_for_unit_tests(
1119
request: pytest.FixtureRequest,

0 commit comments

Comments
 (0)