A Retrieval-Augmented Generation (RAG) Chat Bot that provides accurate responses based on available documents. This application uses PostgreSQL with pgvector extension for storing and searching vector embeddings, and OpenAI for generating embeddings and responses.
- 🔍 Vector-based document search
- 💾 Conversation context storage and retrieval
- 🤝 OpenAI API integration for embeddings and chat
- 🎯 Efficient RAG system for accurate responses
- 📝 Document metadata support
- ⚡ HNSW index for similarity search
- Go 1.21 or newer
- PostgreSQL 15 or newer with pgvector extension
- Docker and Docker Compose (optional, for deployment)
- OpenAI API key
- Clone repository:
git clone https://github.com/zakirkun/rag-chat-bot.git
cd rag-chat-bot
- Copy configuration file:
cp .env.example .env
- Edit
.env
file and adjust configuration:
# Server configuration
SERVER_PORT=8080
# Database configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=ragchatbot
DB_SSL_MODE=disable
# OpenAI API configuration
OPENAI_API_KEY=your-api-key
OPENAI_EMBEDDING_MODEL=text-embedding-ada-002
OPENAI_CHAT_MODEL=gpt-4
- Run PostgreSQL database using Docker Compose:
docker-compose up -d
- Install dependencies:
go mod download
- Run the application:
go run main.go
id
: Unique document IDtitle
: Document titlecontent
: Document contentmetadata
: Document metadata in JSONB formatcreated_at
: Document creation timestamp
id
: Unique embedding IDdocument_id
: Reference to documentembedding
: Vector embedding (1536 dimensions)created_at
: Embedding creation timestamp
id
: Unique conversation IDsession_id
: User session IDcreated_at
: Conversation creation timestamp
id
: Unique message IDconversation_id
: Reference to conversationrole
: Sender role (user/assistant)content
: Message contentcreated_at
: Message timestamp
POST /api/chat
Content-Type: application/json
{
"session_id": "unique-session-id",
"message": "User question or message"
}
POST /api/documents
Content-Type: application/json
{
"title": "Document Title",
"content": "Document content",
"metadata": {
"source": "Document source",
"category": "Document category"
}
}
The application uses a modular architecture with main components:
-
Database Layer
- Handles database operations
- Manages vector embeddings
- Stores conversation history
-
RAG Layer
- Retrieves relevant documents
- Builds context for LLM
- Manages OpenAI interactions
-
API Layer
- Handles HTTP requests
- Manages user sessions
- Provides API endpoints
go test ./...
golangci-lint run
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.