聊天历史记录
¥Chat history
聊天历史记录是用户与聊天模型之间对话的记录。它用于在整个对话过程中维护上下文和状态。聊天历史记录是 messages 的序列,每个 messages 都与特定的 role 相关联,例如 "user"、"assistant"、"system" 或 "tool"。
¥Chat history is a record of the conversation between the user and the chat model. It is used to maintain context and state throughout the conversation. The chat history is sequence of messages, each of which is associated with a specific role, such as "user", "assistant", "system", or "tool".
对话模式
¥Conversation patterns
大多数对话都以设置对话上下文的系统消息开始。接下来是包含用户输入的用户消息,然后是包含模型响应的辅助消息。
¥Most conversations start with a system message that sets the context for the conversation. This is followed by a user message containing the user's input, and then an assistant message containing the model's response.
助手可以直接响应用户,或者如果配置了工具请求,则调用 tool 来执行特定任务。
¥The assistant may respond directly to the user or if configured with tools request that a tool be invoked to perform a specific task.
因此,完整的对话通常涉及两种交替消息模式的组合:
¥So a full conversation often involves a combination of two patterns of alternating messages:
用户和助手之间进行来回对话。
¥The user and the assistant representing a back-and-forth conversation.
助手和工具消息表示 "agentic" 工作流程,其中助手正在调用工具来执行特定任务。
¥The assistant and tool messages representing an "agentic" workflow where the assistant is invoking tools to perform specific tasks.
管理聊天历史记录
¥Managing chat history
由于聊天模型对输入大小有最大限制,因此管理聊天记录并根据需要进行修剪以避免超过 上下文窗口 的限制非常重要。
¥Since chat models have a maximum limit on input size, it's important to manage chat history and trim it as needed to avoid exceeding the context window.
在处理聊天记录时,保持正确的对话结构至关重要。
¥While processing chat history, it's essential to preserve a correct conversation structure.
管理聊天历史记录的主要准则:
¥Key guidelines for managing chat history:
对话应遵循以下结构之一:
¥The conversation should follow one of these structures:
第一条消息要么是 "user" 消息,要么是 "system" 消息,然后是 "user" 消息,最后是 "assistant" 消息。
¥The first message is either a "user" message or a "system" message, followed by a "user" and then an "assistant" message.
最后一条消息应该是包含工具调用结果的 "user" 消息或 "tool" 消息。
¥The last message should be either a "user" message or a "tool" message containing the result of a tool call.
使用 工具调用 时,"tool" 消息应仅跟在请求工具调用的 "assistant" 消息之后。
¥When using tool calling, a "tool" message should only follow an "assistant" message that requested the tool invocation.
相关资源
¥Related resources
内存指南 提供使用 LangGraph 在聊天模型中实现短期和长期记忆的信息。
¥Memory guide for information on implementing short-term and long-term memory in chat models using LangGraph.