-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[OpenAI] BYO Multipart form request support #36621
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
Merged
jpalvarezl
merged 6 commits into
jpalvarezl/whisper_support
from
jpalvarezl/multipart_data_hack
Sep 7, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2394916
Still error, but almost hack for multipart data request
jpalvarezl ae2b22e
Replaced whitespace with line breaks as necessary
jpalvarezl 7a1f0c2
Somehow still failing. A bit out of ideas
jpalvarezl 7838746
Changed the encoding to ASCII
jpalvarezl 3f769f7
Using CRLF instead
jpalvarezl 18f0111
Test pass. Renamed variables to be more selfexplanatory
jpalvarezl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...azure-ai-openai/src/main/java/com/azure/ai/openai/implementation/MultipartDataHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.azure.ai.openai.implementation; | ||
|
|
||
| import com.azure.ai.openai.models.AudioTranslationOptions; | ||
| import com.azure.core.util.BinaryData; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public class MultipartDataHelper { | ||
| private final String boundaryId = UUID.randomUUID().toString().substring(0, 16); | ||
|
|
||
| private final String boundary = "AZ-OAI-JAVA--" + boundaryId; | ||
|
|
||
| private final String partSeparator = "--" + boundary; | ||
| private final String endMarker = partSeparator + "--"; | ||
|
|
||
| private final String CRLF = "\r\n"; | ||
| private final List<MultipartField> fields = new ArrayList<>(); | ||
|
|
||
| public String getBoundary() { | ||
| return boundary; | ||
| } | ||
|
|
||
| public SerializationResult serializeAudioTranscriptionOption ( | ||
| AudioTranslationOptions audioTranscriptionOptions, String fileName) { | ||
| ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
| // File | ||
| String fileFieldPreamble = partSeparator | ||
| + CRLF + "Content-Disposition: form-data; name=\"file\"; filename=\"" | ||
| + fileName + "\"" | ||
| + CRLF + "Content-Type: application/octet-stream" + CRLF + CRLF; | ||
| try { | ||
| byteArrayOutputStream.write(fileFieldPreamble.getBytes(StandardCharsets.US_ASCII)); | ||
| byteArrayOutputStream.write(audioTranscriptionOptions.getFile()); | ||
| for (MultipartField field : fields) { | ||
| byteArrayOutputStream.write(serializeField(field)); | ||
| } | ||
| byteArrayOutputStream.write((CRLF + endMarker).getBytes(StandardCharsets.US_ASCII)); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
|
|
||
| byte[] totalData = byteArrayOutputStream.toByteArray(); | ||
| // Uncomment to verify as string. Seems to check out with structure observed in the curl traces | ||
| System.out.println(new String(totalData, StandardCharsets.US_ASCII)); | ||
| return new SerializationResult(BinaryData.fromBytes(totalData), totalData.length); | ||
| } | ||
|
|
||
| public void addFields(Consumer<List<MultipartField>> fieldAdder) { | ||
| fieldAdder.accept(fields); | ||
| } | ||
|
|
||
| private byte[] serializeField(MultipartField field) { | ||
| String toSerizalise = CRLF + partSeparator | ||
| + CRLF + "Content-Disposition: form-data; name=\"" | ||
| + field.getWireName() + "\"" + CRLF + CRLF | ||
| + field.getValue(); | ||
|
|
||
| return toSerizalise.getBytes(StandardCharsets.US_ASCII); | ||
| } | ||
|
|
||
| public class SerializationResult { | ||
| private final long dataLength; | ||
| private final BinaryData data; | ||
|
|
||
| public SerializationResult(BinaryData data, long contentLength) { | ||
| this.dataLength = contentLength; | ||
| this.data = data; | ||
| } | ||
|
|
||
| public BinaryData getData() { | ||
| return data; | ||
| } | ||
|
|
||
| public long getDataLength() { | ||
| return dataLength; | ||
| } | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
...enai/azure-ai-openai/src/main/java/com/azure/ai/openai/implementation/MultipartField.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.azure.ai.openai.implementation; | ||
|
|
||
| public class MultipartField { | ||
| private final String wireName; | ||
| private final String value; | ||
|
|
||
| public MultipartField(String wireName, String value) { | ||
| this.wireName = wireName; | ||
| this.value = value; | ||
| } | ||
|
|
||
| public String getWireName() { | ||
| return wireName; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.