microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.81k stars 3.24k forks source link

Python: .Net: OpenAI - Support response_format type of json_schema (Structured Outputs) #7946

Open jonnermut opened 2 months ago

jonnermut commented 2 months ago

The latest OpenAI models allow passing json_schema as the response_format type property, and supplying the schema itself under the schema property. This enforces conformance to the schema by validating output tokens, rather than best LLM effort.

Announced here: https://openai.com/index/introducing-structured-outputs-in-the-api/

This is obviously massively useful to any application using json output, especially when deserialising the json to C# types. Please consider adding support for these properties through the SemanticKernel abstractions and .Net client in the short term.

RogerBarreto commented 2 months ago

Update with more context.

Created issue in OpenAI SDK .Net to keep track.

POST /v1/chat/completions
{
  "model": "gpt-4o-2024-08-06",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful math tutor."
    },
    {
      "role": "user",
      "content": "solve 8x + 31 = 2"
    }
  ],
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "math_response",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "steps": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "explanation": {
                  "type": "string"
                },
                "output": {
                  "type": "string"
                }
              },
              "required": ["explanation", "output"],
              "additionalProperties": false
            }
          },
          "final_answer": {
            "type": "string"
          }
        },
        "required": ["steps", "final_answer"],
        "additionalProperties": false
      }
    }
  }
}
papa-pep commented 2 months ago

It looks like OpenAI SDK work has been completed! Looking forward to having this in SK soon!