From 1cbb9655e51f016892b67529c4da149862f958e1 Mon Sep 17 00:00:00 2001
From: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
Date: Thu, 17 Oct 2024 12:23:34 -0700
Subject: [PATCH] .Net: Added example of Structured Outputs with Azure OpenAI
(#9315)
### Motivation and Context
Resolves: https://github.com/microsoft/semantic-kernel/issues/9102
### Contribution Checklist
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone :smile:
---
.../OpenAI_StructuredOutputs.cs | 47 +++++++++++++++++++
1 file changed, 47 insertions(+)
diff --git a/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs b/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs
index e4297e854d65..bfa158e1c519 100644
--- a/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs
+++ b/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json;
+using Azure.Identity;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using OpenAI.Chat;
@@ -190,6 +191,52 @@ public async Task StructuredOutputsWithFunctionCallingAsync()
// ...and more...
}
+ ///
+ /// This method shows how to enable Structured Outputs feature with Azure OpenAI chat completion service.
+ /// Model should be gpt-4o with version 2024-08-06 or later.
+ /// Azure OpenAI chat completion API version should be 2024-08-01-preview or later.
+ ///
+ [Fact]
+ public async Task StructuredOutputsWithAzureOpenAIAsync()
+ {
+ // Initialize kernel.
+ Kernel kernel = Kernel.CreateBuilder()
+ .AddAzureOpenAIChatCompletion(
+ deploymentName: TestConfiguration.AzureOpenAI.ChatDeploymentName,
+ endpoint: TestConfiguration.AzureOpenAI.Endpoint,
+ credentials: new AzureCliCredential(),
+ apiVersion: "2024-08-01-preview")
+ .Build();
+
+ // Specify response format by setting Type object in prompt execution settings.
+ var executionSettings = new OpenAIPromptExecutionSettings
+ {
+ ResponseFormat = typeof(MovieResult)
+ };
+
+ // Send a request and pass prompt execution settings with desired response format.
+ var result = await kernel.InvokePromptAsync("What are the top 10 movies of all time?", new(executionSettings));
+
+ // Deserialize string response to a strong type to access type properties.
+ // At this point, the deserialization logic won't fail, because MovieResult type was specified as desired response format.
+ // This ensures that response string is a serialized version of MovieResult type.
+ var movieResult = JsonSerializer.Deserialize(result.ToString())!;
+
+ // Output the result.
+ this.OutputResult(movieResult);
+
+ // Output:
+
+ // Title: The Lord of the Rings: The Fellowship of the Ring
+ // Director: Peter Jackson
+ // Release year: 2001
+ // Rating: 8.8
+ // Is available on streaming: True
+ // Tags: Adventure,Drama,Fantasy
+
+ // ...and more...
+ }
+
#region private
/// Movie result struct that will be used as desired chat completion response format (structured output).