taaha3244 commited on
Commit
4e5ac28
1 Parent(s): 9c11e6b

Update retrieve.py

Browse files
Files changed (1) hide show
  1. retrieve.py +24 -12
retrieve.py CHANGED
@@ -22,31 +22,43 @@ def retrieve_documents_from_collection(query, api_key, qdrant_url, qdrant_api_ke
22
  embeddings_model = setup_openai_embeddings(api_key)
23
  qdrant_client = setup_qdrant_client(qdrant_url, qdrant_api_key)
24
  qdrant = Qdrant(client=qdrant_client, collection_name=collection_name, embeddings=embeddings_model)
25
- retriever = qdrant.as_retriever(search_kwargs={"k": 10})
26
- prompt = PromptTemplate(
27
- template="""
 
 
 
 
28
  # Your role
29
- 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.
 
 
30
  # Instruction
31
- Your task is to answer the question using the following pieces of retrieved context delimited by XML tags, and to answer in the same language as the question.
32
- <retrieved context>
 
33
  Retrieved Context:
34
  {context}
35
  </retrieved context>
 
 
36
  # Constraint
37
  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.
38
  - 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.
39
- 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.
40
  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.
41
- 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'.
42
- 5. At the end of the response provide metadata provided in the relevant docs, For example:"Metadata: page: 19, source: /content/OCR_RSCA/Analyse docs JVB + mails et convention FOOT INNOVATION.pdf'. Return just the page and source. Provide a list of all the metadata found in the relevant content formatted as bullets
 
 
 
43
  # Question:
44
  {question}""",
45
- input_variables=["context", "question"]
46
- )
47
  llm = openai_llm(api_key=api_key)
48
  rag_chain = (
49
- {"context": retriever | format_document_metadata, "question": RunnablePassthrough()}
50
  | prompt
51
  | llm
52
  | StrOutputParser()
 
22
  embeddings_model = setup_openai_embeddings(api_key)
23
  qdrant_client = setup_qdrant_client(qdrant_url, qdrant_api_key)
24
  qdrant = Qdrant(client=qdrant_client, collection_name=collection_name, embeddings=embeddings_model)
25
+ retriever = qdrant.as_retriever(search_kwargs={"k": 20})
26
+ llm = Cohere(temperature=0,cohere_api_key=userdata.get('COHERE_API_KEY'))
27
+ compressor = CohereRerank(cohere_api_key=os.getenv('COHERE_API_KEY'),top_n=5)
28
+ compression_retriever = ContextualCompressionRetriever(
29
+ base_compressor=compressor, base_retriever=retriever
30
+ )
31
+ template="""
32
  # Your role
33
+ 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.
34
+
35
+
36
  # Instruction
37
+ Your task is to answer the question using the following pieces of retrieved context delimited by XML tags.
38
+
39
+ <retrieved context>
40
  Retrieved Context:
41
  {context}
42
  </retrieved context>
43
+
44
+
45
  # Constraint
46
  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.
47
  - 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.
48
+ 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.
49
  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.
50
+ 4. When you don't have retrieved context for the question or If you have a 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'.
51
+ 5. If required break the answer into proper paragraphs.
52
+ 6. Mention Name of all the documents and page number 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. Remeber to include all of the Document names and pages. Dont missout
53
+
54
+
55
  # Question:
56
  {question}""",
57
+ input_variables=["context","question"]
58
+ )
59
  llm = openai_llm(api_key=api_key)
60
  rag_chain = (
61
+ {"context": compression_retriever | format_document_metadata, "question": RunnablePassthrough()}
62
  | prompt
63
  | llm
64
  | StrOutputParser()