Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -31,7 +31,7 @@ class InfoState(TypedDict):
|
|
| 31 |
def get_wiki_relate(state: InfoState) -> InfoState:
|
| 32 |
"""
|
| 33 |
Tool to Get the wikipedia info from keywords extracted from preprocessing at main_parts.
|
| 34 |
-
|
| 35 |
Uses: Wikipedia API
|
| 36 |
Returns: tool_answer (summary)
|
| 37 |
"""
|
|
@@ -85,17 +85,17 @@ def get_code(state:InfoState) -> InfoState:
|
|
| 85 |
response = model.invoke([HumanMessage(content=prompt)]).content.strip()
|
| 86 |
|
| 87 |
state["answer_code"] = response
|
| 88 |
-
|
| 89 |
|
| 90 |
return state
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
-
def preprocess_text(state: dict) -> InfoState:
|
| 95 |
-
|
| 96 |
"""
|
| 97 |
Preprocess text to get the keywords to help get results directly from wikipedia.
|
| 98 |
-
|
| 99 |
Input: raw question
|
| 100 |
Output: main_parts (list of keywords)
|
| 101 |
"""
|
|
@@ -150,7 +150,7 @@ def preprocess_text(state: dict) -> InfoState:
|
|
| 150 |
def get_answer(state: InfoState) -> InfoState :
|
| 151 |
"""
|
| 152 |
Final Node that returns the final answer organized.
|
| 153 |
-
|
| 154 |
Combines: tool_answer β final_answer
|
| 155 |
"""
|
| 156 |
print("Getting Answer (Gemini)...")
|
|
@@ -165,7 +165,9 @@ def get_answer(state: InfoState) -> InfoState :
|
|
| 165 |
"- Focus on producing one short factual answer.\n"
|
| 166 |
"- Do not include tool names, prefixes, or metadata.\n"
|
| 167 |
"- If the context contains partial hints, you can infer the answer from general knowledge of the same topic.\n"
|
| 168 |
-
"- If
|
|
|
|
|
|
|
| 169 |
"Final Answer:"
|
| 170 |
)
|
| 171 |
|
|
@@ -179,28 +181,22 @@ def get_type(state: InfoState) -> InfoState:
|
|
| 179 |
print("Getting Type (Gemini)...")
|
| 180 |
|
| 181 |
prompt = f"""
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
- WIKI β if the question is informative, factual, or science-related.
|
| 185 |
-
- WebInfo β if it requires up-to-date or current information (like news or dates).
|
| 186 |
-
- MATH β if it involves math, time, or numeric calculation.
|
| 187 |
-
- LLM β if it includes a link or anything else.
|
| 188 |
-
|
| 189 |
-
Question: "{state['question']}"
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
|
| 195 |
-
|
| 196 |
-
Input: "What is the capital of France?"
|
| 197 |
-
Output: WIKI
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
-
Now respond with one word only.
|
| 203 |
-
"""
|
| 204 |
model = ChatGoogleGenerativeAI(model="gemini-2.5-flash")
|
| 205 |
state["answer_type"] = (model.invoke([HumanMessage(content=prompt)]).content)
|
| 206 |
|
|
@@ -260,7 +256,7 @@ def get_graph():
|
|
| 260 |
)
|
| 261 |
|
| 262 |
# Add final edges
|
| 263 |
-
graph.add_edge("get_search_results", "get_answer")
|
| 264 |
#graph.add_edge("get_wiki_relate", "get_answer")
|
| 265 |
|
| 266 |
graph.add_edge("get_code", "execute_code")
|
|
@@ -279,4 +275,4 @@ def ask(compiled_graph,question):
|
|
| 279 |
|
| 280 |
})
|
| 281 |
|
| 282 |
-
return legitimate_result['final_answer']
|
|
|
|
| 31 |
def get_wiki_relate(state: InfoState) -> InfoState:
|
| 32 |
"""
|
| 33 |
Tool to Get the wikipedia info from keywords extracted from preprocessing at main_parts.
|
| 34 |
+
|
| 35 |
Uses: Wikipedia API
|
| 36 |
Returns: tool_answer (summary)
|
| 37 |
"""
|
|
|
|
| 85 |
response = model.invoke([HumanMessage(content=prompt)]).content.strip()
|
| 86 |
|
| 87 |
state["answer_code"] = response
|
| 88 |
+
|
| 89 |
|
| 90 |
return state
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
+
def preprocess_text(state: dict) -> InfoState:
|
| 95 |
+
|
| 96 |
"""
|
| 97 |
Preprocess text to get the keywords to help get results directly from wikipedia.
|
| 98 |
+
|
| 99 |
Input: raw question
|
| 100 |
Output: main_parts (list of keywords)
|
| 101 |
"""
|
|
|
|
| 150 |
def get_answer(state: InfoState) -> InfoState :
|
| 151 |
"""
|
| 152 |
Final Node that returns the final answer organized.
|
| 153 |
+
|
| 154 |
Combines: tool_answer β final_answer
|
| 155 |
"""
|
| 156 |
print("Getting Answer (Gemini)...")
|
|
|
|
| 165 |
"- Focus on producing one short factual answer.\n"
|
| 166 |
"- Do not include tool names, prefixes, or metadata.\n"
|
| 167 |
"- If the context contains partial hints, you can infer the answer from general knowledge of the same topic.\n"
|
| 168 |
+
"- If the question asks about an attached file or audio, reply briefly that you cannot access attachments or audio files."
|
| 169 |
+
"- If absolutely nothing is relevant, reply: I'm not sure because the question depends on a file or missing data.\n\n"
|
| 170 |
+
"- Final answer should be complete text not part of answer"
|
| 171 |
"Final Answer:"
|
| 172 |
)
|
| 173 |
|
|
|
|
| 181 |
print("Getting Type (Gemini)...")
|
| 182 |
|
| 183 |
prompt = f"""
|
| 184 |
+
You are a strict classifier.
|
| 185 |
+
Your job is to classify the following question into ONE of four categories:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
- WIKI β informative, factual, or science question
|
| 188 |
+
- WebInfo β up-to-date, news, or current event question
|
| 189 |
+
- MATH β math, numeric, date, or time calculation
|
| 190 |
+
- LLM β all others, including links, reasoning, and file-related tasks
|
| 191 |
|
| 192 |
+
Question: "{state['question']}"
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
Rules:
|
| 195 |
+
- Reply with exactly one of these words: WIKI, WebInfo, MATH, or LLM
|
| 196 |
+
- Output nothing else. No punctuation, no quotes, no explanation.
|
| 197 |
+
- If unsure, default to LLM.
|
| 198 |
+
"""
|
| 199 |
|
|
|
|
|
|
|
| 200 |
model = ChatGoogleGenerativeAI(model="gemini-2.5-flash")
|
| 201 |
state["answer_type"] = (model.invoke([HumanMessage(content=prompt)]).content)
|
| 202 |
|
|
|
|
| 256 |
)
|
| 257 |
|
| 258 |
# Add final edges
|
| 259 |
+
graph.add_edge("get_search_results", "get_answer")
|
| 260 |
#graph.add_edge("get_wiki_relate", "get_answer")
|
| 261 |
|
| 262 |
graph.add_edge("get_code", "execute_code")
|
|
|
|
| 275 |
|
| 276 |
})
|
| 277 |
|
| 278 |
+
return legitimate_result['final_answer'],legitimate_result
|