skkjodhpur commited on
Commit
54c1a7f
·
verified ·
1 Parent(s): 893f6f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -51,7 +51,7 @@ def custom_output_parser(text):
51
  return {"tool_code": tool_code}
52
  return {"text": text}
53
 
54
- # Function to run the agent
55
  def search(query):
56
  inputs = {
57
  "query": query,
@@ -61,16 +61,23 @@ def search(query):
61
  "intermediate_steps": [] # Initial empty intermediate steps
62
  }
63
  try:
 
64
  output = agent.invoke(inputs)
65
- # Process the output with the custom output parser
66
  parsed_output = custom_output_parser(output)
67
- # Execute tool code if exists
 
 
 
 
 
68
  if "tool_code" in parsed_output:
69
  tool_code = parsed_output["tool_code"]
70
  exec_globals = {"search": tools[0].func} # Assuming 'search' is the first tool
71
  exec(tool_code, exec_globals)
72
  return exec_globals.get("result", "Executed tool code.")
73
- return parsed_output["text"]
 
 
74
  except Exception as e:
75
  # Print the exception and the inputs for debugging
76
  print(f"Error: {e}")
 
51
  return {"tool_code": tool_code}
52
  return {"text": text}
53
 
54
+ # Function to run the agent and fallback to search tool if needed
55
  def search(query):
56
  inputs = {
57
  "query": query,
 
61
  "intermediate_steps": [] # Initial empty intermediate steps
62
  }
63
  try:
64
+ # Attempt to get the answer from the LLM
65
  output = agent.invoke(inputs)
 
66
  parsed_output = custom_output_parser(output)
67
+
68
+ # Check if the output is a valid answer
69
+ if parsed_output["text"].strip() and "I can answer that question" not in parsed_output["text"]:
70
+ return parsed_output["text"]
71
+
72
+ # If not a valid answer, proceed with tool code execution
73
  if "tool_code" in parsed_output:
74
  tool_code = parsed_output["tool_code"]
75
  exec_globals = {"search": tools[0].func} # Assuming 'search' is the first tool
76
  exec(tool_code, exec_globals)
77
  return exec_globals.get("result", "Executed tool code.")
78
+
79
+ return "No valid answer found."
80
+
81
  except Exception as e:
82
  # Print the exception and the inputs for debugging
83
  print(f"Error: {e}")