taaha3244 commited on
Commit
dddfd41
1 Parent(s): 5a47e6d

Create retrieve.py

Browse files
Files changed (1) hide show
  1. retrieve.py +93 -0
retrieve.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.vectorstores import Qdrant
2
+ from langchain_core.output_parsers import StrOutputParser
3
+ from langchain_core.runnables import RunnablePassthrough
4
+ from langchain.prompts import PromptTemplate
5
+ from utils import setup_openai_embeddings,setup_qdrant_client,openai_llm,format_document_metadata
6
+
7
+
8
+
9
+ def retrieve_documents(query, api_key, qdrant_url, qdrant_api_key):
10
+ """Retrieve documents based on the specified query."""
11
+ embeddings_model = setup_openai_embeddings(api_key)
12
+ qdrant_client = setup_qdrant_client(qdrant_url, qdrant_api_key)
13
+ qdrant = Qdrant(client=qdrant_client, collection_name="Lex-v1", embeddings=embeddings_model)
14
+ retriever = qdrant.as_retriever(search_kwargs={"k": 5})
15
+ prompt = PromptTemplate(
16
+ template="""
17
+ # Your role
18
+ 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.
19
+ # Instruction
20
+ Your task is to answer the question using the following pieces of retrieved context delimited by XML tags.
21
+ <retrieved context>
22
+ Retrieved Context:
23
+ {context}
24
+ </retrieved context>
25
+ # Constraint
26
+ 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.
27
+ - 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.
28
+ 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.
29
+ 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.
30
+ 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'.
31
+ 5. Use five sentences maximum. Keep the answer concise but logical/natural/in-depth.
32
+ 6. 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
33
+ # Question:
34
+ {question}""",
35
+ input_variables=["context", "question"]
36
+ )
37
+ llm = openai_llm(api_key=api_key)
38
+ rag_chain = (
39
+ {"context": retriever | format_document_metadata, "question": RunnablePassthrough()}
40
+ | prompt
41
+ | llm
42
+ | StrOutputParser()
43
+ )
44
+ return rag_chain.invoke(query)
45
+
46
+ def retrieve_documents_from_collection(query, api_key, qdrant_url, qdrant_api_key, collection_name):
47
+ """Retrieve documents based on the specified query from a specific collection."""
48
+ embeddings_model = setup_openai_embeddings(api_key)
49
+ qdrant_client = setup_qdrant_client(qdrant_url, qdrant_api_key)
50
+ qdrant = Qdrant(client=qdrant_client, collection_name=collection_name, embeddings=embeddings_model)
51
+ retriever = qdrant.as_retriever(search_kwargs={"k": 5})
52
+ prompt = PromptTemplate(
53
+ template="""
54
+ # Your role
55
+ 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.
56
+ # Instruction
57
+ Your task is to answer the question using the following pieces of retrieved context delimited by XML tags.
58
+ <retrieved context>
59
+ Retrieved Context:
60
+ {context}
61
+ </retrieved context>
62
+ # Constraint
63
+ 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.
64
+ - 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.
65
+ 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.
66
+ 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.
67
+ 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'.
68
+ 5. Use five sentences maximum. Keep the answer concise but logical/natural/in-depth.
69
+ 6. 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
70
+ # Question:
71
+ {question}""",
72
+ input_variables=["context", "question"]
73
+ )
74
+ llm = openai_llm(api_key=api_key)
75
+ rag_chain = (
76
+ {"context": retriever | format_document_metadata, "question": RunnablePassthrough()}
77
+ | prompt
78
+ | llm
79
+ | StrOutputParser()
80
+ )
81
+ return rag_chain.invoke(query)
82
+
83
+ def delete_collection(collection_name, qdrant_url, qdrant_api_key):
84
+ """Delete a Qdrant collection."""
85
+ client = setup_qdrant_client(qdrant_url, qdrant_api_key)
86
+ try:
87
+ client.delete_collection(collection_name=collection_name)
88
+ except Exception as e:
89
+ print("Failed to delete collection:", e)
90
+
91
+ def is_document_embedded(filename):
92
+ """Check if a document is already embedded. Actual implementation needed."""
93
+ return False