C#으로 만들었습니다
1. 프로젝트 초기화
2. Creating a chat history object
소스코드
// ChatHistoryApp/Program.cs
#pragma warning disable SKEXP0001 // 이 경고를 무시하도록 설정
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
namespace ChatHistoryApp
{
class Program
{
static void Main(string[] args)
{
// Create a chat history object
ChatHistory chatHistory = [];
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.");
}
}
}
C#
복사
3. Adding richer messages to a chat history
소스코드
// ChatHistoryApp/Program.cs
#pragma warning disable SKEXP0001 // 이 경고를 무시하도록 설정
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
namespace ChatHistoryApp
{
class Program
{
static void Main(string[] args)
{
// Create a chat history object
ChatHistory chatHistory = [];
// 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.");
// Add system message
chatHistory.Add(
new() {
Role = AuthorRole.System,
AuthorName = "Laimonis Dumins",
Content = "You are a helpful assistant"
}
);
// Add user message with an image
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") }
]
}
);
// Add assistant message
chatHistory.Add(
new() {
Role = AuthorRole.Assistant,
AuthorName = "Restaurant Assistant",
Content = "We have pizza, pasta, and salad available to order. What would you like to order?"
}
);
// Add additional message from a different user
chatHistory.Add(
new() {
Role = AuthorRole.User,
AuthorName = "Ema Vargova",
Content = "I'd like to have the first option, please."
}
);
}
}
}
C#
복사
트러블슈팅
AuthorName이 평가용 기능이라 정식 지원이 안 될 수도 있어서 미래 버전에는 제거되거나 변경될 가능성이 있다는 경고 메세지. 경고 메세지를 무시하거나 AuthRole key를 안 사용하면 됩니다.
저는 경고 메세지를 무시하도록 설정했습니다. 다음 내용을 파일 최상단에 넣어주시면 됩니다.
#pragma warning disable SKEXP0001
C#
복사
4. Simulating function calls
소스코드
#pragma warning disable SKEXP0001 // 이 경고를 무시하도록 설정
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
namespace ChatHistoryApp
{
class Program
{
static void Main(string[] args)
{
// Create a chat history object
ChatHistory chatHistory = [];
// Add a simulated function call from the assistant
chatHistory.Add(
new() {
Role = AuthorRole.Assistant,
Items = [
new FunctionCallContent(
functionName: "get_user_allergies",
pluginName: "User",
id: "0001",
arguments: new () { {"username", "laimonisdumins"} }
),
new FunctionCallContent(
functionName: "get_user_allergies",
pluginName: "User",
id: "0002",
arguments: new () { {"username", "emavargova"} }
)
]
}
);
// Add a simulated function results from the tool role
chatHistory.Add(
new() {
Role = AuthorRole.Tool,
Items = [
new FunctionResultContent(
functionName: "get_user_allergies",
pluginName: "User",
result: "{ \"allergies\": [\"peanuts\", \"gluten\"] }"
)
]
}
);
chatHistory.Add(
new() {
Role = AuthorRole.Tool,
Items = [
new FunctionResultContent(
functionName: "get_user_allergies",
pluginName: "User",
result: "{ \"allergies\": [\"dairy\", \"soy\"] }"
)
]
}
);
}
}
}
C#
복사
트러블슈팅
그냥 id 파라미터를 제거하면 됩니다
5. Inspecting a chat history object
소스코드
using Microsoft.SemanticKernel.ChatCompletion;
ChatHistory chatHistory = [
new() {
Role = AuthorRole.User,
Content = "Please order me a pizza"
}
];
// Get the current length of the chat history object
int currentChatHistoryLength = chatHistory.Count;
// Get the chat message content
ChatMessageContent results = await chatCompletionService.GetChatMessageContentAsync(
chatHistory,
kernel: kernel
);
// Get the new messages added to the chat history object
for (int i = currentChatHistoryLength; i < chatHistory.Count; i++)
{
Console.WriteLine(chatHistory[i]);
}
// Print the final message
Console.WriteLine(results);
// Add the final message to the chat history object
chatHistory.Add(results);
C#
복사



