add itemgetter to fetch context from the vectorstore
Browse files
app.py
CHANGED
@@ -12,6 +12,7 @@ from langchain.schema.output_parser import StrOutputParser
|
|
12 |
from langchain.schema.runnable import RunnablePassthrough
|
13 |
from langchain.schema.runnable.config import RunnableConfig
|
14 |
|
|
|
15 |
# GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
|
16 |
# ---- ENV VARIABLES ---- #
|
17 |
"""
|
@@ -136,7 +137,7 @@ async def start_chat():
|
|
136 |
"""
|
137 |
|
138 |
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
|
139 |
-
lcel_rag_chain = rag_prompt | hf_llm
|
140 |
|
141 |
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
142 |
|
@@ -150,23 +151,11 @@ async def main(message: cl.Message):
|
|
150 |
The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
|
151 |
"""
|
152 |
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
153 |
-
|
154 |
-
# Retrieve context for the user's query
|
155 |
-
context = hf_retriever.retrieve(query=message.content)
|
156 |
-
|
157 |
-
# Combine context into a single string
|
158 |
-
context_text = "\n".join([doc.page_content for doc in context])
|
159 |
-
|
160 |
-
# Prepare input for the prompt
|
161 |
-
input_dict = {
|
162 |
-
"query": message.content,
|
163 |
-
"context": context_text
|
164 |
-
}
|
165 |
|
166 |
msg = cl.Message(content="")
|
167 |
|
168 |
async for chunk in lcel_rag_chain.astream(
|
169 |
-
|
170 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
171 |
):
|
172 |
await msg.stream_token(chunk)
|
|
|
12 |
from langchain.schema.runnable import RunnablePassthrough
|
13 |
from langchain.schema.runnable.config import RunnableConfig
|
14 |
|
15 |
+
|
16 |
# GLOBAL SCOPE - ENTIRE APPLICATION HAS ACCESS TO VALUES SET IN THIS SCOPE #
|
17 |
# ---- ENV VARIABLES ---- #
|
18 |
"""
|
|
|
137 |
"""
|
138 |
|
139 |
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
|
140 |
+
lcel_rag_chain = {"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}| rag_prompt | hf_llm
|
141 |
|
142 |
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
143 |
|
|
|
151 |
The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
|
152 |
"""
|
153 |
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
msg = cl.Message(content="")
|
156 |
|
157 |
async for chunk in lcel_rag_chain.astream(
|
158 |
+
{"query": message.content},
|
159 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
160 |
):
|
161 |
await msg.stream_token(chunk)
|