Update app.py
Browse files
app.py
CHANGED
@@ -77,12 +77,32 @@ def get_docs(input_query, country = None):
|
|
77 |
ls_dict.append(doc)
|
78 |
return(ls_dict)
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
def run_query(input_text):
|
81 |
docs = get_docs(input_text)
|
82 |
res = pipe.run(query=input_text, documents=docs)
|
83 |
output = res["results"][0]
|
84 |
st.write('Response')
|
85 |
st.success(output)
|
|
|
|
|
86 |
|
87 |
|
88 |
# Setup retriever, pulling from local faiss datastore
|
|
|
77 |
ls_dict.append(doc)
|
78 |
return(ls_dict)
|
79 |
|
80 |
+
def get_refs(res):
|
81 |
+
'''
|
82 |
+
Parse response for engineered reference ids (refer to prompt template)
|
83 |
+
Extract documents using reference ids
|
84 |
+
'''
|
85 |
+
import re
|
86 |
+
text = res["results"][0]
|
87 |
+
# This pattern should be returned by gpt3.5
|
88 |
+
# pattern = r'ref\. (\d+)\]\.'
|
89 |
+
pattern = r'ref\. (\d+)'
|
90 |
+
ref_ids = [int(match) for match in re.findall(pattern, text)]
|
91 |
+
# extract
|
92 |
+
for i in range(len(res['documents'])):
|
93 |
+
doc = res['documents'][i].to_dict()
|
94 |
+
ref_id = doc['meta']['ref_id']
|
95 |
+
if ref_id in ref_ids:
|
96 |
+
print("Ref. "+str(ref_id)+" ["+doc['meta']['country']+" "+doc['meta']['document_name']+"]: "+doc['content'])
|
97 |
+
|
98 |
def run_query(input_text):
|
99 |
docs = get_docs(input_text)
|
100 |
res = pipe.run(query=input_text, documents=docs)
|
101 |
output = res["results"][0]
|
102 |
st.write('Response')
|
103 |
st.success(output)
|
104 |
+
st.write('References')
|
105 |
+
st.success(get_refs(res))
|
106 |
|
107 |
|
108 |
# Setup retriever, pulling from local faiss datastore
|