Data framework for your LLM application
npx @tessl/cli install tessl/npm-llamaindex@0.11.0LlamaIndex.TS is a comprehensive TypeScript/JavaScript data framework that enables developers to integrate large language models (LLMs) with their own data. It provides a lightweight, easy-to-use set of tools for building LLM applications that can process and query custom data sources, supporting multiple JavaScript runtimes including Node.js, Deno, Bun, and edge environments.
npm install llamaindeximport { Settings, VectorStoreIndex, Document } from "llamaindex";For submodule imports:
import { ReACTAgent } from "llamaindex/agent";
import { RetrieverQueryEngine } from "llamaindex/engines";
import { SimpleVectorStore } from "llamaindex/vector-store";For CommonJS:
const { Settings, VectorStoreIndex, Document } = require("llamaindex");import { Settings, VectorStoreIndex, Document } from "llamaindex";
// Configure global settings
Settings.llm = "your-llm-instance";
Settings.embedModel = "your-embedding-model";
// Create documents and build index
const documents = [
new Document({ text: "This is a document about AI.", id_: "doc1" }),
new Document({ text: "LlamaIndex helps build LLM apps.", id_: "doc2" }),
];
// Build vector index
const index = await VectorStoreIndex.fromDocuments(documents);
// Query the index
const queryEngine = index.asQueryEngine();
const response = await queryEngine.query("Tell me about AI");
console.log(response.toString());LlamaIndex.TS is built around several key architectural components:
Global configuration system for managing LLMs, embedding models, and processing parameters across the framework.
interface GlobalSettings extends Config {
llm: LLM;
embedModel: BaseEmbedding;
nodeParser: NodeParser;
promptHelper: PromptHelper;
callbackManager: CallbackManager;
chunkSize: number | undefined;
chunkOverlap: number | undefined;
prompt: PromptConfig;
debug: boolean;
withLLM<Result>(llm: LLM, fn: () => Result): Result;
withEmbedModel<Result>(embedModel: BaseEmbedding, fn: () => Result): Result;
withNodeParser<Result>(nodeParser: NodeParser, fn: () => Result): Result;
withPromptHelper<Result>(promptHelper: PromptHelper, fn: () => Result): Result;
withCallbackManager<Result>(callbackManager: CallbackManager, fn: () => Result): Result;
withChunkSize<Result>(chunkSize: number, fn: () => Result): Result;
withChunkOverlap<Result>(chunkOverlap: number, fn: () => Result): Result;
withPrompt<Result>(prompt: PromptConfig, fn: () => Result): Result;
}
const Settings: GlobalSettings;Text processing and document chunking functionality for preparing data for indexing and retrieval.
class SentenceSplitter implements NodeParser {
constructor(options?: { chunkSize?: number; chunkOverlap?: number });
getNodesFromDocuments(documents: Document[]): TextNode[];
}
class Document {
constructor(init: { text: string; id_?: string; metadata?: Record<string, any> });
text: string;
id_: string;
metadata: Record<string, any>;
}Core indexing functionality for creating searchable representations of documents using vector embeddings.
class VectorStoreIndex {
static fromDocuments(documents: Document[]): Promise<VectorStoreIndex>;
asQueryEngine(): QueryEngine;
asRetriever(): BaseRetriever;
insert(document: Document): Promise<void>;
}
interface BaseVectorStore {
add(nodes: BaseNode[]): Promise<string[]>;
query(query: VectorStoreQuery): Promise<VectorStoreQueryResult>;
}Query processing and response synthesis for retrieving and generating answers from indexed data.
interface BaseQueryEngine {
query(query: string): Promise<EngineResponse>;
}
class RetrieverQueryEngine implements BaseQueryEngine {
constructor(retriever: BaseRetriever, responseSynthesizer?: ResponseSynthesizer);
query(query: string): Promise<EngineResponse>;
}Conversational interfaces that maintain context and enable back-and-forth interactions with your data.
interface BaseChatEngine {
chat(message: string): Promise<EngineResponse>;
reset(): void;
}
class ContextChatEngine implements BaseChatEngine {
constructor(options: { retriever: BaseRetriever; memory?: BaseMemory });
chat(message: string): Promise<EngineResponse>;
}ReAct agents and task execution system for complex reasoning and multi-step operations with tool usage.
class ReActAgent {
constructor(params: ReACTAgentParams);
chat(message: string): Promise<AgentChatResponse>;
createTask(input: string): Task;
}
interface AgentParamsBase {
tools: BaseTool[];
llm: LLM;
memory?: BaseMemory;
}Comprehensive integration with large language models including OpenAI, Anthropic, and other providers.
interface LLM {
chat(messages: ChatMessage[]): Promise<ChatResponse>;
complete(prompt: string): Promise<CompletionResponse>;
metadata: LLMMetadata;
}
interface ChatMessage {
role: MessageType;
content: MessageContent;
}
type MessageContent = string | MessageContentDetail[];Text embedding generation and similarity operations for semantic search and retrieval.
interface BaseEmbedding {
getTextEmbedding(text: string): Promise<number[]>;
getQueryEmbedding(query: string): Promise<number[]>;
}
function similarity(embedding1: number[], embedding2: number[]): number;
function getTopKEmbeddings(
queryEmbedding: number[],
embeddings: number[][],
k: number
): number[];Persistent storage backends for documents, indices, and vector data across different storage systems.
interface BaseDocumentStore {
addDocuments(documents: Document[]): Promise<void>;
getDocument(docId: string): Promise<Document | undefined>;
getAllDocuments(): Promise<Document[]>;
}
class StorageContext {
static fromDefaults(options?: {
docStore?: BaseDocumentStore;
indexStore?: BaseIndexStore;
vectorStore?: BaseVectorStore;
}): StorageContext;
}Tool integration system for extending agent capabilities with external functions and APIs.
interface BaseTool {
metadata: ToolMetadata;
call(input: string): Promise<ToolOutput>;
}
class QueryEngineTool implements BaseTool {
constructor(queryEngine: BaseQueryEngine, metadata: ToolMetadata);
call(input: string): Promise<ToolOutput>;
}Response generation and synthesis strategies for combining retrieved information into coherent answers.
interface BaseSynthesizer {
synthesize(query: string, nodes: BaseNode[]): Promise<EngineResponse>;
}
type ResponseMode = "refine" | "compact" | "tree_summarize" | "simple_summarize";
function createResponseSynthesizer(mode: ResponseMode): BaseSynthesizer;Advanced document ingestion system with support for transformation pipelines, duplicate detection, and batch processing.
class IngestionPipeline {
constructor(options: {
transformations?: TransformComponent[];
readers?: Record<string, BaseReader>;
vectorStore?: BaseVectorStore;
cache?: IngestionCache;
docStore?: BaseDocumentStore;
duplicatesStrategy?: DuplicatesStrategy;
});
async run(
documents?: Document[],
inputDir?: string,
cacheCollection?: string
): Promise<BaseNode[]>;
loadDataFromDirectory(inputDir: string, inputFiles?: string[]): Promise<Document[]>;
// Properties
transformations: TransformComponent[];
readers: Record<string, BaseReader>;
vectorStore?: BaseVectorStore;
cache?: IngestionCache;
docStore?: BaseDocumentStore;
duplicatesStrategy: DuplicatesStrategy;
}
enum DuplicatesStrategy {
DUPLICATES_ONLY = "duplicates_only",
UPSERTS = "upserts",
UPSERTS_AND_DELETE = "upserts_and_delete",
}
interface TransformComponent {
transform(nodes: BaseNode[], options?: Record<string, any>): Promise<BaseNode[]>;
}Comprehensive evaluation system for assessing LLM application performance with built-in metrics and custom evaluators.
interface BaseEvaluator {
evaluate(query: string, response: string, contexts?: string[]): Promise<EvaluationResult>;
}
interface EvaluationResult {
query: string;
response: string;
score: number;
feedback: string;
passingGrade?: boolean;
metadata?: Record<string, any>;
}
class FaithfulnessEvaluator implements BaseEvaluator {
constructor(options?: { llm?: LLM });
evaluate(query: string, response: string, contexts: string[]): Promise<EvaluationResult>;
}
class RelevancyEvaluator implements BaseEvaluator {
constructor(options?: { llm?: LLM });
evaluate(query: string, response: string, contexts?: string[]): Promise<EvaluationResult>;
}
class CorrectnessEvaluator implements BaseEvaluator {
constructor(options?: { llm?: LLM });
evaluate(query: string, response: string, contexts?: string[], referenceAnswer?: string): Promise<EvaluationResult>;
}abstract class BaseNode<T extends Metadata = Metadata> {
id_: string;
embedding?: number[];
metadata: T;
excludedEmbedMetadataKeys: string[];
excludedLlmMetadataKeys: string[];
relationships: Partial<Record<NodeRelationship, RelatedNodeType<T>>>;
hash: string;
abstract get type(): ObjectType;
abstract getContent(metadataMode?: MetadataMode): string;
abstract getMetadataStr(metadataMode?: MetadataMode): string;
abstract setContent(value: string): void;
}
class TextNode extends BaseNode {
constructor(init?: {
text?: string;
id_?: string;
metadata?: Record<string, any>;
embedding?: number[];
excludedEmbedMetadataKeys?: string[];
excludedLlmMetadataKeys?: string[];
relationships?: Partial<Record<NodeRelationship, RelatedNodeType>>;
});
text: string;
startCharIdx?: number;
endCharIdx?: number;
textTemplate: string;
metadataTemplate: string;
metadataSeparator: string;
}
class ImageNode extends BaseNode {
constructor(init?: {
image?: string;
text?: string;
mimetype?: string;
imageUrl?: string;
imagePath?: string;
id_?: string;
metadata?: Record<string, any>;
});
image?: string;
imageUrl?: string;
imagePath?: string;
imageMimetype?: string;
}
class Document extends TextNode {
constructor(init: {
text: string;
id_?: string;
metadata?: Record<string, any>;
excludedLlmMetadataKeys?: string[];
excludedEmbedMetadataKeys?: string[];
relationships?: Partial<Record<NodeRelationship, RelatedNodeType>>;
mimetype?: string;
textTemplate?: string;
});
docId?: string;
mimetype?: string;
}
class EngineResponse {
response: string;
sourceNodes?: NodeWithScore[];
metadata?: Record<string, any>;
toString(): string;
print(): void;
}
interface NodeWithScore {
node: BaseNode;
score?: number;
}
enum NodeRelationship {
SOURCE = "SOURCE",
PREVIOUS = "PREVIOUS",
NEXT = "NEXT",
PARENT = "PARENT",
CHILD = "CHILD",
}
enum ObjectType {
TEXT = "TEXT",
IMAGE = "IMAGE",
INDEX = "INDEX",
DOCUMENT = "DOCUMENT",
IMAGE_DOCUMENT = "IMAGE_DOCUMENT",
}
enum MetadataMode {
ALL = "ALL",
EMBED = "EMBED",
LLM = "LLM",
NONE = "NONE",
}
type Metadata = Record<string, any>;
interface RelatedNodeInfo<T extends Metadata = Metadata> {
nodeId: string;
nodeType?: ObjectType;
metadata: T;
hash?: string;
}
type RelatedNodeType<T extends Metadata = Metadata> =
| RelatedNodeInfo<T>
| RelatedNodeInfo<T>[];interface Config {
prompt: PromptConfig;
promptHelper: PromptHelper | null;
embedModel: BaseEmbedding | null;
nodeParser: NodeParser | null;
callbackManager: CallbackManager | null;
chunkSize: number | undefined;
chunkOverlap: number | undefined;
}
interface PromptConfig {
llm?: string;
lang?: string;
}interface StructuredOutput<T> {
rawOutput: string;
parsedOutput: T;
}
type UUID = `${string}-${string}-${string}-${string}-${string}`;
type ToolMetadataOnlyDescription = Pick<ToolMetadata, "description">;const DEFAULT_CHUNK_SIZE: 1024;
const DEFAULT_CHUNK_OVERLAP: 200;
const DEFAULT_CHUNK_OVERLAP_RATIO: 0.1;
const DEFAULT_CONTEXT_WINDOW: 3900;
const DEFAULT_NUM_OUTPUTS: 256;
const DEFAULT_SIMILARITY_TOP_K: 2;
const DEFAULT_PADDING: 5;
const DEFAULT_BASE_URL: string;
const DEFAULT_COLLECTION: string;
const DEFAULT_DOC_STORE_PERSIST_FILENAME: "docstore.json";
const DEFAULT_GRAPH_STORE_PERSIST_FILENAME: "graph_store.json";
const DEFAULT_INDEX_STORE_PERSIST_FILENAME: "index_store.json";
const DEFAULT_NAMESPACE: "default";
const DEFAULT_PERSIST_DIR: "./storage";
const DEFAULT_PROJECT_NAME: string;
const DEFAULT_VECTOR_STORE_PERSIST_FILENAME: "vector_store.json";interface LlamaIndexEventMaps {
"llm-start": LLMStartEvent;
"llm-end": LLMEndEvent;
"llm-stream": LLMStreamEvent;
"llm-tool-call": LLMToolCallEvent;
"llm-tool-result": LLMToolResultEvent;
}
interface LLMStartEvent {
id: string;
timestamp: Date;
payload: {
messages: ChatMessage[];
additionalKwargs?: Record<string, any>;
};
}
interface LLMEndEvent {
id: string;
timestamp: Date;
payload: {
response: ChatResponse | CompletionResponse;
};
}
interface LLMStreamEvent {
id: string;
timestamp: Date;
payload: {
chunk: string;
snapshot: string;
};
}
interface LLMToolCallEvent {
id: string;
timestamp: Date;
payload: {
toolCall: ToolCall;
};
}
interface LLMToolResultEvent {
id: string;
timestamp: Date;
payload: {
toolResult: ToolOutput;
};
}
type JSONValue = string | number | boolean | null | { [key: string]: JSONValue } | JSONValue[];
type JSONObject = { [key: string]: JSONValue };
type JSONArray = JSONValue[];