RAG and Ollama: A Practical Guide

Are you still troubled by the mixed quality of AI in China and its poor performance?

Then let’s take a look at Developer Cat AI (3in1)!

This is an integrated AI assistant that combines GPT-4, Claude3, and Gemini.

It covers all models of the three AI tools.

Including GPT-4o and Gemini flash

Now you can own them for just ¥68.

The official value is ¥420+.

Send “Developer Cat” in the background to start using it.

Become a member now to enjoy one-on-one personal service, ensuring your usage is safeguarded.

RAG and Ollama: A Practical Guide

In the field of Natural Language Processing (NLP), the combination of retrieval and generation functionalities has brought significant advancements. Retrieval-Augmented Generation (RAG) improves the quality of generated text by integrating external information sources. This article demonstrates how to create a RAG system using free large language models (LLMs). We will use OLLAMA and the LLaMA 3 model, providing a practical method to leverage cutting-edge NLP technology at no cost. Whether you are a developer, researcher, or enthusiast, this guide will help you implement a RAG system efficiently and effectively.

Note: Before proceeding, you need to download and run Ollama. You can click here to download.

Here’s an example of how to set up a very basic but intuitive RAG.

Import Libraries

import os
from langchain_community.llms import Ollama
from dotenv import load_dotenv
from langchain_community.embeddings import OllamaEmbeddings
from langchain.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import Chroma
from langchain.chains import create_retrieval_chain
from langchain import hub
from langchain.chains.combine_documents import create_stuff_documents_chain

Load LLM (Language Model)

llm = Ollama(model="llama3.1", base_url="http://127.0.0.1:11434")

Set Up Ollama Embeddings

embed_model = OllamaEmbeddings(
    model="llama3.1",
    base_url='http://127.0.0.1:11434'
)

Loading text…

text = """
    In the lush canopy of a tropical rainforest, two mischievous monkeys, Coco and Mango, swung from branch to branch, their playful antics echoing through the trees. They were inseparable companions, sharing everything from juicy fruits to secret hideouts high above the forest floor. One day, while exploring a new part of the forest, Coco stumbled upon a beautiful orchid hidden among the foliage. Entranced by its delicate petals, Coco plucked it and presented it to Mango with a wide grin. Overwhelmed by Coco's gesture of friendship, Mango hugged Coco tightly, cherishing the bond they shared. From that day on, Coco and Mango ventured through the forest together, their friendship growing stronger with each passing adventure. As they watched the sun dip below the horizon, casting a golden glow over the treetops, they knew that no matter what challenges lay ahead, they would always have each other, and their hearts brimmed with joy.
    """

Split Text into Chunks

text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=128)
chunks = text_splitter.split_text(text)

Create Vector Store from Texts (Chroma)

vector_store = Chroma.from_texts(chunks, embed_model)

Create a Retriever

retriever = vector_store.as_retriever()

Create Retrieval Chain

chain = create_retrieval_chain(combine_docs_chain=llm,retriever=retriever)

Retrieval-QA Chat Prompt

retrieval_qa_chat_prompt = hub.pull("langchain-ai/retrieval-qa-chat")

Combine Documents

combine_docs_chain = create_stuff_documents_chain(
    llm, retrieval_qa_chat_prompt
)

Final Retrieval Chain

retrieval_chain = create_retrieval_chain(retriever, combine_docs_chain)

Invoke Retrieval Chain

response = retrieval_chain.invoke({"input": "Tell me name of monkeys and where do they live"})
print(response['answer'])
Recently, some friends and experts have formed a community for discussing RAG and AGENT, where many experts in AnythingLLM and Ollama communicate. If you want to join us, just scan the QR code below.
RAG and Ollama: A Practical Guide
Previous popular articles:
① Ollama Model Management Tool – Gollama (78)
② Xorbits Inference: The Strongest Competitor to Ollama (73)
③ Environment Variables that can be Set in Ollama (68)

If this is helpful to you, don’t hesitate to hit “Share and View” before leaving.🫦

Leave a Comment