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 Union Types in Definition Struct for JSON Schema #832

Closed
vish0l opened this issue Aug 22, 2024 · 4 comments
Closed

Support Union Types in Definition Struct for JSON Schema #832

vish0l opened this issue Aug 22, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@vish0l
Copy link

vish0l commented Aug 22, 2024

Is your feature request related to a problem? Please describe.
The Definition struct currently does not support multiple data types, limiting the flexibility needed for certain JSON schema definitions. According to OpenAI’s documentation, it is possible to emulate optional parameters by using a union type with multiple data types, which is not currently supported by the existing struct.

    "name": "get_weather",
    "description": "Fetches the weather in the given location",
    "strict": true,
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The location to get the weather for"
            },
            "unit": {
                // highlight-start
                "type": ["string", "null"],
                // highlight-end
                "description": "The unit to return the temperature in",
                "enum": ["F", "C"]
            }
        },
        "additionalProperties": false,
        "required": [
            "location", "unit"
        ]
    }
}

In this example, we can only support a single string type with the existing model.

**Describe the solution you'd like**
Update the Type field in the Definition struct to  allow for multiple data types. This enhancement will enable developers to define union types, aligning the library with OpenAI’s guidelines for structured outputs.

**Describe alternatives you've considered**
Create a new type which support single and multiple type both and write custom marshal and unmarshal to handle that.

**Additional context**
https://platform.openai.com/docs/guides/structured-outputs/supported-schemas

@samarthpatil00
Copy link

Hi @vish0l #831 will solve this as well. Since it will allow you to pass the schema directly.
Alternate solution for your problem is using structured output via function calls, there it can accept raw json schema in the parameter. So you can do something like :

schema := `{
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The location to get the weather for"
            },
            "unit": {
                // highlight-start
                "type": ["string", "null"],
                // highlight-end
                "description": "The unit to return the temperature in",
                "enum": ["F", "C"]
            }
        },
        "additionalProperties": false,
        "required": [
            "location", "unit"
        ]
}`
tool := openai.Tool{
    Type: openai.ToolTypeFunction,
    Function: &openai.FunctionDefinition{
  	Name:       "get_weather",
	Strict:     true,
	Parameters: json.RawMessage(schema),
    },
}

Hope this helps

@vish0l
Copy link
Author

vish0l commented Aug 23, 2024

Thank you for your response, @samarthpatil00. In my use case, I need a structured output, but I won't be using any external functions or tools.

@samarthpatil00
Copy link

@vish0l you can still use tools to get the argument it passes to function and use them as the structured output

resp, err := c.Client.CreateChatCompletion(
  context.Background(),
  openai.ChatCompletionRequest{
	  Model:            string(c.Model),
	  Messages:         messages,
	  Temperature:      temperature,
	  Tools:            []openai.Tool{tool},
  },
)
op := resp.Choices[0].Message.ToolCalls[0].Function.Arguments

@vish0l
Copy link
Author

vish0l commented Aug 23, 2024

what's the point of doing that when straight forward structured output functionality is available 🙂

@vish0l vish0l closed this as completed Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants