Ritesh-hf commited on
Commit
51e41ab
·
verified ·
1 Parent(s): 10732be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -20,6 +20,8 @@ from pinecone_text.sparse import BM25Encoder
20
  from langchain_huggingface import HuggingFaceEmbeddings
21
  from langchain_community.retrievers import PineconeHybridSearchRetriever
22
  from langchain_groq import ChatGroq
 
 
23
 
24
  # Load environment variables
25
  # load_dotenv(".env")
@@ -56,14 +58,9 @@ def initialize_pinecone(index_name: str):
56
  ##################################################
57
  ## Change down here
58
  ##################################################
59
-
60
-
61
  # #### This is for UAE Economic Department Website
62
  pinecone_index = initialize_pinecone("saudi-arabia-ministry-of-justice")
63
  bm25 = BM25Encoder().load("./saudi-arabia-bm25-encoder.json")
64
-
65
-
66
-
67
  ##################################################
68
  ##################################################
69
 
@@ -74,13 +71,19 @@ retriever = PineconeHybridSearchRetriever(
74
  embeddings=embed_model,
75
  sparse_encoder=bm25,
76
  index=pinecone_index,
77
- top_k=20,
78
  alpha=0.5
79
  )
80
 
81
  # Initialize LLM
82
  llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0, max_tokens=1024, max_retries=2)
83
 
 
 
 
 
 
 
84
  # Contextualization prompt and retriever
85
  contextualize_q_system_prompt = """Given a chat history and the latest user question \
86
  which might reference context in the chat history, formulate a standalone question \
@@ -94,7 +97,7 @@ contextualize_q_prompt = ChatPromptTemplate.from_messages(
94
  ("human", "{input}")
95
  ]
96
  )
97
- history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_q_prompt)
98
 
99
  # QA system prompt and chain
100
  qa_system_prompt = """You are a highly skilled information retrieval assistant. Use the following context to answer questions effectively. \
@@ -114,7 +117,7 @@ When responding to queries, follow these guidelines: \
114
  - Downloadable Materials: Provide links to any relevant downloadable resources if applicable. \
115
 
116
  3. Formatting for Readability: \
117
- - The answer should be in a proper HTML format with appropriate tags. \
118
  - For arabic language response align the text to right and convert numbers also.
119
  - Double check if the language of answer is correct or not.
120
  - Use bullet points or numbered lists where applicable to present information clearly. \
 
20
  from langchain_huggingface import HuggingFaceEmbeddings
21
  from langchain_community.retrievers import PineconeHybridSearchRetriever
22
  from langchain_groq import ChatGroq
23
+ from langchain.retrievers import ContextualCompressionRetriever
24
+ from langchain.retrievers.document_compressors import FlashrankRerank
25
 
26
  # Load environment variables
27
  # load_dotenv(".env")
 
58
  ##################################################
59
  ## Change down here
60
  ##################################################
 
 
61
  # #### This is for UAE Economic Department Website
62
  pinecone_index = initialize_pinecone("saudi-arabia-ministry-of-justice")
63
  bm25 = BM25Encoder().load("./saudi-arabia-bm25-encoder.json")
 
 
 
64
  ##################################################
65
  ##################################################
66
 
 
71
  embeddings=embed_model,
72
  sparse_encoder=bm25,
73
  index=pinecone_index,
74
+ top_k=10,
75
  alpha=0.5
76
  )
77
 
78
  # Initialize LLM
79
  llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0, max_tokens=1024, max_retries=2)
80
 
81
+ # Initialize Reranker
82
+ compressor = FlashrankRerank()
83
+ compression_retriever = ContextualCompressionRetriever(
84
+ base_compressor=compressor, base_retriever=retriever
85
+ )
86
+
87
  # Contextualization prompt and retriever
88
  contextualize_q_system_prompt = """Given a chat history and the latest user question \
89
  which might reference context in the chat history, formulate a standalone question \
 
97
  ("human", "{input}")
98
  ]
99
  )
100
+ history_aware_retriever = create_history_aware_retriever(llm, compression_retriever, contextualize_q_prompt)
101
 
102
  # QA system prompt and chain
103
  qa_system_prompt = """You are a highly skilled information retrieval assistant. Use the following context to answer questions effectively. \
 
117
  - Downloadable Materials: Provide links to any relevant downloadable resources if applicable. \
118
 
119
  3. Formatting for Readability: \
120
+ - The answer should be in a proper Markdown format with appropriate tags. \
121
  - For arabic language response align the text to right and convert numbers also.
122
  - Double check if the language of answer is correct or not.
123
  - Use bullet points or numbered lists where applicable to present information clearly. \