anpigon commited on
Commit
5e4d301
β€’
1 Parent(s): b2a6df2

chore: Add CohereRerank and ContextualCompressionRetriever to app.py

Browse files
Files changed (2) hide show
  1. app.py +10 -1
  2. requirements.txt +1 -0
app.py CHANGED
@@ -27,6 +27,9 @@ from langchain_groq import ChatGroq
27
  from langchain_openai import ChatOpenAI, OpenAIEmbeddings
28
  from langchain_text_splitters import Language, RecursiveCharacterTextSplitter
29
 
 
 
 
30
  # Load environment variables
31
  load_dotenv()
32
 
@@ -164,6 +167,12 @@ ensemble_retriever = EnsembleRetriever(
164
  weights=[0.4, 0.3, 0.3],
165
  )
166
 
 
 
 
 
 
 
167
  # Create prompt template
168
  prompt = PromptTemplate.from_template(
169
  """당신은 20λ…„μ°¨ AI κ°œλ°œμžμž…λ‹ˆλ‹€. λ‹Ήμ‹ μ˜ μž„λ¬΄λŠ” 주어진 μ§ˆλ¬Έμ— λŒ€ν•˜μ—¬ μ΅œλŒ€ν•œ λ¬Έμ„œμ˜ 정보λ₯Ό ν™œμš©ν•˜μ—¬ λ‹΅λ³€ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.
@@ -237,7 +246,7 @@ llm = ChatOpenAI(
237
 
238
  # Create retrieval-augmented generation chain
239
  rag_chain = (
240
- {"context": ensemble_retriever, "question": RunnablePassthrough()}
241
  | prompt
242
  | llm
243
  | StrOutputParser()
 
27
  from langchain_openai import ChatOpenAI, OpenAIEmbeddings
28
  from langchain_text_splitters import Language, RecursiveCharacterTextSplitter
29
 
30
+ from langchain_cohere import CohereRerank
31
+ from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
32
+
33
  # Load environment variables
34
  load_dotenv()
35
 
 
167
  weights=[0.4, 0.3, 0.3],
168
  )
169
 
170
+ compressor = CohereRerank(model="rerank-multilingual-v3.0", top_n=10)
171
+ compression_retriever = ContextualCompressionRetriever(
172
+ base_compressor=compressor,
173
+ base_retriever=ensemble_retriever,
174
+ )
175
+
176
  # Create prompt template
177
  prompt = PromptTemplate.from_template(
178
  """당신은 20λ…„μ°¨ AI κ°œλ°œμžμž…λ‹ˆλ‹€. λ‹Ήμ‹ μ˜ μž„λ¬΄λŠ” 주어진 μ§ˆλ¬Έμ— λŒ€ν•˜μ—¬ μ΅œλŒ€ν•œ λ¬Έμ„œμ˜ 정보λ₯Ό ν™œμš©ν•˜μ—¬ λ‹΅λ³€ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.
 
246
 
247
  # Create retrieval-augmented generation chain
248
  rag_chain = (
249
+ {"context": compression_retriever, "question": RunnablePassthrough()}
250
  | prompt
251
  | llm
252
  | StrOutputParser()
requirements.txt CHANGED
@@ -11,4 +11,5 @@ langchain-anthropic==0.1.15
11
  langchain-openai==0.1.8
12
  langchain-core==0.2.5
13
  langchain-groq==0.1.5
 
14
  chromadb==0.5.0
 
11
  langchain-openai==0.1.8
12
  langchain-core==0.2.5
13
  langchain-groq==0.1.5
14
+ langchain_cohere==0.1.7
15
  chromadb==0.5.0