• Retrieval-Augmented Generation(RAG) सिस्टम के ज़रिए मौजूदा आंतरिक knowledge base (wiki, manuals, training और reference materials आदि) के आधार पर सवालों के जवाब देने वाला AI assistant बनाया जा सकता है
  • PostgreSQL, pgvector, ollama, और 200 लाइनों से कम Go code के साथ RAG सिस्टम बनाया जा सकता है

Overview

  • कुछ paragraphs की कहानी को "document corpus" के रूप में इस्तेमाल किया जाता है, और हर document के लिए Meta के Llama3 का उपयोग करके document embeddings बनाए जाते हैं (ollama पर local hosting)
  • documents और embeddings को PostgreSQL table में store किया जाता है, और pgvector extension के ज़रिए embeddings को store और access किया जाता है
  • user query के लिए table से सबसे प्रासंगिक 1 document retrieve किया जाता है और Llama3 का उपयोग करके response generate किया जाता है
  • ollama, OpenAI जैसी HTTP API देता है जो embeddings और chat responses generate करती है
  • Go code, Postgres से communicate करने के लिए jackc/pgx और pgvector-go का उपयोग करता है, और HTTP API calls को handle करने के लिए ollama client API package का उपयोग करता है

Ollama से models चलाना

  • Ollama एक tool है जो open source models को locally चलाने देता है, और OpenAI-style REST API प्रदान करता है
  • ollama pull llama3 कमांड से llama3 model चलाया जा सकता है
  • ollama का HTTP server डिफ़ॉल्ट रूप से 127.0.0.1:11434 पर उपलब्ध होता है

pgvector इंस्टॉल करना

  • pgvector, PostgreSQL 12~16 versions के लिए extension है, और pgdg APT repository इस्तेमाल करने पर sudo apt install postgresql-16-pgvector से install किया जा सकता है
  • install के बाद create extension vector; से database में extension सेट किया जाता है
  • create table items (id serial primary key, doc text, embedding vector(4096)); से documents और embeddings को store करने के लिए table बनाया जाता है

document data

  • Sherlock Holmes की कहानी "The Boscombe Valley Mystery" से 4 paragraphs इस्तेमाल किए गए हैं (public domain - Project Gutenberg)

code

  • GitHub पर MIT license के तहत जारी demo code इस्तेमाल किया जा सकता है
  • document insert करने के लिए INSERT INTO items (doc, embedding) VALUES ($1, $2)
  • सबसे अधिक प्रासंगिक document retrieve करने के लिए SELECT doc FROM items ORDER BY embedding <-> $1 LIMIT 1 (<-> operator pgvector द्वारा दिया जाता है)
  • Ollama API calls के लिए ollama Go package का उपयोग किया जाता है
    • embedding generate करने के लिए api.EmbeddingRequest का उपयोग
    • chat response generate करने के लिए api.ChatRequest का उपयोग (prompt में retrieved document शामिल)

command-line interface

  • ragdemo -insert {path-to-doc-file} से document को database में store किया जाता है
  • ragdemo -query {query-text} से prompt input देकर response generate किया जाता है

पूरा process

  1. -insert option से document store करते समय, file content पढ़कर Llama3 से embedding generate की जाती है और PostgreSQL में store की जाती है
  2. -query option इस्तेमाल करने पर, prompt embedding generate करने के बाद उसे items table की दूसरी embeddings से compare किया जाता है और "nearest" document retrieve किया जाता है (<-> operator से L2 distance calculate होती है)
  3. retrieved document को prompt में शामिल करके Llama3 को भेजा जाता है, chat response generate की जाती है और output दिया जाता है

अतिरिक्त टिप्स

  • embeddings generate करने के लिए खास तौर पर बने model के उपयोग पर विचार करें (llama3 की जगह)
  • English के अलावा दूसरी भाषाओं के लिए अधिक उपयुक्त model तलाशने की ज़रूरत हो सकती है
  • L2 distance के अलावा दूसरे distance calculation methods भी आज़माए जा सकते हैं (pgvector अन्य तरीकों को support करता है)
  • full table scan की scalability कम होती है, इसलिए pgvector index आदि का उपयोग करें
  • generation stage में अधिक documents का उपयोग करना, या keyword matching जैसी विधियों से अतिरिक्त documents लाना भी मददगार हो सकता है
  • generation prompt को adjust करके और अलग-अलग LLMs आज़माकर output quality बेहतर की जा सकती है

अभी कोई टिप्पणी नहीं है.

अभी कोई टिप्पणी नहीं है.