Skip to content

Commit 9431364

Browse files
authored
feat(protocol): add user agent (#153)
1 parent a1740f6 commit 9431364

File tree

6 files changed

+76
-5
lines changed

6 files changed

+76
-5
lines changed

samples/GenerationStreamCall.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public static void streamCall()
3535
GenerationParam param =
3636
GenerationParam.builder()
3737
.model("qwen-turbo")
38-
.prompt("如何做土豆炖猪脚?")
38+
.prompt("你好")
3939
.temperature((float) 1.0)
40+
.incrementalOutput(false)
4041
.repetitionPenalty((float) 1.0)
4142
.topK(50)
4243
.build();

src/main/java/com/alibaba/dashscope/aigc/generation/Generation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ public Flowable<GenerationResult> streamCall(HalfDuplexServiceParam param)
162162
// Intercept and modify incrementalOutput parameter if needed
163163
boolean toMergeResponse = modifyIncrementalOutput(param);
164164

165+
// Build custom user agent suffix with incremental_to_full flag
166+
int flagValue = toMergeResponse ? 1 : 0;
167+
String userAgentSuffix = String.format("incremental_to_full/%d", flagValue);
168+
param.putHeader("user-agent", userAgentSuffix);
169+
165170
serviceOption.setIsSSE(true);
166171
serviceOption.setStreamingMode(StreamingMode.OUT);
167172
return syncApi.streamCall(param)
@@ -193,6 +198,11 @@ public void streamCall(HalfDuplexServiceParam param, ResultCallback<GenerationRe
193198
// Intercept and modify incrementalOutput parameter if needed
194199
boolean toMergeResponse = modifyIncrementalOutput(param);
195200

201+
// Build custom user agent suffix with incremental_to_full flag
202+
int flagValue = toMergeResponse ? 1 : 0;
203+
String userAgentSuffix = String.format("incremental_to_full/%d", flagValue);
204+
param.putHeader("user-agent", userAgentSuffix);
205+
196206
serviceOption.setIsSSE(true);
197207
serviceOption.setStreamingMode(StreamingMode.OUT);
198208
syncApi.streamCall(

src/main/java/com/alibaba/dashscope/aigc/multimodalconversation/MultiModalConversation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public Flowable<MultiModalConversationResult> streamCall(MultiModalConversationP
144144
// Intercept and modify incrementalOutput parameter if needed
145145
boolean toMergeResponse = modifyIncrementalOutput(param);
146146

147+
// Build custom user agent suffix with incremental_to_full flag
148+
int flagValue = toMergeResponse ? 1 : 0;
149+
String userAgentSuffix = String.format("incremental_to_full/%d", flagValue);
150+
param.putHeader("user-agent", userAgentSuffix);
151+
147152
serviceOption.setIsSSE(true);
148153
serviceOption.setStreamingMode(StreamingMode.OUT);
149154
preprocessInput(param);
@@ -181,6 +186,11 @@ public void streamCall(
181186
// Intercept and modify incrementalOutput parameter if needed
182187
boolean toMergeResponse = modifyIncrementalOutput(param);
183188

189+
// Build custom user agent suffix with incremental_to_full flag
190+
int flagValue = toMergeResponse ? 1 : 0;
191+
String userAgentSuffix = String.format("incremental_to_full/%d", flagValue);
192+
param.putHeader("user-agent", userAgentSuffix);
193+
184194
serviceOption.setIsSSE(true);
185195
serviceOption.setStreamingMode(StreamingMode.OUT);
186196
preprocessInput(param);

src/main/java/com/alibaba/dashscope/protocol/DashScopeHeaders.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,42 @@
1010

1111
public final class DashScopeHeaders {
1212
public static String userAgent() {
13+
return userAgent(null);
14+
}
15+
16+
// Generate user agent with optional custom suffix
17+
public static String userAgent(String customUserAgent) {
1318
String userAgent =
1419
String.format(
1520
"dashscope/%s; java/%s; platform/%s; processor/%s",
1621
Version.version,
1722
System.getProperty("java.version"),
1823
System.getProperty("os.name"),
1924
System.getProperty("os.arch"));
25+
if (customUserAgent != null && !customUserAgent.isEmpty()) {
26+
userAgent += "; " + customUserAgent;
27+
}
2028
return userAgent;
2129
}
2230

2331
public static Map<String, String> buildWebSocketHeaders(
2432
String apiKey, boolean isSecurityCheck, String workspace, Map<String, String> customHeaders)
2533
throws NoApiKeyException {
34+
return buildWebSocketHeaders(apiKey, isSecurityCheck, workspace,
35+
customHeaders, null);
36+
}
37+
38+
// Build WebSocket headers with optional custom user agent suffix
39+
public static Map<String, String> buildWebSocketHeaders(
40+
String apiKey,
41+
boolean isSecurityCheck,
42+
String workspace,
43+
Map<String, String> customHeaders,
44+
String customUserAgent)
45+
throws NoApiKeyException {
2646
Map<String, String> headers = new HashMap<>();
2747
headers.put("Authorization", "Bearer " + ApiKey.getApiKey(apiKey));
28-
headers.put("user-agent", userAgent());
48+
headers.put("user-agent", userAgent(customUserAgent));
2949
if (workspace != null && !workspace.isEmpty()) {
3050
headers.put("X-DashScope-WorkSpace", workspace);
3151
}
@@ -47,9 +67,24 @@ public static Map<String, String> buildHttpHeaders(
4767
String workspace,
4868
Map<String, String> customHeaders)
4969
throws NoApiKeyException {
70+
return buildHttpHeaders(apiKey, isSecurityCheck, protocol, isSSE,
71+
isAsyncTask, workspace, customHeaders, null);
72+
}
73+
74+
// Build HTTP headers with optional custom user agent suffix
75+
public static Map<String, String> buildHttpHeaders(
76+
String apiKey,
77+
Boolean isSecurityCheck,
78+
Protocol protocol,
79+
Boolean isSSE,
80+
Boolean isAsyncTask,
81+
String workspace,
82+
Map<String, String> customHeaders,
83+
String customUserAgent)
84+
throws NoApiKeyException {
5085
Map<String, String> headers = new HashMap<>();
5186
headers.put("Authorization", "Bearer " + ApiKey.getApiKey(apiKey));
52-
headers.put("user-agent", userAgent());
87+
headers.put("user-agent", userAgent(customUserAgent));
5388
if (isSecurityCheck) {
5489
headers.put("X-DashScope-DataInspection", "enable");
5590
}

src/main/java/com/alibaba/dashscope/protocol/HalfDuplexRequest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ private String getEncryptionKeyHeader(EncryptionConfig encryptionConfig) throws
8888
}
8989

9090
public HttpRequest getHttpRequest() throws NoApiKeyException, ApiException {
91+
// Extract and filter custom user agent from param headers
92+
Map<String, String> paramHeaders = param.getHeaders();
93+
String customUserAgent = paramHeaders != null ? paramHeaders.get("user-agent") : null;
94+
Map<String, String> filteredHeaders = paramHeaders != null ?
95+
new java.util.HashMap<>(paramHeaders) : new java.util.HashMap<>();
96+
filteredHeaders.remove("user-agent");
97+
9198
Map<String, String> requestHeaders =
9299
DashScopeHeaders.buildHttpHeaders(
93100
param.getApiKey(),
@@ -96,7 +103,8 @@ public HttpRequest getHttpRequest() throws NoApiKeyException, ApiException {
96103
serviceOption.getIsSSE(),
97104
serviceOption.getIsAsyncTask(),
98105
param.getWorkspace(),
99-
param.getHeaders());
106+
filteredHeaders,
107+
customUserAgent);
100108

101109
if (getHttpMethod() == HttpMethod.GET) {
102110
return HttpRequest.builder()

src/main/java/com/alibaba/dashscope/protocol/okhttp/OkHttpWebSocketClient.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,19 @@ private Request buildConnectionRequest(
6262
Map<String, String> customHeaders,
6363
String baseWebSocketUrl)
6464
throws NoApiKeyException {
65+
// Extract and filter custom user agent from param headers
66+
String customUserAgent = customHeaders != null ?
67+
customHeaders.get("user-agent") : null;
68+
Map<String, String> filteredHeaders = customHeaders != null ?
69+
new java.util.HashMap<>(customHeaders) : new java.util.HashMap<>();
70+
filteredHeaders.remove("user-agent");
71+
6572
// build the request builder.
6673
Builder bd = new Request.Builder();
6774
bd.headers(
6875
Headers.of(
6976
DashScopeHeaders.buildWebSocketHeaders(
70-
apiKey, isSecurityCheck, workspace, customHeaders)));
77+
apiKey, isSecurityCheck, workspace, filteredHeaders, customUserAgent)));
7178
String url = Constants.baseWebsocketApiUrl;
7279
if (baseWebSocketUrl != null) {
7380
url = baseWebSocketUrl;

0 commit comments

Comments
 (0)