Spaces:
Sleeping
Sleeping
chore: Add CohereRerank and ContextualCompressionRetriever to app.py
Browse files- app.py +10 -1
- 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":
|
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
|