microsoft / semantic-kernel-java

Semantic Kernel for Java. Integrate cutting-edge LLM technology quickly and easily into your Java based apps. See https://aka.ms/semantic-kernel.
MIT License
95 stars 17 forks source link

Java: Add Support for OpenAI new Structured Outputs. #168

Open d3r3kk opened 3 months ago

d3r3kk commented 3 months ago

Support for Structured Outputs

As mentioned in the recent blog post from OpenAI.

Now is possible to specify the response_format as json_schema:

This issue is a desired improvement which currently depends on the support being provided by the OpenAI Java SDK.

TODO: Create an issue in OpenAI Java SDK 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
      }
    }
  }
}

Related to microsoft/semantic-kernel#7951 Related to microsoft/semantic-kernel#7946

d3r3kk commented 1 month ago

Fixed by microsoft/semantic-kernel-java#8