naman1102 commited on
Commit
cb60ebc
·
1 Parent(s): c079543

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -25
app.py CHANGED
@@ -16,25 +16,6 @@ from langchain.schema import HumanMessage, AIMessage, SystemMessage
16
  # Create a ToolNode that knows about your web_search function
17
  import json
18
 
19
- def parse_tool_json(text: str) -> dict | None:
20
- """
21
- Given a string like '{"tool":"web_search","query":"..."}'
22
- or '"{\"tool\":\"web_search\",\"query\":\"...\"}"', return
23
- the parsed dict. Otherwise, return None.
24
- """
25
- t = text.strip()
26
- # If it’s wrapped in single or double quotes, remove them:
27
- if (t.startswith('"') and t.endswith('"')) or (t.startswith("'") and t.endswith("'")):
28
- t = t[1:-1]
29
- try:
30
- obj = json.loads(t)
31
- if isinstance(obj, dict) and "tool" in obj:
32
- return obj
33
- except Exception:
34
- return None
35
- return None
36
-
37
-
38
  # (Keep Constan
39
  #
40
  #
@@ -65,7 +46,7 @@ def respond_to_input(user_input: str) -> str:
65
  "⚠️ **MANDATORY** ⚠️: If (and only if) you need to call a tool, your entire response MUST be exactly ONE JSON OBJECT and NOTHING ELSE. \n"
66
  "For example, if you want to call web_search, you must respond with exactly:\n"
67
  "```json\n"
68
- "{\"tool\":\"web_search\",\"query\":\"Mercedes Sosa studio albums 2000-2009\"}\n"
69
  "```\n"
70
  "That JSON string must start at the very first character of your response and end at the very last character—"
71
  "no surrounding quotes, no markdown fences, no explanatory text. \n\n"
@@ -81,12 +62,11 @@ def respond_to_input(user_input: str) -> str:
81
  ]
82
  }
83
 
84
- # C) FIRST PASS: invoke with only initial_state (no second argument!)
85
  try:
86
  first_pass = compiled_graph.invoke(initial_state)
87
  except Exception as e:
88
  print("‼️ ERROR during first invoke:", repr(e))
89
- # If you want extra debug, you can try printing first_pass["messages"] if defined
90
  return "" # return fallback
91
 
92
  # D) Log the AIMessage(s) from first_pass
@@ -103,15 +83,25 @@ def respond_to_input(user_input: str) -> str:
103
  last_msg = msg.content
104
  break
105
 
106
- # F) Attempt to parse last_msg as JSON for a tool call
107
- tool_dict = parse_tool_json(last_msg or "")
 
 
 
 
 
 
 
 
 
 
108
  if tool_dict:
109
  # G) If valid JSON, run the tool
110
  print(">> Parsed tool call:", tool_dict)
111
  tool_result = tool_node.run(tool_dict)
112
  print(f">> Tool '{tool_dict['tool']}' returned: {repr(tool_result)}")
113
 
114
- # H) SECOND PASS: feed the tools output back in as an AIMessage,
115
  # with no new human input
116
  continuation_state = {
117
  "messages": [
 
16
  # Create a ToolNode that knows about your web_search function
17
  import json
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # (Keep Constan
20
  #
21
  #
 
46
  "⚠️ **MANDATORY** ⚠️: If (and only if) you need to call a tool, your entire response MUST be exactly ONE JSON OBJECT and NOTHING ELSE. \n"
47
  "For example, if you want to call web_search, you must respond with exactly:\n"
48
  "```json\n"
49
+ '{"tool":"web_search","query":"Mercedes Sosa studio albums 2000-2009"}'\n"
50
  "```\n"
51
  "That JSON string must start at the very first character of your response and end at the very last character—"
52
  "no surrounding quotes, no markdown fences, no explanatory text. \n\n"
 
62
  ]
63
  }
64
 
65
+ # C) FIRST PASS: invoke with only initial_state (no second argument!)
66
  try:
67
  first_pass = compiled_graph.invoke(initial_state)
68
  except Exception as e:
69
  print("‼️ ERROR during first invoke:", repr(e))
 
70
  return "" # return fallback
71
 
72
  # D) Log the AIMessage(s) from first_pass
 
83
  last_msg = msg.content
84
  break
85
 
86
+ # F) Attempt to parse last_msg as JSON for a tool call (inline, no parse_tool_json)
87
+ tool_dict = None
88
+ t = (last_msg or "").strip()
89
+ if (t.startswith('"') and t.endswith('"')) or (t.startswith("'") and t.endswith("'")):
90
+ t = t[1:-1]
91
+ try:
92
+ obj = json.loads(t)
93
+ if isinstance(obj, dict) and "tool" in obj:
94
+ tool_dict = obj
95
+ except Exception:
96
+ tool_dict = None
97
+
98
  if tool_dict:
99
  # G) If valid JSON, run the tool
100
  print(">> Parsed tool call:", tool_dict)
101
  tool_result = tool_node.run(tool_dict)
102
  print(f">> Tool '{tool_dict['tool']}' returned: {repr(tool_result)}")
103
 
104
+ # H) SECOND PASS: feed the tool's output back in as an AIMessage,
105
  # with no new human input
106
  continuation_state = {
107
  "messages": [