Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -157,20 +157,12 @@ def idk(query: str):
|
|
157 |
|
158 |
@outlines.prompt
|
159 |
def self_knowledge(query: str):
|
160 |
-
"""
|
161 |
|
162 |
Question: {{ query }}
|
163 |
"""
|
164 |
|
165 |
def main(query: str, client: QdrantClient, collection_name: str, llm, dense_model: OptimumEncoder, sparse_model: SparseTextEmbedding, past_messages: str):
|
166 |
-
seen_values = set()
|
167 |
-
result_metadatas = "\n\n".join(
|
168 |
-
f'{value}'
|
169 |
-
for metadata in metadatas
|
170 |
-
for key, value in metadata.items()
|
171 |
-
if (value not in seen_values and not seen_values.add(value))
|
172 |
-
)
|
173 |
-
|
174 |
prompt = build_initial_prompt(query)
|
175 |
gen_text = outlines.generate.text(llm)
|
176 |
|
@@ -194,6 +186,14 @@ def main(query: str, client: QdrantClient, collection_name: str, llm, dense_mode
|
|
194 |
action = gen_choice(prompt, max_tokens=128, sampling_params=SamplingParams(temperature=0))
|
195 |
|
196 |
if action == 'Yes':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
prompt = answer_with_context(context, query)
|
198 |
answer = gen_text(prompt, max_tokens=500, sampling_params=SamplingParams(temperature=0.3))
|
199 |
answer = f"{answer}\n\n\nSource(s) :\n\n{result_metadatas}"
|
@@ -202,57 +202,14 @@ def main(query: str, client: QdrantClient, collection_name: str, llm, dense_mode
|
|
202 |
answer = f'Documents Based :\n\n{answer}'
|
203 |
else:
|
204 |
if st.session_state.documents_only:
|
205 |
-
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
return answer
|
208 |
-
|
209 |
-
|
210 |
-
args = {'context': context, 'query': query}
|
211 |
-
messages = [
|
212 |
-
{"role": "system", "content": 'You are a helpful assistant.'},
|
213 |
-
{"role": "user", "content": st.session_state.toggle_docs['qa_prompt'].format(**args)}
|
214 |
-
]
|
215 |
-
prompts = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
216 |
-
|
217 |
-
outputs = llm.generate(
|
218 |
-
prompts=prompts,
|
219 |
-
sampling_params=vllm.SamplingParams(
|
220 |
-
temperature=0,
|
221 |
-
max_tokens=3000
|
222 |
-
)
|
223 |
-
)
|
224 |
-
print(f'TEXT: {outputs}')
|
225 |
-
|
226 |
-
text = outputs[0].outputs[0].text
|
227 |
-
|
228 |
-
messages_2 = [
|
229 |
-
{"role": "system", "content": """Act like a professional summary writer. You have been providing summarization services for various types of documents, including academic papers, legal texts, and business reports, for over 20 years.
|
230 |
-
Your expertise includes extracting key points and important details concisely without adding unnecessary introductory phrases."""
|
231 |
-
},
|
232 |
-
{"role": "user", "content": f"""Write a summary of the following text delimited by triple backquotes. Ensure the summary covers the key points of the text. Do not introduce the summary with sentences like "Here is the summary:" or similar. The summary should be detailed, precise, and directly convey the essential information from the text.
|
233 |
-
|
234 |
-
```{text}```
|
235 |
-
|
236 |
-
Let's think step-by-step."""
|
237 |
-
}
|
238 |
-
]
|
239 |
-
prompts_2 = tokenizer.apply_chat_template(messages_2, tokenize=False, add_generation_prompt=True)
|
240 |
-
|
241 |
-
outputs_2 = llm.generate(
|
242 |
-
prompts=prompts_2,
|
243 |
-
sampling_params=vllm.SamplingParams(
|
244 |
-
temperature=0.3,
|
245 |
-
max_tokens=3000
|
246 |
-
)
|
247 |
-
)
|
248 |
-
|
249 |
-
answer = outputs_2[0].outputs[0].text
|
250 |
-
answer_with_metadatas = f"{answer}\n\n\nSource(s) :\n\n{result_metadatas}"
|
251 |
-
|
252 |
-
if st.session_state.documents_only:
|
253 |
-
return answer if 'no_answer' in text else answer_with_metadatas
|
254 |
-
else:
|
255 |
-
return f'Internal Knowledge :\n\n{answer}' if 'knowledge_topic' in text else f'Documents Based :\n\n{answer_with_metadatas}'
|
256 |
|
257 |
def collect_files(conn, cursor, directory, pattern):
|
258 |
array = []
|
@@ -548,20 +505,11 @@ def chunk_documents(texts: List[str], metadatas: List[dict], dense_model: Optimu
|
|
548 |
def on_change_documents_only():
|
549 |
if st.session_state.documents_only:
|
550 |
st.session_state.toggle_docs = {
|
551 |
-
'qa_prompt': """You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, reply with 'no_answer'. Use three sentences maximum and keep the answer concise.
|
552 |
-
|
553 |
-
Question: {query}
|
554 |
-
|
555 |
-
Context: {context}
|
556 |
-
|
557 |
-
Answer:
|
558 |
-
""",
|
559 |
'tooltip': 'The AI answer your questions only considering the documents provided',
|
560 |
'display': True
|
561 |
}
|
562 |
else:
|
563 |
st.session_state.toggle_docs = {
|
564 |
-
'qa_prompt': "{query}",
|
565 |
'tooltip': """The AI answer your questions considering the documents provided, and if it doesn't found the answer in them, try to find in its own internal knowledge""",
|
566 |
'display': False
|
567 |
}
|
|
|
157 |
|
158 |
@outlines.prompt
|
159 |
def self_knowledge(query: str):
|
160 |
+
"""Answer the following question by using your own knowledge about the topic.
|
161 |
|
162 |
Question: {{ query }}
|
163 |
"""
|
164 |
|
165 |
def main(query: str, client: QdrantClient, collection_name: str, llm, dense_model: OptimumEncoder, sparse_model: SparseTextEmbedding, past_messages: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
prompt = build_initial_prompt(query)
|
167 |
gen_text = outlines.generate.text(llm)
|
168 |
|
|
|
186 |
action = gen_choice(prompt, max_tokens=128, sampling_params=SamplingParams(temperature=0))
|
187 |
|
188 |
if action == 'Yes':
|
189 |
+
seen_values = set()
|
190 |
+
result_metadatas = "\n\n".join(
|
191 |
+
f'{value}'
|
192 |
+
for metadata in metadatas
|
193 |
+
for key, value in metadata.items()
|
194 |
+
if (value not in seen_values and not seen_values.add(value))
|
195 |
+
)
|
196 |
+
|
197 |
prompt = answer_with_context(context, query)
|
198 |
answer = gen_text(prompt, max_tokens=500, sampling_params=SamplingParams(temperature=0.3))
|
199 |
answer = f"{answer}\n\n\nSource(s) :\n\n{result_metadatas}"
|
|
|
202 |
answer = f'Documents Based :\n\n{answer}'
|
203 |
else:
|
204 |
if st.session_state.documents_only:
|
205 |
+
prompt = idk(query)
|
206 |
+
answer = gen_text(prompt, max_tokens=500, sampling_params=SamplingParams(temperature=0.3))
|
207 |
+
else:
|
208 |
+
prompt = self_knowledge(query)
|
209 |
+
answer = gen_text(prompt, max_tokens=500, sampling_params=SamplingParams(temperature=0.3))
|
210 |
+
answer = f'Internal Knowledge :\n\n{answer}'
|
211 |
|
212 |
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
def collect_files(conn, cursor, directory, pattern):
|
215 |
array = []
|
|
|
505 |
def on_change_documents_only():
|
506 |
if st.session_state.documents_only:
|
507 |
st.session_state.toggle_docs = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
508 |
'tooltip': 'The AI answer your questions only considering the documents provided',
|
509 |
'display': True
|
510 |
}
|
511 |
else:
|
512 |
st.session_state.toggle_docs = {
|
|
|
513 |
'tooltip': """The AI answer your questions considering the documents provided, and if it doesn't found the answer in them, try to find in its own internal knowledge""",
|
514 |
'display': False
|
515 |
}
|