File size: 2,343 Bytes
8baf998 317cd88 8baf998 317cd88 8baf998 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import os
import pathlib
import dotenv
from smolagents import CodeAgent, OpenAIServerModel
if pathlib.Path(".env").exists():
dotenv.load_dotenv(".env")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", None)
if not OPENAI_API_KEY:
exit(1)
model = OpenAIServerModel(
model_id="gpt-4o-mini",
api_key=OPENAI_API_KEY,
)
def test_simple_calculation():
from prompt import calc_agent_prompt
calc_agent = CodeAgent(
model=model,
prompt_templates=calc_agent_prompt,
additional_authorized_imports=["pandas", "numpy"],
tools=[]
)
question = "What is the result of 5 + 3 + 1294.678?"
print(f"\n=== Testing Calculation Agent ===")
print(f"Question: {question}")
print("Running agent...")
try:
result = calc_agent.run(question)
print(f"Result: {result}")
print("β
Calculation agent test passed!")
return True
except Exception as e:
print(f"β Calculation agent test failed: {e}")
return False
def test_simple_search():
from smolagents import VisitWebpageTool, WebSearchTool, WikipediaSearchTool
from prompt import web_search_agent_prompt
web_agent = CodeAgent(
model=model,
prompt_templates=web_search_agent_prompt,
additional_authorized_imports=["requests", "beautifulsoup4"],
tools=[
WebSearchTool(),
VisitWebpageTool(),
WikipediaSearchTool(),
],
)
question = "Who is the current president of France?"
print(f"\n=== Testing Web Search Agent ===")
print(f"Question: {question}")
print("Running agent...")
try:
result = web_agent.run(question)
print(f"Result: {result}")
print("β
Web search agent test passed!")
return True
except Exception as e:
print(f"β Web search agent test failed: {e}")
return False
if __name__ == "__main__":
print("Testing our updated agents with final_answer() support...")
calc_success = test_simple_calculation()
search_success = test_simple_search()
if calc_success and search_success:
print("\nπ All tests passed! Agents are working correctly with final_answer()")
else:
print("\nβ οΈ Some tests failed. Check the error messages above.")
|