File size: 1,237 Bytes
9e0ec52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, WikipediaSearchTool
import asyncio

class MagAgent:
    def __init__(self):
        """Initialize the CodeAgent with search tools."""
        print("Initializing CodeAgent with search tools...")
        self.agent = CodeAgent(
            model=OpenAIServerModel(model_name="gpt-4o-mini"),  # Use a lightweight model
            tools=[
                DuckDuckGoSearchTool(),
                WikipediaSearchTool()
            ]
        )
        print("CodeAgent initialized.")

    async def __call__(self, question: str) -> str:
        """Process a question asynchronously using the CodeAgent."""
        print(f"CodeAgent received question (first 50 chars): {question[:50]}...")
        try:
            # Run the agent asynchronously
            response = await asyncio.to_thread(
                self.agent.run,
                prompt=f"Answer the following question accurately and concisely: {question}"
            )
            print(f"CodeAgent response: {response[:50]}...")
            return response
        except Exception as e:
            error_msg = f"Error processing question: {e}"
            print(error_msg)
            return error_msg