openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI
MIT License
1.55k stars 163 forks source link

Add 'extra_body' Parameter to ChatCompletionOptions in .NET SDK #132

Open sdcb opened 4 months ago

sdcb commented 4 months ago

In the OpenAI Python SDK, there is an extra_body parameter available in the Completions.create method, which allows users to add additional JSON properties to the request body. This feature can be very useful in various scenarios, especially when needing to send extra data that is not covered by the predefined parameters.

However, this parameter does not exist in the .NET SDK. Including this parameter would help maintain consistency across SDKs and provide .NET users with the same level of flexibility that Python users have.

Reference to Python SDK Code: Here is the implementation in the Python SDK for reference: https://github.com/openai/openai-python/blob/af8f606b3eef1af99f98929847f1b5baf19171ae/src/openai/resources/chat/completions.py#L76

Suggested Change: Add an extra_body property to the ChatCompletionOptions class in the .NET SDK that allows additional JSON properties to be serialized into the request body.

This could look something like:

public class ChatCompletionOptions
{
    // Existing properties...

    // New property for additional body parameters
    [JsonProperty]
    public Dictionary<string, object>? ExtraBody { get; set; }

    // Method to include this in the serialized output...
}

Please consider adding this feature to improve the SDK's flexibility and usability.

### Tasks
sdcb commented 4 months ago

Current workaround:

ChatCompletionOptions cco = new ();
cco = ((IJsonModel<ChatCompletionOptions>)cco).Create(new BinaryData("""
{
    // this is extra_body content
    "enable_search": true
}
"""), ModelReaderWriterOptions.Json);
fobos531 commented 3 months ago

I presume this is even more relevant now that structured outputs have been added to the API: https://openai.com/index/introducing-structured-outputs-in-the-api/

OcWebb commented 5 days ago

This would benefit me for a few reasons and allow for faster adoption of new features of this client lags behind the API. I hope this is considered.