It’s the TypeScript port of LlamaIndex Python, designed to work across multiple JavaScript runtimes including Node.js, Deno, Bun, and edge environments.
What's the difference between LlamaIndex.TS and LlamaIndex Python?
Both frameworks share the same core concepts but have some differences:Similarities:
Same core abstractions (indices, query engines, chat engines)
Similar API design and patterns
Support for the same LLM providers
RAG and agent capabilities
Differences:
Language: TypeScript vs Python
Package structure: Modular npm packages vs Python namespace packages
Runtime support: Multi-runtime JS vs Python only
Feature parity: Python has more features currently
No! LlamaIndex.TS is a standalone TypeScript/JavaScript library. While the concepts are similar to the Python version, you don’t need any Python knowledge to use it.If you’re coming from Python, check out the Migration from Python guide.
Vercel Edge Runtime ✅ (limited file system access)
Cloudflare Workers ✅ (limited file system access)
Not Supported:
Browser ❌ (due to lack of AsyncLocalStorage-like APIs)
The framework uses conditional exports to provide runtime-specific entry points.
Can I use LlamaIndex.TS in the browser?
Browser support is currently limited due to the lack of support for AsyncLocalStorage-like APIs in browsers. However, work is ongoing to improve browser compatibility.For now, we recommend using LlamaIndex.TS on the server-side (Node.js, edge runtimes) and calling it from your browser via API routes.
LlamaIndex.TS provides several ways to load data:1. SimpleDirectoryReader (for files):
import { SimpleDirectoryReader } from "llamaindex";const reader = new SimpleDirectoryReader();const documents = await reader.loadData({ directoryPath: "./data" });
2. Specialized readers:
import { PDFReader, DocxReader } from "@llamaindex/readers";const pdfReader = new PDFReader();const docs = await pdfReader.loadData("document.pdf");
3. Manual document creation:
import { Document } from "llamaindex";const doc = new Document({ text: "Your content here" });
How do I query my data?
Create an index and query it:
import { VectorStoreIndex } from "llamaindex";// Create indexconst index = await VectorStoreIndex.fromDocuments(documents);// Queryconst queryEngine = index.asQueryEngine();const response = await queryEngine.query({ query: "What is LlamaIndex?",});console.log(response.toString());
How do I build a chatbot?
Use a chat engine:
import { VectorStoreIndex } from "llamaindex";const index = await VectorStoreIndex.fromDocuments(documents);const chatEngine = index.asChatEngine();// Chat with contextconst response = await chatEngine.chat({ message: "What does the document say about X?",});console.log(response.toString());
Chat engines maintain conversation history automatically.
How do I use a different LLM provider?
Configure Settings.llm:
import { Settings } from "llamaindex";// Anthropicimport { Anthropic } from "@llamaindex/anthropic";Settings.llm = new Anthropic({ model: "claude-3-sonnet" });// Google Geminiimport { Gemini } from "@llamaindex/google";Settings.llm = new Gemini({ model: "gemini-pro" });// Ollama (local)import { Ollama } from "@llamaindex/ollama";Settings.llm = new Ollama({ model: "llama3" });
How do I use a vector database?
Install the vector store provider and use it:
import { VectorStoreIndex } from "llamaindex";import { PineconeVectorStore } from "@llamaindex/pinecone";const vectorStore = new PineconeVectorStore({ indexName: "my-index", apiKey: process.env.PINECONE_API_KEY,});const index = await VectorStoreIndex.fromDocuments(documents, { vectorStore,});
Supported vector stores:
Pinecone, Qdrant, Chroma, Weaviate
MongoDB, PostgreSQL (pgvector)
Supabase, Milvus, Astra
And more!
How do I stream responses?
Set stream: true in your query:
const stream = await queryEngine.query({ query: "What is LlamaIndex?", stream: true,});for await (const chunk of stream) { process.stdout.write(chunk.response);}
import { OpenAI } from "@llamaindex/openai";Settings.llm = new OpenAI({ maxRetries: 3, timeout: 60000,});
Use a smaller model:
Settings.llm = new OpenAI({ model: "gpt-3.5-turbo" });
Process in batches:
for (let i = 0; i < documents.length; i += 10) { const batch = documents.slice(i, i + 10); await index.insert(batch); await new Promise(resolve => setTimeout(resolve, 1000));}
My embeddings are too slow. How can I speed them up?
Options:
Use batch embedding:
import { OpenAIEmbedding } from "@llamaindex/openai";Settings.embedModel = new OpenAIEmbedding({ batchSize: 100,});
Use a faster model:
Settings.embedModel = new OpenAIEmbedding({ model: "text-embedding-3-small", // Faster than large});
Cache embeddings in a vector store.
How do I debug issues?
Enable debug mode:
import { Settings } from "llamaindex";Settings.debug = true;