-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ For concrete examples you can have a look at the following links. Some of the mo | |
| * [Streaming chat completions sample](#streaming-chat-completions "Streaming chat completions") | ||
| * [Embeddings sample](#text-embeddings "Text Embeddings") | ||
| * [Image Generation sample](#image-generation "Image Generation") | ||
| * [Audio Transcription sample](#audio-transcription "Audio Transcription") | ||
| * [Audio Translation sample](#audio-translation "Audio Translation") | ||
|
|
||
| If you want to see the full code for these snippets check out our [samples folder][samples_folder]. | ||
|
|
||
|
|
@@ -150,6 +152,8 @@ The following sections provide several code snippets covering some of the most c | |
| * [Streaming chat completions sample](#streaming-chat-completions "Streaming chat completions") | ||
| * [Embeddings sample](#text-embeddings "Text Embeddings") | ||
| * [Image Generation sample](#image-generation "Image Generation") | ||
| * [Audio Transcription sample](#audio-transcription "Audio Transcription") | ||
| * [Audio Translation sample](#audio-translation "Audio Translation") | ||
|
|
||
| ### Text completions | ||
|
|
||
|
|
@@ -286,6 +290,44 @@ for (ImageLocation imageLocation : images.getData()) { | |
|
|
||
| For a complete sample example, see sample [Image Generation][sample_image_generation]. | ||
|
|
||
| ### Audio Transcription | ||
| The OpenAI service starts supporting `audio transcription` since model `Whisper`. | ||
| The following code snippet shows how to use the service to transcribe audio. | ||
|
|
||
| ```java readme-sample-audioTranscription | ||
| String fileName = "{your-file-name}"; | ||
| Path filePath = Paths.get("{your-file-path}" + fileName); | ||
|
|
||
| byte[] file = BinaryData.fromFile(filePath).toBytes(); | ||
| AudioTranscriptionOptions transcriptionOptions = new AudioTranscriptionOptions(file) | ||
| .setResponseFormat(AudioTranscriptionFormat.JSON); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. We should have a Public API to take |
||
|
|
||
| AudioTranscription transcription = client.getAudioTranscription("{deploymentOrModelId}", transcriptionOptions, fileName); | ||
|
|
||
| System.out.println("Transcription: " + transcription.getText()); | ||
| ``` | ||
| For a complete sample example, see sample [Audio Transcription][sample_audio_transcription]. | ||
| Please refer to the service documentation for a conceptual discussion of [Whisper][microsoft_docs_whisper_model]. | ||
|
|
||
| ### Audio Translation | ||
| The OpenAI service starts supporting `audio translation` since model `Whisper`. | ||
|
mssfang marked this conversation as resolved.
Outdated
|
||
| The following code snippet shows how to use the service to translate audio. | ||
|
|
||
| ```java readme-sample-audioTranslation | ||
| String fileName = "{your-file-name}"; | ||
| Path filePath = Paths.get("{your-file-path}" + fileName); | ||
|
|
||
| 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()); | ||
| ``` | ||
| For a complete sample example, see sample [Audio Translation][sample_audio_translation]. | ||
| Please refer to the service documentation for a conceptual discussion of [Whisper][microsoft_docs_whisper_model]. | ||
|
|
||
| ## Troubleshooting | ||
| ### Enable client logging | ||
| You can set the `AZURE_LOG_LEVEL` environment variable to view logging statements made in the client library. For | ||
|
|
@@ -327,6 +369,7 @@ For details on contributing to this repository, see the [contributing guide](htt | |
| [logLevels]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java | ||
| [microsoft_docs_openai_completion]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/completions | ||
| [microsoft_docs_openai_embedding]: https://learn.microsoft.com/azure/cognitive-services/openai/concepts/understand-embeddings | ||
| [microsoft_docs_whisper_model]: https://learn.microsoft.com/azure/ai-services/openai/whisper-quickstart?tabs=command-line | ||
| [non_azure_openai_authentication]: https://platform.openai.com/docs/api-reference/authentication | ||
| [performance_tuning]: https://github.com/Azure/azure-sdk-for-java/wiki/Performance-Tuning | ||
| [product_documentation]: https://azure.microsoft.com/services/ | ||
|
|
@@ -342,6 +385,8 @@ For details on contributing to this repository, see the [contributing guide](htt | |
| [sample_get_completions_streaming]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/samples/java/com/azure/ai/openai/usage/GetCompletionsStreamSample.java | ||
| [sample_get_embedding]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/samples/java/com/azure/ai/openai/usage/GetEmbeddingsSample.java | ||
| [sample_image_generation]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/samples/java/com/azure/ai/openai/usage/GetImagesSample.java | ||
| [sample_audio_transcription]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/samples/java/com/azure/ai/openai/usage/AudioTranscriptionSample | ||
| [sample_audio_translation]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/samples/java/com/azure/ai/openai/usage/AudioTranslationSample.java | ||
| [openai_client_async]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/main/java/com/azure/ai/openai/OpenAIAsyncClient.java | ||
| [openai_client_builder]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/main/java/com/azure/ai/openai/OpenAIClientBuilder.java | ||
| [openai_client_sync]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/openai/azure-ai-openai/src/main/java/com/azure/ai/openai/OpenAIClient.java | ||
|
|
||
| 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); | ||
| } | ||
| } |
| 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()); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
| 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); | ||
| } | ||
| } |
| 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()); | ||
| } | ||
| } |
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 would word this a little bit differently. How about "with the introduction of
Whispermodels" ?