Skip to content

Commit 5df625a

Browse files
author
hexiaochun
committed
feat: optimize example
1 parent 93fbb24 commit 5df625a

File tree

8 files changed

+57
-38
lines changed

8 files changed

+57
-38
lines changed
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.volcengine.ark.runtime.service;
22

33

4+
import okhttp3.ConnectionPool;
5+
import okhttp3.Dispatcher;
6+
47
import java.time.Duration;
8+
import java.util.concurrent.TimeUnit;
59

610

711
/**
812
* The interface ark service.
913
*/
1014
public abstract class ArkBaseService {
1115

12-
static final String BASE_URL = "https://ark.cn-beijing.volces.com";
13-
static final String BASE_REGION = "cn-beijing";
14-
static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(10);
15-
static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofMinutes(1);
16-
String apiKey = "";
17-
String ak = "";
18-
String sk = "";
19-
16+
public static final String BASE_URL = "https://ark.cn-beijing.volces.com";
17+
public static final String BASE_REGION = "cn-beijing";
18+
public static final Duration DEFAULT_TIMEOUT = Duration.ofMinutes(10);
19+
public static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofMinutes(1);
20+
public static final ConnectionPool DEFAULT_CONNECTION_POOL = new ConnectionPool(5, 1, TimeUnit.SECONDS);
21+
public static final Dispatcher DEFAULT_DISPATCHER = new Dispatcher();
2022
}

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/service/ArkService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,13 @@ public ArkService build() {
314314
if (connectionPool != null) {
315315
clientBuilder.connectionPool(connectionPool);
316316
} else {
317-
clientBuilder.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS));
317+
clientBuilder.connectionPool(DEFAULT_CONNECTION_POOL);
318318
}
319319

320320
if (dispatcher != null) {
321321
clientBuilder.dispatcher(dispatcher);
322+
} else {
323+
clientBuilder.dispatcher(DEFAULT_DISPATCHER);
322324
}
323325

324326
OkHttpClient client = clientBuilder

volcengine-java-sdk-ark-runtime/test/java/com/volcengine/ark/runtime/BotChatCompletionsExample.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33

44
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
55
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
6-
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
76
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
87
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
98
import com.volcengine.ark.runtime.service.ArkService;
109

1110
import java.util.ArrayList;
1211
import java.util.List;
1312

13+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_CONNECTION_POOL;
14+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_DISPATCHER;
15+
1416
/*
1517
# pom.xml
1618
<dependency>
@@ -37,11 +39,11 @@ public class BotChatCompletionsExample {
3739
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
3840
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
3941
*/
40-
public static void main(String[] args) {
4142

42-
String apiKey = System.getenv("ARK_API_KEY");
43-
ArkService service = new ArkService(apiKey);
43+
static String apiKey = System.getenv("ARK_API_KEY");
44+
static ArkService service = ArkService.builder().dispatcher(DEFAULT_DISPATCHER).connectionPool(DEFAULT_CONNECTION_POOL).apiKey(apiKey).build();
4445

46+
public static void main(String[] args) {
4547
System.out.println("\n----- standard request -----");
4648
final List<ChatMessage> messages = new ArrayList<>();
4749
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
@@ -84,7 +86,7 @@ public static void main(String[] args) {
8486
}
8587
);
8688

87-
// shutdown service
89+
// shutdown service after all requests is finished
8890
service.shutdownExecutor();
8991
}
9092

volcengine-java-sdk-ark-runtime/test/java/com/volcengine/ark/runtime/ChatCompletionsExample.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.util.ArrayList;
1010
import java.util.List;
1111

12+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_CONNECTION_POOL;
13+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_DISPATCHER;
14+
1215
/*
1316
# pom.xml
1417
<dependency>
@@ -35,11 +38,11 @@ public class ChatCompletionsExample {
3538
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
3639
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
3740
*/
38-
public static void main(String[] args) {
3941

40-
String apiKey = System.getenv("ARK_API_KEY");
41-
ArkService service = new ArkService(apiKey);
42+
static String apiKey = System.getenv("ARK_API_KEY");
43+
static ArkService service = ArkService.builder().dispatcher(DEFAULT_DISPATCHER).connectionPool(DEFAULT_CONNECTION_POOL).apiKey(apiKey).build();
4244

45+
public static void main(String[] args) {
4346
System.out.println("\n----- standard request -----");
4447
final List<ChatMessage> messages = new ArrayList<>();
4548
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
@@ -76,7 +79,7 @@ public static void main(String[] args) {
7679
}
7780
);
7881

79-
// shutdown service
82+
// shutdown service after all requests is finished
8083
service.shutdownExecutor();
8184
}
8285

volcengine-java-sdk-ark-runtime/test/java/com/volcengine/ark/runtime/ChatCompletionsFunctionCallExample.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import java.util.*;
88

