Spaces:
Sleeping
Sleeping
graph test
Browse files
app.py
CHANGED
|
@@ -64,6 +64,7 @@ Last sentence should of your response shoule either:
|
|
| 64 |
- start **EXACTLY** with `FINAL ANSWER: ` followed by the answer.
|
| 65 |
- OR start **EXACTLY** with `TOOL: ` followed by the tool name and the request to the tool, example `TOOL: DuckDuckGoSearchAgent(request='search query')`
|
| 66 |
|
|
|
|
| 67 |
"""
|
| 68 |
|
| 69 |
|
|
@@ -75,6 +76,7 @@ class AnswerState(TypedDict):
|
|
| 75 |
is_final_answer: bool
|
| 76 |
search_request: Optional[str]
|
| 77 |
wiki_request: Optional[str]
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
class BasicAgent:
|
|
@@ -85,6 +87,7 @@ class BasicAgent:
|
|
| 85 |
self.graph.add_node("invoke_model", self.invoke_model)
|
| 86 |
self.graph.add_node("web_search", self.web_search)
|
| 87 |
self.graph.add_node("wiki_search", self.wiki_search)
|
|
|
|
| 88 |
self.graph.add_node("decide", self.decide)
|
| 89 |
self.graph.add_node("final_answer", self.final_answer)
|
| 90 |
self.graph.add_node("exceeded_attempts", self.exceeded_attempts)
|
|
@@ -93,6 +96,7 @@ class BasicAgent:
|
|
| 93 |
self.graph.add_edge("log_question", "invoke_model")
|
| 94 |
self.graph.add_edge("web_search", "invoke_model")
|
| 95 |
self.graph.add_edge("wiki_search", "invoke_model")
|
|
|
|
| 96 |
|
| 97 |
# Add conditional edges
|
| 98 |
self.graph.add_conditional_edges(
|
|
@@ -103,6 +107,7 @@ class BasicAgent:
|
|
| 103 |
"EXCEEDED_ATTEMPTS": "exceeded_attempts",
|
| 104 |
"WEB_SEARCH": "web_search",
|
| 105 |
"WIKI_SEARCH": "wiki_search",
|
|
|
|
| 106 |
},
|
| 107 |
)
|
| 108 |
|
|
@@ -122,6 +127,7 @@ class BasicAgent:
|
|
| 122 |
"is_final_answer": False,
|
| 123 |
"search_request": None,
|
| 124 |
"wiki_request": None,
|
|
|
|
| 125 |
}
|
| 126 |
)
|
| 127 |
return res.get("answer", "N/A")
|
|
@@ -175,6 +181,31 @@ class BasicAgent:
|
|
| 175 |
)
|
| 176 |
return {"messages": state["messages"]}
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
def web_search(self, state: AnswerState) -> Dict[str, Any]:
|
| 179 |
print("[web_search] Searching for: " + str(state["search_request"]))
|
| 180 |
search_tool = GoogleSearchAPIWrapper()
|
|
@@ -223,12 +254,16 @@ class BasicAgent:
|
|
| 223 |
return "WEB_SEARCH"
|
| 224 |
elif state["wiki_request"]:
|
| 225 |
return "WIKI_SEARCH"
|
|
|
|
|
|
|
| 226 |
return "FINAL_ANSWER"
|
| 227 |
|
|
|
|
| 228 |
def invoke_model(self, state: AnswerState) -> Dict[str, Any]:
|
| 229 |
print("[invoke_model] Agent trying to answer")
|
| 230 |
state["search_request"] = None
|
| 231 |
state["wiki_request"] = None
|
|
|
|
| 232 |
|
| 233 |
response = self.model.invoke(state["messages"])
|
| 234 |
print(f"Agent response: {response.content}")
|
|
@@ -259,6 +294,13 @@ class BasicAgent:
|
|
| 259 |
request = request_full.split("request=")[-1].strip("'")
|
| 260 |
print(f"Wiki request: {request}")
|
| 261 |
state["wiki_request"] = request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
return {
|
| 263 |
"messages": state["messages"],
|
| 264 |
"attempt": state.get("attempt", 0) + 1,
|
|
|
|
| 64 |
- start **EXACTLY** with `FINAL ANSWER: ` followed by the answer.
|
| 65 |
- OR start **EXACTLY** with `TOOL: ` followed by the tool name and the request to the tool, example `TOOL: DuckDuckGoSearchAgent(request='search query')`
|
| 66 |
|
| 67 |
+
Before answering reread the question and make sure you are answering the exact question asked.
|
| 68 |
"""
|
| 69 |
|
| 70 |
|
|
|
|
| 76 |
is_final_answer: bool
|
| 77 |
search_request: Optional[str]
|
| 78 |
wiki_request: Optional[str]
|
| 79 |
+
python_request: Optional[str]
|
| 80 |
|
| 81 |
|
| 82 |
class BasicAgent:
|
|
|
|
| 87 |
self.graph.add_node("invoke_model", self.invoke_model)
|
| 88 |
self.graph.add_node("web_search", self.web_search)
|
| 89 |
self.graph.add_node("wiki_search", self.wiki_search)
|
| 90 |
+
self.graph.add_node("python_exec", self.python_exec)
|
| 91 |
self.graph.add_node("decide", self.decide)
|
| 92 |
self.graph.add_node("final_answer", self.final_answer)
|
| 93 |
self.graph.add_node("exceeded_attempts", self.exceeded_attempts)
|
|
|
|
| 96 |
self.graph.add_edge("log_question", "invoke_model")
|
| 97 |
self.graph.add_edge("web_search", "invoke_model")
|
| 98 |
self.graph.add_edge("wiki_search", "invoke_model")
|
| 99 |
+
self.graph.add_edge("python_exec", "invoke_model")
|
| 100 |
|
| 101 |
# Add conditional edges
|
| 102 |
self.graph.add_conditional_edges(
|
|
|
|
| 107 |
"EXCEEDED_ATTEMPTS": "exceeded_attempts",
|
| 108 |
"WEB_SEARCH": "web_search",
|
| 109 |
"WIKI_SEARCH": "wiki_search",
|
| 110 |
+
"PYTHON_EXEX": "python_exec",
|
| 111 |
},
|
| 112 |
)
|
| 113 |
|
|
|
|
| 127 |
"is_final_answer": False,
|
| 128 |
"search_request": None,
|
| 129 |
"wiki_request": None,
|
| 130 |
+
"python_request": None,
|
| 131 |
}
|
| 132 |
)
|
| 133 |
return res.get("answer", "N/A")
|
|
|
|
| 181 |
)
|
| 182 |
return {"messages": state["messages"]}
|
| 183 |
|
| 184 |
+
def python_exec(self, state: AnswerState) -> Dict[str, Any]:
|
| 185 |
+
print("[python_exec] Executing: " + str(state["python_request"]))
|
| 186 |
+
import io
|
| 187 |
+
import contextlib
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
# Redirect stdout to capture output
|
| 192 |
+
output = io.StringIO()
|
| 193 |
+
sandbox_globals = {}
|
| 194 |
+
sandbox_locals = {}
|
| 195 |
+
|
| 196 |
+
with contextlib.redirect_stdout(output):
|
| 197 |
+
exec(str(state["python_request"]), sandbox_globals, sandbox_locals)
|
| 198 |
+
|
| 199 |
+
res = output.getvalue()
|
| 200 |
+
print(f"Python results: {res}")
|
| 201 |
+
state["messages"].append(
|
| 202 |
+
ChatMessage(
|
| 203 |
+
role="user",
|
| 204 |
+
content="CodeExecutorAgent tool results are: " + res,
|
| 205 |
+
)
|
| 206 |
+
)
|
| 207 |
+
return {"messages": state["messages"]}
|
| 208 |
+
|
| 209 |
def web_search(self, state: AnswerState) -> Dict[str, Any]:
|
| 210 |
print("[web_search] Searching for: " + str(state["search_request"]))
|
| 211 |
search_tool = GoogleSearchAPIWrapper()
|
|
|
|
| 254 |
return "WEB_SEARCH"
|
| 255 |
elif state["wiki_request"]:
|
| 256 |
return "WIKI_SEARCH"
|
| 257 |
+
elif state["python_request"]:
|
| 258 |
+
return "PYTHON_EXEC"
|
| 259 |
return "FINAL_ANSWER"
|
| 260 |
|
| 261 |
+
|
| 262 |
def invoke_model(self, state: AnswerState) -> Dict[str, Any]:
|
| 263 |
print("[invoke_model] Agent trying to answer")
|
| 264 |
state["search_request"] = None
|
| 265 |
state["wiki_request"] = None
|
| 266 |
+
state["python_request"] = None
|
| 267 |
|
| 268 |
response = self.model.invoke(state["messages"])
|
| 269 |
print(f"Agent response: {response.content}")
|
|
|
|
| 294 |
request = request_full.split("request=")[-1].strip("'")
|
| 295 |
print(f"Wiki request: {request}")
|
| 296 |
state["wiki_request"] = request
|
| 297 |
+
|
| 298 |
+
elif "TOOL: CodeExecutorAgent" in response.content:
|
| 299 |
+
request_full = response.text().split("TOOL: WikipediaAgent(")[-1].strip(")")
|
| 300 |
+
print(f"Tool invocation request: {request_full}")
|
| 301 |
+
request = request_full.split("request=")[-1].strip("'")
|
| 302 |
+
print(f"Python request: {request}")
|
| 303 |
+
state["python_request"] = request
|
| 304 |
return {
|
| 305 |
"messages": state["messages"],
|
| 306 |
"attempt": state.get("attempt", 0) + 1,
|