Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Python RAG example code


Python RAG example code


Here is an example of how to build a Retrieval Augmented Generation (RAG) application in Python using the LangChain library:

python
from langchain.agents import create_rag_agent
from langchain.chains import RetrievalQA
from langchain.document_loaders import TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.vectorstores import Chroma

# Load your data
loader = TextLoader('your_data.txt')
documents = loader.load()

# Create the vector store
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(documents, embeddings)

# Create the RAG agent
llm = OpenAI()
retriever = vectorstore.as_retriever()
rag_agent = create_rag_agent(llm, retriever)

# Use the RAG agent to answer a query
query = "What is the capital of France?"
result = rag_agent.run(query)
print(result)

Here's a breakdown of what's happening:

1. We first load our data using a `TextLoader` and create a `Chroma` vector store from the documents.
2. We create an `OpenAIEmbeddings` instance to embed the documents, and use that to create the `Chroma` vector store.
3. We create an `OpenAI` language model instance and a retriever from the vector store.
4. We use the `create_rag_agent` function from LangChain to create a RAG agent that combines the language model and retriever.
5. Finally, we use the RAG agent to answer a query by calling the `run` method.

The key aspects of this RAG implementation are:

- Using a vector store (Chroma) to store and retrieve relevant documents
- Combining a language model (OpenAI) with the retriever to generate the final answer
- Leveraging the `create_rag_agent` function from LangChain to set up the RAG agent

This is a basic example, but you can further customize the components (loader, vector store, language model, etc.) to fit your specific use case. The LangChain documentation has more advanced examples and tutorials on building RAG applications.

Citations:
[1] https://www.youtube.com/watch?v=sVcwVQRHIc8
[2] https://github.com/alfredodeza/learn-retrieval-augmented-generation
[3] https://www.youtube.com/watch?v=2TJxpyO3ei4
[4] https://www.freecodecamp.org/news/mastering-rag-from-scratch/
[5] https://python.langchain.com/v0.2/docs/tutorials/rag/