go-openai icon indicating copy to clipboard operation
go-openai copied to clipboard

Support for extra_body parameter for embeddings API

Open AyushSawant18588 opened this issue 11 months ago • 10 comments

Describe the change This change adds support for an ExtraBody parameter in the EmbeddingRequest struct, allowing developers to include additional, arbitrary key-value pairs in the API request payload. This ensures extensibility and flexibility when working with the OpenAI Embeddings API, especially for experimental features, future API updates, or custom parameters that are not explicitly defined in the current request structure. The addition of ExtraBody is particularly valuable for supporting LLM engines like vLLM, Nvidia NIM, etc, which may require additional fields beyond the standard parameters. Refrence There is an issue created that ExtraBody parameter support is required for vLLM engine also. https://github.com/sashabaranov/go-openai/issues/898 In this PR ExtraBody parameter support is added only for embedding API but it can be extended for other APIs as well like chat completions

Provide OpenAI documentation link OpenAI's official python client also supports this extra_body field which this go client could also support. Refrence

Describe your solution The solution involves: Introducing the ExtraBody field in the EmbeddingRequest struct. This is a map of string keys to arbitrary values (map[string]any) that allows developers to pass additional fields to the API dynamically. Updating the CreateEmbeddings method to merge the ExtraBody values into the main request payload (body) before sending it to the API. This is done through the withExtraBody request option, which ensures the fields in ExtraBody are included without overwriting existing mandatory parameters.

Tests Added unit tests and done sanity check of embeddings api with extra_body param for Nvidia NIM embedding model

AyushSawant18588 avatar Nov 23 '24 16:11 AyushSawant18588