@ai-sdk/llamaindex Adapter

API Reference for the LlamaIndex Adapter.

The @ai-sdk/llamaindex package provides helper functions to transform LlamaIndex output streams into data streams and data stream responses. See the LlamaIndex Adapter documentation for more information.

It supports:

  • LlamaIndex ChatEngine streams
  • LlamaIndex QueryEngine streams

Import

import { toDataResponse } from "@ai-sdk/llamaindex"

API Signature

Methods

Examples

Convert LlamaIndex ChatEngine Stream

app/api/completion/route.ts
import { OpenAI, SimpleChatEngine } from 'llamaindex';
import { toDataStreamResponse } from '@ai-sdk/llamaindex';

export async function POST(req: Request) {
  const { prompt } = await req.json();

  const llm = new OpenAI({ model: 'gpt-4o' });
  const chatEngine = new SimpleChatEngine({ llm });

  const stream = await chatEngine.chat({
    message: prompt,
    stream: true,
  });

  return toDataStreamResponse(stream);
}