Download test_agent.py from Hemimoon/Agents-Course-Final-Assignment: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/Hemimoon/Agents-Course-Final-Assignment/resolve/main/test_agent.py
- Command line
-
hf download hf://spaces/Hemimoon/Agents-Course-Final-Assignment/test_agent.py
-
curl -L -o test_agent.py https://huggingface.co/spaces/Hemimoon/Agents-Course-Final-Assignment/resolve/main/test_agent.py
2.34 kB
| 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.") | |