Skip to content

Conversation

@dmytrostruk
Copy link
Member

@dmytrostruk dmytrostruk commented Sep 9, 2024

Motivation and Context

Related: #7946
Closes: #8519

Structured Outputs is a feature in OpenAI API that ensures the model will always generate responses based on provided JSON Schema. This gives more control over model responses, allows to avoid model hallucinations and write simpler prompts without a need to be specific about response format.

This PR contains an ADR which describes several options how to enable this functionality in .NET version of Semantic Kernel and implementation for Option 1 and Option 2.

Contribution Checklist

@dmytrostruk dmytrostruk self-assigned this Sep 9, 2024
@dmytrostruk dmytrostruk requested a review from a team as a code owner September 9, 2024 22:43
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel documentation labels Sep 9, 2024
@jonnermut
Copy link

So does this end up implementing both options?

To us Option#1 is essential and #2 is a nice to have.

The reason for that is that we are making Json Schemas for json output dynamically at runtime.
The do happen to be based on a C# type, but we send a subset of the types properties to the LLM depending on what we want to generate.

@dmytrostruk dmytrostruk added this pull request to the merge queue Sep 17, 2024
Merged via the queue into microsoft:main with commit 9d28474 Sep 17, 2024
@dmytrostruk dmytrostruk deleted the dotnet-openai-structured-outputs branch September 17, 2024 15:21
@dmytrostruk
Copy link
Member Author

So does this end up implementing both options?

@jonnermut That's correct.

Here is an example how to use Option 1 with manually constructed JSON Schema:

public async Task StructuredOutputsWithChatResponseFormatAsync()

And this is an example of Option 2 with automatically constructed JSON Schema:

public async Task StructuredOutputsWithTypeInExecutionSettingsAsync()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Support for Structured Outputs

9 participants