Update app.py
Browse files
app.py
CHANGED
@@ -194,77 +194,78 @@ if QUESTION:
|
|
194 |
print(d['url'], score, r['score'], meta_score)
|
195 |
resplist.append(d)
|
196 |
|
197 |
-
|
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 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
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))
|