Skip to content

Commit c0c8aad

Browse files
feat(arkruntime): support rolling tokens
1 parent 43f9493 commit c0c8aad

File tree

5 files changed

+88
-22
lines changed

5 files changed

+88
-22
lines changed

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/Const.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ public class Const {
1515

1616
public static final String RESOURCE_TYPE_BOT = "bot";
1717
public static final String RESOURCE_TYPE_ENDPOINT = "endpoint";
18+
19+
public static final String CONTEXT_MODE_SESSION = "session";
20+
public static final String TRUNCATION_STRATEGY_TYPE_LAST_HISTORY_TOKENS = "last_history_tokens";
21+
public static final String TRUNCATION_STRATEGY_TYPE_ROLLING_TOKENS = "rolling_tokens";
1822
}

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/model/context/CreateContextRequest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class CreateContextRequest {
1212
@JsonProperty("model")
1313
private String model;
1414

15+
@JsonProperty("mode")
16+
private String mode;
17+
1518
@JsonProperty("messages")
1619
private List<ChatMessage> messages;
1720

@@ -24,8 +27,9 @@ public class CreateContextRequest {
2427
public CreateContextRequest() {
2528
}
2629

27-
public CreateContextRequest(String model, List<ChatMessage> messages, Integer ttl, TruncationStrategy truncationStrategy) {
30+
public CreateContextRequest(String model, String mode, List<ChatMessage> messages, Integer ttl, TruncationStrategy truncationStrategy) {
2831
this.model = model;
32+
this.mode = mode;
2933
this.messages = messages;
3034
this.ttl = ttl;
3135
this.truncationStrategy = truncationStrategy;
@@ -39,6 +43,14 @@ public void setModel(String model) {
3943
this.model = model;
4044
}
4145

46+
public String getMode() {
47+
return mode;
48+
}
49+
50+
public void setMode(String mode) {
51+
this.mode = mode;
52+
}
53+
4254
public List<ChatMessage> getMessages() {
4355
return messages;
4456
}
@@ -67,6 +79,7 @@ public void setTruncationStrategy(TruncationStrategy truncationStrategy) {
6779
public String toString() {
6880
return "CreateContextRequest{" +
6981
"model='" + model + '\'' +
82+
", mode='" + mode + '\'' +
7083
", messages=" + messages +
7184
", ttl=" + ttl +
7285
", truncationStrategy=" + truncationStrategy +
@@ -79,6 +92,7 @@ public static CreateContextRequest.Builder builder() {
7992

8093
public static class Builder {
8194
private String model;
95+
private String mode;
8296
private List<ChatMessage> messages;
8397
private Integer ttl;
8498
private TruncationStrategy truncationStrategy;
@@ -91,6 +105,11 @@ public Builder model(String model) {
91105
return this;
92106
}
93107

108+
public Builder mode(String mode) {
109+
this.mode = mode;
110+
return this;
111+
}
112+
94113
public Builder messages(List<ChatMessage> messages) {
95114
this.messages = messages;
96115
return this;
@@ -109,6 +128,7 @@ public Builder truncationStrategy(TruncationStrategy truncationStrategy) {
109128
public CreateContextRequest build() {
110129
CreateContextRequest createContextRequest = new CreateContextRequest();
111130
createContextRequest.setModel(model);
131+
createContextRequest.setMode(mode);
112132
createContextRequest.setMessages(messages);
113133
createContextRequest.setTtl(ttl);
114134
createContextRequest.setTruncationStrategy(truncationStrategy);

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/model/context/CreateContextResult.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.volcengine.ark.runtime.model.Usage;
56

67
@JsonIgnoreProperties(ignoreUnknown = true)
78
public class CreateContextResult {
@@ -12,12 +13,18 @@ public class CreateContextResult {
1213
@JsonProperty("model")
1314
private String model;
1415

16+
@JsonProperty("mode")
17+
private String mode;
18+
1519
@JsonProperty("ttl")
1620
private Integer ttl;
1721

1822
@JsonProperty("truncation_strategy")
1923
private TruncationStrategy truncationStrategy;
2024

25+
@JsonProperty("usage")
26+
private Usage usage;
27+
2128
public String getId() {
2229
return id;
2330
}
@@ -34,6 +41,14 @@ public void setModel(String model) {
3441
this.model = model;
3542
}
3643

44+
public String getMode() {
45+
return mode;
46+
}
47+
48+
public void setMode(String mode) {
49+
this.mode = mode;
50+
}
51+
3752
public Integer getTtl() {
3853
return ttl;
3954
}
@@ -50,13 +65,23 @@ public void setTruncationStrategy(TruncationStrategy truncationStrategy) {
5065
this.truncationStrategy = truncationStrategy;
5166
}
5267

68+
public Usage getUsage() {
69+
return usage;
70+
}
71+
72+
public void setUsage(Usage usage) {
73+
this.usage = usage;
74+
}
75+
5376
@Override
5477
public String toString() {
5578
return "CreateContextResult{" +
5679
"id='" + id + '\'' +
5780
", model='" + model + '\'' +
81+
", mode='" + mode + '\'' +
5882
", ttl=" + ttl +
5983
", truncationStrategy=" + truncationStrategy +
84+
", usage=" + usage +
6085
'}';
6186
}
6287
}

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/model/context/TruncationStrategy.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,53 @@
66
@JsonIgnoreProperties(ignoreUnknown = true)
77
public class TruncationStrategy {
88

9-
public static final String TRUNCATION_STRATEGY_TYPE_LAST_HISTORY_TOKENS = "last_history_tokens";
10-
119
@JsonProperty("type")
12-
private String Type;
10+
private String type;
1311

1412
@JsonProperty("last_history_tokens")
15-
private Integer LastHistoryTokens;
13+
private Integer lastHistoryTokens;
14+
15+
@JsonProperty("rolling_tokens")
16+
private Boolean rollingTokens;
1617

1718
public TruncationStrategy() {
1819
}
1920

2021
public TruncationStrategy(String type, Integer lastHistoryTokens) {
21-
Type = type;
22-
LastHistoryTokens = lastHistoryTokens;
22+
this.type = type;
23+
this.lastHistoryTokens = lastHistoryTokens;
2324
}
2425

2526
public String getType() {
26-
return Type;
27+
return type;
2728
}
2829

2930
public void setType(String type) {
30-
Type = type;
31+
this.type = type;
3132
}
3233

3334
public Integer getLastHistoryTokens() {
34-
return LastHistoryTokens;
35+
return lastHistoryTokens;
3536
}
3637

3738
public void setLastHistoryTokens(Integer lastHistoryTokens) {
38-
LastHistoryTokens = lastHistoryTokens;
39+
this.lastHistoryTokens = lastHistoryTokens;
40+
}
41+
42+
public Boolean getRollingTokens() {
43+
return rollingTokens;
44+
}
45+
46+
public void setRollingTokens(Boolean rollingTokens) {
47+
this.rollingTokens = rollingTokens;
3948
}
4049

4150
@Override
4251
public String toString() {
4352
return "TruncationStrategy{" +
44-
"Type='" + Type + '\'' +
45-
", LastHistoryTokens=" + LastHistoryTokens +
53+
"type='" + type + '\'' +
54+
", lastHistoryTokens=" + lastHistoryTokens +
55+
", rollingTokens=" + rollingTokens +
4656
'}';
4757
}
4858

@@ -51,26 +61,33 @@ public static TruncationStrategy.Builder builder() {
5161
}
5262

5363
public static class Builder {
54-
private Integer LastHistoryTokens;
55-
private String Type;
64+
private Integer lastHistoryTokens;
65+
private String type;
66+
private Boolean rollingTokens;
5667

5768
private Builder() {
5869
}
5970

60-
public Builder LastHistoryTokens(Integer LastHistoryTokens) {
61-
this.LastHistoryTokens = LastHistoryTokens;
71+
public Builder lastHistoryTokens(Integer LastHistoryTokens) {
72+
this.lastHistoryTokens = LastHistoryTokens;
73+
return this;
74+
}
75+
76+
public Builder type(String Type) {
77+
this.type = Type;
6278
return this;
6379
}
6480

65-
public Builder Type(String Type) {
66-
this.Type = Type;
81+
public Builder rollingTokens(Boolean RollingTokens) {
82+
this.rollingTokens = RollingTokens;
6783
return this;
6884
}
6985

7086
public TruncationStrategy build() {
7187
TruncationStrategy truncationStrategy = new TruncationStrategy();
72-
truncationStrategy.setLastHistoryTokens(LastHistoryTokens);
73-
truncationStrategy.setType(Type);
88+
truncationStrategy.setLastHistoryTokens(lastHistoryTokens);
89+
truncationStrategy.setType(type);
90+
truncationStrategy.setRollingTokens(rollingTokens);
7491
return truncationStrategy;
7592
}
7693
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public static void main(String[] args) {
4141
System.out.println("\n----- create context -----");
4242
CreateContextRequest createContextRequest = CreateContextRequest.builder()
4343
.model("${YOUR_ENDPOINT_ID}")
44+
.mode(Const.CONTEXT_MODE_SESSION)
4445
.messages(Collections.singletonList(ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build()))
4546
.ttl(3600)
46-
.truncationStrategy(TruncationStrategy.builder().Type(TruncationStrategy.TRUNCATION_STRATEGY_TYPE_LAST_HISTORY_TOKENS).LastHistoryTokens(4096).build())
4747
.build();
4848

4949
CreateContextResult createContextResult = service.createContext(createContextRequest);

0 commit comments

Comments
 (0)