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

开始使用

从索引构建一个对话引擎:

chat_engine = index.as_chat_engine()

提示

如何构建索引,见索引(Indexing)

与你的数据展开对话:

response = chat_engine.chat("Tell me a joke.")

重置聊天历史以开始新一轮对话:

chat_engine.reset()

进入交互式聊天 REPL:

chat_engine.chat_repl()

配置对话引擎

配置对话引擎与配置查询引擎非常相似。

高层 API

你可以用一行代码直接从索引构建并配置一个对话引擎:

chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)

注:通过指定 chat_mode 关键字参数即可访问不同的对话引擎。condense_question 对应 CondenseQuestionChatEngine,react 对应 ReActChatEngine,context 对应 ContextChatEngine。

注:高层 API 以便用为先,但并不提供完整的可配置能力。

可用的聊天模式

  • best - 把查询引擎变成一个工具,供 ReAct 数据 agent 或 OpenAI 数据 agent 使用,具体取决于你的 LLM 支持哪一种。OpenAI 数据 agent 需要 gpt-3.5-turbo 或 gpt-4,因为它们使用 OpenAI 的函数调用 API。
  • condense_question - 查看聊天历史,把用户消息改写成面向索引的查询。读取到查询引擎的回答后,再返回响应。
  • context - 针对每一条用户消息从索引检索节点。检索到的文本会被插入系统提示词,使对话引擎既能自然回答,也能利用查询引擎带来的上下文。
  • condense_plus_context - condense_question 与 context 的结合。查看聊天历史,把用户消息改写成面向索引的检索查询;检索到的文本会被插入系统提示词,使对话引擎既能自然回答,也能利用查询引擎带来的上下文。
  • simple - 直接与 LLM 对话,不涉及查询引擎。
  • react - 与 best 相同,但强制使用 ReAct 数据 agent。
  • openai - 与 best 相同,但强制使用 OpenAI 数据 agent。

低层组装 API

需要更细粒度的控制时,可以使用低层组装 API。具体来说,你要显式构造一个 ChatEngine 对象,而不是调用 index.as_chat_engine(...)。

注:你可能需要查阅 API 参考或示例 notebook。

下面这个例子做了如下配置:

  • 配置 condense question 提示词,
  • 用一段已有历史来初始化对话,
  • 打印详细的调试信息。
from llama_index.core import PromptTemplate
from llama_index.core.llms import ChatMessage, MessageRole
from llama_index.core.chat_engine import CondenseQuestionChatEngine

custom_prompt = PromptTemplate(
    """\
Given a conversation (between Human and Assistant) and a follow up message from Human, \
rewrite the message to be a standalone question that captures all relevant context \
from the conversation.

<Chat History>
{chat_history}

<Follow Up Message>
{question}

<Standalone question>
"""
)

# list of `ChatMessage` objects
custom_chat_history = [
    ChatMessage(
        role=MessageRole.USER,
        content="Hello assistant, we are having a insightful discussion about Paul Graham today.",
    ),
    ChatMessage(role=MessageRole.ASSISTANT, content="Okay, sounds good."),
]

query_engine = index.as_query_engine()
chat_engine = CondenseQuestionChatEngine.from_defaults(
    query_engine=query_engine,
    condense_question_prompt=custom_prompt,
    chat_history=custom_chat_history,
    verbose=True,
)

流式

要启用流式,只需调用 stream_chat 端点而不是 chat 端点。

!!! warning 这一点与查询引擎(传入 streaming=True 开关)有些不一致。我们正在让两边行为更加统一!

同步(stream_chat):

chat_engine = index.as_chat_engine()
streaming_response = chat_engine.stream_chat("Tell me a joke.")

for token in streaming_response.response_gen:
    print(token, end="")

异步(astream_chat):

在使用异步框架(例如 FastAPI)时,请使用 astream_chat。注意你必须 await 该调用,并遍历它提供的异步流式接口(例如 async_response_gen() 或 achat_stream)。

chat_engine = index.as_chat_engine()
streaming_response = await chat_engine.astream_chat("Tell me a joke.")

async for token in streaming_response.async_response_gen():
    print(token, end="")

可参见一个端到端教程

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