Support binary image URLs without copying image data #853
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe the change
This PR allows memory allocation to be optimised when providing a binary image in a ChatCompletionRequest.
It allows for providing the raw image bytes by reference, instead of having to copy the base64 encoded string.
Provide OpenAI documentation link
No API documentation is relevant.
Describe your solution
The solution makes it possible to provide any type of image URL and implements a BinaryImageURL struct that using MarshalJSON converts the raw image bytes to JSON in the most memory-effective way.
Tests
I've tested the changes using a memory profiler. I've profiled the memory allocation using a base64 encoded image URL and using the BinaryImageURL.
Using a 20mb picture, a 31,7% or 74 mb decrease in memory allocation can be seen for the provided tests.
Additional context