π Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/rag-and-knowledge/vector-database/single_turn_rag_pgvector
Single-Turn RAG with PostgreSQL (pgvector) for C# .NET Applications
π― Purpose of the Demo
The Single-Turn RAG with PostgreSQL (pgvector) demo illustrates how to integrate LM-Kit.NET with a PostgreSQL (pgvector) vector store to build a single-turn Q&A system that uses Retrieval-Augmented Generation (RAG). This solution demonstrates how semantic search and large language models (LLMs) can be combined in a .NET application to provide accurate, context-aware responses from stored vector embeddings.
π₯ Industry Target Audience
This sample benefits developers and organizations in areas like:
- ποΈ Customer Support: Query large knowledge bases with fast, relevant results.
- π Education: Build tutoring systems based on textbooks and training content.
- π₯ Healthcare: Provide real-time answers from medical guidelines or literature.
- π¦ Logistics & Industry: Search across technical documentation and manuals.
- ποΈ E-commerce: Enable chat-based access to product catalogs and FAQs.
π Problem Solved
Managing large corpora of data and generating precise answers is a complex task. This demo solves the problem by:
- Persisting embeddings in a vector database (PostgreSQL with pgvector) for faster future access.
- Combining semantic search with generative AI for accurate, context-aware responses.
- Allowing reuse of processed data sources across multiple sessions.
π» Sample Application Description
This is a console-based chatbot demo that loads a series of eBooks, stores their embeddings in a local PostgreSQL (pgvector) instance, and uses RAG to answer queries.
β¨ Key Features
- π PostgreSQL (pgvector) Integration: Uses LM-Kit.NET.Data.Connectors.PgVector to persist embeddings locally.
- π¦ Model Loading: Load chat and embedding models from predefined URLs or custom paths.
- π§ Persistent Vector Store: Avoid redundant embedding computation across sessions.
- π Semantic Search: Retrieve relevant content chunks before generation.
- π Performance Tracking: Measure data source loading time.
- β»οΈ Cached Data Handling: Reuse indexed vectors when available.
π§ Supported Models
- Chat Model: Google Gemma 4 E4B (~6 GB VRAM)
- Embeddings Model: EmbeddingGemma 300M (~300 MB VRAM)
π§Ύ Data Sources
Three classic eBooks are loaded as semantic knowledge bases:
- Romeo and Juliet by William Shakespeare
- Moby Dick by Herman Melville
- Pride and Prejudice by Jane Austen
π οΈ Getting Started
π Prerequisites
- .NET 8.0+ SDK
- PostgreSQL with the pgvector extension
- Docker (for running PostgreSQL + pgvector locally)
π§° PostgreSQL (pgvector) Setup
Run PostgreSQL with the pgvector extension locally using Docker:
docker run --name lmkit-pgvector -e POSTGRES_PASSWORD=postgres -p 5433:5432 -d pgvector/pgvector:pg17
The demo connects with a PostgreSQL connection string, for example:
Host=localhost;Port=5433;Database=vector_test;Username=postgres;Password=postgres
π₯ Download the Project
βΆοΈ Run the Application
- π Clone the repository:
git clone https://github.com/LM-Kit/lm-kit-net-samples
- π Navigate to the demo directory:
cd lm-kit-net-samples/console_net/rag-and-knowledge/vector-database/single_turn_rag_pgvector
- π¨ Build and run:
dotnet build
dotnet run
π¬ Example Usage
- Load both models (chat and embedding) from URLs or specify local paths.
- The demo will load the eBooks, embedding them via LM-Kit and storing them in PostgreSQL (pgvector).
- Enter your query, e.g.:
"Who are the main characters in Romeo and Juliet?""What is Captain Ahabβs goal in Moby Dick?"
- The chatbot retrieves matching content and generates a response.
- The app skips re-embedding if the data is already present in PostgreSQL (pgvector).
π οΈ Special Behavior
- β Reuse of stored collections: Embeddings are reused if already indexed.
- π Fresh vector indexing: If a collection is not present, itβs created and persisted.
- π Dynamic top-k: GPU-optimized systems select more partitions for improved accuracy.
π Licensing
To run the application, you can use a free community license available at:
π https://lm-kit.com/products/community-edition/
π Related Repositories
- Demo source code (.NET 8.0): single_turn_rag_pgvector
- PostgreSQL (pgvector) Connector Assembly (Open Source): LM-Kit.NET.Data.Connectors.PgVector
By combining the power of semantic search and LLMs, this demo offers a robust foundation for building production-ready, intelligent chatbots in C# with persistent, scalable vector storage.
π Related Content
- How-To: Build a RAG Pipeline: Step-by-step guide to setting up retrieval-augmented generation with LM-Kit.NET.
- Glossary: Vector Database: Explains vector databases like PostgreSQL (pgvector) used for persistent embedding storage.
- Glossary: RAG: Covers Retrieval-Augmented Generation, the core technique behind this demo.
- Glossary: Embeddings: Explains vector embeddings that power semantic search in PostgreSQL (pgvector).
- Single-Turn RAG Demo: Simpler RAG demo using local file storage instead of a vector database.
- Single-Turn RAG with Qdrant: The same RAG pipeline backed by the Qdrant vector database.