𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗔 𝗥𝗔𝗚 𝗙𝗿𝗼𝗺 𝗦𝗰𝗿𝗮𝘁𝗰𝗵
My first AI version told me I sold a hydraulic excavator. I do not sell excavators. It gave me a fake price and a fake description with total confidence.
That was the moment I stopped trusting prompts alone. I rebuilt the system with one rule: it answers from the catalog, or it does not answer at all.
Here is how I built a reliable RAG (Retrieval-Augmented Generation) system using Postgres and Python.
𝗧𝗵𝗲 𝗗𝗮𝘁𝗮 𝗣𝗿𝗲𝗽 Most tutorials skip the hard part: cleaning data. I split my process into two stages:
- Stage 1: Download HTML files to disk. I save metadata as a comment at the top of each file. This makes the process idempotent. If a file exists, I skip it.
- Stage 2: Parse those files offline. This turns HTML into a clean JSON catalog.
I check field coverage after parsing. If a field like weight or price is empty, I find out immediately. Clean data is where the real work happens.
𝗧𝗵𝗲 𝗔𝗜 𝗣𝗮𝗿𝘁 I turn each product into a block of text and convert it into a vector using the bge-m3 model. I store these vectors in Postgres using the pgvector extension.
I use a hybrid search approach to find products:
- Semantic Search: Uses vectors to find products that match the meaning of your question.
- Structured Filters: I use an LLM to turn a query like "Siemens motors under €2000" into JSON. This allows me to run a SQL query with exact filters for brand and price.
One SQL statement handles both the fuzzy search and the hard filters. This keeps everything in sync.
𝗧𝗵𝗲 𝗚𝘂𝗮𝗿𝗱𝗿𝗮𝗶𝗹𝘀 A good RAG must know when to shut up. I use two layers to prevent hallucinations:
- Similarity Threshold: Every match gets a score. If the score is below a set limit, I drop the results. If no results pass, the system says "not found" without even calling the LLM. You cannot hallucinate if the model never sees the data.
- Strict System Prompt: I tell the model to answer only from the provided products. If the products are irrelevant, it must refuse.
The threshold makes bad behavior impossible. The prompt just asks for good behavior. Use both.
𝗧𝗵𝗿𝗼𝘂𝗴𝗵𝗽𝘂𝘁 𝗦𝘂𝗺𝗺𝗮𝗿𝘆
- Collect carefully.
- Clean honestly.
- Embed simply.
- Refuse by design.
The refusal is what makes the system trustworthy. Trust comes from architecture, not from asking a model to be nice.
Sıfırdan bir RAG oluşturmak: Topla, Temizle, Vektörleştir, Getir
RAG (Retrieval-Augmented Generation - Arama ile Güçlendirilmiş Üretim), Büyük Dil Modellerinin (LLM) eğitim verilerinde bulunmayan güncel veya özel bilgilere erişmesini sağlayan bir tekniktir. Bu makalede, bir RAG sisteminin temel bileşenlerini ve bu sistemi sıfırdan nasıl inşa edeceğinizi adım adım inceleyeceğiz.
RAG Nedir?
LLM'ler (GPT-4, Llama 3 vb.) muazzam miktarda bilgiye sahiptir ancak iki ana sınırlamaları vardır:
- Bilgi Kesintisi (Knowledge Cutoff): Eğitim verileri belirli bir tarihte biter.
- Halüsinasyonlar: Bilmedikleri konularda kendinden emin bir şekilde yanlış bilgi üretebilirler.
RAG, modele bir "açık kitap sınavı" imkanı tanır. Model, soruyu cevaplamadan önce kendisine sağlanan güvenilir kaynaklardan ilgili bilgileri "arar" ve bu bilgileri kullanarak yanıt üretir.
RAG İş Akışı
Bir RAG sistemi dört ana aşamadan oluşur:
- Veri Toplama (Collection)
- Veri Temizleme ve Parçalama (Cleaning & Chunking)
- Vektörleştirme (Embedding)
- Getirme (Retrieval)
1. Veri Toplama (Data Collection)
İlk adım, modelin kullanacağı bilgi kaynağını belirlemektir. Veriler şunlar olabilir:
- Web siteleri (Scraping yoluyla)
- PDF, Word veya metin dosyaları
- Veritabanları (SQL, NoSQL)
- API'ler (Notion, Slack, GitHub vb.)
2. Veri Temizleme ve Parçalama (Data Cleaning & Chunking)
Ham veri genellikle "gürültülüdür" (HTML etiketleri, gereksiz boşluklar, reklamlar vb.). Temiz bir veri seti, daha kaliteli sonuçlar sağlar.
Temizleme: Gereksiz karakterleri, özel sembolleri ve tekrarlayan metinleri kaldırın.
Parçalama (Chunking): LLM'lerin bir "bağlam penceresi" (context window) sınırı vardır. Tüm bir kitabı tek seferde modele veremezsiniz. Bu nedenle, metni anlamlı parçalara (chunk'lara) bölmeniz gerekir.
- Sabit Boyutlu Parçalama: Metni her seferinde belirli bir karakter veya token sayısına göre böler.
- Anlamsal Parçalama: Paragraflara veya cümlelere göre böler.
3. Vektörleştirme (Embedding)
Metin parçalarını bilgisayarların anlayabileceği sayısal formatlara (vektörlere) dönüştürmemiz gerekir. Bir Embedding Modeli (örneğin OpenAI text-embedding-3-small veya HuggingFace modelleri), metnin anlamsal anlamını yüksek boyutlu bir vektör uzayında temsil eder.
Benzer anlamdaki cümleler, bu vektör uzayında birbirine yakın konumlanır.
4. Getirme (Retrieval)
Vektörleştirilen veriler bir Vektör Veritabanında (Pinecone, ChromaDB, FAISS veya Weaviate) saklanır.
Süreç şöyle işler:
- Kullanıcı bir soru sorar.
- Soru, aynı embedding modeli kullanılarak bir vektöre dönüştürülür.
- Vektör veritabanında, kullanıcının sorusuna en yakın (en benzer) metin parçaları bulunur (genellikle Cosine Similarity kullanılarak).
- Bulunan bu parçalar, orijinal soruyla birlikte LLM'e gönderilir.
Özet
Sıfırdan bir RAG sistemi kurmak, verinin kalitesine ve parçalama stratejinize doğrudan bağlıdır. İyi bir RAG sistemi:
- Doğru veriyi toplar.
- Gürültüden arındırılmış, anlamlı parçalar oluşturur.
- Güçlü embedding modelleri kullanır.
- Hızlı ve etkili bir vektör arama mekanizmasına sahiptir.
İsteğe bağlı öğrenme topluluğu: https://t.me/GyaanSetuAi