ositamiles commited on
Commit
a6006a0
·
verified ·
1 Parent(s): 775f4d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -11
app.py CHANGED
@@ -11,15 +11,20 @@ def query(payload):
11
  response.raise_for_status() # Raise an error for bad HTTP responses
12
  data = response.json() # Parse the JSON response
13
 
14
- # Extract toolOutput if present in the response
15
- tool_outputs = []
16
  if "agentReasoning" in data:
17
  for reasoning in data["agentReasoning"]:
18
  if "usedTools" in reasoning:
19
- tool_outputs.extend(reasoning["usedTools"])
 
 
 
 
 
20
 
21
- # Return the full response and toolOutput
22
- return {"raw_response": data, "tool_output": tool_outputs}
23
  except requests.exceptions.RequestException as e:
24
  return {"error": str(e)}
25
 
@@ -40,13 +45,36 @@ def main():
40
  "question": user_input,
41
  })
42
 
43
- # Display the extracted toolOutput on the main page
44
- st.subheader("Extracted Tool Output:")
45
- tool_output = response.get("tool_output", [])
46
- if tool_output:
47
- st.write(tool_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  else:
49
- st.write("No toolOutput data found in the response.")
50
 
51
  # Display the raw JSON response in the sidebar
52
  st.sidebar.header("Raw JSON Response")
 
11
  response.raise_for_status() # Raise an error for bad HTTP responses
12
  data = response.json() # Parse the JSON response
13
 
14
+ # Extract toolOutput and tool details
15
+ tool_details = []
16
  if "agentReasoning" in data:
17
  for reasoning in data["agentReasoning"]:
18
  if "usedTools" in reasoning:
19
+ for tool in reasoning["usedTools"]:
20
+ tool_details.append({
21
+ "tool": tool.get("tool", ""),
22
+ "toolInput": tool.get("toolInput", {}).get("input", ""),
23
+ "toolOutput": tool.get("toolOutput", "")
24
+ })
25
 
26
+ # Return the full response and tool details
27
+ return {"raw_response": data, "tool_details": tool_details}
28
  except requests.exceptions.RequestException as e:
29
  return {"error": str(e)}
30
 
 
45
  "question": user_input,
46
  })
47
 
48
+ # Check for errors
49
+ if "error" in response:
50
+ st.error(f"Error: {response['error']}")
51
+ return
52
+
53
+ # Display Online Resources section
54
+ st.subheader("Online Resources")
55
+ tool_details = response.get("tool_details", [])
56
+
57
+ if tool_details:
58
+ # Create an expandable section for each resource
59
+ for idx, tool in enumerate(tool_details, 1):
60
+ # Use toolInput as the resource name
61
+ resource_name = tool.get("toolInput", f"Resource {idx}")
62
+
63
+ # Create an expandable section
64
+ with st.expander(resource_name):
65
+ # Display the tool output
66
+ tool_output = tool.get("toolOutput", "No output available")
67
+
68
+ # Check if toolOutput is a list or dict and pretty print
69
+ if isinstance(tool_output, (list, dict)):
70
+ st.json(tool_output)
71
+ else:
72
+ st.write(tool_output)
73
+
74
+ # Optional: Add tool type information
75
+ st.caption(f"Source: {tool.get('tool', 'Unknown')}")
76
  else:
77
+ st.write("No resources found.")
78
 
79
  # Display the raw JSON response in the sidebar
80
  st.sidebar.header("Raw JSON Response")