Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Ketika model AI menerima perintah yang berisi daftar fungsi, model AI dapat memilih satu atau beberapa fungsi untuk pemanggilan untuk menyelesaikan perintah. Ketika fungsi dipilih oleh model, fungsi perlu dipanggil oleh Kernel Semantik.
Fungsi memanggil subsistem dalam Kernel Semantik memiliki dua mode pemanggilan fungsi: otomatis dan manual.
Tergantung pada mode pemanggilan, Kernel Semantik melakukan pemanggilan fungsi end-to-end atau memberikan kontrol pemanggil atas proses pemanggilan fungsi.
Pemanggilan Fungsi Otomatis
Pemanggilan fungsi otomatis adalah mode default subsistem panggilan fungsi Kernel Semantik. Ketika model AI memilih satu atau beberapa fungsi, Semantic Kernel secara otomatis memanggil fungsi yang dipilih. Hasil pemanggilan fungsi ini ditambahkan ke riwayat obrolan dan dikirim ke model secara otomatis dalam permintaan berikutnya. Model kemudian beralasan tentang riwayat obrolan, memilih fungsi tambahan jika diperlukan, atau menghasilkan respons akhir. Pendekatan ini sepenuhnya otomatis dan tidak memerlukan intervensi manual dari pemanggil.
Contoh ini menunjukkan cara menggunakan pemanggilan fungsi otomatis di Kernel Semantik. Model AI memutuskan fungsi mana yang akan dipanggil untuk menyelesaikan perintah dan Semantic Kernel melakukan sisanya dan memanggilnya secara otomatis.
using Microsoft.SemanticKernel;
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>();
Kernel kernel = builder.Build();
// By default, functions are set to be automatically invoked.
// If you want to explicitly enable this behavior, you can do so with the following code:
// PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(autoInvoke: true) };
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
await kernel.InvokePromptAsync("Given the current time of day and weather, what is the likely color of the sky in Boston?", new(settings));
Beberapa model AI mendukung panggilan fungsi paralel, di mana model memilih beberapa fungsi untuk pemanggilan. Ini dapat berguna dalam kasus ketika memanggil fungsi yang dipilih membutuhkan waktu lama. Misalnya, AI dapat memilih untuk mengambil berita terbaru dan waktu saat ini secara bersamaan, daripada melakukan perjalanan pulang pergi per fungsi.
Kernel Semantik dapat memanggil fungsi-fungsi ini dengan dua cara berbeda:
- Secara berurutan: Fungsi dipanggil satu demi satu. Ini adalah perilaku default.
- Secara bersamaan: Fungsi dipanggil secara bersamaan. Ini dapat diaktifkan dengan mengatur
FunctionChoiceBehaviorOptions.AllowConcurrentInvocationproperti ketrue, seperti yang ditunjukkan pada contoh di bawah ini.
using Microsoft.SemanticKernel;
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<NewsUtils>();
builder.Plugins.AddFromType<DateTimeUtils>();
Kernel kernel = builder.Build();
// Enable concurrent invocation of functions to get the latest news and the current time.
FunctionChoiceBehaviorOptions options = new() { AllowConcurrentInvocation = true };
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: options) };
await kernel.InvokePromptAsync("Good morning! What is the current time and latest news headlines?", new(settings));
Pemanggilan Fungsi Manual
Dalam kasus ketika penelepon ingin memiliki lebih banyak kontrol atas proses pemanggilan fungsi, pemanggilan fungsi manual dapat digunakan.
Ketika pemanggilan fungsi manual diaktifkan, Kernel Semantik tidak secara otomatis memanggil fungsi yang dipilih oleh model AI. Sebaliknya, ia mengembalikan daftar fungsi yang dipilih ke pemanggil, yang kemudian dapat memutuskan fungsi mana yang akan dipanggil, memanggilnya secara berurutan atau paralel, menangani pengecualian, dan sebagainya. Hasil pemanggilan fungsi perlu ditambahkan ke riwayat obrolan dan dikembalikan ke model, yang beralasan tentang mereka dan memutuskan apakah akan memilih fungsi tambahan atau menghasilkan respons akhir.
Contoh di bawah ini menunjukkan cara menggunakan pemanggilan fungsi manual.
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion("<model-id>", "<api-key>");
builder.Plugins.AddFromType<WeatherForecastUtils>();
builder.Plugins.AddFromType<DateTimeUtils>();
Kernel kernel = builder.Build();
IChatCompletionService chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Manual function invocation needs to be enabled explicitly by setting autoInvoke to false.
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = Microsoft.SemanticKernel.FunctionChoiceBehavior.Auto(autoInvoke: false) };
ChatHistory chatHistory = [];
chatHistory.AddUserMessage("Given the current time of day and weather, what is the likely color of the sky in Boston?");
while (true)
{
ChatMessageContent result = await chatCompletionService.GetChatMessageContentAsync(chatHistory, settings, kernel);
// Check if the AI model has generated a response.
if (result.Content is not null)
{
Console.Write(result.Content);
// Sample output: "Considering the current weather conditions in Boston with a tornado watch in effect resulting in potential severe thunderstorms,
// the sky color is likely unusual such as green, yellow, or dark gray. Please stay safe and follow instructions from local authorities."
break;
}
// Adding AI model response containing chosen functions to chat history as it's required by the models to preserve the context.
chatHistory.Add(result);
// Check if the AI model has chosen any function for invocation.
IEnumerable<FunctionCallContent> functionCalls = FunctionCallContent.GetFunctionCalls(result);
if (!functionCalls.Any())
{
break;
}
// Sequentially iterating over each chosen function, invoke it, and add the result to the chat history.
foreach (FunctionCallContent functionCall in functionCalls)
{
try
{
// Invoking the function
FunctionResultContent resultContent = await functionCall.InvokeAsync(kernel);
// Adding the function result to the chat history
chatHistory.Add(resultContent.ToChatMessage());
}
catch (Exception ex)
{
// Adding function exception to the chat history.
chatHistory.Add(new FunctionResultContent(functionCall, ex).ToChatMessage());
// or
//chatHistory.Add(new FunctionResultContent(functionCall, "Error details that the AI model can reason about.").ToChatMessage());
}
}
}
Catatan
Kelas FunctionCallContent dan FunctionResultContent digunakan untuk mewakili panggilan fungsi model AI dan hasil pemanggilan fungsi Kernel Semantik. Mereka berisi informasi tentang fungsi yang dipilih, seperti ID fungsi, nama, dan argumen, dan hasil pemanggilan fungsi, seperti ID panggilan fungsi dan hasil.
Segera hadir
Info lebih lanjut akan segera hadir.
Segera hadir
Info lebih lanjut akan segera hadir.