mirjam-m commited on
Commit
a465c33
·
1 Parent(s): 6647079

graph test

Browse files
Files changed (1) hide show
  1. app.py +21 -0
app.py CHANGED
@@ -57,10 +57,12 @@ Question: {question}
57
 
58
  class AnswerState(TypedDict):
59
  question: str
 
60
  answer: Optional[str]
61
  messages: List[Dict[str, Any]]
62
  is_final_answer: bool
63
  search_request: Optional[str]
 
64
 
65
 
66
  class BasicAgent:
@@ -87,14 +89,17 @@ class BasicAgent:
87
  self.compiled_graph = self.graph.compile()
88
 
89
  def __call__(self, question: str) -> str:
 
90
  print(f"Agent received question (first 50 chars): {question[:50]}...")
91
  res = self.compiled_graph.invoke(
92
  {
93
  "question": question,
 
94
  "answer": None,
95
  "messages": [],
96
  "is_final_answer": False,
97
  "search_request": None,
 
98
  }
99
  )
100
  return res.get("answer", "N/A")
@@ -112,6 +117,12 @@ class BasicAgent:
112
  }
113
 
114
  def try_answer(self, state: AnswerState) -> str:
 
 
 
 
 
 
115
  print("[try_answer] Agent trying to answer")
116
  prompt = PROMPT.format(question=state["question"])
117
  state["messages"] = state.get("messages", []) + [
@@ -137,6 +148,16 @@ class BasicAgent:
137
  print(f"Agent returning answer: {state['answer']}")
138
  state["answer"] = answer
139
 
 
 
 
 
 
 
 
 
 
 
140
  return "FINAL_ANSWER"
141
 
142
  return "FINAL_ANSWER"
 
57
 
58
  class AnswerState(TypedDict):
59
  question: str
60
+ attempt: int
61
  answer: Optional[str]
62
  messages: List[Dict[str, Any]]
63
  is_final_answer: bool
64
  search_request: Optional[str]
65
+ search_response: Optional[str]
66
 
67
 
68
  class BasicAgent:
 
89
  self.compiled_graph = self.graph.compile()
90
 
91
  def __call__(self, question: str) -> str:
92
+
93
  print(f"Agent received question (first 50 chars): {question[:50]}...")
94
  res = self.compiled_graph.invoke(
95
  {
96
  "question": question,
97
+ "attempt": 0,
98
  "answer": None,
99
  "messages": [],
100
  "is_final_answer": False,
101
  "search_request": None,
102
+ "search_response": None,
103
  }
104
  )
105
  return res.get("answer", "N/A")
 
117
  }
118
 
119
  def try_answer(self, state: AnswerState) -> str:
120
+ if state["attempt"] > 3:
121
+ state["answer"] = "Exceeded max number of attempts."
122
+ return "FINAL_ANSWER"
123
+ else:
124
+ state["attempt"] += 1
125
+
126
  print("[try_answer] Agent trying to answer")
127
  prompt = PROMPT.format(question=state["question"])
128
  state["messages"] = state.get("messages", []) + [
 
148
  print(f"Agent returning answer: {state['answer']}")
149
  state["answer"] = answer
150
 
151
+ return "FINAL_ANSWER"
152
+ if "TOOL: GoogleSearchAgent" in response.content:
153
+ request_full = (
154
+ response.text().split("TOOL: GoogleSearchAgent(")[-1].strip(")")
155
+ )
156
+ print(f"Tool invocation request: {request_full}")
157
+ request = request_full.split("request=")[-1].strip("'")
158
+ print(f"Search request: {request}")
159
+ state["search_request"] = request
160
+
161
  return "FINAL_ANSWER"
162
 
163
  return "FINAL_ANSWER"