Spaces:
Sleeping
Sleeping
Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, WikipediaSearchTool
|
| 2 |
+
import asyncio
|
| 3 |
+
|
| 4 |
+
class MagAgent:
|
| 5 |
+
def __init__(self):
|
| 6 |
+
"""Initialize the CodeAgent with search tools."""
|
| 7 |
+
print("Initializing CodeAgent with search tools...")
|
| 8 |
+
self.agent = CodeAgent(
|
| 9 |
+
model=OpenAIServerModel(model_name="gpt-4o-mini"), # Use a lightweight model
|
| 10 |
+
tools=[
|
| 11 |
+
DuckDuckGoSearchTool(),
|
| 12 |
+
WikipediaSearchTool()
|
| 13 |
+
]
|
| 14 |
+
)
|
| 15 |
+
print("CodeAgent initialized.")
|
| 16 |
+
|
| 17 |
+
async def __call__(self, question: str) -> str:
|
| 18 |
+
"""Process a question asynchronously using the CodeAgent."""
|
| 19 |
+
print(f"CodeAgent received question (first 50 chars): {question[:50]}...")
|
| 20 |
+
try:
|
| 21 |
+
# Run the agent asynchronously
|
| 22 |
+
response = await asyncio.to_thread(
|
| 23 |
+
self.agent.run,
|
| 24 |
+
prompt=f"Answer the following question accurately and concisely: {question}"
|
| 25 |
+
)
|
| 26 |
+
print(f"CodeAgent response: {response[:50]}...")
|
| 27 |
+
return response
|
| 28 |
+
except Exception as e:
|
| 29 |
+
error_msg = f"Error processing question: {e}"
|
| 30 |
+
print(error_msg)
|
| 31 |
+
return error_msg
|