Build RAG Application Using Ollama

In this tutorial, we will build a Retrieval Augmented Generation(RAG) Application using Ollama and Langchain. For the vector store, we will be using Chroma, but you are free to use any vector store of your choice.

There are 4 key steps to building your RAG application –

  1. Load your documents
  2. Add them to the vector store using the embedding function of your choice.
  3. Define your prompt template.
  4. Deinfe your Retrieval Chatbot using the LLM of your choice.

In case you want the collab notebook, you can click here.

First we load the required libraries.

# Loading required libraries
import os

from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain.memory import ConversationSummaryMemory
from langchain_openai import OpenAIEmbeddings
from langchain.prompts import PromptTemplate
from langchain.llms import Ollama

Then comes step 1 which is to load our documents. Here I’ll be using Elden Ring Wiki PDF, you can just visit the Wikipedia page and download it as a PDF file.

data_path = "./data/Elden_Ring.pdf"
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=2000,
chunk_overlap=30,
length_function=len,)

documents = PyPDFLoader(data_path).load_and_split(text_splitter=text_splitter)

The next step is to use an embedding function that will convert our text into embeddings. I prefer using OpenAI embeddings, but you can use any embedding function. Using this embedding function we will add our documents to the Chroma vector database.

embedding_func = OpenAIEmbeddings(api_key=os.environ.get("OPENAI_API_KEY"))
vectordb = Chroma.from_documents(documents, embedding=embedding_func)

Moving on, we have to define a prompt template. I’ll be using the mistral model, so its a very basic prompt template that mistral provides.

template = """<s>[INST] Given the context - {context} </s>[INST] [INST] Answer the following question - {question}[/INST]"""
pt = PromptTemplate(
template=template, input_variables=["context", "question"]
)

All that is left to do is to define our memory and Retrieval Chatbot using Ollama as the LLM.

rag = RetrievalQA.from_chain_type(
llm=Ollama(model="mistral"),
retriever=vectordb.as_retriever(),
memory=ConversationSummaryMemory(llm = Ollama(model="mistral")),
chain_type_kwargs={"prompt": pt, "verbose": True},
)
rag.invoke("What is Elden Ring ?")
>>> {'query': 'What is Elden Ring ?',
'history': '',
'result': ' Elden Ring is a 2022 action role-playing game developed by FromSoftware. It was published for PlayStation 4, PlayStation 5, Windows, Xbox One, and Xbox Series X/S. In the game, players control a customizable character on a quest to repair the Elden Ring and become the new Elden Lord. The game is set in an open world, presented through a third-person perspective, and includes several types of weapons and magic spells. Players can traverse the six main areas using their steed Torrent and discover linear hidden dungeons and checkpoints that enable fast travel and attribute improvements. Elden Ring features online multiplayer mode for cooperative play or player-versus-player combat. The game was developed with inspirations from Dark Souls series, and contributions from George R.R. Martin on the narrative and Tsukasa Saitoh, Shoi Miyazawa, Tai Tomisawa, Yuka Kitamura, and Yoshimi Kudo for the original soundtrack. Elden Ring received critical acclaim for its open world, gameplay systems, and setting, with some criticism for technical performance. It sold over 20 million copies and a downloadable content expansion, Shadow of the Erdtree, is planned to be released in June 2024.'}

We see that it was even able to tell us when Shadow of the Erdtree is planned to release for which I’m really excited about. Let me know in the comments if you want to cover anything else.

Comments

One response to “Build RAG Application Using Ollama”

  1. Is Llama 3 Really Better Than Mistral? – ML EXPLAINED Avatar

    […] using Ollama. In case you want to know how you can do it yourself, you can check out this post. I used the Elden Ring Wikipedia article as the document for contextual retrieval. I was using […]

    Like

Leave a comment