Sabrás
- Create an isolated Python virtual environment and install LangChain, Ollama, and Chroma dependencies
- Run a local LLM through Ollama and invoke it from Python with a LangChain prompt chain
- Build an interactive question loop that sends user input through a model chain
- Embed CSV rows as documents into a persistent Chroma vector store using a local embedding model
- Implement retrieval-augmented generation by feeding retrieved documents into the prompt before the model answers
Conviene saber antes
- Basic Python syntax, including functions, loops, and dictionaries
- Comfort using a terminal and command line
- Familiarity with VS Code or another code editor
- Basic understanding of CSV files
Temario
7 lecciones
- 1
Demo of the finished local review assistant Gratis
You get a first look at the finished app: a terminal program that answers questions by reading a CSV of restaurant reviews. It runs entirely on your own computer, with no paid cloud account.
2 de código2 pasos de construcción3 ejercicios3 preguntas - 2
Crea la carpeta del proyecto y el entorno virtual Beyond Pass
Vas a crear una carpeta para el proyecto, crear un entorno virtual de Python dentro de ella, activarlo e instalar las bibliotecas que necesita el agente. Preparar esto primero mantiene los paquetes de tu proyecto separados de todo lo demás de tu ordenador.
1 de código6 pasos de construcción2 ejercicios3 preguntas - 3
Instalar Ollama y descargar un modelo de chat y un modelo de embeddings Beyond Pass
Instalarás Ollama, que ejecuta modelos de IA en tu propio ordenador, y después descargarás dos modelos: un modelo de chat (llama3.2) y un modelo de embeddings (mxbai-embed-large). Comprobarás que ambos están disponibles antes de escribir código.
1 de código4 pasos de construcción3 ejercicios4 preguntas - 4
Invoca un modelo local con una cadena de prompts de LangChain Beyond Pass
Vas a conectar un script de Python a un modelo que se ejecuta en tu propio ordenador a través de Ollama, usando LangChain. Escribirás una plantilla de prompt con marcadores de posición y la unirás al modelo en una cadena que puedes invocar.
1 de código6 pasos de construcción3 ejercicios3 preguntas - 5
Añadir un bucle de preguntas interactivo Beyond Pass
Vas a envolver la cadena en un bucle while True para que el usuario pueda seguir haciendo preguntas hasta escribir q. Aprenderás cómo funcionan juntos input(), break y chain.invoke() para crear un sencillo programa de preguntas y respuestas.
1 de código6 pasos de construcción3 ejercicios3 preguntas - 6
Load the CSV and build a persistent Chroma vector store Beyond Pass
You will turn each row of a restaurant-reviews CSV into a Document, convert the text into vectors with a local Ollama embedding model, and store them in a persistent Chroma database that is built only once. You will also see why IDs, metadata and a "does the database already exist?" check matter.
1 de código4 pasos de construcción3 ejercicios3 preguntas - 7
Crea un recuperador (retriever) e integra RAG en la cadena Beyond Pass
Conviertes el almacén vectorial de Chroma en un recuperador que obtiene las reseñas más relevantes para una pregunta. Después, pasas esas reseñas al prompt para que el LLM local responda usando tus propios datos.
1 de código5 pasos de construcción3 ejercicios3 preguntas
Vista previa gratis · La lección 1 completa
Demo of the finished local review assistant
You get a first look at the finished app: a terminal program that answers questions by reading a CSV of restaurant reviews. It runs entirely on your own computer, with no paid cloud account.
Escribe los comandos de la lección, rompe cosas y descubre por qué.
Dibuja y escribe junto al vídeo.
La idea
This chapter is a preview. Before any code is written, the video shows the finished product so you know what you are building. The app is a small Python program run from the command prompt. It asks you a question, looks through a file of fake pizza restaurant reviews, and answers using what it found.
The data lives in a CSV file called realistic_restaurant_reviews.csv. A CSV is a plain text table where each line is a row and commas separate the columns. Here the columns are Title, Date, Rating and Review. A review might read 'Best pizza in town' with a date, a rating out of five, and a long text describing the pizza.
The idea behind the app is retrieval augmented generation, often shortened to RAG. The program first retrieves the reviews that are relevant to your question from the file, then gives those reviews to a language model, which writes the answer. The model does not have to know anything about this restaurant in advance; it only sees the pieces the search found.
The tools are all free and run locally. The video uses Ollama to run a language model on your machine, LangChain to connect the pieces, and ChromaDB as a vector database that finds relevant reviews. No OpenAI account or cloud service is needed.
In the demo, the user asks 'how is the quality of the pizza?'. The agent pulls matching reviews and writes a conclusion that notes the answer is hard to score from the reviews alone. Then the user asks 'are there vegan options?' and the agent points to a vegan pizza mentioned in a review, quoting the review's metadata. The answer depends on the reviews in the file, so it is only as good as the data.
Puntos clave
- The finished app answers questions about restaurant reviews from a local CSV file. 0:00
- RAG means retrieving relevant information from a file and giving it to the model before it answers. 0:12
- Everything is free and runs on your own computer: no OpenAI or cloud account is needed. 0:24
- The tools are Ollama (local model), LangChain (connects the pieces) and ChromaDB (vector search database). 0:36
- The app is run from the command prompt with python main.py and loops asking questions until you type q. 0:59
Palabras clave
- CSV file
- A plain text table. Each line is a row and commas separate the columns.
- RAG (retrieval augmented generation)
- First find the pieces of data that fit the question, then give them to a language model so it writes the answer from them.
- Language model
- A program that writes text. Here it runs on your own computer.
- Ollama
- A free tool that runs a language model on your own machine.
- LangChain
- A Python library that connects the parts of the app, such as the model and the database.
- ChromaDB
- A vector database. It stores text so it can find the pieces most relevant to a question.
Código de la pantalla
Title,Date,Rating,Review
Best pizza in town,2024-03-15,5,"The crust was perfectly crispy on the outside and chewy inside. Their signature pepperoni pizza had the perfect ratio of sauce to cheese, and the pepperoni curled up into little cups of deliciousness. Will definitely be back!"
Disappointed with service,2024-02-20,2,"While the pizza itself was decent, we waited over an hour for delivery despite being told it would be 30 minutes. When it finally arrived, it was barely warm. The flavors were good but the experience ruined it."Open the file in any text editor to see the rows; no code needs to run for this chapter.
C:\Users\User\Desktop\Local Ai Agent>python main.py
--------------------
Ask your question here (q to quit):In a terminal inside your project folder, type python main.py and press Enter.
Construye a la vez en la terminal de práctica
- Open a terminal and move into your project folder (the video uses a folder called Local Ai Agent on the Desktop). Deberías ver: The prompt now shows the Desktop folder.
$ cd Desktop C:\Users\User>cd Desktop C:\Users\User\Desktop>
- Run the app with python main.py. The video shows this as a preview, so you may not have the file yet; this step shows what the finished program looks like when it starts. Deberías ver: The program prints a line of dashes and waits for your question.
$ python main.py -------------------- Ask your question here (q to quit):
Rómpelo a propósito
$ python main.py python: can't open file 'main.py': [Errno 2] No such file or directoryPor qué: Python can only run a file that exists in the folder you are in. Check that you cd-ed into the project folder where main.py was saved.
$ cd Local Ai Agent The system cannot find the path specified.Por qué: Folder names with spaces must be wrapped in quotes, so use cd "Local Ai Agent".
Ojo
Práctica
In your own words, explain what retrieval augmented generation means, using the pizza reviews as the example.
Solución
- Retrieval: find the reviews in the CSV that match the question, e.g. reviews mentioning vegan.
- Augmented generation: give those reviews to the language model so it writes an answer based on them.
RAG first searches a document for the pieces relevant to a question (retrieval), then passes those pieces to a language model that writes the answer (generation). For example, when asked about vegan options, the app finds the reviews that mention vegan food and the model answers using them.
Write one new line for the reviews CSV about a pizza place. Use the columns Title, Date, Rating, Review in that order. Put the Review text in double quotes.
Pista
Look at the example lines: four values separated by commas, and the long text is in quotes because it contains commas.
Solución
- The columns are Title, Date, Rating, Review, so the line needs four values separated by commas.
- Write a short title, then a date such as 2024-04-01, then a rating from 1 to 5.
- Put the review text last and wrap it in double quotes, because the text may contain commas.
Example: Great vegan pizza,2024-04-01,4,"They had a vegan pizza with lots of vegetables, and the crust was lovely."
Match each tool to its job: Ollama, LangChain, ChromaDB. Jobs: (a) connects the pieces together, (b) runs the language model on your computer, (c) finds the reviews relevant to a question.
Pista
Think about what each tool is described as doing in the lesson.
Solución
- Ollama runs the language model locally.
- LangChain connects the pieces of the app.
- ChromaDB is the vector database that finds relevant reviews.
Ollama = (b), LangChain = (a), ChromaDB = (c).
En tu bloc de notas
En tu cuaderno de borradores, dibuja tres cajas con las etiquetas 'CSV reviews', 'Relevant reviews' y 'Model answer', con flechas entre ellas. Escribe una frase debajo de cada flecha que describa lo que ocurre.
Test
¿Qué usa la aplicación para responder preguntas sobre el restaurante?
- Solo su propio conocimiento integrado sobre pizza
- Reseñas relevantes recuperadas del archivo CSV, que se pasan a un modelo
- Una cuenta de pago en la nube de OpenAI
- Una búsqueda web en internet
Mostrar la respuesta
B. Reseñas relevantes recuperadas del archivo CSV, que se pasan a un modelo · La aplicación recupera las reseñas relevantes del CSV y se las pasa a un modelo local (RAG).
0:19 ver el momento¿Qué herramienta es la base de datos de búsqueda vectorial en este proyecto?
- Ollama
- LangChain
- ChromaDB
- Sublime Text
Mostrar la respuesta
C. ChromaDB · ChromaDB es la base de datos vectorial; Ollama ejecuta el modelo y LangChain conecta las piezas.
0:08 ver el momento¿Cómo se cierra la aplicación?
- Pulsa Ctrl+Alt+Supr
- Escribe q en el aviso de la pregunta
- Escribe exit en el archivo CSV
- Cierra la ventana de Sublime Text
Mostrar la respuesta
B. Escribe q en el aviso de la pregunta · El aviso dice 'q to quit', así que escribir q termina el programa.
1:18 ver el momento
Lecciones 2–7
Se abren con Beyond Pass: todas las lecciones de todos los cursos, completas. Un solo pago: 14,99 $ por 90 días o 29 $ por un año. Sin suscripción.
Hecho por Beyond a partir del vídeo «How to Build a Local AI Agent With Python (Ollama, LangChain & RAG)» de Tech With Tim. El vídeo se reproduce en el propio reproductor de YouTube. Míralo en YouTube · Todas las lecciones
