add openrouter
Browse files
app.py
CHANGED
@@ -34,6 +34,23 @@ def generate_text(prompt):
|
|
34 |
).choices[0].message.content
|
35 |
return message
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def question_answer(chat_history, file, question):
|
38 |
suffix = Path(file.name).suffix
|
39 |
with NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
@@ -41,7 +58,7 @@ def question_answer(chat_history, file, question):
|
|
41 |
tmp_path = Path(tmp.name)
|
42 |
|
43 |
load_recommender(str(tmp_path))
|
44 |
-
answer =
|
45 |
chat_history.append([question, answer])
|
46 |
return chat_history
|
47 |
|
|
|
34 |
).choices[0].message.content
|
35 |
return message
|
36 |
|
37 |
+
def generate_answer(question):
|
38 |
+
topn_chunks = recommender(question)
|
39 |
+
prompt = 'search results:\n\n'
|
40 |
+
for c in topn_chunks:
|
41 |
+
prompt += c + '\n\n'
|
42 |
+
|
43 |
+
prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
|
44 |
+
"Cite each reference using [ Page Number] notation. "\
|
45 |
+
"Only answer what is asked. The answer should be short and concise. "\
|
46 |
+
"If asked in Chinese, respond in Chinese; if asked in English, respond"\
|
47 |
+
"in English \n\nQuery: "
|
48 |
+
|
49 |
+
prompt += f"{question}\nAnswer:"
|
50 |
+
answer = generate_text(prompt)
|
51 |
+
return answer
|
52 |
+
|
53 |
+
|
54 |
def question_answer(chat_history, file, question):
|
55 |
suffix = Path(file.name).suffix
|
56 |
with NamedTemporaryFile(delete=False, suffix=suffix) as tmp:
|
|
|
58 |
tmp_path = Path(tmp.name)
|
59 |
|
60 |
load_recommender(str(tmp_path))
|
61 |
+
answer = generate_answer(question)
|
62 |
chat_history.append([question, answer])
|
63 |
return chat_history
|
64 |
|