Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookPrompt + LLMRAGMultiple chainsQuerying a SQL DBAgentsCode writingAdding memoryAdding moderationUsing toolsLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageCookbookRAGOn this pageRAGLet's look at adding in a retrieval step to a prompt and LLM, which adds up to a "retrieval-augmented generation" chainpip install langchain openai faiss-cpu tiktokenfrom operator import itemgetterfrom langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAIfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthrough, RunnableLambdafrom langchain.vectorstores import FAISSvectorstore = FAISS.from_texts(["harrison worked at kensho"], embedding=OpenAIEmbeddings())retriever = vectorstore.as_retriever()template = """Answer the question based only on the following context:{context}Question: {question}"""prompt = ChatPromptTemplate.from_template(template)model = ChatOpenAI()chain = (    {"context": retriever, "question": RunnablePassthrough()}     | prompt     | model     | StrOutputParser())chain.invoke("where did harrison work?")    'Harrison worked at Kensho.'template = """Answer the question based only on the following context:{context}Question: {question}Answer in the following language: {language}"""prompt = ChatPromptTemplate.from_template(template)chain = {    "context": itemgetter("question") | retriever,     "question": itemgetter("question"),     "language": itemgetter("language")} | prompt | model | StrOutputParser()chain.invoke({"question": "where did harrison work", "language": "italian"})    'Harrison ha lavorato a
Let's look at adding in a retrieval step to a prompt and LLM, which adds up to a "retrieval-augmented generation" chain
Let's look at adding in a retrieval step to a prompt and LLM, which adds up to a "retrieval-augmented generation" chain ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookPrompt + LLMRAGMultiple chainsQuerying a SQL DBAgentsCode writingAdding memoryAdding moderationUsing toolsLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãORetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreLangChain Expression LanguageCookbookRAGOn this pageRAGLet's look at adding in a retrieval step to a prompt and LLM, which adds up to a "retrieval-augmented generation" chainpip install langchain openai faiss-cpu tiktokenfrom operator import itemgetterfrom langchain.prompts import ChatPromptTemplatefrom langchain.chat_models import ChatOpenAIfrom langchain.embeddings import OpenAIEmbeddingsfrom langchain.schema.output_parser import StrOutputParserfrom langchain.schema.runnable import RunnablePassthrough, RunnableLambdafrom langchain.vectorstores import FAISSvectorstore = FAISS.from_texts(["harrison worked at kensho"], embedding=OpenAIEmbeddings())retriever = vectorstore.as_retriever()template = """Answer the question based only on the following context:{context}Question: {question}"""prompt = ChatPromptTemplate.from_template(template)model = ChatOpenAI()chain = (    {"context": retriever, "question": RunnablePassthrough()}     | prompt     | model     | StrOutputParser())chain.invoke("where did harrison work?")    'Harrison worked at Kensho.'template = """Answer the question based only on the following context:{context}Question: {question}Answer in the following language: {language}"""prompt = ChatPromptTemplate.from_template(template)chain = {    "context": itemgetter("question") | retriever,     "question": itemgetter("question"),     "language": itemgetter("language")} | prompt | model | StrOutputParser()chain.invoke({"question": "where did harrison work", "language": "italian"})    'Harrison ha lavorato a