存储聊天历史记录
语义内核 SDK 支持一个 ChatHistory 对象,该对象保留聊天会话中交换的消息的记录。 它存储来自不同作者的消息和元数据,例如用户、助理、工具或系统。 该 ChatHistory 对象对于维护会话中的上下文和连续性至关重要。 以下是一个 ChatHistory 对象的部分示例输出:
system: You are a helpful assistant.
user: What's available to order?
assistant: We have pizza, pasta, and salad available to order. What would you like to order?
user: I'd like to have the first option, please.
创建聊天历史记录对象
在创建 ChatHistory 对象之前,需要导入相应的包。 聊天历史记录对象是一个列表,充当用于管理聊天会话中消息序列的容器。
// Import the chat completion namespace
using Microsoft.SemanticKernel.ChatCompletion;
// Create a chat history object
ChatHistory chatHistory = [];
// Add role messages to the chat history
chatHistory.AddSystemMessage("You are a helpful assistant.");
chatHistory.AddUserMessage("What's available to order?");
chatHistory.AddAssistantMessage("We have pizza, pasta, and salad available to order. What would you like to order?");
chatHistory.AddUserMessage("I'd like to have the first option, please.");
for (int i = 0; i < chatHistory.Count; i++)
{
Console.WriteLine($"{chatHistory[i].Role}: {chatHistory[i]}");
}
from semantic_kernel.contents.chat_history import ChatHistory
# Create a chat history object
chat_history = ChatHistory()
# Add role messages to the chat history
chat_history.add_system_message("You are a helpful assistant.")
chat_history.add_user_message("What's available to order?")
chat_history.add_assistant_message("We have pizza, pasta, and salad available to order. What would you like to order?")
chat_history.add_user_message("I'd like to have the first option, please.")
# Print chat history
for message in chat_history:
print(f"{message.role}: {message.content}")
在此示例中,将创建 ChatHistory 对象,并填充来自不同作者的消息:
- 系统消息:设置助理的角色或行为,例如,“你是一个有用的助手”。
- 用户消息:捕获用户的输入,例如请求可用项或下订单。
- 助手消息:包含 AI 助手生成的响应、提供建议或确认动作。
通过使用聊天历史记录,可以生成智能的上下文感知聊天体验,这些体验感觉自然,并响应用户需求。
还可以通过创建 ChatMessage 对象向聊天历史记录添加更多详细信息。 该 ChatMessage 对象支持其他信息,如用户名和图像内容。 下面是一个示例:
// Add user message with an image
#pragma warning disable SKEXP0001 // AuthorName is subject to change and emits a warning
chatHistory.Add(
new() {
Role = AuthorRole.User,
AuthorName = "Laimonis Dumins",
Items = [
new TextContent { Text = "What available on this menu" },
new ImageContent { Uri = new Uri("https://example.com/menu.jpg") }
]
}
);
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.chat_message_content import ChatMessageContent, AuthorRole
from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.contents.image_content import ImageContent
# Create a chat history object
chat_history = ChatHistory()
# Add a user message with author name, text, and image content
chat_history.add(
ChatMessageContent(
role=AuthorRole.USER,
author_name="Laimonis Dumins",
items=[
TextContent(text="What available on this menu"),
ImageContent(uri="https://example.com/menu.jpg")
]
)
)
# Print the last message to verify
last_message = chat_history[-1]
print(f"{last_message.role} ({last_message.author_name}):")
for item in last_message.items:
if isinstance(item, TextContent):
print(f" Text: {item.text}")
elif isinstance(item, ImageContent):
print(f" Image: {item.uri}")
在项目中使用聊天历史记录有助于增强用户交互。 例如,聊天历史记录通过跨多个交换保留上下文来确保连续性,使助手能够更准确地响应,而无需用户重复信息。 还可以使用聊天历史记录来分析用户交互,以改进 AI 响应,例如识别常见查询或优化聊天流。
ChatHistory 对象允许跟踪聊天会话期间交换的消息,确保对话保持上下文感知和自然。 通过添加系统、用户和助理消息,可以定义创建一致用户体验的行为和交互。 ChatMessage 对象通过支持用户名和多媒体内容等详细信息提供灵活性。 这些工具可以轻松设计动态和个性化的聊天应用程序。