Learn vectorstores
Installation
Quick Start
Section titled “Quick Start”Install the core package:
npm i @vectorstores/corepnpm add @vectorstores/coreyarn add @vectorstores/corebun add @vectorstores/coreAnd then the package for the vector database you want to use, e.g. for Qdrant use:
npm i @vectorstores/qdrantpnpm add @vectorstores/qdrantyarn add @vectorstores/qdrantbun add @vectorstores/qdrantTypeScript Configuration
Section titled “TypeScript Configuration”vectorstores is built with TypeScript and provides excellent type safety. Add these settings to your tsconfig.json:
{ "compilerOptions": { // Essential for module resolution "moduleResolution": "bundler", // or "nodenext" | "node16" | "node"
// Required for Web Stream API support "lib": ["DOM.AsyncIterable"],
// Recommended for better compatibility "target": "es2020", "module": "esnext" }}Your first ingestion example
Section titled “Your first ingestion example”Set up
Section titled “Set up”If you don’t already have a project, you can create a new one in a new folder:
npm initnpm i -D typescript @types/node tsxnpm i @vectorstores/core openaipnpm initpnpm add -D typescript @types/node tsxpnpm add @vectorstores/core openaiyarn inityarn add -D typescript @types/node tsxyarn add @vectorstores/core openaibun initbun add -D typescript @types/node tsxbun add @vectorstores/core openaiYou’ll also need to set your OpenAI API key:
export OPENAI_API_KEY=your-api-key-hereCreate the example
Section titled “Create the example”Create the file example.ts. This code will:
- Configure OpenAI embeddings for generating vector representations
- Load a text file from your filesystem
- Create a
Documentobject from the text - Build a
VectorStoreIndexthat automatically splits the text and generates embeddings - Create a retriever to search the indexed content
- Query the index and display results in a formatted table
import { Document, VectorStoreIndex, Settings, type TextEmbedFunc } from "@vectorstores/core";import { OpenAI } from "openai";import fs from "node:fs/promises";import { fileURLToPath } from "node:url";
// Configure OpenAI embeddingsconst openai = new OpenAI();Settings.embedFunc = async (input: string[]): Promise<number[][]> => { const { data } = await openai.embeddings.create({ model: "text-embedding-3-small", input, }); return data.map((d) => d.embedding);};
async function main() { // Load a text file (create a sample.txt file with some text content) const filePath = fileURLToPath(new URL("./sample.txt", import.meta.url)); const text = await fs.readFile(filePath, "utf-8");
// Create Document object with the text const document = new Document({ text, id_: filePath });
// Split text and create embeddings. Store them in a VectorStoreIndex const index = await VectorStoreIndex.fromDocuments([document]);
// Create a retriever from the index const retriever = index.asRetriever();
// Query the index const response = await retriever.retrieve({ query: "What is the main topic?", });
// Display results console.log(`Found ${response.length} result(s):\n`); response.forEach((result, index) => { console.log(`${index + 1}. Score: ${(result.score! * 100).toFixed(2)}%`); console.log(` Text: ${result.node.text?.substring(0, 100)}...\n`); });}
main().catch(console.error);Create a sample text file sample.txt in the same directory:
Machine learning is a subset of artificial intelligence that focuses on algorithmsthat can learn from data. These algorithms build mathematical models based ontraining data to make predictions or decisions without being explicitly programmedto perform the task.To run the code:
npx tsx example.tspnpm dlx tsx example.tsyarn dlx tsx example.tsbunx tsx example.tsYou should expect output something like:
Found 1 result(s):
1. Score: 85.32% Text: Machine learning is a subset of artificial intelligence that focuses on algorithmsthat can learn from data. These algorithms build mathemat...This example demonstrates the core workflow of vectorstores:
- Ingestion: Converting your text into
Documentobjects - Indexing: Automatically splitting text into chunks and generating embeddings
- Retrieval: Querying the index to find semantically similar content
What’s Next?
Section titled “What’s Next?”Show me code examples