Overview
LlamaIndex.TS provides a unified interface for working with Large Language Models (LLMs) from various providers. All LLMs implement theBaseLLM interface, allowing you to switch between providers with minimal code changes.
BaseLLM Interface
TheBaseLLM abstract class from @llamaindex/core/llms provides the foundation for all LLM implementations:
LLM Metadata
Every LLM instance exposes metadata about its configuration:Chat vs Completion
LlamaIndex.TS supports two interaction modes:Chat API
The chat API uses message-based conversations with role-aware messages:system- System instructions that guide the model’s behavioruser- User messages/queriesassistant- Model responsesdeveloper- Developer messages (for some providers)memory- Memory/context messages
Completion API
The completion API is simpler, using direct text prompts:The
complete method internally converts to chat messages, so both APIs use the same underlying implementation.Streaming
All LLMs support streaming responses for real-time output:Streaming Chat
Streaming Completion
Function Calling
Modern LLMs support function calling (also called tool calling) to interact with external tools:Using Tools
Structured Output with exec()
Theexec() method provides an easier way to handle tool calling and structured output:
Streaming with Tools
Configuration Options
All LLMs support common configuration options:Provider-Specific Options
Some providers offer additional options viaadditionalChatOptions:
Multi-Modal Support
Many LLMs support images, audio, and other modalities:Images
Files (PDFs, etc.)
Examples
OpenAI
Anthropic
Ollama (Local Models)
Google Gemini
Best Practices
Choose the right temperature
Choose the right temperature
- 0.0-0.3: Deterministic, factual tasks (extraction, classification)
- 0.4-0.7: Balanced (general chat, Q&A)
- 0.8-1.0: Creative tasks (writing, brainstorming)
Handle errors gracefully
Handle errors gracefully
Stream for better UX
Stream for better UX
Always use streaming for user-facing applications to provide immediate feedback:
Use environment variables for API keys
Use environment variables for API keys
Next Steps
Embeddings
Learn about embedding models for semantic search
Providers
Explore all available LLM providers