Skip to content
LlamaIndex 文档版本 跟随上游 源快照 a8bdf467 更新于 2026-10-15

提示

关于我们支持的 LLM 清单及其能力对比,请看 LLM 模块指南。

构建基于 LLM 的应用,第一步往往就是选哪个 LLM:它们各有强弱、价格不同,你甚至可能同时用好几个。

LlamaIndex 为大量不同的 LLM 提供了统一的接口。使用一个 LLM 可以很简单,装好对应的集成即可:

终端窗口

pip install llama-index-llms-openai

然后一行代码就能调用它:

from llama_index.llms.openai import OpenAI

response = OpenAI().complete("William Shakespeare is ")
print(response)

注意这需要在你环境里准备好名为 OPENAI_API_KEY 的 API key;更多细节见起步教程。

complete 也有对应的异步方法 acomplete。

你也可以通过 stream_complete 获得流式响应,它返回一个在 token 生成时就逐个产出的生成器:

handle = OpenAI().stream_complete("William Shakespeare is ")

for token in handle:
    print(token.delta, end="", flush=True)

stream_complete 同样有异步版本 astream_complete。

对话接口

LLM 类还实现了 chat 方法,让你能进行更精细的交互:

messages = [
    ChatMessage(role="system", content="You are a helpful assistant."),
    ChatMessage(role="user", content="Tell me a joke."),
]
chat_response = llm.chat(messages)

若要流式响应(token 一边生成一边产出),可以使用 stream_chat 和 astream_chat。

同步(stream_chat):

在可以接受阻塞调用的普通 Python 脚本或 notebook 里使用它。它直接返回一个生成器。

stream_response = llm.stream_chat(messages)

for token in stream_response:
    print(token.delta, end="", flush=True)

异步(astream_chat):

使用 FastAPI 这类异步框架时,记得 await 方法调用,并遍历返回的异步流。

stream_response = await llm.astream_chat(messages)

async for token in stream_response:
    print(token.delta, end="", flush=True)

指定模型

许多 LLM 集成提供不止一个模型。你可以在构造 LLM 时传入 model 参数来指定模型:

llm = OpenAI(model="gpt-4o-mini")
response = llm.complete("Who is Laurie Voss?")
print(response)

多模态 LLM

一些 LLM 支持多模态对话消息。也就是说,你可以把文本与其他模态(图像、音频、视频等)混在一起传入,LLM 会自行处理。

目前 LlamaIndex 支持在 ChatMessage 中通过 content block 使用文本、图像与音频。

from llama_index.core.llms import ChatMessage, TextBlock, ImageBlock
from llama_index.llms.openai import OpenAI

llm = OpenAI(model="gpt-4o")

messages = [
    ChatMessage(
        role="user",
        blocks=[
            ImageBlock(path="image.png"),
            TextBlock(text="Describe the image in a few sentences."),
        ],
    )
]

resp = llm.chat(messages)
print(resp.message.content)

工具调用

一些 LLM(OpenAI、Anthropic、Gemini、Ollama 等)在 API 调用层面就直接支持工具调用——这意味着无需专门的提示词与解析机制即可调用工具与函数。

from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI

def generate_song(name: str, artist: str) -> Song:
    """Generates a song with provided name and artist."""
    return {"name": name, "artist": artist}

tool = FunctionTool.from_defaults(fn=generate_song)

llm = OpenAI(model="gpt-4o")
response = llm.predict_and_call(
    [tool],
    "Pick a random song for me",
)
print(str(response))

想深入了解更高级的工具调用,请看用 OpenAI 做的详解指南。同样的做法适用于任何支持工具/函数的 LLM(例如 Anthropic、Gemini、Ollama 等)。

关于工具与 agent 的更多内容,见工具指南。

可用的 LLM

我们支持 OpenAI、Anthropic、Mistral、DeepSeek、Hugging Face 以及数十家其他集成。完整清单(包括如何运行本地模型)见LLM 模块指南。

提示

关于隐私与 LLM 使用的一般性说明,见隐私页面。

使用本地 LLM

LlamaIndex 并非只支持托管的 LLM API;你也可以在本地跑一个模型,比如 Meta 的 Llama 3。例如你已经安装并运行了 Ollama:

from llama_index.llms.ollama import Ollama

llm = Ollama(
    model="llama3.3",
    request_timeout=60.0,
    # Manually set the context window to limit memory usage
    context_window=8000,
)

关于使用和配置 LLM 模型的更多细节,见自定义 LLM 操作指南。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。