-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[OpenAI] Added sample and updated READMEs #36806
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
mssfang
merged 2 commits into
Azure:jpalvarezl/whisper_support_looser_types
from
mssfang:OpenAI-Whisper-Sample-README-JavaDoc
Sep 18, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
Binary file added
BIN
+71.3 KB
...i/azure-ai-openai/src/samples/java/com/azure/ai/openai/resources/JP_it_is_rainy_today.wav
Binary file not shown.
Binary file added
BIN
+3.5 MB
sdk/openai/azure-ai-openai/src/samples/java/com/azure/ai/openai/resources/batman.wav
Binary file not shown.
52 changes: 52 additions & 0 deletions
52
...e-ai-openai/src/samples/java/com/azure/ai/openai/usage/AudioTranscriptionAsyncSample.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,52 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.openai.usage; | ||
|
|
||
| import com.azure.ai.openai.OpenAIAsyncClient; | ||
| import com.azure.ai.openai.OpenAIClientBuilder; | ||
| import com.azure.ai.openai.models.AudioTranscriptionFormat; | ||
| import com.azure.ai.openai.models.AudioTranscriptionOptions; | ||
| import com.azure.core.credential.AzureKeyCredential; | ||
| import com.azure.core.util.BinaryData; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * An asynchronous sample demonstrates how to transcript a given audio file. | ||
| */ | ||
| public class AudioTranscriptionAsyncSample { | ||
| /** | ||
| * Runs the sample algorithm and demonstrates how to transcript a given audio file. | ||
| * | ||
| * @param args Unused. Arguments to the program. | ||
| */ | ||
| public static void main(String[] args) throws InterruptedException { | ||
| String azureOpenaiKey = "{azure-open-ai-key}"; | ||
| String endpoint = "{azure-open-ai-endpoint}"; | ||
| String deploymentOrModelId = "{azure-open-ai-deployment-model-id}"; | ||
| String fileName = "batman.wav"; | ||
| Path filePath = Paths.get("src/samples/java/com/azure/ai/openai/resources/" + fileName); | ||
|
|
||
| OpenAIAsyncClient client = new OpenAIClientBuilder() | ||
| .endpoint(endpoint) | ||
| .credential(new AzureKeyCredential(azureOpenaiKey)) | ||
| .buildAsyncClient(); | ||
|
|
||
| byte[] file = BinaryData.fromFile(filePath).toBytes(); | ||
| AudioTranscriptionOptions transcriptionOptions = new AudioTranscriptionOptions(file) | ||
| .setResponseFormat(AudioTranscriptionFormat.JSON); | ||
|
|
||
| client.getAudioTranscription(deploymentOrModelId, transcriptionOptions, fileName) | ||
| .subscribe(transcription -> { | ||
| System.out.println("Transcription: " + transcription.getText()); | ||
| }); | ||
|
|
||
| // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep | ||
| // the thread so the program does not end before the send operation is complete. Using .block() instead of | ||
| // .subscribe() will turn this into a synchronous call. | ||
| TimeUnit.SECONDS.sleep(10); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
.../azure-ai-openai/src/samples/java/com/azure/ai/openai/usage/AudioTranscriptionSample.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,49 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.openai.usage; | ||
|
|
||
| import com.azure.ai.openai.OpenAIClient; | ||
| import com.azure.ai.openai.OpenAIClientBuilder; | ||
| import com.azure.ai.openai.models.AudioTranscription; | ||
| import com.azure.ai.openai.models.AudioTranscriptionFormat; | ||
| import com.azure.ai.openai.models.AudioTranscriptionOptions; | ||
| import com.azure.core.credential.AzureKeyCredential; | ||
| import com.azure.core.util.BinaryData; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| /** | ||
| * A sample demonstrates how to transcript a given audio file. | ||
| */ | ||
| public class AudioTranscriptionSample { | ||
| /** | ||
| * Runs the sample algorithm and demonstrates how to get the images for a given prompt. | ||
| * | ||
| * @param args Unused. Arguments to the program. | ||
| */ | ||
| public static void main(String[] args) { | ||
| String azureOpenaiKey = "{azure-open-ai-key}"; | ||
| String endpoint = "{azure-open-ai-endpoint}"; | ||
| String deploymentOrModelId = "{azure-open-ai-deployment-model-id}"; | ||
| String fileName = "batman.wav"; | ||
| Path filePath = Paths.get("src/samples/java/com/azure/ai/openai/resources/" + fileName); | ||
|
|
||
| OpenAIClient client = new OpenAIClientBuilder() | ||
| .endpoint(endpoint) | ||
| .credential(new AzureKeyCredential(azureOpenaiKey)) | ||
| .buildClient(); | ||
|
|
||
| byte[] file = BinaryData.fromFile(filePath).toBytes(); | ||
| AudioTranscriptionOptions transcriptionOptions = new AudioTranscriptionOptions(file) | ||
| .setResponseFormat(AudioTranscriptionFormat.JSON); | ||
|
|
||
| AudioTranscription transcription = client.getAudioTranscription(deploymentOrModelId, transcriptionOptions, fileName); | ||
|
|
||
| System.out.println("Transcription: " + transcription.getText()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
51 changes: 51 additions & 0 deletions
51
...ure-ai-openai/src/samples/java/com/azure/ai/openai/usage/AudioTranslationAsyncSample.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,51 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.openai.usage; | ||
|
|
||
| import com.azure.ai.openai.OpenAIAsyncClient; | ||
| import com.azure.ai.openai.OpenAIClientBuilder; | ||
| import com.azure.ai.openai.models.AudioTranscriptionFormat; | ||
| import com.azure.ai.openai.models.AudioTranslationOptions; | ||
| import com.azure.core.credential.AzureKeyCredential; | ||
| import com.azure.core.util.BinaryData; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * An asynchronous sample demonstrates how to translate a given audio file. | ||
| */ | ||
| public class AudioTranslationAsyncSample { | ||
| /** | ||
| * Runs the sample algorithm and demonstrates how to translate a given audio file. | ||
| * | ||
| * @param args Unused. Arguments to the program. | ||
| */ | ||
| public static void main(String[] args) throws InterruptedException { | ||
| String azureOpenaiKey = "{azure-open-ai-key}"; | ||
| String endpoint = "{azure-open-ai-endpoint}"; | ||
| String deploymentOrModelId = "{azure-open-ai-deployment-model-id}"; | ||
| String fileName = "JP_it_is_rainy_today.wav"; | ||
| Path filePath = Paths.get("src/samples/java/com/azure/ai/openai/resources/" + fileName); | ||
|
|
||
| OpenAIAsyncClient client = new OpenAIClientBuilder() | ||
| .endpoint(endpoint) | ||
| .credential(new AzureKeyCredential(azureOpenaiKey)) | ||
| .buildAsyncClient(); | ||
| byte[] file = BinaryData.fromFile(filePath).toBytes(); | ||
| AudioTranslationOptions translationOptions = new AudioTranslationOptions(file) | ||
| .setResponseFormat(AudioTranscriptionFormat.JSON); | ||
|
|
||
| client.getAudioTranslation(deploymentOrModelId, translationOptions, fileName) | ||
| .subscribe(translation -> { | ||
| System.out.println("Translation: " + translation.getText()); | ||
| }); | ||
|
|
||
| // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep | ||
| // the thread so the program does not end before the send operation is complete. Using .block() instead of | ||
| // .subscribe() will turn this into a synchronous call. | ||
| TimeUnit.SECONDS.sleep(10); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
...ai/azure-ai-openai/src/samples/java/com/azure/ai/openai/usage/AudioTranslationSample.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,45 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.ai.openai.usage; | ||
|
|
||
| import com.azure.ai.openai.OpenAIClient; | ||
| import com.azure.ai.openai.OpenAIClientBuilder; | ||
| import com.azure.ai.openai.models.AudioTranscription; | ||
| import com.azure.ai.openai.models.AudioTranscriptionFormat; | ||
| import com.azure.ai.openai.models.AudioTranslationOptions; | ||
| import com.azure.core.credential.AzureKeyCredential; | ||
| import com.azure.core.util.BinaryData; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
|
|
||
| /** | ||
| * A sample demonstrates how to translate a given audio file. | ||
| */ | ||
| public class AudioTranslationSample { | ||
| /** | ||
| * Runs the sample algorithm and demonstrates how to translate a given audio file. | ||
| * | ||
| * @param args Unused. Arguments to the program. | ||
| */ | ||
| public static void main(String[] args) { | ||
| String azureOpenaiKey = "{azure-open-ai-key}"; | ||
| String endpoint = "{azure-open-ai-endpoint}"; | ||
| String deploymentOrModelId = "{azure-open-ai-deployment-model-id}"; | ||
| String fileName = "JP_it_is_rainy_today.wav"; | ||
| Path filePath = Paths.get("src/samples/java/com/azure/ai/openai/resources/" + fileName); | ||
|
|
||
| OpenAIClient client = new OpenAIClientBuilder() | ||
| .endpoint(endpoint) | ||
| .credential(new AzureKeyCredential(azureOpenaiKey)) | ||
| .buildClient(); | ||
| byte[] file = BinaryData.fromFile(filePath).toBytes(); | ||
| AudioTranslationOptions translationOptions = new AudioTranslationOptions(file) | ||
| .setResponseFormat(AudioTranscriptionFormat.JSON); | ||
|
|
||
| AudioTranscription translation = client.getAudioTranslation(deploymentOrModelId, translationOptions, fileName); | ||
|
|
||
| System.out.println("Translation: " + translation.getText()); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering if should actually have a convenience method encapsulating this bit of logic so that users don't have to repeat the boilerplate each time. WDYT? I think is good to have the documentation as is though. Files might not be the only source of
byte[]that a user may want to use.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. We should have a Public API to take
byte[]data. Which should be convenience for customers.