Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Experiment using JsonSchemaExporter in .NET 9 to improve developer experience with OpenAI's Structured Outputs

License

NotificationsYou must be signed in to change notification settings

jamesmcroft/openai-structured-outputs-with-dotnet

Repository files navigation

The goal of this sample is to demonstrate how to leverage the Azure OpenAI GPT-4o structured outputs feature by generating valid OpenAI JSON schemas for a given Type in .NET.

How it works

Important

The original vision was to leverage theJsonSchemaExporter in .NET 9 to generate JSON schemas. However, the generated schemas are not directly compatible with the OpenAI SDK. The current implementation is a workaround to generate valid schemas for structured outputs.

TheOpenAIJsonSchema class provides a mechanism to generate a valid OpenAI JSON schema for any given model Type. The class uses reflection to traverse the properties of the model Type and generate the schema based on the property types.

TheStructuredOutputsExtensions class provides a genericCreateJsonSchemaFormat method that can be passed a model object type and will return theChatResponseFormat object that can be used by the OpenAI SDK. This method uses theOpenAIJsonSchema.For method to generate the schema for the model object type.

In addition to the genericCreateJsonSchemaFormat method, a genericCompleteChat andCompleteChatAsync extension is provided for theChatClient that will return a wrappedChatCompletion object asParsedChatCompletion that includes aParsed property containing the deserialized model object.

Example

classFamily{publicList<Person>Parents{get;set;}publicList<Person>?Children{get;set;}classPerson{publicstringName{get;set;}publicintAge{get;set;}}}ChatCompletionOptionsoptions=new(){ResponseFormat=StructuredOutputsExtensions.CreateJsonSchemaFormat<Family>("family",jsonSchemaIsStrict:true),MaxOutputTokenCount=4096,Temperature=0.1f,TopP=0.1f};List<ChatMessage>messages=[newSystemChatMessage("You are an AI assistant that creates families."),newUserChatMessage("Create a family with 2 parents and 2 children.")];ParsedChatCompletion<Family?>completion=chatClient.CompleteChat(messages,options);Family?family=completion.Parsed;

You can also provide descriptions for schema properties by using theDescriptionAttribute:

classPerson{[Description("The name of the person.")]publicstringName{get;set;}[Description("The age of the person.")]publicintAge{get;set;}}

These are included in the generated schema:

{"type":"object","properties": {"Name": {"type":"string","description":"The name of the person."    },"Age": {"type":"integer","description":"The age of the person."    }  }}

Running the sample

Additionally, you will require:

  • An Azure subscription. If you don't have an Azure subscription, create anaccount.

To setup a local development environment, follow these steps:

Note

For the most optimal sample experience, it is recommended to deploy the necessary infrastructure in a region that supportsGPT-4o version2024-08-06. Find out more about region availability for theGPT-4o model.

az login./Setup-Environment.ps1-DeploymentName<UniqueDeploymentName>-Location<AzureRegion>-SkipInfrastructure$false

The script will deploy the following resources to your Azure subscription:

  • Azure AI Services, a managed service for all Azure AI Services, including Azure OpenAI.
    • Note: GPT-4o will be deployed as Global Standard with 10K TPM quota allocation. This can be adjusted based on your quota availability in themain.bicep file.

Note

Resources are secured by default with Microsoft Entra ID using Azure RBAC. Your user client ID will be added with the necessary least-privilege roles to access the resources created.

After the script completes, a.env file will be crated in thesrc/StructuredOutputs folder and you can run the sample project by following these steps:

cd src/StructuredOutputsdotnet run

Resources

License

This project is licensed under the MIT License - see theLICENSE file for details.

About

Experiment using JsonSchemaExporter in .NET 9 to improve developer experience with OpenAI's Structured Outputs

Topics

Resources

License

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp