Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON response: CreateChatCompletionRequest class supports response_format field #115

Closed
wants to merge 2 commits into from

Conversation

redclock
Copy link

Supports specifying the response return format, either JSON or text. Address #105

See
OpenAI API document of response_format parameter
GPT-4-turbo and GPT-3.5-turbo-1106 support this parameter.

Specifying the return result as JSON is useful for workflows.

Code sample:

                _task = _openAi.CreateChatCompletion(new CreateChatCompletionRequest()
                {
                    Model = "gpt-3.5-turbo",
                    Messages = new List<ChatMessage>()
                    {
                        new() {Role = "system", Content = "Output JSON"},
                        new() {Role = "user", Content = "What's US and China's population?"}
                    },
                    ResponseFormat = ResponseFormat.JsonObject
                });

Response:

{
    "country_populations": {
        "United States": "331.9 million",
        "China": "1.41 billion"
    }
}

If you removed the ResponseFormat = ResponseFormat.JsonObject, the response becomes:

As of September 2021, the population of the United States is approximately 331 million and the population of China is approximately 1.4 billion.

Note: According to the document, when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message.

@redclock
Copy link
Author

redclock commented Feb 29, 2024

I pushed one more commit in this branch mistakenly, so I closed this PR.

New PR here: #116

@redclock redclock closed this Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant