5.0 KiB
CLAUDE.md - Examples Package
This directory contains comprehensive examples demonstrating LlamaIndex.TS functionality across different use cases and integrations.
Running Examples
All examples are executable TypeScript files that can be run directly:
# Run a specific example
npx tsx ./rag/starter.ts
npx tsx ./agents/agent/single-agent.ts
npx tsx ./models/openai/openai.ts
# Or use the package script
npm start # runs ./starter.ts (if it exists)
Environment Setup
Most examples require API keys. Set environment variables before running:
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-..."
export PINECONE_API_KEY="..."
# Add other provider keys as needed
Example Categories
Agents (agents/)
Demonstrates agent functionality and workflows:
agent/- Modern agent implementations using@llamaindex/workflowsingle-agent.ts- Basic agent with tool usagemultiple-agents.ts- Multi-agent coordinationblog-writer.ts- Content generation agentwith-anthropic.ts,with-ollama.ts- Provider-specific agents
workflow/- Workflow orchestration examplestoolsStream.ts- Streaming tool interactions
RAG (Retrieval-Augmented Generation) (rag/)
Core RAG functionality examples:
starter.ts- Basic RAG setup with VectorStoreIndexchatEngine.ts- Conversational RAG interfacechat-engine/- Different chat engine implementationsextractors/- Metadata extraction examplesnodeParser/- Custom text chunking strategiessplit.ts,sentenceWindow.ts- Text processing techniques
Models (models/)
Provider-specific LLM and embedding examples:
openai/- OpenAI integration (chat, completions, embeddings, multimodal)anthropic/- Claude models with streaming and cachinggemini/- Google Gemini including live API examplesollama/,groq/,mistral/- Alternative LLM providersrerankers/- Result reranking implementations
Storage (storage/)
Vector store and database integrations:
pinecone-vector-store/- Pinecone setup and queryingchromadb/,qdrantdb/,weaviate/- Alternative vector storesmongodb/,pg/,firestore/- Database integrationsmetadata-filter/- Filtering and search parameters
Multimodal (multimodal/)
Vision and multimodal capabilities:
chat.ts- Image analysis with chatload.ts,retrieve.ts- Multimodal document processingclip.ts- CLIP embeddings for images
Readers (readers/)
Document ingestion from various sources:
src/- File format readers (PDF, DOCX, CSV, JSON, HTML)llamaparse.ts- LlamaParse document processingdiscord/,notion/,assemblyai/- Platform-specific readers
Cloud (cloud/)
LlamaCloud integration examples:
chat.ts,query.ts- Cloud-based RAGfrom-documents.ts- Document upload to cloud
Deprecated (deprecated/)
Legacy agent implementations for reference (prefer agents/agent/ for new code).
Key Development Patterns
Example Structure
Most examples follow this pattern:
import { ... } from "llamaindex";
import { ... } from "@llamaindex/provider";
async function main() {
// Setup (API keys, configuration)
// Create components (LLM, embeddings, vector store)
// Build index or engine
// Execute query/chat
// Output results
}
main().catch(console.error);
Provider Imports
Examples use modular provider imports:
// Specific provider packages
import { OpenAI } from "@llamaindex/openai";
import { claude } from "@llamaindex/anthropic";
// Core functionality
import { VectorStoreIndex, Document } from "llamaindex";
Error Handling
Include proper error handling and API key validation:
if (!process.env.OPENAI_API_KEY) {
console.log("API key required");
process.exit(1);
}
Dependencies
The examples package includes all major LlamaIndex.TS providers and integrations. Key dependencies:
- Core:
llamaindex,@llamaindex/core - Providers: All LLM, embedding, and vector store providers
- Tools:
@llamaindex/workflow,@llamaindex/tools - Utilities:
tsxfor TypeScript execution,dotenvfor environment variables
Usage Notes
- Build First: Some examples may require building the packages first:
pnpm build - Data Files: Many examples reference files in
./data/directory - API Costs: Be aware that running examples will consume API credits
- Environment: Examples are designed to run in Node.js environment
- Interactive Examples: Some examples include readline interfaces for interactive testing
Creating New Examples
When adding new examples:
- Follow the established directory structure by category
- Use descriptive filenames that indicate functionality
- Include proper imports from modular packages
- Add error handling and environment validation
- Include comments explaining key concepts
- Test with minimal required dependencies