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

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:

1ollama serve2ollama pull llama33ollama 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)

1pip install poetry

Create a new project and add dependencies:

1poetry new ai_tutorial2cd ai_tutorial3poetry 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:

1Best Pizza in Town!,2025-01-06,1,The crust was perfectly crispy and the 2toppings were fresh. Will definitely return!3Delicious and Affordable,2024-08-12,1,"A bit overpriced for what you get, but 4overall a decent meal."5Mediocre Experience,2025-01-05,1,Incredible flavors! The margherita pizza 6reminded me of Italy.7Absolutely Loved It!,2024-06-14,5,"Service was slow and the place was crowded, 8but the pizza made up for it."9Won't Be Coming Back,2025-03-15,2,Disappointed by the soggy crust and bland 10sauce. Expected better.11A Slice of Heaven,2024-10-16,2,Amazing pizza with a great balance of cheese and12sauce.13Perfect for a Quick Bite,2024-08-21,2,"Nothing special, just an average pizza14place."15Not Worth the Hype,2024-10-21,4,Great for families. My kids loved the pepperoni16pizza!17Family Favorite,2024-11-15,4,The truffle mushroom pizza is a must-try!

step-4-run-singlestore-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 :

1import pandas as pd2from singlestoredb.server import docker3from langchain_ollama import OllamaLLM, OllamaEmbeddings4from langchain_singlestore import SingleStoreVectorStore5from langchain_core.prompts import ChatPromptTemplate6from langchain_core.documents import Document7
8
9def 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
16def 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
32def main():33    """34    Run a pizza review Q&A application using SingleStoreDB vector store and 35LLaMA.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 pizza41reviews42    """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 each61query62        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 = """69You are an expert in answering questions about a pizza restaurant.70
71Here are some relevant reviews: {reviews}72
73Here 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 find 81relevant 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 87quit): ")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
98if __name__ == "__main__":99    main()

run-the-applicationRun the application

Simply start the application from the command line

1poetry run python ai_tutorial/main.py

conversation-exampleConversation example

1Starting SingleStoreDB server for vector storage...2Loading and embedding pizza reviews...3Initializing LLaMA 3.2 model...4
5------------------------------------------6Pizza Review Question & Answer System7Ask questions about pizza reviews, and the system will find relevant reviews8and generate an answer based on those reviews.9------------------------------------------10
11
12Enter your question about pizza (or 'exit' to quit): What do people think about 13the crust?14
15Finding relevant reviews and generating answer...16--- Answer ---17Based on the reviews provided, it seems that opinions about the crust are 18mixed. 19
20One reviewer mentioned that their base (likely referring to the crust) was 21soggy, which may indicate issues with the cooking or handling of the crust. On 22the other hand, another reviewer stated that they found the crust too salty and 23also mentioned that it was burnt, suggesting a problem with the quality control 24or preparation of the crust.25
26Overall, while not all reviews were negative, these two opinions suggest that 27some customers have had issues with the crust in terms of texture (soggy) and 28flavor (burnt).29
30Enter your question about pizza (or 'exit' to quit): Are there vegetarian 31options?32
33Finding relevant reviews and generating answer...34
35--- Answer ---36Unfortunately, based on the reviews provided, it seems that this pizza 37restaurant may not have many vegetarian options. The first review from 382024-06-22 mentions "Needs Improvement" and specifically says "Wish there were39more vegetarian options." While the second review is positive, it doesn't 40mention anything about vegetarian options, implying that they might not be 41available or plentiful at this establishment.42
43Enter your question about pizza (or 'exit' to quit): Is the pizza overpriced?44
45Finding relevant reviews and generating answer...46
47--- Answer ---48Based on the reviews provided, it seems that opinions about the pricing of the49pizza are mixed.50
51The first review (Document #6) states that "Nothing special, just an average 52pizza place." while giving a rating of 2 out of presumably 5. This suggests 53that the reviewer was somewhat disappointed with their experience and didn't 54think it was particularly good or worth the price.55
56On the other hand, the second review (Document #7) gives a higher rating (4 out 57of 5) but notes that "Not Worth the Hype". Although the reviewer mentions that 58it's "Great for families" and their kids enjoyed the pizza, they don't mention 59anything about the pricing being over or under par. 60
61Based on these reviews alone, I would say that opinions about the price are not 62strongly supported either way, but some reviewers do seem to think it might be 63a bit pricey (although this is purely based on limited evidence).64
65Enter 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.

How to build an app with Python and SingleStore

Step-by-step guides for setting up your free SingleStore Helios® account, connecting to your database, and running your first queries.
Start building

Share

Start building with SingleStore