naman1102 commited on
Commit
4288fbd
·
1 Parent(s): 65abbbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -4
app.py CHANGED
@@ -142,14 +142,42 @@ compiled_graph = graph.compile()
142
 
143
  def respond_to_input(user_input: str) -> str:
144
  """
145
- Initialize with an empty messages list. Then run through plan tools → run_tools → finalize.
146
- Return the "final_answer" from the final state.
147
  """
148
- initial_state: AgentState = {"messages": []}
149
- final_state = compiled_graph.invoke(initial_state, user_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  return final_state.get("final_answer", "Error: No final answer generated.")
151
 
152
 
 
153
  class BasicAgent:
154
  def __init__(self):
155
  print("BasicAgent initialized.")
 
142
 
143
  def respond_to_input(user_input: str) -> str:
144
  """
145
+ Initialize with a SystemMessage (tools description) and the user’s question as a HumanMessage.
146
+ Then run through plan → tools → run_tools → finalize. Return the "final_answer" from final_state.
147
  """
148
+ # 1) Create a SystemMessage that tells the agent about its tools
149
+ system_msg = SystemMessage(
150
+ content=(
151
+ "You have access to exactly these tools:\n"
152
+ " 1) web_search(query:str) → Returns the top search results for the query.\n"
153
+ " 2) parse_excel(path:str, sheet_name:str) → Reads an Excel file and returns its contents.\n"
154
+ " 3) ocr_image(path:str) → Runs OCR on an image and returns any detected text.\n\n"
155
+ "If you need a tool, set exactly one of these keys in a Python‐dict:\n"
156
+ " • web_search_query: <search terms>\n"
157
+ " • ocr_path: <path to image>\n"
158
+ " • excel_path: <path to xlsx>\n"
159
+ " • excel_sheet_name: <sheet name>\n"
160
+ "Otherwise, set final_answer: <your answer>.\n"
161
+ "Respond with that Python dict literal—no extra text or explanation."
162
+ )
163
+ )
164
+
165
+ # 2) Wrap the user_input in a HumanMessage
166
+ human_msg = HumanMessage(content=user_input)
167
+
168
+ # 3) Build the initial state so that "messages" contains both messages
169
+ initial_state: AgentState = {
170
+ "messages": [system_msg, human_msg]
171
+ }
172
+
173
+ # 4) Invoke the compiled graph (no second argument needed)
174
+ final_state = compiled_graph.invoke(initial_state)
175
+
176
+ # 5) Return the final answer (or a fallback if missing)
177
  return final_state.get("final_answer", "Error: No final answer generated.")
178
 
179
 
180
+
181
  class BasicAgent:
182
  def __init__(self):
183
  print("BasicAgent initialized.")