In this tutorial, you’ll learn how to build a local Retrieval-Augmented Generation (RAG) AI agent using Python.

This agent will run entirely on your machine and leverage:
- Ollama for open-source LLMs and embeddings
- LangChain for orchestration
- SingleStore as the vector store
By the end of this tutorial, you’ll have a fully working Q+A system powered by your local data and models.
Prerequisites
Ensure the following are installed and configured:
Step 1. Set up Ollama
Install Ollama
Visit ollama.com and download the installer for your OS. Follow the on-screen instructions.
Download the required models
We'll use:
llama3.2
for LLM-based question answeringmxbai-embed-large
for generating vector embeddings
Run the following in your terminal:
1
ollama serve2
ollama pull llama33
ollama pull mxbai-embed-large
.png?width=1024&disable=upscale&auto=webp)
Step 2. Prepare the Python environment
We'll use Poetry for dependency management.
Install Poetry (if not already installed)
1
pip install poetry
Create a new project and add dependencies:
1
poetry new ai_tutorial2
cd ai_tutorial3
poetry add langchain langchain-ollama langchain-singlestore docker pandas
Step 3. Prepare your data
Assume you have a CSV file of pizza reviews (pizza_reviews.csv). Here’s an example row:
1
Best Pizza in Town!,2025-01-06,1,The crust was perfectly crispy and the2
toppings were fresh. Will definitely return!3
Delicious and Affordable,2024-08-12,1,"A bit overpriced for what you get, but4
overall a decent meal."5
Mediocre Experience,2025-01-05,1,Incredible flavors! The margherita pizza6
reminded me of Italy.7
Absolutely Loved It!,2024-06-14,5,"Service was slow and the place was crowded,8
but the pizza made up for it."9
Won't Be Coming Back,2025-03-15,2,Disappointed by the soggy crust and bland10
sauce. Expected better.11
A Slice of Heaven,2024-10-16,2,Amazing pizza with a great balance of cheese and12
sauce.13
Perfect for a Quick Bite,2024-08-21,2,"Nothing special, just an average pizza14
place."15
Not Worth the Hype,2024-10-21,4,Great for families. My kids loved the pepperoni16
pizza!17
Family Favorite,2024-11-15,4,The truffle mushroom pizza is a must-try!
Step 4. Run SingleStore locally using Docker
The following Python script provided will automatically start a SingleStore instance using Docker. Just make sure Docker is running.
Step 5. The Python code
Here’s the core logic of your AI agent. Save the following code to ai_tutorial/main.py
:
1
import pandas as pd2
from singlestoredb.server import docker3
from langchain_ollama import OllamaLLM, OllamaEmbeddings4
from langchain_singlestore import SingleStoreVectorStore5
from langchain_core.prompts import ChatPromptTemplate6
from langchain_core.documents import Document7
8
9
def setup_database(s2db):10
"""Initialize the SingleStore database."""11
with s2db.connect() as conn:12
with conn.cursor() as cursor:13
cursor.execute("CREATE DATABASE IF NOT EXISTS testdb")14
15
16
def load_documents():17
"""Load pizza reviews from CSV and convert to Document objects."""18
df = pd.read_csv("pizza_reviews.csv")19
documents = []20
for i, row in df.iterrows():21
content = f"{row['Title']} {row['Review']}"22
documents.append(23
Document(24
page_content=content,25
metadata={"rating": row["Rating"], "date": row["Date"]},26
id=str(i)27
)28
)29
return documents30
31
32
def main():33
"""34
Run a pizza review Q&A application using SingleStoreDB vector store and35
LLaMA.36
37
This example demonstrates:38
1. Setting up a vector database with SingleStoreDB39
2. Embedding pizza reviews with Ollama40
3. Creating a retrieval-based QA system that answers questions about pizza41
reviews42
"""43
print("Starting SingleStoreDB server for vector storage...")44
with docker.start(license="") as s2db:45
setup_database(s2db)46
47
print("Loading and embedding pizza reviews...")48
documents = load_documents()49
embedding = OllamaEmbeddings(model="mxbai-embed-large")50
51
# Set up vector store with the embedded documents52
vector_store = SingleStoreVectorStore(53
embedding=embedding,54
host=s2db.connection_url,55
database="testdb",56
table_name="pizza_reviews",57
)58
vector_store.add_documents(documents)59
60
# Create retriever that fetches the 2 most relevant reviews for each61
query62
retriever = vector_store.as_retriever(search_kwargs={"k": 2})63
64
print("Initializing LLaMA 3.2 model...")65
model = OllamaLLM(model="llama3.2")66
67
# Define prompt template with clean formatting68
template = """69
You are an expert in answering questions about a pizza restaurant.70
71
Here are some relevant reviews: {reviews}72
73
Here is the question: {question}74
"""75
prompt = ChatPromptTemplate.from_template(template)76
chain = prompt | model77
78
print("\n------------------------------------------")79
print("Pizza Review Question & Answer System")80
print("Ask questions about pizza reviews, and the system will find81
relevant reviews")82
print("and generate an answer based on those reviews.")83
print("------------------------------------------\n")84
85
while True:86
user_input = input("\nEnter your question about pizza (or 'exit' to87
quit): ")88
if user_input.lower() == "exit":89
break90
print("\nFinding relevant reviews and generating answer...")91
reviews = retriever.invoke(user_input)92
result = chain.invoke({"reviews": reviews, "question": user_input})93
94
print("\n--- Answer ---")95
print(result)96
97
98
if __name__ == "__main__":99
main()
1
poetry run python ai_tutorial/main.py
1
Starting SingleStoreDB server for vector storage...2
Loading and embedding pizza reviews...3
Initializing LLaMA 3.2 model...4
5
------------------------------------------6
Pizza Review Question & Answer System7
Ask questions about pizza reviews, and the system will find relevant reviews8
and generate an answer based on those reviews.9
------------------------------------------10
11
12
Enter your question about pizza (or 'exit' to quit): What do people think about13
the crust?14
15
Finding relevant reviews and generating answer...16
--- Answer ---17
Based on the reviews provided, it seems that opinions about the crust are18
mixed.19
20
One reviewer mentioned that their base (likely referring to the crust) was21
soggy, which may indicate issues with the cooking or handling of the crust. On22
the other hand, another reviewer stated that they found the crust too salty and23
also mentioned that it was burnt, suggesting a problem with the quality control24
or preparation of the crust.25
26
Overall, while not all reviews were negative, these two opinions suggest that27
some customers have had issues with the crust in terms of texture (soggy) and28
flavor (burnt).29
30
Enter your question about pizza (or 'exit' to quit): Are there vegetarian31
options?32
33
Finding relevant reviews and generating answer...34
35
--- Answer ---36
Unfortunately, based on the reviews provided, it seems that this pizza37
restaurant may not have many vegetarian options. The first review from38
2024-06-22 mentions "Needs Improvement" and specifically says "Wish there were39
more vegetarian options." While the second review is positive, it doesn't40
mention anything about vegetarian options, implying that they might not be41
available or plentiful at this establishment.42
43
Enter your question about pizza (or 'exit' to quit): Is the pizza overpriced?44
45
Finding relevant reviews and generating answer...46
47
--- Answer ---48
Based on the reviews provided, it seems that opinions about the pricing of the49
pizza are mixed.50
51
The first review (Document #6) states that "Nothing special, just an average52
pizza place." while giving a rating of 2 out of presumably 5. This suggests53
that the reviewer was somewhat disappointed with their experience and didn't54
think it was particularly good or worth the price.55
56
On the other hand, the second review (Document #7) gives a higher rating (4 out57
of 5) but notes that "Not Worth the Hype". Although the reviewer mentions that58
it's "Great for families" and their kids enjoyed the pizza, they don't mention59
anything about the pricing being over or under par.60
61
Based on these reviews alone, I would say that opinions about the price are not62
strongly supported either way, but some reviewers do seem to think it might be63
a bit pricey (although this is purely based on limited evidence).64
65
Enter your question about pizza (or 'exit' to quit): exit
You’ve now built a local RAG application that uses:
- Open-source LLMs via Ollama
- LangChain for orchestration
- SingleStore for vector storage
— all running locally on your machine.
Feel free to experiment with:
- Different LLMs or embedding models via Ollama
- Other datasets
- Custom prompt templates
Ready to build your own AI agent with SingleStore? Start free today.