Skip to content

Commit 25db762

Browse files
author
潘婉宁
committed
chore: add reasoning example
1 parent c8c1630 commit 25db762

File tree

2 files changed

+106
-9
lines changed

2 files changed

+106
-9
lines changed

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/model/completion/chat/ChatMessage.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ public void setContent(Object content) {
5151
this.content = content;
5252
}
5353

54-
public Object getReasoningContent() {
55-
return reasoningContent;
56-
}
57-
58-
public void setReasoningContent(String reasoningContent) {
59-
this.reasoningContent = reasoningContent;
60-
}
61-
6254

6355
public String getName() {
6456
return name;
@@ -92,6 +84,14 @@ public void setToolCallId(String toolCallId) {
9284
this.toolCallId = toolCallId;
9385
}
9486

87+
public String getReasoningContent() {
88+
return reasoningContent;
89+
}
90+
91+
public void setReasoningContent(String reasoningContent) {
92+
this.reasoningContent = reasoningContent;
93+
}
94+
9595
public static Builder builder() {
9696
return new Builder();
9797
}
@@ -100,7 +100,7 @@ public static Builder builder() {
100100
public String toString() {
101101
return "ChatMessage{" +
102102
"role=" + role +
103-
", content='" + content + '\'' +
103+
", content=" + content +
104104
", reasoningContent='" + reasoningContent + '\'' +
105105
", name='" + name + '\'' +
106106
", functionCall=" + functionCall +
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.volcengine.ark.runtime;
2+
3+
4+
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
5+
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
6+
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
7+
import com.volcengine.ark.runtime.service.ArkService;
8+
import okhttp3.ConnectionPool;
9+
import okhttp3.Dispatcher;
10+
import org.apache.commons.lang.StringUtils;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.concurrent.TimeUnit;
15+
16+
/*
17+
# pom.xml
18+
<dependency>
19+
<groupId>com.volcengine</groupId>
20+
<artifactId>volcengine-java-sdk-ark-runtime</artifactId>
21+
<version>LATEST</version>
22+
</dependency>
23+
*/
24+
25+
public class ChatCompletionsReasoningExample {
26+
27+
/**
28+
* Authentication
29+
* 1.If you authorize your endpoint using an API key, you can set your api key to environment variable "ARK_API_KEY"
30+
* String apiKey = System.getenv("ARK_API_KEY");
31+
* ArkService service = new ArkService(apiKey);
32+
* Note: If you use an API key, this API key will not be refreshed.
33+
* To prevent the API from expiring and failing after some time, choose an API key with no expiration date.
34+
*
35+
* 2.If you authorize your endpoint with Volcengine Identity and Access Management(IAM), set your api key to environment variable "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
36+
* String ak = System.getenv("VOLC_ACCESSKEY");
37+
* String sk = System.getenv("VOLC_SECRETKEY");
38+
* ArkService service = new ArkService(ak, sk);
39+
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
40+
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
41+
*/
42+
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();
47+
48+
public static void main(String[] args) {
49+
System.out.println("\n----- standard request -----");
50+
final List<ChatMessage> messages = new ArrayList<>();
51+
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("How many Rs are there in the word 'strawberry'?").build();
52+
messages.add(userMessage);
53+
54+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
55+
.model("${YOUR_ENDPOINT_ID}")
56+
.messages(messages)
57+
.build();
58+
59+
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(
60+
choice-> {
61+
if(StringUtils.isNotEmpty(choice.getMessage().getReasoningContent())){
62+
System.out.println(choice.getMessage().getReasoningContent());
63+
}else{
64+
System.out.println(choice.getMessage().getContent());
65+
}
66+
}
67+
);
68+
69+
System.out.println("\n----- streaming request -----");
70+
final List<ChatMessage> streamMessages = new ArrayList<>();
71+
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("How many Rs are there in the word 'strawberry'?").build();
72+
streamMessages.add(streamUserMessage);
73+
74+
ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
75+
.model("${YOUR_ENDPOINT_ID}")
76+
.messages(streamMessages)
77+
.build();
78+
79+
service.streamChatCompletion(streamChatCompletionRequest)
80+
.doOnError(Throwable::printStackTrace)
81+
.blockingForEach(
82+
delta -> {
83+
if (!delta.getChoices().isEmpty()) {
84+
if(StringUtils.isNotEmpty(delta.getChoices().get(0).getMessage().getReasoningContent())){
85+
System.out.print(delta.getChoices().get(0).getMessage().getReasoningContent());
86+
}else{
87+
System.out.print(delta.getChoices().get(0).getMessage().getContent());
88+
}
89+
}
90+
}
91+
);
92+
93+
// shutdown service after all requests is finished
94+
service.shutdownExecutor();
95+
}
96+
97+
}

0 commit comments

Comments
 (0)