Spaces:
Running
Running
David de la Iglesia Castro
commited on
enh(agents/openai): Reuse smolagents tools. (#14)
Browse files* enh(agents/openai): Reuse smolagents tools.
- Use wrapped DuckDuckGoSearchTool and VisitWebpageTool by default.
Generates an actually usable trace compared to openai's `WebSearchTool`.
* fix(test_unit_openai): Use `ANY` in `tools` assertion.
src/surf_spot_finder/agents/openai.py
CHANGED
@@ -47,8 +47,29 @@ def run_openai_agent(
|
|
47 |
AsyncOpenAI,
|
48 |
OpenAIChatCompletionsModel,
|
49 |
Runner,
|
50 |
-
|
51 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
if api_key_var and base_url:
|
54 |
external_client = AsyncOpenAI(
|
@@ -62,14 +83,14 @@ def run_openai_agent(
|
|
62 |
model=model_id,
|
63 |
openai_client=external_client,
|
64 |
),
|
65 |
-
tools=[
|
66 |
)
|
67 |
else:
|
68 |
agent = Agent(
|
69 |
model=model_id,
|
70 |
instructions=instructions,
|
71 |
name=name,
|
72 |
-
tools=[
|
73 |
)
|
74 |
result = Runner.run_sync(agent, prompt)
|
75 |
logger.info(result.final_output)
|
|
|
47 |
AsyncOpenAI,
|
48 |
OpenAIChatCompletionsModel,
|
49 |
Runner,
|
50 |
+
function_tool,
|
51 |
)
|
52 |
+
from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
|
53 |
+
|
54 |
+
@function_tool
|
55 |
+
def search_web(query: str) -> str:
|
56 |
+
"""Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results.
|
57 |
+
|
58 |
+
Args:
|
59 |
+
query: The search query to perform.
|
60 |
+
"""
|
61 |
+
search_tool = DuckDuckGoSearchTool()
|
62 |
+
return search_tool.forward(query)
|
63 |
+
|
64 |
+
@function_tool
|
65 |
+
def visit_webpage(url: str) -> str:
|
66 |
+
"""Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages.
|
67 |
+
|
68 |
+
Args:
|
69 |
+
url: The url of the webpage to visit.
|
70 |
+
"""
|
71 |
+
visit_tool = VisitWebpageTool()
|
72 |
+
return visit_tool.forward(url)
|
73 |
|
74 |
if api_key_var and base_url:
|
75 |
external_client = AsyncOpenAI(
|
|
|
83 |
model=model_id,
|
84 |
openai_client=external_client,
|
85 |
),
|
86 |
+
tools=[search_web, visit_webpage],
|
87 |
)
|
88 |
else:
|
89 |
agent = Agent(
|
90 |
model=model_id,
|
91 |
instructions=instructions,
|
92 |
name=name,
|
93 |
+
tools=[search_web, visit_webpage],
|
94 |
)
|
95 |
result = Runner.run_sync(agent, prompt)
|
96 |
logger.info(result.final_output)
|
tests/unit/agents/test_unit_openai.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import os
|
2 |
import pytest
|
3 |
-
from unittest.mock import patch, MagicMock
|
4 |
|
5 |
from surf_spot_finder.agents.openai import run_openai_agent
|
6 |
|
@@ -32,9 +32,7 @@ def test_run_openai_agent_default(mock_agents_module):
|
|
32 |
model="gpt-4o",
|
33 |
instructions=None,
|
34 |
name="surf-spot-finder",
|
35 |
-
tools=
|
36 |
-
mock_agents_module["WebSearchTool"].return_value,
|
37 |
-
],
|
38 |
)
|
39 |
|
40 |
|
|
|
1 |
import os
|
2 |
import pytest
|
3 |
+
from unittest.mock import patch, MagicMock, ANY
|
4 |
|
5 |
from surf_spot_finder.agents.openai import run_openai_agent
|
6 |
|
|
|
32 |
model="gpt-4o",
|
33 |
instructions=None,
|
34 |
name="surf-spot-finder",
|
35 |
+
tools=ANY,
|
|
|
|
|
36 |
)
|
37 |
|
38 |
|