Skip to content

Commit ebfc111

Browse files
authored
Supports automatic resource release (#13)
2 parents 00d4208 + 62a8d86 commit ebfc111

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: Azure OpenAI
3+
---
4+
5+
!!! note
6+
7+
Support the Open Ai service provided by Microsoft, product address: [https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/](https://azure.microsoft.com/zh-cn/products/cognitive-services/openai-service/)
8+
9+
### Required
10+
11+
---
12+
13+
!!! note
14+
15+
The following are some configurations that must be specified to invoke the service.
16+
17+
| name | description |
18+
|:----------:|-----------------------------------------------------------------------------|
19+
| `apiHost` | Created zone markers in the format `${your-resource-name}.openai.azure.com` |
20+
| `apiKey` | Azure token |
21+
| `provider` | Specify `ProviderModel.azure` |
22+
| `model` | Model name deployed in Azure |
23+
| `version` | Model version deployed in Azure |
24+
25+
### Example
26+
27+
---
28+
29+
```java
30+
// Automatic resource release
31+
try(OpenAiClient client=OpenAiClient.builder()
32+
.apiHost("https://eus-chatgpt.openai.azure.com")
33+
.apiKey(System.getProperty("azure.token"))
34+
.provider(ProviderModel.azure)
35+
.model("text-davinci-002")
36+
.version("2022-12-01")
37+
.build())
38+
{
39+
client.createCompletion(configure).getChoices();
40+
}
41+
```
42+

docs/mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,7 @@ nav:
4848
- reference/models.md
4949
- reference/completions.md
5050
- reference/completions_chat.md
51+
- Provider:
52+
- reference/provider/azure.md
5153
- released.md
5254
- powered_by.md

src/main/java/org/devlive/sdk/openai/DefaultClient.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.devlive.sdk.openai;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import okhttp3.OkHttpClient;
5+
import org.apache.commons.lang3.ObjectUtils;
46
import org.devlive.sdk.openai.entity.CompletionChatEntity;
57
import org.devlive.sdk.openai.entity.CompletionEntity;
68
import org.devlive.sdk.openai.entity.ModelEntity;
@@ -14,10 +16,11 @@
1416
import org.devlive.sdk.openai.utils.ProviderUtils;
1517

1618
@Slf4j
17-
public abstract class DefaultClient
19+
public abstract class DefaultClient implements AutoCloseable
1820
{
1921
protected DefaultApi api;
2022
protected ProviderModel provider;
23+
protected OkHttpClient client;
2124

2225
public ModelResponse getModels()
2326
{
@@ -54,4 +57,13 @@ public UserKeyResponse createUserAPIKey(UserKeyEntity configure)
5457
return this.api.fetchCreateUserAPIKey(configure)
5558
.blockingGet();
5659
}
60+
61+
public void close()
62+
{
63+
if (ObjectUtils.isNotEmpty(this.client)) {
64+
this.client.dispatcher().cancelAll();
65+
this.client.connectionPool().evictAll();
66+
this.client = null;
67+
}
68+
}
5769
}

src/main/java/org/devlive/sdk/openai/OpenAiClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private OpenAiClient(OpenAiClientBuilder builder)
7070
}
7171

7272
super.provider = builder.provider;
73+
super.client = builder.client;
7374
// Build a remote API client
7475
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
7576
this.api = new Retrofit.Builder()

src/test/java/org/devlive/sdk/openai/OpenAiClientTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ public void testClient()
5656
@Test
5757
public void testNoAuthorized()
5858
{
59-
client = OpenAiClient.builder()
59+
try (OpenAiClient client = OpenAiClient.builder()
6060
.apiKey(invalidApiKey)
61-
.build();
62-
Assert.assertThrows(AuthorizedException.class, () -> client.getModels());
61+
.build()) {
62+
Assert.assertThrows(AuthorizedException.class, () -> client.getModels());
63+
}
6364
}
6465

6566
@Test

0 commit comments

Comments
 (0)