Skip to content

Commit 5e46470

Browse files
author
BitsAdmin
committed
Merge branch 'feat/ark/fix_example' into 'integration_2024-12-05_629870411266'
feat: [development task] ark-runtime-manual-Java (896944) See merge request iaasng/volcengine-java-sdk!312
2 parents 70d762b + bb0b6c8 commit 5e46470

File tree

8 files changed

+79
-38
lines changed

8 files changed

+79
-38
lines changed

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/interceptor/RequestIdInterceptor.java

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

33
import com.volcengine.ark.runtime.Const;
4+
import com.volcengine.version.Version;
45
import okhttp3.Interceptor;
56
import okhttp3.Request;
67
import okhttp3.Response;
@@ -22,6 +23,7 @@ public Response intercept(Chain chain) throws IOException {
2223
if (chain.request().header(Const.CLIENT_REQUEST_HEADER) == null || chain.request().header(Const.CLIENT_REQUEST_HEADER).length() == 0) {
2324
requestBuilder = requestBuilder.header(Const.CLIENT_REQUEST_HEADER, genRequestId());
2425
}
26+
requestBuilder.header("User-Agent", getUserAgent());
2527

2628
Request request = requestBuilder.build();
2729
return chain.proceed(request);
@@ -32,4 +34,14 @@ private String genRequestId() {
3234
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
3335
return dateFormat.format(date) + RandomStringUtils.randomAlphanumeric(20);
3436
}
37+
38+
private static String getUserAgent() {
39+
String format = "%s/%s/(%s;%s;%s)";
40+
41+
String osInfo = System.getProperty("os.name") + "-" + System.getProperty("os.version");
42+
String jdkInfo = "java-" + System.getProperty("java.version");
43+
String arch = System.getProperty("os.arch");
44+
45+
return String.format(format, Version.SDK_NAME, Version.SDK_VERSION, jdkInfo, osInfo, arch);
46+
}
3547
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
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);
2020
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
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;
9+
import okhttp3.ConnectionPool;
10+
import okhttp3.Dispatcher;
1011

1112
import java.util.ArrayList;
1213
import java.util.List;
14+
import java.util.concurrent.TimeUnit;
1315

