Skip to content

Commit 59c3ab2

Browse files
authored
[OpenAI] Merged feature branch, "feature/open-ai" into main branch (Azure#35048)
1 parent 9e50199 commit 59c3ab2

File tree

81 files changed

+6972
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+6972
-3
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@
166166
# PRLabel: %Mixed Reality Authentication
167167
/sdk/mixedreality/azure-mixedreality-authentication/ @craigktreasure
168168

169+
# PRLabel: %Open AI
170+
/sdk/openai/azure-ai-openai/ @brandom-msft @jpalvarezl @mssfang
171+
169172
# PRLabel: %Remote Rendering
170173
/sdk/remoterendering/ @MichaelZp0 @ChristopherManthei
171174

.vscode/cspell.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,15 @@
308308
"linksource",
309309
"listbyconfigurationprofileassignments",
310310
"localizable",
311+
"logit",
312+
"logprobs",
311313
"logz",
312314
"Lucene",
313315
"mgmt",
314316
"mibps",
315317
"Mirena",
316318
"Mockito",
319+
"Mordor",
317320
"mosca",
318321
"msal",
319322
"msix",
@@ -327,6 +330,7 @@
327330
"onboarded",
328331
"Onco",
329332
"onenote",
333+
"openai",
330334
"premf",
331335
"pwsh",
332336
"protonj",

eng/code-quality-reports/src/main/resources/checkstyle/checkstyle-suppressions.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ the main ServiceBusClientBuilder. -->
398398
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientCheck" files="com.azure.messaging.eventhubs.(EventHubBufferedProducerClient|EventHubBufferedProducerAsyncClient).java"/>
399399
<suppress checks="com.azure.tools.checkstyle.checks.UseCaughtExceptionCauseCheck" files="com.azure.messaging.eventhubs.EventDataBatch.java"/>
400400

401+
<!-- Checkstyle suppression for OpenAI client APIs that use Flux instead of PagedFlux for methods that return a collection -->
402+
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientCheck" files="com.azure.ai.openai.(OpenAIClient|OpenAIAsyncClient).java"/>
403+
401404
<!-- jdbc sdk suppression -->
402405
<suppress checks="com.azure.tools.checkstyle.checks.ExternalDependencyExposedCheck"
403406
files="com.azure.identity.extensions.jdbc.(mysql|postgresql).(AzureMysqlAuthenticationPlugin|AzurePostgresqlAuthenticationPlugin).java"/>

eng/versioning/version_client.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ com.azure:azure-sdk-all;1.0.0;1.0.0
3737
com.azure:azure-sdk-parent;1.6.0;1.6.0
3838
com.azure:azure-client-sdk-parent;1.7.0;1.7.0
3939
com.azure:azure-ai-anomalydetector;3.0.0-beta.5;3.0.0-beta.6
40+
com.azure:azure-ai-documenttranslator;1.0.0-beta.1;1.0.0-beta.2
4041
com.azure:azure-ai-formrecognizer;4.0.7;4.1.0-beta.3
4142
com.azure:azure-ai-formrecognizer-perf;1.0.0-beta.1;1.0.0-beta.1
42-
com.azure:azure-ai-personalizer;1.0.0-beta.1;1.0.0-beta.2
43-
com.azure:azure-ai-documenttranslator;1.0.0-beta.1;1.0.0-beta.2
44-
com.azure:azure-ai-translation-text;1.0.0-beta.1;1.0.0-beta.2
4543
com.azure:azure-ai-metricsadvisor;1.1.13;1.2.0-beta.1
4644
com.azure:azure-ai-metricsadvisor-perf;1.0.0-beta.1;1.0.0-beta.1
45+
com.azure:azure-ai-openai;1.0.0-beta.1;1.0.0-beta.1
46+
com.azure:azure-ai-personalizer;1.0.0-beta.1;1.0.0-beta.2
4747
com.azure:azure-ai-textanalytics;5.2.6;5.3.0-beta.3
4848
com.azure:azure-ai-textanalytics-perf;1.0.0-beta.1;1.0.0-beta.1
49+
com.azure:azure-ai-translation-text;1.0.0-beta.1;1.0.0-beta.2
4950
com.azure:azure-analytics-purview-catalog;1.0.0-beta.4;1.0.0-beta.5
5051
com.azure:azure-analytics-purview-scanning;1.0.0-beta.2;1.0.0-beta.3
5152
com.azure:azure-analytics-purview-administration;1.0.0-beta.1;1.0.0-beta.2

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<module>sdk/nginx</module>
128128
<module>sdk/notificationhubs</module>
129129
<module>sdk/oep</module>
130+
<module>sdk/openai</module>
130131
<module>sdk/operationsmanagement</module>
131132
<module>sdk/orbital</module>
132133
<module>sdk/paloaltonetworks</module>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Release History
2+
3+
## 1.0.0-beta.1 (Unreleased)
4+
5+
- Azure OpenAI client library for Java. This package contains Microsoft Azure OpenAI client library.
6+
7+
### Features Added
8+
9+
### Breaking Changes
10+
11+
### Bugs Fixed
12+
13+
### Other Changes

sdk/openai/azure-ai-openai/README.md

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
# Azure OpenAI client library for Java
2+
3+
Azure OpenAI is a managed service that allows developers to deploy, tune, and generate content from OpenAI models on
4+
Azure resources.
5+
6+
The Azure OpenAI client library for Java is an adaptation of OpenAI's REST APIs that provides an idiomatic interface
7+
and rich integration with the rest of the Azure SDK ecosystem.
8+
9+
Use the client library for Azure OpenAI to:
10+
11+
* [Create a completion for text][microsoft_docs_openai_completion]
12+
* [Create a text embedding for comparisons][microsoft_docs_openai_embedding]
13+
14+
[Source code][source_code] | [API reference documentation][docs] | [Product Documentation][product_documentation] | [Samples][samples_readme]
15+
16+
## Getting started
17+
18+
### Prerequisites
19+
20+
- [Java Development Kit (JDK)][jdk] with version 8 or above
21+
- [Azure Subscription][azure_subscription]
22+
- [Azure OpenAI access][azure_openai_access]
23+
- [Quickstart: Get started generating text using Azure OpenAI Service][quickstart]
24+
25+
### Adding the package to your product
26+
27+
[//]: # ({x-version-update-start;com.azure:azure-ai-openai;current})
28+
```xml
29+
<dependency>
30+
<groupId>com.azure</groupId>
31+
<artifactId>azure-ai-openai</artifactId>
32+
<version>1.0.0-beta.1</version>
33+
</dependency>
34+
```
35+
[//]: # ({x-version-update-end})
36+
37+
### Authentication
38+
39+
In order to interact with the Azure OpenAI service you'll need to create an instance of client class,
40+
[OpenAIAsyncClient][openai_client_async] or [OpenAIClient][openai_client_sync] by using
41+
[OpenAIClientBuilder][openai_client_builder]. To configure a client for use with
42+
Azure OpenAI, provide a valid endpoint URI to an Azure OpenAI resource along with a corresponding key credential,
43+
token credential, or [Azure Identity][azure_identity] credential that's authorized to use the Azure OpenAI resource.
44+
45+
#### Create a Azure OpenAI client with key credential
46+
Get Azure OpenAI `key` credential from the Azure Portal.
47+
48+
```java readme-sample-createSyncClientKeyCredential
49+
OpenAIClient client = new OpenAIClientBuilder()
50+
.credential(new AzureKeyCredential("{key}"))
51+
.endpoint("{endpoint}")
52+
.buildClient();
53+
```
54+
or
55+
```java readme-sample-createAsyncClientKeyCredential
56+
OpenAIAsyncClient client = new OpenAIClientBuilder()
57+
.credential(new AzureKeyCredential("{key}"))
58+
.endpoint("{endpoint}")
59+
.buildAsyncClient();
60+
```
61+
62+
#### Create an Azure OpenAI client with Azure Active Directory credential
63+
Azure SDK for Java supports an Azure Identity package, making it easy to get credentials from Microsoft identity
64+
platform.
65+
66+
Authentication with AAD requires some initial setup:
67+
* Add the Azure Identity package
68+
69+
[//]: # ({x-version-update-start;com.azure:azure-identity;dependency})
70+
```xml
71+
<dependency>
72+
<groupId>com.azure</groupId>
73+
<artifactId>azure-identity</artifactId>
74+
<version>1.9.0</version>
75+
</dependency>
76+
```
77+
[//]: # ({x-version-update-end})
78+
79+
After setup, you can choose which type of [credential][azure_identity_credential_type] from azure.identity to use.
80+
As an example, [DefaultAzureCredential][wiki_identity] can be used to authenticate the client:
81+
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables:
82+
`AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`.
83+
84+
Authorization is easiest using [DefaultAzureCredential][wiki_identity]. It finds the best credential to use in its
85+
running environment. For more information about using Azure Active Directory authorization with OpenAI service, please
86+
refer to [the associated documentation][aad_authorization].
87+
88+
```java readme-sample-createOpenAIClientWithAAD
89+
TokenCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
90+
OpenAIClient client = new OpenAIClientBuilder()
91+
.credential(defaultCredential)
92+
.endpoint("{endpoint}")
93+
.buildClient();
94+
```
95+
96+
#### Create a client with proxy options
97+
Create an OpenAI client with proxy options.
98+
```java readme-sample-createOpenAIClientWithProxyOption
99+
// Proxy options
100+
final String hostname = "{your-host-name}";
101+
final int port = 447; // your port number
102+
103+
ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(hostname, port))
104+
.setCredentials("{username}", "{password}");
105+
106+
OpenAIClient client = new OpenAIClientBuilder()
107+
.credential(new AzureKeyCredential("{key}"))
108+
.endpoint("{endpoint}")
109+
.clientOptions(new HttpClientOptions().setProxyOptions(proxyOptions))
110+
.buildClient();
111+
```
112+
113+
## Key concepts
114+
115+
## Examples
116+
The following sections provide several code snippets covering some of the most common OpenAI service tasks, including:
117+
118+
* [Text completions sample](#text-completions "Text completions")
119+
* [Streaming text completions sample](#streaming-text-completions "Streaming text completions")
120+
* [Chat completions sample](#chat-completions "Chat completions")
121+
* [Streaming chat completions sample](#streaming-chat-completions "Streaming chat completions")
122+
* [Embeddings sample](#text-embeddings "Text Embeddings")
123+
124+
### Text completions
125+
126+
``` java readme-sample-getCompletions
127+
List<String> prompt = new ArrayList<>();
128+
prompt.add("Say this is a test");
129+
130+
Completions completions = client.getCompletions("{deploymentOrModelId}", new CompletionsOptions(prompt));
131+
132+
System.out.printf("Model ID=%s is created at %d.%n", completions.getId(), completions.getCreated());
133+
for (Choice choice : completions.getChoices()) {
134+
System.out.printf("Index: %d, Text: %s.%n", choice.getIndex(), choice.getText());
135+
}
136+
```
137+
138+
### Streaming text completions
139+
140+
```java readme-sample-getCompletionsStream
141+
List<String> prompt = new ArrayList<>();
142+
prompt.add("How to bake a cake?");
143+
144+
IterableStream<Completions> completionsStream = client
145+
.getCompletionsStream("{deploymentOrModelId}", new CompletionsOptions(prompt));
146+
147+
completionsStream.forEach(completions -> {
148+
System.out.printf("Model ID=%s is created at %d.%n", completions.getId(), completions.getCreated());
149+
for (Choice choice : completions.getChoices()) {
150+
System.out.printf("Index: %d, Text: %s.%n", choice.getIndex(), choice.getText());
151+
}
152+
});
153+
```
154+
155+
### Chat completions
156+
157+
``` java readme-sample-getChatCompletions
158+
List<ChatMessage> chatMessages = new ArrayList<>();
159+
chatMessages.add(new ChatMessage(ChatRole.SYSTEM).setContent("You are a helpful assistant. You will talk like a pirate."));
160+
chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Can you help me?"));
161+
chatMessages.add(new ChatMessage(ChatRole.ASSISTANT).setContent("Of course, me hearty! What can I do for ye?"));
162+
chatMessages.add(new ChatMessage(ChatRole.USER).setContent("What's the best way to train a parrot?"));
163+
164+
ChatCompletions chatCompletions = client.getChatCompletions("{deploymentOrModelId}",
165+
new ChatCompletionsOptions(chatMessages));
166+
167+
System.out.printf("Model ID=%s is created at %d.%n", chatCompletions.getId(), chatCompletions.getCreated());
168+
for (ChatChoice choice : chatCompletions.getChoices()) {
169+
ChatMessage message = choice.getMessage();
170+
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
171+
System.out.println("Message:");
172+
System.out.println(message.getContent());
173+
}
174+
```
175+
Please refer to the service documentation for a conceptual discussion of [text completion][microsoft_docs_openai_completion].
176+
177+
### Streaming chat completions
178+
179+
```java readme-sample-getChatCompletionsStream
180+
List<ChatMessage> chatMessages = new ArrayList<>();
181+
chatMessages.add(new ChatMessage(ChatRole.SYSTEM).setContent("You are a helpful assistant. You will talk like a pirate."));
182+
chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Can you help me?"));
183+
chatMessages.add(new ChatMessage(ChatRole.ASSISTANT).setContent("Of course, me hearty! What can I do for ye?"));
184+
chatMessages.add(new ChatMessage(ChatRole.USER).setContent("What's the best way to train a parrot?"));
185+
186+
IterableStream<ChatCompletions> chatCompletionsStream = client.getChatCompletionsStream("{deploymentOrModelId}",
187+
new ChatCompletionsOptions(chatMessages));
188+
189+
chatCompletionsStream.forEach(chatCompletions -> {
190+
System.out.printf("Model ID=%s is created at %d.%n", chatCompletions.getId(), chatCompletions.getCreated());
191+
for (ChatChoice choice : chatCompletions.getChoices()) {
192+
ChatMessageDelta message = choice.getDelta();
193+
if (message != null) {
194+
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
195+
System.out.println("Message:");
196+
System.out.println(message.getContent());
197+
}
198+
}
199+
200+
CompletionsUsage usage = chatCompletions.getUsage();
201+
if (usage != null) {
202+
System.out.printf("Usage: number of prompt token is %d, "
203+
+ "number of completion token is %d, and number of total tokens in request and response is %d.%n",
204+
usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
205+
}
206+
});
207+
```
208+
209+
### Text embeddings
210+
211+
```java readme-sample-getEmbedding
212+
EmbeddingsOptions embeddingsOptions = new EmbeddingsOptions(
213+
Arrays.asList("Your text string goes here"));
214+
215+
Embeddings embeddings = client.getEmbeddings("{deploymentOrModelId}", embeddingsOptions);
216+
217+
for (EmbeddingItem item : embeddings.getData()) {
218+
System.out.printf("Index: %d.%n", item.getIndex());
219+
for (Double embedding : item.getEmbedding()) {
220+
System.out.printf("%f;", embedding);
221+
}
222+
}
223+
```
224+
Please refer to the service documentation for a conceptual discussion of [openAI embedding][microsoft_docs_openai_embedding].
225+
226+
## Troubleshooting
227+
### Enable client logging
228+
You can set the `AZURE_LOG_LEVEL` environment variable to view logging statements made in the client library. For
229+
example, setting `AZURE_LOG_LEVEL=2` would show all informational, warning, and error log messages. The log levels can
230+
be found here: [log levels][logLevels].
231+
232+
### Default HTTP Client
233+
All client libraries by default use the Netty HTTP client. Adding the above dependency will automatically configure
234+
the client library to use the Netty HTTP client. Configuring or changing the HTTP client is detailed in the
235+
[HTTP clients wiki](https://github.com/Azure/azure-sdk-for-java/wiki/HTTP-clients).
236+
237+
### Default SSL library
238+
All client libraries, by default, use the Tomcat-native Boring SSL library to enable native-level performance for SSL
239+
operations. The Boring SSL library is an uber jar containing native libraries for Linux / macOS / Windows, and provides
240+
better performance compared to the default SSL implementation within the JDK. For more information, including how to
241+
reduce the dependency size, refer to the [performance tuning][performance_tuning] section of the wiki.
242+
243+
## Next steps
244+
- Samples are explained in detail [here][samples_readme].
245+
246+
## Contributing
247+
248+
For details on contributing to this repository, see the [contributing guide](https://github.com/Azure/azure-sdk-for-java/blob/main/CONTRIBUTING.md).
249+
250+
1. Fork it
251+
1. Create your feature branch (`git checkout -b my-new-feature`)
252+
1. Commit your changes (`git commit -am 'Add some feature'`)
253+
1. Push to the branch (`git push origin my-new-feature`)
254+
1. Create new Pull Request
255+
256+
<!-- LINKS -->
257+
[aad_authorization]: https://docs.microsoft.com/azure/cognitive-services/authentication#authenticate-with-azure-active-directory
258+
[azure_identity]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/identity/azure-identity
259+
[azure_identity_credential_type]: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/identity/azure-identity#credentials
260+
[azure_openai_access]: https://learn.microsoft.com/azure/cognitive-services/openai/overview#how-do-i-get-access-to-azure-openai
261+
[azure_subscription]: https://azure.microsoft.com/free/
262+
[docs]: https://azure.github.io/azure-sdk-for-java/
263+
[jdk]: https://docs.microsoft.com/java/azure/jdk/
264+
[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
265+
[microsoft_docs_openai_completion]: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/completions
266+
[microsoft_docs_openai_embedding]: https://learn.microsoft.com/azure/cognitive-services/openai/concepts/understand-embeddings
267+
[performance_tuning]: https://github.com/Azure/azure-sdk-for-java/wiki/Performance-Tuning
268+
[product_documentation]: https://azure.microsoft.com/services/
269+
[quickstart]: https://learn.microsoft.com/azure/cognitive-services/openai/quickstart
270+
[source_code]: https://github.com/Azure/azure-sdk-for-java/tree/feature/open-ai/sdk/openai/azure-ai-openai/src
271+
[samples_readme]: https://github.com/Azure/azure-sdk-for-java/tree/feature/open-ai/sdk/openai/azure-ai-openai/src/samples
272+
[openai_client_async]: https://github.com/Azure/azure-sdk-for-java/blob/feature/open-ai/sdk/openai/azure-ai-openai/src/main/java/com/azure/ai/openai/OpenAIAsyncClient.java
273+
[openai_client_builder]: https://github.com/Azure/azure-sdk-for-java/blob/feature/open-ai/sdk/openai/azure-ai-openai/src/main/java/com/azure/ai/openai/OpenAIClientBuilder.java
274+
[openai_client_sync]: https://github.com/Azure/azure-sdk-for-java/blob/feature/open-ai/sdk/openai/azure-ai-openai/src/main/java/com/azure/ai/openai/OpenAIClient.java
275+
[wiki_identity]: https://github.com/Azure/azure-sdk-for-java/wiki/Identity-and-Authentication
276+

0 commit comments

Comments
 (0)