Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
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 |
-
"⚠️
|
67 |
-
"
|
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
|
72 |
-
"no quotes, no code fences, no extra explanation.\n\n"
|
73 |
-
"If you do
|
74 |
)
|
75 |
)
|
76 |
|
77 |
-
#
|
78 |
initial_state = { "messages": [system_msg] }
|
|
|
|
|
79 |
final_state = compiled_graph.invoke(initial_state, user_input)
|
80 |
|
81 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
#
|
89 |
tool_dict = parse_tool_json(last_msg or "")
|
90 |
-
if tool_dict
|
91 |
-
#
|
|
|
92 |
result = tool_node.run(tool_dict)
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
96 |
}
|
97 |
-
second_pass = compiled_graph.invoke(
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
for msg in reversed(second_pass["messages"]):
|
100 |
if isinstance(msg, AIMessage):
|
101 |
-
return msg.content
|
102 |
return ""
|
103 |
else:
|
104 |
-
#
|
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.")
|