主题
开始使用
从索引构建一个对话引擎:
chat_engine = index.as_chat_engine()1
提示
如何构建索引,见索引(Indexing)
与你的数据展开对话:
response = chat_engine.chat("Tell me a joke.")1
重置聊天历史以开始新一轮对话:
chat_engine.reset()1
进入交互式聊天 REPL:
chat_engine.chat_repl()1
配置对话引擎
配置对话引擎与配置查询引擎非常相似。
高层 API
你可以用一行代码直接从索引构建并配置一个对话引擎:
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=True)1
注:通过指定
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,
)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
流式
要启用流式,只需调用 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="")1
2
3
4
5
2
3
4
5
异步(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="")1
2
3
4
5
2
3
4
5
可参见一个端到端教程