Skip to content

Commit

Permalink
.Net: Added example of Structured Outputs with Azure OpenAI (microsof…
Browse files Browse the repository at this point in the history
…t#9315)

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Resolves: microsoft#9102

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [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 😄
  • Loading branch information
dmytrostruk authored Oct 17, 2024
1 parent 20b5032 commit 1cbb965
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -190,6 +191,52 @@ public async Task StructuredOutputsWithFunctionCallingAsync()
// ...and more...
}

/// <summary>
/// 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.
/// </summary>
[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<MovieResult>(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

/// <summary>Movie result struct that will be used as desired chat completion response format (structured output).</summary>
Expand Down

0 comments on commit 1cbb965

Please sign in to comment.