Skip to content

transcendence12/rag-nodejs-langbase

Repository files navigation

RAG Node.js Langbase

A Retrieval-Augmented Generation (RAG) system built with Node.js and Langbase that provides AI-powered support for answering questions based on uploaded documentation.

Project Overview

This project implements a RAG system that combines document retrieval with AI-generated responses. It uses Langbase as the backend service for memory management and AI pipe execution. The system is designed to answer user queries by retrieving relevant information from uploaded documents and generating contextual responses.

Architecture

The project consists of several TypeScript modules that work together to create a complete RAG pipeline:

Core Components

  1. Memory Management (create-memory.ts) - Creates and configures the knowledge base
  2. Document Upload (upload-docs.ts) - Uploads documents to the knowledge base
  3. AI Agent Setup (create-pipe.ts) - Creates the AI support agent pipeline
  4. RAG Pipeline (agents.ts) - Implements the retrieval and generation logic
  5. Main Application (index.ts) - Orchestrates the complete RAG workflow

Features

  • Document Storage: Upload and store documents in a vectorized knowledge base
  • Semantic Search: Retrieve relevant document chunks based on user queries
  • AI-Powered Responses: Generate contextual answers using retrieved information
  • Source Citation: Automatically cite sources in responses with proper formatting
  • Metadata Support: Add metadata to documents for better organization

Prerequisites

  • Node.js (v14 or higher)
  • TypeScript
  • Langbase API key

Installation

  1. Clone the repository:
git clone <repository-url>
cd rag-nodejs-langbase
  1. Install dependencies:
npm install
  1. Create a .env file in the root directory and add your Langbase API key:
LANGBASE_API_KEY=your_langbase_api_key_here

Usage

Setup Phase (Run Once)

Before using the RAG system, you need to set up the infrastructure:

1. Create Memory/Knowledge Base

npx tsx create-memory.ts

This creates a new memory called "knowledge-base" with OpenAI's text-embedding-3-large model.

2. Upload Documents

npx tsx upload-docs.ts

This uploads the FAQ document from docs/langbase-faq.txt to the knowledge base with metadata.

3. Create AI Support Agent

npx tsx create-pipe.ts

This creates the "ai-support-agent" pipeline that will be used for generating responses.

Runtime Phase

4. Run the RAG System

npx tsx index.ts

This executes the main RAG pipeline, which:

  • Takes a user query ("How do I upgrade individual plan?")
  • Retrieves relevant chunks from the knowledge base
  • Generates a contextual response using the AI support agent
  • Displays the completion with proper source citations

File Descriptions

create-memory.ts

Creates a new memory instance in Langbase for storing and retrieving document chunks. Uses OpenAI's text-embedding-3-large model for vectorization.

upload-docs.ts

Uploads documents to the knowledge base. Currently configured to upload the FAQ document with metadata including category and topic information.

create-pipe.ts

Creates an AI pipeline (pipe) in Langbase that will be used for generating responses. The pipe is configured with a system prompt for helpful assistance.

agents.ts

Contains the core RAG logic:

  • runMemoryAgent(): Retrieves relevant document chunks based on a query
  • runAiSupportAgent(): Generates AI responses using retrieved chunks
  • getSystemPrompt(): Creates a system prompt that includes retrieved context and citation instructions

index.ts

Main application file that demonstrates the complete RAG workflow by running a sample query through the system.

Configuration

The system is configured to:

  • Use "knowledge-base" as the memory name
  • Retrieve top 4 most relevant chunks (topK: 4)
  • Use "ai-support-agent" as the pipe name
  • Include source citations in responses with proper formatting

Dependencies

  • langbase: Official Langbase SDK for Node.js
  • dotenv: Environment variable management
  • fs/promises: File system operations for document reading

Author

Małgorzata Krawczuk

License

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published