Test_Magus / agent.py
SergeyO7's picture
Update agent.py
38d1986 verified
raw
history blame
1.2 kB
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, WikipediaSearchTool
import asyncio
class MagAgent:
def __init__(self):
"""Initialize the MagAgent with search tools."""
print("Initializing MagAgent with search tools...")
self.agent = CodeAgent(
model=OpenAIServerModel(model_id="gpt-4o-mini"),
tools=[
DuckDuckGoSearchTool(),
WikipediaSearchTool()
]
)
print("MagAgent initialized.")
async def __call__(self, question: str) -> str:
"""Process a question asynchronously using the MagAgent."""
print(f"MagAgent received question (first 50 chars): {question[:50]}...")
try:
# Run the agent asynchronously
response = await asyncio.to_thread(
self.agent.run,
query=f"Answer the following question accurately and concisely: {question}"
)
print(f"MagAgent response: {response[:50]}...")
return response
except Exception as e:
error_msg = f"Error processing question: {e}"
print(error_msg)
return error_msg