Tonic commited on
Commit
6b796ae
1 Parent(s): 593b7a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -72,19 +72,35 @@ def query_vectara(question):
72
  verify=True,
73
  headers=api_key_header
74
  )
75
-
76
-
77
  if response.status_code == 200:
78
  query_data = response.json()
79
- print(query_data)
80
  if query_data:
81
- # Extract summary and the first 5 sources
82
- response_set = query_data.get('responseSet', [{}])[0] # get the first response set
83
- summary = response_set.get('summary', [{}])[0] # get the first summary
84
- summary_text = summary.get('text', 'No summary available')
85
- sources = response_set.get('response', [])[:5]
86
- sources_text = [source.get('text', '') for source in sources]
87
- return f"Summary: {summary_text}\n\nSources:\n{json.dumps(sources_text, indent=2)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  else:
89
  return "No data found in the response."
90
  else:
 
72
  verify=True,
73
  headers=api_key_header
74
  )
75
+
 
76
  if response.status_code == 200:
77
  query_data = response.json()
78
+ print(query_data)
79
  if query_data:
80
+ sources_info = []
81
+
82
+ # Iterate over all response sets
83
+ for response_set in query_data.get('responseSet', []):
84
+ # Extract sources
85
+ for source in response_set.get('response', []):
86
+ source_metadata = source.get('metadata', [])
87
+ source_info = {}
88
+
89
+ for metadata in source_metadata:
90
+ metadata_name = metadata.get('name', '')
91
+ metadata_value = metadata.get('value', '')
92
+
93
+ if metadata_name == 'title':
94
+ source_info['title'] = metadata_value
95
+ elif metadata_name == 'author':
96
+ source_info['author'] = metadata_value
97
+ elif metadata_name == 'pageNumber':
98
+ source_info['page number'] = metadata_value
99
+
100
+ if source_info:
101
+ sources_info.append(source_info)
102
+
103
+ return f"Sources:\n{json.dumps(sources_info, indent=2)}"
104
  else:
105
  return "No data found in the response."
106
  else: