sujitb commited on
Commit
75237fd
1 Parent(s): 32ad03a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -69
app.py CHANGED
@@ -194,77 +194,78 @@ if QUESTION:
194
  print(d['url'], score, r['score'], meta_score)
195
  resplist.append(d)
196
 
197
- sorted_indices = sorted(range(len(resplist)), key=lambda i: resplist[i]['score'], reverse=True)
198
-
199
- # Get the elements with the top 2 highest values
200
- top_2 = [resplist[i] for i in sorted_indices[:2]]
201
-
202
- # covert to array
203
 
204
- json_data = json.dumps(top_2)
205
-
206
- st.write(json_data)
207
- goodmatch=False
208
- if resplist[sorted_indices[0]]['score']>.5:
209
- goodmatch=True
210
- mode = "two" # two passages
211
-
212
- client = OpenAI()
213
-
214
- if mode=="one":
215
- instr=system_instructions_text
216
-
217
- out= resplist[sorted_indices[0]]['content']
218
- content="""
219
- <text>
220
- {}
221
- </text>
222
- """.format(out)
223
-
224
- if mode=="two":
225
- instr=json_instructions
226
- content=json_data
227
-
228
- response = client.chat.completions.create(
229
- model="gpt-3.5-turbo",
230
- messages=[
231
- {"role": "system", "content":instr },
232
- {"role": "user", "content": content},
233
- {"role": "user", "content": "Question:"+QUESTION}
234
- ]
235
- )
236
-
237
- ans= response.choices[0].message.content
238
- else:
239
- ans='Weak match to your query. Please try reframing your question'
240
-
241
- #st.write("Matched URL:{} Score:{}".format(url,score))
242
- testing = False
243
- if testing:
244
- if len(resp)>=1:
245
- st.write("2nd Matched URL:{} Score:{}".format(resp[1]['id'],resp[1]['score']))
246
- if len(resp)>=2:
247
- st.write("3rd Matched URL:{} Score:{}".format(resp[2]['id'],resp[2]['score']))
248
-
249
- with st.chat_message("assistant"):
250
- response = st.write_stream(response_generator(ans))
251
- if goodmatch:
252
- st.write('Resources:'+top_2[0]['url'])
253
- st.write(top_2[1]['url'])
254
- # Add assistant response to chat history
255
- st.session_state.messages.append({"role": "assistant", "content": response})
256
- #st.write(ans)
257
-
258
- #st.write(' ----------------------')
259
- #st.write(out)
260
-
261
- now= str(datetime.utcnow())
262
- df_log.loc[len(df_log)]=[QUESTION,url,score,ans,now]
263
- write_log(QUESTION,url, score, ans, now)
264
- #df.to_csv("hf://datasets/sujitb/data/test.csv")
265
- #df_log.to_csv("hf://datasets/sujitb/data/"+logfile)
 
 
 
 
 
266
 
267
- else:
268
  #st.write("No matches for query")
269
  ans= "No matches for query"
270
  response = st.write_stream(response_generator(ans))
 
194
  print(d['url'], score, r['score'], meta_score)
195
  resplist.append(d)
196
 
197
+ if len(resplist)>0:
198
+ sorted_indices = sorted(range(len(resplist)), key=lambda i: resplist[i]['score'], reverse=True)
 
 
 
 
199
 
200
+ # Get the elements with the top 2 highest values
201
+ top_2 = [resplist[i] for i in sorted_indices[:2]]
202
+
203
+ # covert to array
204
+
205
+ json_data = json.dumps(top_2)
206
+
207
+ st.write(json_data)
208
+ goodmatch=False
209
+ if resplist[sorted_indices[0]]['score']>.5:
210
+ goodmatch=True
211
+ mode = "two" # two passages
212
+
213
+ client = OpenAI()
214
+
215
+ if mode=="one":
216
+ instr=system_instructions_text
217
+
218
+ out= resplist[sorted_indices[0]]['content']
219
+ content="""
220
+ <text>
221
+ {}
222
+ </text>
223
+ """.format(out)
224
+
225
+ if mode=="two":
226
+ instr=json_instructions
227
+ content=json_data
228
+
229
+ response = client.chat.completions.create(
230
+ model="gpt-3.5-turbo",
231
+ messages=[
232
+ {"role": "system", "content":instr },
233
+ {"role": "user", "content": content},
234
+ {"role": "user", "content": "Question:"+QUESTION}
235
+ ]
236
+ )
237
+
238
+ ans= response.choices[0].message.content
239
+ else:
240
+ ans='Weak match to your query. Please try reframing your question'
241
+
242
+ #st.write("Matched URL:{} Score:{}".format(url,score))
243
+ testing = False
244
+ if testing:
245
+ if len(resp)>=1:
246
+ st.write("2nd Matched URL:{} Score:{}".format(resp[1]['id'],resp[1]['score']))
247
+ if len(resp)>=2:
248
+ st.write("3rd Matched URL:{} Score:{}".format(resp[2]['id'],resp[2]['score']))
249
+
250
+ with st.chat_message("assistant"):
251
+ response = st.write_stream(response_generator(ans))
252
+ if goodmatch:
253
+ st.write('Resources:'+top_2[0]['url'])
254
+ st.write(top_2[1]['url'])
255
+ # Add assistant response to chat history
256
+ st.session_state.messages.append({"role": "assistant", "content": response})
257
+ #st.write(ans)
258
+
259
+ #st.write(' ----------------------')
260
+ #st.write(out)
261
+
262
+ now= str(datetime.utcnow())
263
+ df_log.loc[len(df_log)]=[QUESTION,url,score,ans,now]
264
+ write_log(QUESTION,url, score, ans, now)
265
+ #df.to_csv("hf://datasets/sujitb/data/test.csv")
266
+ #df_log.to_csv("hf://datasets/sujitb/data/"+logfile)
267
 
268
+ else: ## Zero response from pinecone query
269
  #st.write("No matches for query")
270
  ans= "No matches for query"
271
  response = st.write_stream(response_generator(ans))