Creating a RAG chain
Now to bring all the components together in your RAG workflow! You've prepared the documents and ingested them into a Chroma database for retrieval. You created a prompt template to include the retrieved chunks from the academic paper and answer questions.
The prompt template you created in the previous exercise is available as prompt_template, an OpenAI model has been initialized as llm, and the code to recreate your retriever has be included in the script.
Questo esercizio fa parte del corso
Developing LLM Applications with LangChain
Istruzioni dell'esercizio
- Create an LCEL chain using the pipe operator (
|) to connectretriever,prompt_template, andllmin sequence. - The chain should map the retriever to
"context"and user input to"question". - Invoke the chain using the
.invoke()method on the question provided.
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
vectorstore = Chroma.from_documents(
docs,
embedding=OpenAIEmbeddings(api_key='', model='text-embedding-3-small'),
persist_directory=os.getcwd()
)
retriever = vectorstore.as_retriever(
search_type="similarity",
search_kwargs={"k": 3}
)
# Create a chain to link retriever, prompt_template, and llm
rag_chain = ({"context": ____, "question": ____}
| prompt_template
| llm)
# Invoke the chain
response = rag_chain.____("Which popular LLMs were considered in the paper?")
print(response.content)