Solshine commited on
Commit
2e8b875
1 Parent(s): 62bc446

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -4
app.py CHANGED
@@ -65,6 +65,110 @@ def dspy_generate_agent_prompts(prompt):
65
 
66
  return agent_prompts
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  # Define the main function to be used with Gradio
69
  def generate_outputs(user_prompt):
70
  # 1. Process prompt with langchain (replace with your actual implementation)
@@ -79,11 +183,12 @@ def generate_outputs(user_prompt):
79
  # 4. Generate prompts for agents using DSPy
80
  agent_prompts = dspy_generate_agent_prompts(processed_prompt)
81
 
82
- # 5. Use the chosen LLM for two of the prompts
83
- output_1 = llm(agent_prompts[0], max_length=100)[0]["generated_text"]
84
- output_2 = llm(agent_prompts[1], max_length=100)[0]["generated_text"]
 
85
 
86
- # 6. Produce outputs with Langchain or DSPy (replace with your actual implementation)
87
  report, recommendations, visualization = produce_outputs(combined_data)
88
 
89
  return report, recommendations, visualization
 
65
 
66
  return agent_prompts
67
 
68
+ def query_vectara(text):
69
+ user_message = text
70
+
71
+ # Read authentication parameters from the .env file
72
+ customer_id = os.getenv('CUSTOMER_ID')
73
+ corpus_id = os.getenv('CORPUS_ID')
74
+ api_key = os.getenv('API_KEY')
75
+
76
+ # Define the headers
77
+ api_key_header = {
78
+ "customer-id": customer_id,
79
+ "x-api-key": api_key
80
+ }
81
+
82
+ # Define the request body in the structure provided in the example
83
+ request_body = {
84
+ "query": [
85
+ {
86
+ "query": user_message,
87
+ "queryContext": "",
88
+ "start": 1,
89
+ "numResults": 25,
90
+ "contextConfig": {
91
+ "charsBefore": 0,
92
+ "charsAfter": 0,
93
+ "sentencesBefore": 2,
94
+ "sentencesAfter": 2,
95
+ "startTag": "%START_SNIPPET%",
96
+ "endTag": "%END_SNIPPET%",
97
+ },
98
+ "rerankingConfig": {
99
+ "rerankerId": 272725718,
100
+ "mmrConfig": {
101
+ "diversityBias": 0.35
102
+ }
103
+ },
104
+ "corpusKey": [
105
+ {
106
+ "customerId": customer_id,
107
+ "corpusId": corpus_id,
108
+ "semantics": 0,
109
+ "metadataFilter": "",
110
+ "lexicalInterpolationConfig": {
111
+ "lambda": 0
112
+ },
113
+ "dim": []
114
+ }
115
+ ],
116
+ "summary": [
117
+ {
118
+ "maxSummarizedResults": 5,
119
+ "responseLang": "auto",
120
+ "summarizerPromptName": "vectara-summary-ext-v1.2.0"
121
+ }
122
+ ]
123
+ }
124
+ ]
125
+ }
126
+
127
+ # Make the API request using Gradio
128
+ response = requests.post(
129
+ "https://api.vectara.io/v1/query",
130
+ json=request_body, # Use json to automatically serialize the request body
131
+ verify=True,
132
+ headers=api_key_header
133
+ )
134
+
135
+ if response.status_code == 200:
136
+ query_data = response.json()
137
+ if query_data:
138
+ sources_info = []
139
+
140
+ # Extract the summary.
141
+ summary = query_data['responseSet'][0]['summary'][0]['text']
142
+
143
+ # Iterate over all response sets
144
+ for response_set in query_data.get('responseSet', []):
145
+ # Extract sources
146
+ # Limit to top 5 sources.
147
+ for source in response_set.get('response', [])[:5]:
148
+ source_metadata = source.get('metadata', [])
149
+ source_info = {}
150
+
151
+ for metadata in source_metadata:
152
+ metadata_name = metadata.get('name', '')
153
+ metadata_value = metadata.get('value', '')
154
+
155
+ if metadata_name == 'title':
156
+ source_info['title'] = metadata_value
157
+ elif metadata_name == 'author':
158
+ source_info['author'] = metadata_value
159
+ elif metadata_name == 'pageNumber':
160
+ source_info['page number'] = metadata_value
161
+
162
+ if source_info:
163
+ sources_info.append(source_info)
164
+
165
+ result = {"summary": summary, "sources": sources_info}
166
+ return f"{json.dumps(result, indent=2)}"
167
+ else:
168
+ return "No data found in the response."
169
+ else:
170
+ return f"Error: {response.status_code}"
171
+
172
  # Define the main function to be used with Gradio
173
  def generate_outputs(user_prompt):
174
  # 1. Process prompt with langchain (replace with your actual implementation)
 
183
  # 4. Generate prompts for agents using DSPy
184
  agent_prompts = dspy_generate_agent_prompts(processed_prompt)
185
 
186
+ # 5. Use the chosen LLM for two of the prompts and vectara tool use for the third agent
187
+ output_1 = llm(agent_prompts[0], max_length=100)[0][combined_data]
188
+ output_2 = llm(agent_prompts[1], max_length=100)[0][combined_data]
189
+ output_3 = query_vectara(prompt)
190
 
191
+ # 6. Produce outputs with Langchain or DSPy (stand in section)
192
  report, recommendations, visualization = produce_outputs(combined_data)
193
 
194
  return report, recommendations, visualization