9+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_CONNECTION_POOL;
10+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_DISPATCHER;
11+
912
public class ChatCompletionsFunctionCallExample {
1013

1114
/**
@@ -23,12 +26,12 @@ public class ChatCompletionsFunctionCallExample {
2326
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
2427
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
2528
*/
26-
public static void main(String[] args) {
27-
String ak = System.getenv("VOLC_ACCESSKEY");
28-
String sk = System.getenv("VOLC_SECRETKEY");
29-
ArkService service = new ArkService(ak, sk);
3029

31-
System.out.println("\nCreating completion...");
30+
static String apiKey = System.getenv("ARK_API_KEY");
31+
static ArkService service = ArkService.builder().dispatcher(DEFAULT_DISPATCHER).connectionPool(DEFAULT_CONNECTION_POOL).apiKey(apiKey).build();
32+
33+
public static void main(String[] args) {
34+
System.out.println("\n----- function call request -----");
3235
final List<ChatMessage> messages = new ArrayList<>();
3336
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("What's the weather like in Boston today?").build();
3437
messages.add(userMessage);
@@ -66,7 +69,7 @@ public static void main(String[] args) {
6669
.doOnError(Throwable::printStackTrace)
6770
.blockingForEach(System.out::println);
6871

69-
// shutdown service
72+
// shutdown service after all requests is finished
7073
service.shutdownExecutor();
7174
}
7275

volcengine-java-sdk-ark-runtime/test/java/com/volcengine/ark/runtime/ChatCompletionsVisionExample.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.util.ArrayList;
1010
import java.util.List;
1111

12+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_CONNECTION_POOL;
13+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_DISPATCHER;
14+
1215
/*
1316
# pom.xml
1417
<dependency>
@@ -35,11 +38,11 @@ public class ChatCompletionsVisionExample {
3538
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
3639
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
3740
*/
38-
public static void main(String[] args) {
3941

40-
String apiKey = System.getenv("ARK_API_KEY");
41-
ArkService service = new ArkService(apiKey);
42+
static String apiKey = System.getenv("ARK_API_KEY");
43+
static ArkService service = ArkService.builder().dispatcher(DEFAULT_DISPATCHER).connectionPool(DEFAULT_CONNECTION_POOL).apiKey(apiKey).build();
4244

45+
public static void main(String[] args) {
4346
System.out.println("----- image input -----");
4447
final List<ChatMessage> messages = new ArrayList<>();
4548
final List<ChatCompletionContentPart> multiParts = new ArrayList<>();
@@ -62,7 +65,7 @@ public static void main(String[] args) {
6265

6366
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));
6467

65-
// shutdown service
68+
// shutdown service after all requests is finished
6669
service.shutdownExecutor();
6770
}
6871

volcengine-java-sdk-ark-runtime/test/java/com/volcengine/ark/runtime/EmbeddingsExample.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.util.ArrayList;
99
import java.util.List;
1010

11+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_CONNECTION_POOL;
12+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_DISPATCHER;
13+
1114
public class EmbeddingsExample {
1215

1316
/**
@@ -25,11 +28,11 @@ public class EmbeddingsExample {
2528
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
2629
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
2730
*/
28-
public static void main(String[] args) {
2931

30-
String apiKey = System.getenv("ARK_API_KEY");
31-
ArkService service = new ArkService(apiKey);
32+
static String apiKey = System.getenv("ARK_API_KEY");
33+
static ArkService service = ArkService.builder().dispatcher(DEFAULT_DISPATCHER).connectionPool(DEFAULT_CONNECTION_POOL).apiKey(apiKey).build();
3234

35+
public static void main(String[] args) {
3336
System.out.println("\n----- embeddings request -----");
3437

3538
List<String> inputs = new ArrayList<>();
@@ -42,7 +45,7 @@ public static void main(String[] args) {
4245
EmbeddingResult res = service.createEmbeddings(chatCompletionRequest);
4346
System.out.println(res);
4447

45-
// shutdown service
48+
// shutdown service after all requests is finished
4649
service.shutdownExecutor();
4750
}
4851

volcengine-java-sdk-ark-runtime/test/java/com/volcengine/ark/runtime/TokenizationExample.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.volcengine.ark.runtime;
22

33

4-
import com.volcengine.ark.runtime.model.embeddings.EmbeddingRequest;
5-
import com.volcengine.ark.runtime.model.embeddings.EmbeddingResult;
64
import com.volcengine.ark.runtime.model.tokenization.TokenizationRequest;
75
import com.volcengine.ark.runtime.model.tokenization.TokenizationResult;
86
import com.volcengine.ark.runtime.service.ArkService;
97

108
import java.util.ArrayList;
119
import java.util.List;
1210

11+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_CONNECTION_POOL;
12+
import static com.volcengine.ark.runtime.service.ArkBaseService.DEFAULT_DISPATCHER;
13+
1314
public class TokenizationExample {
1415

1516
/**
@@ -27,11 +28,11 @@ public class TokenizationExample {
2728
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
2829
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
2930
*/
30-
public static void main(String[] args) {
3131

32-
String apiKey = System.getenv("ARK_API_KEY");
33-
ArkService service = new ArkService(apiKey);
32+
static String apiKey = System.getenv("ARK_API_KEY");
33+
static ArkService service = ArkService.builder().dispatcher(DEFAULT_DISPATCHER).connectionPool(DEFAULT_CONNECTION_POOL).apiKey(apiKey).build();
3434

35+
public static void main(String[] args) {
3536
System.out.println("\n----- tokenization request -----");
3637
List<String> texts = new ArrayList<>();
3738
texts.add("花椰菜又称菜花、花菜,是一种常见的蔬菜。");
@@ -43,7 +44,7 @@ public static void main(String[] args) {
4344
TokenizationResult res = service.createTokenization(tokenizationRequest);
4445
System.out.println(res);
4546

46-
// shutdown service
47+
// shutdown service after all requests is finished
4748
service.shutdownExecutor();
4849
}
4950

0 commit comments

Comments
 (0)