Update agent.py
Browse files
agent.py
CHANGED
@@ -10,7 +10,7 @@ from llama_index.llms.openai import OpenAI
|
|
10 |
from llama_index.core.agent import FunctionCallingAgent
|
11 |
from llama_index.core.tools import FunctionTool
|
12 |
|
13 |
-
#
|
14 |
|
15 |
def reverse_sentence(sentence: str) -> str:
|
16 |
"""Reverse a sentence character by character."""
|
@@ -33,7 +33,11 @@ def convert_table_if_detected(question: str, file_context: str) -> str:
|
|
33 |
"""If question contains a table about * on set S, try parsing non-commutative set."""
|
34 |
if "* on the set" in question and file_context:
|
35 |
try:
|
36 |
-
table_lines = [
|
|
|
|
|
|
|
|
|
37 |
headers = re.split(r'\|+', table_lines[0])[1:-1]
|
38 |
data_rows = [re.split(r'\|+', row)[1:-1] for row in table_lines[1:]]
|
39 |
index = [row[0] for row in data_rows]
|
@@ -68,10 +72,10 @@ def extract_excel_total_food_sales(file_path: str) -> str:
|
|
68 |
total += float(amount)
|
69 |
return f"${total:.2f}"
|
70 |
|
71 |
-
#
|
72 |
llm = OpenAI(model="gpt-4o")
|
73 |
|
74 |
-
#
|
75 |
tools = [
|
76 |
FunctionTool.from_defaults(fn=reverse_sentence),
|
77 |
FunctionTool.from_defaults(fn=extract_vegetables_from_list),
|
@@ -82,16 +86,15 @@ agent = FunctionCallingAgent.from_tools(
|
|
82 |
tools=tools,
|
83 |
llm=llm,
|
84 |
system_prompt=(
|
85 |
-
"You are
|
86 |
-
"You
|
87 |
-
"
|
88 |
-
"
|
89 |
-
"Respond with the answer only, without explanation."
|
90 |
),
|
91 |
-
verbose=True
|
92 |
)
|
93 |
|
94 |
-
#
|
95 |
def answer_question(question: str, task_id: str = None, file_content: str = "") -> str:
|
96 |
file_context = file_content or ""
|
97 |
file_context = convert_table_if_detected(question, file_context)
|
|
|
10 |
from llama_index.core.agent import FunctionCallingAgent
|
11 |
from llama_index.core.tools import FunctionTool
|
12 |
|
13 |
+
# === TOOL FUNCTIONS ===
|
14 |
|
15 |
def reverse_sentence(sentence: str) -> str:
|
16 |
"""Reverse a sentence character by character."""
|
|
|
33 |
"""If question contains a table about * on set S, try parsing non-commutative set."""
|
34 |
if "* on the set" in question and file_context:
|
35 |
try:
|
36 |
+
table_lines = [
|
37 |
+
line.strip()
|
38 |
+
for line in file_context.splitlines()
|
39 |
+
if '|' in line and '*' not in line[:2]
|
40 |
+
]
|
41 |
headers = re.split(r'\|+', table_lines[0])[1:-1]
|
42 |
data_rows = [re.split(r'\|+', row)[1:-1] for row in table_lines[1:]]
|
43 |
index = [row[0] for row in data_rows]
|
|
|
72 |
total += float(amount)
|
73 |
return f"${total:.2f}"
|
74 |
|
75 |
+
# === LLM SETUP ===
|
76 |
llm = OpenAI(model="gpt-4o")
|
77 |
|
78 |
+
# === TOOLS ===
|
79 |
tools = [
|
80 |
FunctionTool.from_defaults(fn=reverse_sentence),
|
81 |
FunctionTool.from_defaults(fn=extract_vegetables_from_list),
|
|
|
86 |
tools=tools,
|
87 |
llm=llm,
|
88 |
system_prompt=(
|
89 |
+
"You are a strict and factual research agent solving GAIA benchmark questions. "
|
90 |
+
"You must answer precisely, based only on available information. "
|
91 |
+
"Never hallucinate, and always return concise, well-formatted answers. "
|
92 |
+
"Use tools where necessary, and return plain text only — no extra explanation."
|
|
|
93 |
),
|
94 |
+
verbose=True
|
95 |
)
|
96 |
|
97 |
+
# === MAIN AGENT CALL ===
|
98 |
def answer_question(question: str, task_id: str = None, file_content: str = "") -> str:
|
99 |
file_context = file_content or ""
|
100 |
file_context = convert_table_if_detected(question, file_context)
|