srcnalt / OpenAI-Unity

An unofficial OpenAI Unity Package that aims to help you use OpenAI API directly in Unity Game engine.
MIT License
648 stars 144 forks source link

Support JSON response: CreateChatCompletionRequest class supports response_format field #116

Open redclock opened 4 months ago

redclock commented 4 months ago

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.