Spaces:
Runtime error
Runtime error
Update retrieve.py
Browse files- retrieve.py +14 -20
retrieve.py
CHANGED
@@ -26,39 +26,32 @@ def retrieve_documents_from_collection(query, api_key, qdrant_url, qdrant_api_ke
|
|
26 |
qdrant_client = setup_qdrant_client(qdrant_url, qdrant_api_key)
|
27 |
qdrant = Qdrant(client=qdrant_client, collection_name=collection_name, embeddings=embeddings_model)
|
28 |
retriever = qdrant.as_retriever(search_kwargs={"k": 20})
|
29 |
-
llm = Cohere(temperature=0,cohere_api_key=userdata.get('COHERE_API_KEY'))
|
30 |
-
compressor = CohereRerank(cohere_api_key=os.getenv('COHERE_API_KEY'),top_n=5)
|
31 |
compression_retriever = ContextualCompressionRetriever(
|
32 |
base_compressor=compressor, base_retriever=retriever
|
33 |
-
|
34 |
-
template="""
|
35 |
# Your role
|
36 |
-
You are a brilliant expert at understanding the intent of the questioner and the crux of the question, and providing the most optimal answer
|
37 |
-
|
38 |
-
|
39 |
# Instruction
|
40 |
-
Your task is to answer the question
|
41 |
-
|
42 |
-
<retrieved context>
|
43 |
Retrieved Context:
|
44 |
{context}
|
45 |
</retrieved context>
|
46 |
-
|
47 |
-
|
48 |
# Constraint
|
49 |
1. Think deeply and multiple times about the user's question\nUser's question:\n{question}\nYou must understand the intent of their question and provide the most appropriate answer.
|
50 |
- Ask yourself why to understand the context of the question and why the questioner asked it, reflect on it, and provide an appropriate response based on what you understand.
|
51 |
-
2. Choose the most relevant content(the key content that directly relates to the question) from the retrieved context and use it to generate an answer.
|
52 |
3. Generate a concise, logical answer. When generating the answer, Do Not just list your selections, But rearrange them in context so that they become paragraphs with a natural flow.
|
53 |
-
4. When you don't have retrieved context for the question or
|
54 |
5. If required break the answer into proper paragraphs.
|
55 |
-
6. Mention
|
56 |
-
|
57 |
-
|
58 |
# Question:
|
59 |
{question}""",
|
60 |
-
input_variables=["context","question"]
|
61 |
-
|
62 |
llm = openai_llm(api_key=api_key)
|
63 |
rag_chain = (
|
64 |
{"context": compression_retriever | format_document_metadata, "question": RunnablePassthrough()}
|
@@ -66,7 +59,8 @@ def retrieve_documents_from_collection(query, api_key, qdrant_url, qdrant_api_ke
|
|
66 |
| llm
|
67 |
| StrOutputParser()
|
68 |
)
|
69 |
-
return rag_chain.invoke(query,{"callbacks":[langfuse_handler]})
|
|
|
70 |
|
71 |
def delete_collection(collection_name, qdrant_url, qdrant_api_key):
|
72 |
"""Delete a Qdrant collection."""
|
|
|
26 |
qdrant_client = setup_qdrant_client(qdrant_url, qdrant_api_key)
|
27 |
qdrant = Qdrant(client=qdrant_client, collection_name=collection_name, embeddings=embeddings_model)
|
28 |
retriever = qdrant.as_retriever(search_kwargs={"k": 20})
|
29 |
+
llm = Cohere(temperature=0, cohere_api_key=userdata.get('COHERE_API_KEY'))
|
30 |
+
compressor = CohereRerank(cohere_api_key=os.getenv('COHERE_API_KEY'), top_n=5)
|
31 |
compression_retriever = ContextualCompressionRetriever(
|
32 |
base_compressor=compressor, base_retriever=retriever
|
33 |
+
)
|
34 |
+
template = """
|
35 |
# Your role
|
36 |
+
You are a brilliant expert at understanding the intent of the questioner and the crux of the question, and providing the most optimal answer from the docs to the questioner's needs from the documents you are given.
|
|
|
|
|
37 |
# Instruction
|
38 |
+
Your task is to answer the question using the following pieces of retrieved context delimited by XML tags.
|
39 |
+
<retrieved context>
|
|
|
40 |
Retrieved Context:
|
41 |
{context}
|
42 |
</retrieved context>
|
|
|
|
|
43 |
# Constraint
|
44 |
1. Think deeply and multiple times about the user's question\nUser's question:\n{question}\nYou must understand the intent of their question and provide the most appropriate answer.
|
45 |
- Ask yourself why to understand the context of the question and why the questioner asked it, reflect on it, and provide an appropriate response based on what you understand.
|
46 |
+
2. Choose the most relevant content (the key content that directly relates to the question) from the retrieved context and use it to generate an answer.
|
47 |
3. Generate a concise, logical answer. When generating the answer, Do Not just list your selections, But rearrange them in context so that they become paragraphs with a natural flow.
|
48 |
+
4. When you don't have retrieved context for the question or if you have retrieved documents, but their content is irrelevant to the question, you should answer 'I can't find the answer to that question in the material I have'.
|
49 |
5. If required break the answer into proper paragraphs.
|
50 |
+
6. Mention the name of all the documents and page numbers you used in generating the response from the context provided. e.g. 1. Doc name: RSCA/etienne.pdf, Page number: 1 /n 2. Doc name: RSCA/rubric.pdf, Page number: 10. Remember to include all of the document names and pages. Don't miss out.
|
|
|
|
|
51 |
# Question:
|
52 |
{question}""",
|
53 |
+
input_variables=["context", "question"]
|
54 |
+
)
|
55 |
llm = openai_llm(api_key=api_key)
|
56 |
rag_chain = (
|
57 |
{"context": compression_retriever | format_document_metadata, "question": RunnablePassthrough()}
|
|
|
59 |
| llm
|
60 |
| StrOutputParser()
|
61 |
)
|
62 |
+
return rag_chain.invoke(query, {"callbacks": [langfuse_handler]})
|
63 |
+
|
64 |
|
65 |
def delete_collection(collection_name, qdrant_url, qdrant_api_key):
|
66 |
"""Delete a Qdrant collection."""
|