Build a Local AI Agent with Python, Ollama, LangChain and SingleStore

Clock Icon

2 min read

Pencil Icon

May 21, 2025

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

Build a Local AI Agent with Python, Ollama, LangChain and SingleStore

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.

prerequisitesPrerequisites

Ensure the following are installed and configured:

step-1-set-up-ollamaStep 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 answering
  • mxbai-embed-large for generating vector embeddings

Run the following in your terminal:

1

ollama serve

2

ollama pull llama3

3

ollama pull mxbai-embed-large

step-2-prepare-the-python-environmentStep 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_tutorial

2

cd ai_tutorial

3

poetry add langchain langchain-ollama langchain-singlestore docker pandas

step-3-prepare-your-dataStep 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 the

2

toppings were fresh. Will definitely return!

3

Delicious and Affordable,2024-08-12,1,"A bit overpriced for what you get, but

4

overall a decent meal."

5

Mediocre Experience,2025-01-05,1,Incredible flavors! The margherita pizza

6

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 bland

10

sauce. Expected better.

11

A Slice of Heaven,2024-10-16,2,Amazing pizza with a great balance of cheese and

12

sauce.

13

Perfect for a Quick Bite,2024-08-21,2,"Nothing special, just an average pizza

14

place."

15

Not Worth the Hype,2024-10-21,4,Great for families. My kids loved the pepperoni

16

pizza!

17

Family Favorite,2024-11-15,4,The truffle mushroom pizza is a must-try!

step-4-run-single-store-locally-using-dockerStep 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-codeStep 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 pd

2

from singlestoredb.server import docker

3

from langchain_ollama import OllamaLLM, OllamaEmbeddings

4

from langchain_singlestore import SingleStoreVectorStore

5

from langchain_core.prompts import ChatPromptTemplate

6

from langchain_core.documents import Document

7

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 documents

30

31

32

def main():

33

"""

34

Run a pizza review Q&A application using SingleStoreDB vector store and

35

LLaMA.

36

37

This example demonstrates:

38

1. Setting up a vector database with SingleStoreDB

39

2. Embedding pizza reviews with Ollama

40

3. Creating a retrieval-based QA system that answers questions about pizza

41

reviews

42

"""

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 documents

52

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 each

61

query

62

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 formatting

68

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 | model

77

78

print("\n------------------------------------------")

79

print("Pizza Review Question & Answer System")

80

print("Ask questions about pizza reviews, and the system will find

81

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' to

87

quit): ")

88

if user_input.lower() == "exit":

89

break

90

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()

run-the-applicationRun the application

Simply start the application from the command line

1

poetry run python ai_tutorial/main.py

conversation-exampleConversation example

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 System

7

Ask questions about pizza reviews, and the system will find relevant reviews

8

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 about

13

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 are

18

mixed.

19

20

One reviewer mentioned that their base (likely referring to the crust) was

21

soggy, which may indicate issues with the cooking or handling of the crust. On

22

the other hand, another reviewer stated that they found the crust too salty and

23

also mentioned that it was burnt, suggesting a problem with the quality control

24

or preparation of the crust.

25

26

Overall, while not all reviews were negative, these two opinions suggest that

27

some customers have had issues with the crust in terms of texture (soggy) and

28

flavor (burnt).

29

30

Enter your question about pizza (or 'exit' to quit): Are there vegetarian

31

options?

32

33

Finding relevant reviews and generating answer...

34

35

--- Answer ---

36

Unfortunately, based on the reviews provided, it seems that this pizza

37

restaurant may not have many vegetarian options. The first review from

38

2024-06-22 mentions "Needs Improvement" and specifically says "Wish there were

39

more vegetarian options." While the second review is positive, it doesn't

40

mention anything about vegetarian options, implying that they might not be

41

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 the

49

pizza are mixed.

50

51

The first review (Document #6) states that "Nothing special, just an average

52

pizza place." while giving a rating of 2 out of presumably 5. This suggests

53

that the reviewer was somewhat disappointed with their experience and didn't

54

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 out

57

of 5) but notes that "Not Worth the Hype". Although the reviewer mentions that

58

it's "Great for families" and their kids enjoyed the pizza, they don't mention

59

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 not

62

strongly supported either way, but some reviewers do seem to think it might be

63

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.


Share