How To Use Structured Responses in ChatGPT API
If you’ve ever tried using the ChatGPT API for chat completion, you know what a headache it can be to parse the API response into your desired model. With the OpenAI .NET package for structured responses, this is no longer a pain.
What Was the Problem?
Before the OpenAI team introduced their .NET package, it was really difficult to parse API responses into a strongly typed response model in your code. I used to specify the exact structure I needed for the response, and creating complex responses became quite a headache.
For example, here’s a prompt I used to call the ChatGPT API to get my desired response.
And the GPT REST Client Would Be Like…
With the new OpenAI .NET package, you no longer need to manually handle complex parsing logic. The package allows you to easily generate strongly typed models directly from API responses.
In the above implementation, we clearly explain the desired response structure. After receiving the response, we applied some sanitization to remove unnecessary or extra characters from the response to cast it to our model, which looked something like this:
And it’s highly possible to face a ParsingException here due to unwanted characters in the response!
Now Solution: Using Structured Response
If we want to achieve the same thing with a structured response, we first need to create a GPT Client Service as follows
And here is the new prompt version
Magic Happened
No more need to explain the response structure in the prompt. Just by sending your desired ChatCompletionOptions, you can ensure the generated response will have your defined structure.🤠👏
Hope you enjoyed this post! Happy to share my experience working with AI APIs and building software on top of it. Let’s connect on LinkedIn to ensure we can go through this journey together. Here’s my LinkedIn profile
Cheers. Matt Ghafouri