Skip to main content

LlamaIndex Adapter

LlamaIndex is a framework for building LLM-powered applications. LlamaIndex helps you ingest, structure, and access private or domain-specific data. LlamaIndex.TS offers the core features of LlamaIndex for Python for popular runtimes like Node.js (official support), Vercel Edge Functions (experimental), and Deno (experimental).

Installation

llamaindex is a required peer dependency.

Features

  • Transform LlamaIndex ChatEngine and QueryEngine streams to AI SDK UIMessageStream
  • Seamless integration with AI SDK UI components like useCompletion
  • Support for RAG (Retrieval Augmented Generation) workflows
  • Compatible with LlamaIndex’s document processing and indexing capabilities

Example: Completion

Here is a basic example that uses both AI SDK and LlamaIndex together with the Next.js App Router. The AI SDK @ai-sdk/llamaindex package uses the stream result from calling the chat method on a LlamaIndex ChatEngine or the query method on a LlamaIndex QueryEngine to pipe text to the client.
Then, we use the AI SDK’s useCompletion method in the page component to handle the completion:

Example: RAG with QueryEngine

LlamaIndex excels at building RAG applications. Here’s an example using a QueryEngine with document indexing:

Example: Chat with Context

Build a conversational interface with document context:
Use with the useCompletion hook on the client:

API Reference

toUIMessageStream(stream)

Converts a LlamaIndex ChatEngine or QueryEngine stream to an AI SDK UIMessageStream.
Parameters:
  • stream: AsyncIterable - Stream from LlamaIndex ChatEngine or QueryEngine
Returns: ReadableStream<UIMessageChunk>

Integration with LlamaIndex Features

The adapter works seamlessly with LlamaIndex’s powerful features:

Document Loaders

  • Load documents from various sources (files, URLs, databases)
  • Support for multiple file formats (PDF, Markdown, JSON, etc.)
  • Custom document readers

Vector Stores

  • In-memory vector storage
  • Integration with external vector databases
  • Efficient similarity search

Retrievers

  • Vector similarity retrieval
  • Keyword-based retrieval
  • Hybrid retrieval strategies

Query Engines

  • Simple query engine for basic RAG
  • Sub-question query engine for complex queries
  • Custom query engines

Chat Engines

  • Simple chat engine
  • Context chat engine with retrieval
  • Condense question chat engine

More Examples

create-llama is the easiest way to get started with LlamaIndex. It uses the AI SDK to connect to LlamaIndex in all its generated code.

Learn More