Interface between LLMs and your data for building retrieval-augmented generation (RAG) applications
—
Unified interface for various language models with support for completion, chat, and function calling APIs across multiple providers.
Core LLM abstraction providing unified interface across providers.
class LLM:
"""
Base class for Language Model integrations.
Args:
model: Model name or identifier
temperature: Sampling temperature (0.0 to 1.0)
max_tokens: Maximum tokens to generate
additional_kwargs: Provider-specific arguments
callback_manager: Callback manager for events
**kwargs: Additional arguments
"""
def __init__(
self,
model=None,
temperature=0.1,
max_tokens=None,
additional_kwargs=None,
callback_manager=None,
**kwargs
): ...
def complete(self, prompt, **kwargs):
"""
Complete a text prompt.
Args:
prompt: Text prompt to complete
**kwargs: Additional completion arguments
Returns:
CompletionResponse: Generated completion
"""
def chat(self, messages, **kwargs):
"""
Generate chat response from message history.
Args:
messages: List of ChatMessage objects
**kwargs: Additional chat arguments
Returns:
ChatResponse: Generated chat response
"""
def stream_complete(self, prompt, **kwargs):
"""Stream completion response."""
def stream_chat(self, messages, **kwargs):
"""Stream chat response."""
async def acomplete(self, prompt, **kwargs):
"""Async completion."""
async def achat(self, messages, **kwargs):
"""Async chat."""OpenAI language models including GPT-3.5, GPT-4, and function calling support.
class OpenAI(LLM):
"""
OpenAI language model integration.
Args:
model: OpenAI model name (e.g., "gpt-3.5-turbo", "gpt-4")
temperature: Sampling temperature
max_tokens: Maximum tokens to generate
api_key: OpenAI API key
api_base: Custom API base URL
api_version: API version
**kwargs: Additional OpenAI arguments
"""
def __init__(
self,
model="gpt-3.5-turbo",
temperature=0.1,
max_tokens=None,
api_key=None,
api_base=None,
api_version=None,
**kwargs
): ...
def complete(self, prompt, **kwargs):
"""OpenAI completion."""
def chat(self, messages, **kwargs):
"""OpenAI chat completion."""
def get_tool_calls_from_response(self, response, error_on_no_tool_call=True):
"""
Extract tool calls from OpenAI response.
Args:
response: OpenAI API response
error_on_no_tool_call: Whether to error if no tool calls found
Returns:
List: List of tool call objects
"""Message representations for chat-based LLM interactions.
class ChatMessage:
"""
Chat message representation.
Args:
role: Message role (user, assistant, system, tool)
content: Message content
additional_kwargs: Additional message metadata
"""
def __init__(self, role, content=None, additional_kwargs=None): ...
@property
def role(self):
"""Message role."""
@property
def content(self):
"""Message content."""
class MessageRole:
"""Message role constants."""
USER = "user"
ASSISTANT = "assistant"
SYSTEM = "system"
TOOL = "tool"
FUNCTION = "function" # Deprecated, use TOOLResponse objects for LLM completions and chat.
class CompletionResponse:
"""
Response from completion API.
Args:
text: Generated text
additional_kwargs: Additional response metadata
raw: Raw response from provider
"""
def __init__(self, text=None, additional_kwargs=None, raw=None): ...
@property
def text(self):
"""Generated text."""
class ChatResponse:
"""
Response from chat API.
Args:
message: Generated message
additional_kwargs: Additional response metadata
raw: Raw response from provider
"""
def __init__(self, message=None, additional_kwargs=None, raw=None): ...
@property
def message(self):
"""Generated chat message."""Support for structured function calling with LLMs.
class FunctionCallingLLM(LLM):
"""Base class for LLMs with function calling support."""
def predict_and_call(self, tools, user_msg, **kwargs):
"""
Predict and execute function calls.
Args:
tools: List of available tools/functions
user_msg: User message
**kwargs: Additional arguments
Returns:
AgentChatResponse: Response with function call results
"""
def get_tool_calls_from_response(self, response, error_on_no_tool_call=True):
"""Extract tool calls from response."""
class ToolSelection:
"""
Tool selection for function calling.
Args:
tool_id: Tool identifier
tool_name: Tool name
tool_kwargs: Tool arguments
"""
def __init__(self, tool_id, tool_name, tool_kwargs=None): ...Base classes for implementing custom LLM integrations.
class CustomLLM(LLM):
"""
Base class for custom LLM implementations.
Subclasses should implement:
- _complete: Core completion logic
- _chat: Core chat logic (optional)
- metadata: Model metadata
"""
@property
def metadata(self):
"""
Model metadata including context window and model name.
Returns:
LLMMetadata: Model metadata object
"""
def _complete(self, prompt, **kwargs):
"""
Implementation-specific completion logic.
Args:
prompt: Text prompt
**kwargs: Additional arguments
Returns:
CompletionResponse: Generated completion
"""
raise NotImplementedError
def _chat(self, messages, **kwargs):
"""Implementation-specific chat logic."""Support for LLMs that can process images and other media.
class MultiModalLLM(LLM):
"""Base class for multi-modal LLMs."""
def complete(self, prompt, image_documents=None, **kwargs):
"""
Complete with optional image inputs.
Args:
prompt: Text prompt
image_documents: List of image documents
**kwargs: Additional arguments
Returns:
CompletionResponse: Generated completion
"""
def chat(self, messages, **kwargs):
"""Chat with multi-modal message support."""class LLMMetadata:
"""
Metadata about an LLM model.
Args:
context_window: Maximum context window size
num_output: Maximum output tokens
is_chat_model: Whether model supports chat API
is_function_calling_model: Whether model supports function calling
model_name: Model identifier
"""
def __init__(
self,
context_window=None,
num_output=None,
is_chat_model=False,
is_function_calling_model=False,
model_name=None,
): ...
def resolve_llm(llm=None):
"""
Resolve LLM instance from various inputs.
Args:
llm: LLM instance, string identifier, or None
Returns:
LLM: Resolved LLM instance
"""# OpenAI
from llama_index.llms.openai import OpenAI
llm = OpenAI(model="gpt-4", temperature=0.1)
# Anthropic
from llama_index.llms.anthropic import Anthropic
llm = Anthropic(model="claude-3-sonnet")
# Local models via Ollama
from llama_index.llms.ollama import Ollama
llm = Ollama(model="llama2")
# Azure OpenAI
from llama_index.llms.azure_openai import AzureOpenAI
llm = AzureOpenAI(
model="gpt-35-turbo",
deployment_name="my-deployment",
api_key="...",
azure_endpoint="https://my-resource.openai.azure.com/"
)Install with Tessl CLI
npx tessl i tessl/pypi-llama-index