Update agent.py
Browse files
agent.py
CHANGED
@@ -7,22 +7,14 @@ from llama_index.core.agent.react import ReActAgent
|
|
7 |
from llama_index.core.tools import FunctionTool
|
8 |
from duckduckgo_search import DDGS
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
}
|
17 |
-
def __call__(self, query: str) -> str:
|
18 |
-
try:
|
19 |
-
with DDGS() as ddg:
|
20 |
-
results = ddg.text(query=query, region="wt-wt", max_results=3)
|
21 |
-
return "\n".join(r.get('body','') for r in results if r.get('body'))
|
22 |
-
except Exception as e:
|
23 |
-
return f"ERROR: {e}"
|
24 |
|
25 |
-
# --- Tools ---
|
26 |
def eval_python_code(code: str) -> str:
|
27 |
try:
|
28 |
return str(eval(code, {"__builtins__": {}}))
|
@@ -40,7 +32,6 @@ def format_gaia_answer(answer: str, question: str = "") -> str:
|
|
40 |
ans = ans[1:-1]
|
41 |
if not re.match(r'^[A-Za-z]+\.$', ans):
|
42 |
ans = re.sub(r'\.$', '', ans)
|
43 |
-
|
44 |
if question:
|
45 |
if re.search(r'how many|number of|at bats|total sales|albums|output.*python|highest number', question, re.I):
|
46 |
m = re.search(r'(\$?\d[\d,\.]*)', ans)
|
@@ -74,11 +65,10 @@ def format_gaia_answer(answer: str, question: str = "") -> str:
|
|
74 |
return ', '.join(items)
|
75 |
return ans.strip().rstrip('.')
|
76 |
|
77 |
-
#
|
78 |
-
llm = OpenAI(model="gpt-4o", api_key=os.getenv("OPENAI_API_KEY"))
|
79 |
|
80 |
tools = [
|
81 |
-
FunctionTool.from_defaults(
|
82 |
FunctionTool.from_defaults(eval_python_code, name="python_eval", description="Evaluate Python code"),
|
83 |
FunctionTool.from_defaults(format_gaia_answer, name="format_gaia_answer", description="GAIA output formatting")
|
84 |
]
|
|
|
7 |
from llama_index.core.tools import FunctionTool
|
8 |
from duckduckgo_search import DDGS
|
9 |
|
10 |
+
def duckduckgo_search(query: str) -> str:
|
11 |
+
try:
|
12 |
+
with DDGS() as ddg:
|
13 |
+
results = ddg.text(query=query, region="wt-wt", max_results=3)
|
14 |
+
return "\n".join(r.get('body', '') for r in results if r.get('body'))
|
15 |
+
except Exception as e:
|
16 |
+
return f"ERROR: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
18 |
def eval_python_code(code: str) -> str:
|
19 |
try:
|
20 |
return str(eval(code, {"__builtins__": {}}))
|
|
|
32 |
ans = ans[1:-1]
|
33 |
if not re.match(r'^[A-Za-z]+\.$', ans):
|
34 |
ans = re.sub(r'\.$', '', ans)
|
|
|
35 |
if question:
|
36 |
if re.search(r'how many|number of|at bats|total sales|albums|output.*python|highest number', question, re.I):
|
37 |
m = re.search(r'(\$?\d[\d,\.]*)', ans)
|
|
|
65 |
return ', '.join(items)
|
66 |
return ans.strip().rstrip('.')
|
67 |
|
68 |
+
llm = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # no model= param
|
|
|
69 |
|
70 |
tools = [
|
71 |
+
FunctionTool.from_defaults(duckduckgo_search, name="duckduckgo_search", description="Web search tool"),
|
72 |
FunctionTool.from_defaults(eval_python_code, name="python_eval", description="Evaluate Python code"),
|
73 |
FunctionTool.from_defaults(format_gaia_answer, name="format_gaia_answer", description="GAIA output formatting")
|
74 |
]
|