儲存聊天記錄
Semantic Kernel 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 物件藉由支援使用者名稱和多媒體內容等詳細數據來提供彈性。 這些工具可讓您輕鬆地設計動態和個人化的聊天應用程式。