Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, LiteLLMModel # HfApiModel, OpenAIServerModel
|
|
|
2 |
import asyncio
|
3 |
import os
|
|
|
4 |
|
5 |
class MagAgent:
|
6 |
def __init__(self):
|
@@ -16,11 +18,16 @@ class MagAgent:
|
|
16 |
# model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/",
|
17 |
# custom_role_conversions=None,
|
18 |
)
|
|
|
|
|
|
|
|
|
19 |
self.agent = CodeAgent(
|
20 |
model= model, # OpenAIServerModel(model_id="gpt-4o"),
|
21 |
tools=[
|
22 |
DuckDuckGoSearchTool(),
|
23 |
-
WikipediaSearchTool()
|
|
|
24 |
]
|
25 |
)
|
26 |
print("MagAgent initialized.")
|
@@ -29,14 +36,22 @@ class MagAgent:
|
|
29 |
"""Process a question asynchronously using the MagAgent."""
|
30 |
print(f"MagAgent received question (first 50 chars): {question[:50]}...")
|
31 |
try:
|
32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
33 |
response = await asyncio.to_thread(
|
34 |
self.agent.run,
|
35 |
-
task=
|
36 |
)
|
|
|
|
|
|
|
37 |
print(f"MagAgent response: {response[:50]}...")
|
38 |
return response
|
39 |
except Exception as e:
|
40 |
-
error_msg = f"Error processing question: {e}"
|
41 |
print(error_msg)
|
42 |
return error_msg
|
|
|
1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, LiteLLMModel # HfApiModel, OpenAIServerModel
|
2 |
+
from tools.final_answer import FinalAnswerTool
|
3 |
import asyncio
|
4 |
import os
|
5 |
+
import yaml
|
6 |
|
7 |
class MagAgent:
|
8 |
def __init__(self):
|
|
|
18 |
# model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/",
|
19 |
# custom_role_conversions=None,
|
20 |
)
|
21 |
+
# Load prompt templates
|
22 |
+
with open("prompts.yaml", 'r') as stream:
|
23 |
+
prompt_templates = yaml.safe_load(stream)
|
24 |
+
|
25 |
self.agent = CodeAgent(
|
26 |
model= model, # OpenAIServerModel(model_id="gpt-4o"),
|
27 |
tools=[
|
28 |
DuckDuckGoSearchTool(),
|
29 |
+
WikipediaSearchTool(),
|
30 |
+
FinalAnswerToolnswer()
|
31 |
]
|
32 |
)
|
33 |
print("MagAgent initialized.")
|
|
|
36 |
"""Process a question asynchronously using the MagAgent."""
|
37 |
print(f"MagAgent received question (first 50 chars): {question[:50]}...")
|
38 |
try:
|
39 |
+
# Define a task with fallback search logic
|
40 |
+
task = (
|
41 |
+
f"Answer the following question accurately and concisely: {question}\n"
|
42 |
+
"First, try searching Wikipedia with 'Mercedes Sosa'. If that fails, "
|
43 |
+
"use DuckDuckGo to search 'Mercedes Sosa discography 2000-2009'."
|
44 |
+
)
|
45 |
response = await asyncio.to_thread(
|
46 |
self.agent.run,
|
47 |
+
task=task
|
48 |
)
|
49 |
+
if not response or "No Wikipedia page found" in response:
|
50 |
+
# Fallback response if search fails
|
51 |
+
response = "Unable to retrieve exact data. Please refine the question or check external sources."
|
52 |
print(f"MagAgent response: {response[:50]}...")
|
53 |
return response
|
54 |
except Exception as e:
|
55 |
+
error_msg = f"Error processing question: {str(e)}. Check API key or network connectivity."
|
56 |
print(error_msg)
|
57 |
return error_msg
|