Dersler · Kodlama · orta · Tech With Tim

How to Build a Local AI Agent With Python (Ollama, LangChain & RAG)

Tech With Tim kanalının “How to Build a Local AI Agent With Python (Ollama, LangChain & RAG)” videosu 7 ders halinde: konunun anlatımı, ekrandaki kod, alıştırma terminalinde denenecek adımlar, çözümlü alıştırmalar ve cevabı videodaki ana götüren bir quiz.

7ders
8kod parçası
20alıştırma
22soru
28dk video
Derslere git →1. ders ücretsiz · Beyond Pass hepsini açar

Diğer dillerde: English · Español

Video İngilizce. YouTube oynatıcısında altyazıyı açıp Otomatik çevir → Türkçe seçebilirsin.

How to Build a Local AI Agent With Python (Ollama, LangChain & RAG), video by Tech With Tim▶ 28:08 · Tech With Tim
1234567
0:00videonun tamamına yayılmış 7 ders28:08

Neler yapabileceksin

  • 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

Önce bilmen iyi olur

  • 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

Müfredat

7 ders

  1. 1

    Demo of the finished local review assistant Ücretsiz

    0:00–2:012 dk video

    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 kod2 adım3 alıştırma3 soru
  2. 2

    Proje klasörünü ve sanal ortamı kurun Beyond Pass

    2:01–4:333 dk video

    Proje klasörünü oluşturacak, içinde izole bir Python sanal ortamı oluşturup etkinleştirecek ve agent'ın ihtiyaç duyduğu kütüphaneleri kuracaksın. Bunu en başta yapmak, projenin paketlerini bilgisayarındaki diğer her şeyden ayrı tutar.

    1 kod6 adım2 alıştırma3 soru
  3. 3

    Ollama'yı kur ve bir sohbet modeli ile bir gömme (embedding) modelini indir Beyond Pass

    4:33–7:033 dk video

    Ollama'yı kuracaksın; bu, yapay zeka modellerini kendi bilgisayarında çalıştıran bir program. Ardından iki model indireceksin: bir sohbet modeli (llama3.2) ve bir gömme modeli (mxbai-embed-large). Kod yazmadan önce ikisinin de kullanılabilir olduğunu doğrulayacaksın.

    1 kod4 adım3 alıştırma4 soru
  4. 4

    Bir LangChain istem zinciriyle yerel bir model çağır Beyond Pass

    7:03–13:357 dk video

    Bu derste, Ollama aracılığıyla kendi bilgisayarınızda çalışan bir modele LangChain kullanarak bir Python betiği bağlayacaksın. Yer tutucuları olan bir istem şablonu yazacak ve bunu modele bağlayarak çağırabileceğin bir zincir oluşturacaksın.

    1 kod6 adım3 alıştırma3 soru
  5. 5

    Bir soru döngüsü ekle Beyond Pass

    13:35–14:501 dk video

    Zinciri bir while True döngüsüne alacaksın; böylece kullanıcı q yazana kadar soru sormaya devam edebilir. input(), break ve chain.invoke() komutlarının birlikte nasıl çalıştığını ve basit bir soru-cevap programının nasıl yapıldığını öğreneceksin.

    1 kod6 adım3 alıştırma3 soru
  6. 6

    Load the CSV and build a persistent Chroma vector store Beyond Pass

    14:50–23:259 dk video

    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 kod4 adım3 alıştırma3 soru
  7. 7

    Bir retriever oluştur ve RAG'i zincire bağla Beyond Pass

    23:25–28:085 dk video

    Chroma vektör deposunu, bir soru için en ilgili yorumları getiren bir retriever'a dönüştürürsün. Ardından bu yorumları prompt'a aktarırsın; böylece yerel LLM kendi verilerini kullanarak yanıt verir.

    1 kod5 adım3 alıştırma3 soru

Ücretsiz önizleme · 1. dersin tamamı

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.

0:00 videoda 2:01 anına kadar

Alıştırma terminali

Dersin komutlarını yaz, boz ve nedenini gör.

Karalama defteri

Videonun yanında çiz ve yaz.

Fikir

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.

Ana noktalar

  • 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

Bilmen gereken kelimeler

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.

Ekrandaki kod

