Hello @vkudlay , I’m at the final of the Building RAG course and stuck at this point where my RAG route is not working. Can anyone please help to guide me what I am missing ?
This is my code:
`%%writefile server_app.py
🦜️🏓 LangServe | 🦜️🔗 LangChain
from fastapi import FastAPI
from langserve import add_routes
from langchain_nvidia_ai_endpoints import ChatNVIDIA, NVIDIAEmbeddings
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableLambda, RunnableBranch
from langchain_core.runnables.passthrough import RunnableAssign
from langchain.document_transformers import LongContextReorder
from langchain_community.vectorstores import FAISS
from operator import itemgetter
app = FastAPI(
title=“LangChain Server”,
version=“1.0”,
description=“A simple api server using Langchain’s Runnable interfaces”,
)
llm = ChatNVIDIA(model=‘mixtral_8x7b’)
chat_prompt = ChatPromptTemplate.from_messages([(“system”,
“You are a document chatbot. Help the user as they ask questions about documents.”
" User messaged just asked you a question: {input}\n\n"
" The following information may be useful for your response: "
" Document Retrieval:\n{context}\n\n"
" (Answer only from retrieval. Only cite sources that are used. Make your response conversational)"
), (‘user’, ‘{input}’)])
embedder = NVIDIAEmbeddings(model=‘nvolveqa_40k’)
docstore = FAISS.load_local(“docstore_index”, embedder)
docs = list(docstore.docstore._dict.values())
def docs2str(docs, title=“Document”):
“”“Useful utility for making chunks into context string. Optional, but useful”“”
out_str = “”
for doc in docs:
doc_name = getattr(doc, ‘metadata’, {}).get(‘Title’, title)
if doc_name: out_str += f"[Quote from {doc_name}] "
out_str += getattr(doc, ‘page_content’, str(doc)) + “\n”
return out_str
def output_puller(inputs):
“”““Output generator. Useful if your chain returns a dictionary with key ‘output’””"
for token in inputs:
if token.get(‘output’):
yield token.get(‘output’)
long_reorder = RunnableLambda(LongContextReorder().transform_documents) ## GIVEN
context_getter = itemgetter(‘input’) | docstore.as_retriever() | long_reorder | docs2str
retrieval_chain = {‘input’ : (lambda x: x)} | RunnableAssign({‘context’ : context_getter})
generator_chain = RunnableAssign({“output” : chat_prompt | llm }) ## TODO
generator_chain = generator_chain | output_puller ## GIVEN
rag_chain = retrieval_chain | generator_chain
add_routes(
app,
llm,
path=“/basic_chat”,
)
add_routes(
app,
retrieval_chain,
path=“/retriever”,
)
add_routes(
app,
generator_chain,
path=“/generator”,
)
if name == “main”:
import uvicorn
uvicorn.run(app, host=“0.0.0.0”, port=9012)`