Building a Vector Database Pipeline with Pinecone and Node.js

Vector databases are essential for RAG (Retrieval Augmented Generation). Here’s a complete setup with Pinecone:

npm install @pinecone-database/pinecone openai

import { Pinecone } from '@pinecone-database/pinecone';
import OpenAI from 'openai';

const pinecone = new Pinecone({ apiKey: process.env.PINECONE_API_KEY });
const openai = new OpenAI();
const index = pinecone.Index('documents');

// Generate embeddings
async function embed(text) {
  const response = await openai.embeddings.create({
    model: 'text-embedding-3-small',
    input: text
  });
  return response.data[0].embedding;
}

// Store document
async function storeDoc(id, content) {
  const vector = await embed(content);
  await index.upsert([{ id, values: vector, metadata: { content } }]);
}

// Query similar docs
async function querySimilar(query, topK = 5) {
  const vector = await embed(query);
  const results = await index.query({ vector, topK, includeMetadata: true });
  return results.matches.map(m => m.metadata.content);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.