1416
/*
1517
# pom.xml
@@ -37,11 +39,13 @@ 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 ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
45+
static Dispatcher dispatcher = new Dispatcher();
46+
static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).apiKey(apiKey).build();
4447

48+
public static void main(String[] args) {
4549
System.out.println("\n----- standard request -----");
4650
final List<ChatMessage> messages = new ArrayList<>();
4751
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
@@ -57,7 +61,9 @@ public static void main(String[] args) {
5761
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);
5862
chatCompletionResult.getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));
5963
// the references example
60-
chatCompletionResult.getReferences().forEach(ref -> System.out.println(ref.getUrl()));
64+
if (chatCompletionResult.getReferences() != null) {
65+
chatCompletionResult.getReferences().forEach(ref -> System.out.println(ref.getUrl()));
66+
}
6167

6268
System.out.println("\n----- streaming request -----");
6369
final List<ChatMessage> streamMessages = new ArrayList<>();
@@ -84,7 +90,7 @@ public static void main(String[] args) {
8490
}
8591
);
8692

87-
// shutdown service
93+
// shutdown service after all requests is finished
8894
service.shutdownExecutor();
8995
}
9096

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
66
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
77
import com.volcengine.ark.runtime.service.ArkService;
8+
import okhttp3.ConnectionPool;
9+
import okhttp3.Dispatcher;
810

911
import java.util.ArrayList;
1012
import java.util.List;
13+
import java.util.concurrent.TimeUnit;
1114

1215
/*
1316
# pom.xml
@@ -35,11 +38,13 @@ 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 ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
44+
static Dispatcher dispatcher = new Dispatcher();
45+
static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).apiKey(apiKey).build();
4246

47+
public static void main(String[] args) {
4348
System.out.println("\n----- standard request -----");
4449
final List<ChatMessage> messages = new ArrayList<>();
4550
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
@@ -76,7 +81,7 @@ public static void main(String[] args) {
7681
}
7782
);
7883

79-
// shutdown service
84+
// shutdown service after all requests is finished
8085
service.shutdownExecutor();
8186
}
8287

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
import com.volcengine.ark.runtime.model.completion.chat.*;
55
import com.volcengine.ark.runtime.service.ArkService;
6+
import okhttp3.ConnectionPool;
7+
import okhttp3.Dispatcher;
68

79
import java.util.*;
10+
import java.util.concurrent.TimeUnit;
811

912
public class ChatCompletionsFunctionCallExample {
1013

@@ -23,12 +26,14 @@ 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 ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
32+
static Dispatcher dispatcher = new Dispatcher();
33+
static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).apiKey(apiKey).build();
34+
35+
public static void main(String[] args) {
36+
System.out.println("\n----- function call request -----");
3237
final List<ChatMessage> messages = new ArrayList<>();
3338
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("What's the weather like in Boston today?").build();
3439
messages.add(userMessage);
@@ -66,7 +71,7 @@ public static void main(String[] args) {
6671
.doOnError(Throwable::printStackTrace)
6772
.blockingForEach(System.out::println);
6873

69-
// shutdown service
74+
// shutdown service after all requests is finished
7075
service.shutdownExecutor();
7176
}
7277

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
66
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
77
import com.volcengine.ark.runtime.service.ArkService;
8+
import okhttp3.ConnectionPool;
9+
import okhttp3.Dispatcher;
810

911
import java.util.ArrayList;
1012
import java.util.List;
13+
import java.util.concurrent.TimeUnit;
1114

1215
/*
1316
# pom.xml
@@ -35,11 +38,13 @@ 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 ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
44+
static Dispatcher dispatcher = new Dispatcher();
45+
static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).apiKey(apiKey).build();
4246

47+
public static void main(String[] args) {
4348
System.out.println("----- image input -----");
4449
final List<ChatMessage> messages = new ArrayList<>();
4550
final List<ChatCompletionContentPart> multiParts = new ArrayList<>();
@@ -62,7 +67,7 @@ public static void main(String[] args) {
6267

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

65-
// shutdown service
70+
// shutdown service after all requests is finished
6671
service.shutdownExecutor();
6772
}
6873

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import com.volcengine.ark.runtime.model.embeddings.EmbeddingRequest;
55
import com.volcengine.ark.runtime.model.embeddings.EmbeddingResult;
66
import com.volcengine.ark.runtime.service.ArkService;
7+
import okhttp3.ConnectionPool;
8+
import okhttp3.Dispatcher;
79

810
import java.util.ArrayList;
911
import java.util.List;
12+
import java.util.concurrent.TimeUnit;
1013

1114
public class EmbeddingsExample {
1215

@@ -25,11 +28,13 @@ 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 ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
34+
static Dispatcher dispatcher = new Dispatcher();
35+
static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).apiKey(apiKey).build();
3236

37+
public static void main(String[] args) {
3338
System.out.println("\n----- embeddings request -----");
3439

3540
List<String> inputs = new ArrayList<>();
@@ -42,7 +47,7 @@ public static void main(String[] args) {
4247
EmbeddingResult res = service.createEmbeddings(chatCompletionRequest);
4348
System.out.println(res);
4449

45-
// shutdown service
50+
// shutdown service after all requests is finished
4651
service.shutdownExecutor();
4752
}
4853

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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;
7+
import okhttp3.ConnectionPool;
8+
import okhttp3.Dispatcher;
99

1010
import java.util.ArrayList;
1111
import java.util.List;
12+
import java.util.concurrent.TimeUnit;
1213

1314
public class TokenizationExample {
1415

@@ -27,11 +28,13 @@ 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 ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
34+
static Dispatcher dispatcher = new Dispatcher();
35+
static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).apiKey(apiKey).build();
3436

37+
public static void main(String[] args) {
3538
System.out.println("\n----- tokenization request -----");
3639
List<String> texts = new ArrayList<>();
3740
texts.add("花椰菜又称菜花、花菜,是一种常见的蔬菜。");
@@ -43,7 +46,7 @@ public static void main(String[] args) {
4346
TokenizationResult res = service.createTokenization(tokenizationRequest);
4447
System.out.println(res);
4548

46-
// shutdown service
49+
// shutdown service after all requests is finished
4750
service.shutdownExecutor();
4851
}
4952

0 commit comments

Comments
 (0)