naman1102 commited on
Commit
6a920fc
·
1 Parent(s): c9256cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -19
app.py CHANGED
@@ -56,55 +56,77 @@ compiled_graph = graph.compile()
56
 
57
  # 3) The corrected respond_to_input:
58
  def respond_to_input(user_input: str) -> str:
59
- # 1) The tightened system prompt
60
  system_msg = SystemMessage(
61
  content=(
62
  "You are an assistant with access to exactly these tools:\n"
63
  " 1) web_search(query:str)\n"
64
  " 2) parse_excel(path:str,sheet_name:str)\n"
65
  " 3) ocr_image(path:str)\n\n"
66
- "⚠️ **IMPORTANT** ⚠️: If (and only if) you need to call one of these tools, "
67
- "output exactly one JSON object and nothing else. For example:\n"
68
  "```json\n"
69
  '{"tool":"web_search","query":"Mercedes Sosa albums 2000-2009"}\n'
70
  "```\n"
71
- "That JSON must start at the very first character of your response and end at the very last character—"
72
- "no quotes, no code fences, no extra explanation.\n\n"
73
- "If you do NOT need any tool, simply reply with your final answer as plain text (no JSON)."
74
  )
75
  )
76
 
77
- # 2) Kick off the graph with only that system prompt
78
  initial_state = { "messages": [system_msg] }
 
 
79
  final_state = compiled_graph.invoke(initial_state, user_input)
80
 
81
- # 3) Find the last AIMessage the LLM returned
 
 
 
 
 
 
 
82
  last_msg = None
83
  for msg in reversed(final_state["messages"]):
84
  if isinstance(msg, AIMessage):
85
  last_msg = msg.content
86
  break
87
 
88
- # 4) Try to parse that as a toolcall dict
89
  tool_dict = parse_tool_json(last_msg or "")
90
- if tool_dict is not None:
91
- # 4.a) If it’s valid, run the tool
 
92
  result = tool_node.run(tool_dict)
93
- # 4.b) Feed the tool’s output back into the agent a second time
94
- new_state = {
95
- "messages": [*final_state["messages"], AIMessage(content=result)]
 
 
 
 
 
96
  }
97
- second_pass = compiled_graph.invoke(new_state, "")
98
- # 4.c) Return the last AIMessage from that second pass
 
 
 
 
 
 
 
 
99
  for msg in reversed(second_pass["messages"]):
100
  if isinstance(msg, AIMessage):
101
- return msg.content
102
  return ""
103
  else:
104
- # 5) If it wasn’t valid JSON just return the plain last_msg
105
  return last_msg or ""
106
 
107
-
108
  class BasicAgent:
109
  def __init__(self):
110
  print("BasicAgent initialized.")
 
56
 
57
  # 3) The corrected respond_to_input:
58
  def respond_to_input(user_input: str) -> str:
59
+ # A) Describe your tools exactly—no extra text allowed around JSON
60
  system_msg = SystemMessage(
61
  content=(
62
  "You are an assistant with access to exactly these tools:\n"
63
  " 1) web_search(query:str)\n"
64
  " 2) parse_excel(path:str,sheet_name:str)\n"
65
  " 3) ocr_image(path:str)\n\n"
66
+ "⚠️ If (and only if) you need to call a tool, output exactly one JSON object "
67
+ "and nothing else. For example:\n"
68
  "```json\n"
69
  '{"tool":"web_search","query":"Mercedes Sosa albums 2000-2009"}\n'
70
  "```\n"
71
+ "That JSON must start at the very first character of your response and end at the last character—"
72
+ "no quotes, no code fences, and no extra explanation.\n\n"
73
+ "If you do not need any tool, simply reply with your final answer as plain text."
74
  )
75
  )
76
 
77
+ # B) Start the graph with only the SystemMessage in state["messages"]
78
  initial_state = { "messages": [system_msg] }
79
+
80
+ # C) Invoke the graph, passing user_input separately
81
  final_state = compiled_graph.invoke(initial_state, user_input)
82
 
83
+ # D) Log out everything the agent wrote, so you can diagnose
84
+ print("===== AGENT MESSAGES =====")
85
+ for i, msg in enumerate(final_state["messages"]):
86
+ if isinstance(msg, AIMessage):
87
+ print(f" [AIMessage #{i}]: {repr(msg.content)}")
88
+ print("==========================")
89
+
90
+ # E) Take the very last AIMessage that the agent produced
91
  last_msg = None
92
  for msg in reversed(final_state["messages"]):
93
  if isinstance(msg, AIMessage):
94
  last_msg = msg.content
95
  break
96
 
97
+ # F) Try to parse that last message as a JSON‐dict for a tool call
98
  tool_dict = parse_tool_json(last_msg or "")
99
+ if tool_dict:
100
+ # G) If valid JSON, run the tool
101
+ print(">> Parsed tool call:", tool_dict)
102
  result = tool_node.run(tool_dict)
103
+
104
+ # H) Feed back the tool’s output as the next AIMessage, then re‐invoke
105
+ # (Note: we pass an empty string as user_input because we’re continuing the same turn)
106
+ continuation_state = {
107
+ "messages": [
108
+ *final_state["messages"],
109
+ AIMessage(content=result)
110
+ ]
111
  }
112
+ second_pass = compiled_graph.invoke(continuation_state, "")
113
+
114
+ # I) Log that second pass
115
+ print("===== AGENT SECOND PASS =====")
116
+ for i, msg in enumerate(second_pass["messages"]):
117
+ if isinstance(msg, AIMessage):
118
+ print(f" [AIMessage2 #{i}]: {repr(msg.content)}")
119
+ print("==============================")
120
+
121
+ # J) Return the final AIMessage from that second pass
122
  for msg in reversed(second_pass["messages"]):
123
  if isinstance(msg, AIMessage):
124
+ return msg.content or ""
125
  return ""
126
  else:
127
+ # K) If it wasn’t valid JSON, just return the plain last_msg
128
  return last_msg or ""
129
 
 
130
  class BasicAgent:
131
  def __init__(self):
132
  print("BasicAgent initialized.")