realistic_restaurant_reviews.csv (first lines shown on screen)text
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.

Running the apptext
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.

Alıştırma terminalinde birlikte yap

  1. Open a terminal and move into your project folder (the video uses a folder called Local Ai Agent on the Desktop).
    $ cd Desktop
    C:\Users\User>cd Desktop
    C:\Users\User\Desktop>
    Göreceğin: The prompt now shows the Desktop folder.
  2. 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.
    $ python main.py
    --------------------
    Ask your question here (q to quit):
    Göreceğin: The program prints a line of dashes and waits for your question.

Bilerek boz

Practice terminal
$ python main.py
python: can't open file 'main.py': [Errno 2] No such file or directory
Neden: 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.
Practice terminal
$ cd Local Ai Agent
The system cannot find the path specified.
Neden: Folder names with spaces must be wrapped in quotes, so use cd "Local Ai Agent".

Dikkat

  • The app is not a general chatbot: it answers from the reviews in the file, so questions the reviews do not cover get weak answers. 0:24
  • Local models run on your own hardware, so responses can be slower than a cloud service. 0:59

Alıştırma

Açıkla Çözümü kontrol edildi

In your own words, explain what retrieval augmented generation means, using the pizza reviews as the example.

Çözüm
  1. Retrieval: find the reviews in the CSV that match the question, e.g. reviews mentioning vegan.
  2. 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.

Yap Çözümü kontrol edildi

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.

İpucu

Look at the example lines: four values separated by commas, and the long text is in quotes because it contains commas.

Çözüm
  1. The columns are Title, Date, Rating, Review, so the line needs four values separated by commas.
  2. Write a short title, then a date such as 2024-04-01, then a rating from 1 to 5.
  3. 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."

Açıkla Çözümü kontrol edildi

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.

İpucu

Think about what each tool is described as doing in the lesson.

Çözüm
  1. Ollama runs the language model locally.
  2. LangChain connects the pieces of the app.
  3. ChromaDB is the vector database that finds relevant reviews.

Ollama = (b), LangChain = (a), ChromaDB = (c).

Karalama defterinde

Karalama defterinizde üç kutu çizin, etiketleri 'CSV yorumları', 'İlgili yorumlar' ve 'Model yanıtı' olsun ve kutular arasına oklar çizin. Her okun altına, orada ne olduğunu anlatan bir cümle yazın.

Test

  1. Uygulama restoran hakkındaki soruları yanıtlamak için neyi kullanır?

    1. Yalnızca kendi yerleşik pizza bilgisini
    2. CSV dosyasından alınan ilgili yorumların bir modele verilmesi
    3. Ücretli bir OpenAI bulut hesabını
    4. İnternette web araması yapmayı
    Cevabı göster

    B. CSV dosyasından alınan ilgili yorumların bir modele verilmesi · Uygulama CSV'den ilgili yorumları alır ve bunları yerel bir modele iletir (RAG).

    0:19 o anı izle
  2. Bu projede vektör arama veritabanı hangi araçtır?

    1. Ollama
    2. LangChain
    3. ChromaDB
    4. Sublime Text
    Cevabı göster

    C. ChromaDB · ChromaDB vektör veritabanıdır; Ollama modeli çalıştırır ve LangChain parçaları birbirine bağlar.

    0:08 o anı izle
  3. Uygulamadan nasıl çıkarsın?

    1. Ctrl+Alt+Delete tuşlarına basarak
    2. Soru isteminde q yazarak
    3. CSV dosyasına exit yazarak
    4. Sublime Text penceresini kapatarak
    Cevabı göster

    B. Soru isteminde q yazarak · İstemde 'q to quit' yazıyor, yani q yazmak programı sonlandırır.

    1:18 o anı izle

1. dersi videoyla aç →

2–7. dersler

Beyond Pass ile açılırlar: her kursun her dersi, eksiksiz. Tek ödeme: 90 gün için $14.99 ya da bir yıl için $29. Abonelik yok.

Beyond, Tech With Tim kanalının “How to Build a Local AI Agent With Python (Ollama, LangChain & RAG)” videosundan yaptı. Video YouTube’un kendi oynatıcısında oynar. YouTube’da izle · Bütün dersler