The @openlibx402/ragbot package provides a complete toolkit for implementing Retrieval-Augmented Generation (RAG) pipelines. It includes services for document processing, vector embeddings, semantic search, and LLM-powered question answering.
This package powers the OpenLibx402 RAG Chatbot and can be used to build your own intelligent chatbots and Q&A systems.
import{EmbeddingsService,VectorStoreService,RAGService,LLMService,}from'@openlibx402/ragbot';// Initialize servicesconstembeddings=newEmbeddingsService(process.env.OPENAI_API_KEY);constvectorStore=newVectorStoreService({apiKey:process.env.PINECONE_API_KEY!,indexName:'openlibx402-docs',});// Create RAG serviceconstrag=newRAGService(embeddings,vectorStore);// Create LLM serviceconstllm=newLLMService({apiKey:process.env.OPENAI_API_KEY!,model:'gpt-4o-mini',temperature:0.7,});// Process a user queryasyncfunctionanswerQuestion(query:string){// 1. Retrieve relevant context from vector databaseconstcontext=awaitrag.retrieve(query,{topK:5,minScore:0.7,});// 2. Format context for LLMconstformattedContext=rag.formatContext(context);// 3. Generate responseconstmessages=[{role:'user'asconst,content:query},];constresponse=awaitllm.complete(messages,formattedContext);// 4. Return answer with sourcesreturn{answer:response.message,sources:rag.getSources(context),model:response.model,tokensUsed:response.usage?.total_tokens,};}// Usageconstresult=awaitanswerQuestion('How do I use OpenLibx402 with Express?');console.log(result.answer);console.log('Sources:',result.sources);
Parameters:
- apiKey: OpenAI API key
- options.model: Embedding model to use (default: 'text-embedding-3-small')
- options.dimensions: Embedding dimensions (default: 1536)
Parameters:
- apiKey: OpenAI API key
- model: Chat model to use (default: 'gpt-4o-mini')
- temperature: Sampling temperature 0-2 (default: 0.7)
- maxTokens: Maximum tokens to generate (default: 1000)
- embeddingModel: Embedding model (default: 'text-embedding-3-small')
constllm=newLLMService({apiKey:process.env.OPENAI_API_KEY!,model:'gpt-4o-mini',temperature:0.7,});constresponse=awaitllm.complete([{role:'user',content:'Explain OpenLibx402'}],'OpenLibx402 is a library ecosystem...');console.log(response.message);console.log('Tokens used:',response.usage?.total_tokens);
import{EmbeddingsService,VectorStoreService,RAGService,LLMService,DocumentChunker,}from'@openlibx402/ragbot';import{readFileSync,readdirSync}from'fs';import{join}from'path';classRAGChatbot{privaterag:RAGService;privatellm:LLMService;constructor(openaiKey:string,pineconeKey:string,indexName:string){constembeddings=newEmbeddingsService(openaiKey);constvectorStore=newVectorStoreService({apiKey:pineconeKey,indexName,});this.rag=newRAGService(embeddings,vectorStore);this.llm=newLLMService({apiKey:openaiKey,model:'gpt-4o-mini',temperature:0.7,});}// Index documentation filesasyncindexDocuments(docsDir:string){constchunker=newDocumentChunker({maxChunkSize:1000,chunkOverlap:200,});constfiles=readdirSync(docsDir).filter(f=>f.endsWith('.md'));for(constfileoffiles){constcontent=readFileSync(join(docsDir,file),'utf-8');constchunks=chunker.chunk(content);// Generate embeddings for each chunkconstchunkContents=chunks.map(chunk=>chunk.content);constembeddingsArr=awaitthis.rag['embeddingsService'].generateBatchEmbeddings(chunkContents);constvectors=chunks.map((chunk,i)=>({id:`${file}-chunk-${i}`,values:embeddingsArr[i],metadata:{text:chunk.content,source:file,title:file.replace('.md',''),},}));// Upsert to vector databaseawaitthis.rag['vectorStoreService'].upsert(vectors);}}// Answer a questionasyncask(question:string):Promise<{answer:string;sources:string[];tokensUsed:number;}>{// Retrieve relevant contextconstcontext=awaitthis.rag.retrieve(question,{topK:5,minScore:0.7,});// Format contextconstformattedContext=this.rag.formatContext(context);// Generate responseconstresponse=awaitthis.llm.complete([{role:'user',content:question}],formattedContext);// Extract sourcesconstsources=this.rag.getSources(context).map(s=>s.source);return{answer:response.message,sources,tokensUsed:response.usage?.total_tokens??0,};}// Stream an answerasync*askStream(question:string){constcontext=awaitthis.rag.retrieve(question);constformattedContext=this.rag.formatContext(context);constmessages=[{role:'user'asconst,content:question}];forawait(constchunkofthis.llm.stream(messages,formattedContext)){yieldchunk;}}}// Usageconstchatbot=newRAGChatbot(process.env.OPENAI_API_KEY!,process.env.PINECONE_API_KEY!,'openlibx402-docs');// Index documentationawaitchatbot.indexDocuments('./docs');// Ask questionsconstresult=awaitchatbot.ask('How do I install OpenLibx402?');console.log(result.answer);console.log('Sources:',result.sources);
// High precision (fewer but more relevant results)constcontext=awaitrag.retrieve(query,{topK:3,minScore:0.85,});// High recall (more results, potentially less relevant)constcontext=awaitrag.retrieve(query,{topK:10,minScore:0.65,});
import{X402AutoClient}from'@openlibx402/client';import{RAGService,LLMService}from'@openlibx402/ragbot';// Create X402 client with payment supportconstclient=newX402AutoClient(keypair);// Fetch data from payment-protected APIasyncfunctionanswerWithPaidData(query:string){// Retrieve from local vector databaseconstcontext=awaitrag.retrieve(query);// Fetch additional data from paid APIconstpaidData=awaitclient.get('https://api.example.com/premium-data');// Combine contextsconstcombinedContext=rag.formatContext(context)+'\n\n'+paidData.data;// Generate responsereturnawaitllm.complete([{role:'user',content:query}],combinedContext);}