Skip to content

Commit 6fc79e3

Browse files
author
BitsAdmin
committed
Merge branch 'feat/llm_stream_check' into 'integration_2025-05-08_883154236930'
feat: [development task] waf runtime (1207543) See merge request iaasng/volcengine-java-sdk!473
2 parents bab9426 + 3783592 commit 6fc79e3

File tree

3 files changed

+172
-54
lines changed

3 files changed

+172
-54
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.volcengine.wafruntime;
2+
import com.volcengine.waf.model.CheckLLMResponseStreamResponse;
3+
4+
/**
5+
* 表示一个流会话的类,包含流缓冲区、流发送长度和消息 ID 等信息。
6+
*/
7+
public class LLMStreamSession {
8+
// 流缓冲区,用于存储流数据
9+
private String streamBuf;
10+
// 流发送的长度
11+
private int streamSendLen;
12+
// 消息的唯一标识符
13+
private String msgID;
14+
// 存储默认响应体
15+
private CheckLLMResponseStreamResponse defaultBody;
16+
17+
/**
18+
* 默认构造方法,初始化成员变量为默认值。
19+
*/
20+
public LLMStreamSession() {
21+
this.streamBuf = "";
22+
this.streamSendLen = 0;
23+
this.msgID = "";
24+
this.defaultBody = null;
25+
}
26+
27+
/**
28+
* 构造方法,允许在创建对象时初始化所有成员变量。
29+
*
30+
* @param streamBuf 流缓冲区的初始值
31+
* @param streamSendLen 流发送长度的初始值
32+
* @param msgID 消息 ID 的初始值
33+
* @param defaultBody 默认响应体的初始值
34+
*/
35+
public LLMStreamSession(String streamBuf, int streamSendLen, String msgID, CheckLLMResponseStreamResponse defaultBody) {
36+
this.streamBuf = streamBuf;
37+
this.streamSendLen = streamSendLen;
38+
this.msgID = msgID;
39+
this.defaultBody = defaultBody;
40+
}
41+
42+
/**
43+
* 获取流缓冲区的值。
44+
*
45+
* @return 流缓冲区的值
46+
*/
47+
String getStreamBuf() {
48+
return streamBuf;
49+
}
50+
51+
/**
52+
* 设置流缓冲区的值。
53+
*
54+
* @param streamBuf 要设置的流缓冲区的值
55+
*/
56+
void setStreamBuf(String streamBuf) {
57+
this.streamBuf = streamBuf;
58+
}
59+
60+
/**
61+
* 获取流发送长度的值。
62+
*
63+
* @return 流发送长度的值
64+
*/
65+
int getStreamSendLen() {
66+
return streamSendLen;
67+
}
68+
69+
/**
70+
* 设置流发送长度的值。
71+
*
72+
* @param streamSendLen 要设置的流发送长度的值
73+
*/
74+
void setStreamSendLen(int streamSendLen) {
75+
this.streamSendLen = streamSendLen;
76+
}
77+
78+
/**
79+
* 获取消息 ID 的值。
80+
*
81+
* @return 消息 ID 的值
82+
*/
83+
String getMsgID() {
84+
return msgID;
85+
}
86+
87+
/**
88+
* 设置消息 ID 的值。
89+
*
90+
* @param msgID 要设置的消息 ID 的值
91+
*/
92+
void setMsgID(String msgID) {
93+
this.msgID = msgID;
94+
}
95+
96+
/**
97+
* 获取默认响应体。
98+
*
99+
* @return 默认响应体
100+
*/
101+
CheckLLMResponseStreamResponse getDefaultBody() {
102+
return defaultBody;
103+
}
104+
105+
/**
106+
* 设置默认响应体。
107+
*
108+
* @param defaultBody 要设置的默认响应体
109+
*/
110+
void setDefaultBody(CheckLLMResponseStreamResponse defaultBody) {
111+
this.defaultBody = defaultBody;
112+
}
113+
114+
/**
115+
* 向流缓冲区追加一个字符串,并更新流发送长度。
116+
* 如果 defaultBody 不为空,也可以在这里进行相关操作
117+
*
118+
* @param str 要追加的字符串
119+
*/
120+
void appendStreamBuf(String str) {
121+
if (str != null) {
122+
this.streamBuf += str;
123+
this.streamSendLen += str.length();
124+
}
125+
}
126+
127+
}

volcengine-java-sdk-wafruntime/src/main/java/com/volcengine/wafruntime/WafApiRuntime.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,45 @@
1919
import com.volcengine.waf.WafApi;
2020
import com.volcengine.waf.model.CheckLLMResponseStreamRequest;
2121
import com.volcengine.waf.model.CheckLLMResponseStreamResponse;
22-
2322
public class WafApiRuntime extends WafApi {
24-
private String Streambuf = "";
25-
private int StreamSendLen = 0;
26-
private String MsgID = "";
27-
private ApiClient apiClient;
28-
private CheckLLMResponseStreamResponse defaultBody;
29-
30-
// WafApiRuntime 的构造函数,显式调用父类的构造函数
23+
public WafApiRuntime() {
24+
super();
25+
}
3126
public WafApiRuntime(ApiClient apiClient) {
32-
super(apiClient); // 调用父类的带参数构造函数
33-
this.apiClient = apiClient;
27+
super(apiClient);
3428
}
29+
public CheckLLMResponseStreamResponse checkLLMResponseStream(CheckLLMResponseStreamRequest body, LLMStreamSession session) throws ApiException {
30+
// 检查输入参数是否为空
31+
if (body == null || session == null) {
32+
throw new IllegalArgumentException("输入参数 body 或 session 为空,无法进行处理。");
33+
}
3534

36-
// 提供一个方法用于更新拼接字符串和发送长度
37-
public void appendStreambuf(String value) {
38-
Streambuf = Streambuf + value;
39-
StreamSendLen += Streambuf.length();
40-
}
35+
String content = body.getContent();
36+
if (content == null) {
37+
content = "";
38+
}
39+
session.appendStreamBuf(content);
4140

42-
@Override
43-
public CheckLLMResponseStreamResponse checkLLMResponseStream(CheckLLMResponseStreamRequest body) throws ApiException {
44-
appendStreambuf(body.getContent());
4541
// 发送长度小于10个字符并且不是第一次也不是最后一次的条件下则缓存content
46-
if (StreamSendLen < 10 && body.getMsgID() != null && body.getUseStream() != 2) {
47-
return defaultBody;
48-
}
49-
StreamSendLen = 0;
50-
body.setContent(Streambuf);
51-
if (!MsgID.isEmpty()) {
52-
body.setMsgID(MsgID);
42+
if (session.getStreamSendLen() < 10 && body.getMsgID() != null && body.getUseStream() != 2) {
43+
return session.getDefaultBody();
5344
}
45+
session.setStreamSendLen(0);
46+
body.setContent(session.getStreamBuf());
5447

48+
String msgID = session.getMsgID();
49+
if (msgID != null && !msgID.isEmpty()) {
50+
body.setMsgID(msgID);
51+
}
5552

5653
if (!body.getContent().isEmpty()) {
5754
ApiResponse<CheckLLMResponseStreamResponse> resp = checkLLMResponseStreamWithHttpInfo(body);
58-
if (MsgID.isEmpty()) {
59-
MsgID = resp.getData().getMsgID();
55+
if (session.getMsgID().isEmpty() && resp.getData() != null) {
56+
session.setMsgID(resp.getData().getMsgID());
6057
}
61-
defaultBody = resp.getData();
62-
return resp.getData();
58+
session.setDefaultBody(resp.getData() );
59+
return resp.getData() ;
6360
}
64-
return null;
61+
return session.getDefaultBody();
6562
}
66-
}
63+
}
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,63 @@
1-
2-
/**
3-
* Example Code generated by Beijing Volcanoengine Technology.
4-
*/
51
package com.volcengine.wafruntime;
62

73
import com.volcengine.ApiClient;
84
import com.volcengine.ApiException;
95
import com.volcengine.sign.Credentials;
106
import com.volcengine.waf.model.CheckLLMResponseStreamRequest;
117
import com.volcengine.waf.model.CheckLLMResponseStreamResponse;
12-
8+
/**
9+
* Example Code generated by Beijing Volcengine Technology.
10+
*/
1311
public class TestCheckLLMResponseStream {
1412

15-
public static void main(String[] args) throws Exception {
13+
public static void main(String[] args) {
1614

1715
// 注意示例代码安全,代码泄漏会导致AK/SK泄漏,有极大的安全风险。
18-
String ak = "YOUR AK";
19-
String sk = "YOUR SK";
20-
String region = "cn-beijing";
16+
String ak = "YOUR_AK";
17+
String sk = "YOUR_SK";
18+
String region = "YOUR_REGION";
2119
//这是一个超长的字符串,您可以替换成从输入流中读取到的任何内容,提交给SDK进行检测
2220
String longString = "这是一个超长的字符串,您可以替换成从输入流中读取到的任何内容,提交给SDK进行检测";
2321

2422
ApiClient apiClient = new ApiClient()
2523
.setCredentials(Credentials.getCredentials(ak, sk))
26-
.setRegion(region).setEndpoint("YOUR ENDPOINT");
27-
24+
.setRegion(region).setEndpoint("YOUR_ENDPOINT");
2825
WafApiRuntime api = new WafApiRuntime(apiClient);
29-
26+
LLMStreamSession session = new LLMStreamSession();
3027
CheckLLMResponseStreamRequest checkLLMResponseStreamRequest = new CheckLLMResponseStreamRequest();
3128
checkLLMResponseStreamRequest.setContentType(1);
32-
checkLLMResponseStreamRequest.setHost("1eb7e5555abf1791364ee0dfb946c77c.access.omni-shield.volces.com");
29+
checkLLMResponseStreamRequest.setHost("YOUR_HOST");
3330
checkLLMResponseStreamRequest.setMsgClass(0);
3431
checkLLMResponseStreamRequest.setRegion(region);
3532
checkLLMResponseStreamRequest.setUseStream(1);
3633

37-
byte[] bytes = longString.getBytes();
38-
int length = bytes.length;
39-
// 这里我们每次输入9个字节,直到将整个字符串输入完毕,您可以替换成任意长度的字节数组,多次调用SDK
34+
int length = longString.length();
35+
// 这里我们每次输入 9 个字符,直到将整个字符串输入完毕,您可以替换成任意长度的字符,多次调用SDK
4036
for (int i = 0; i < length; i += 9) {
4137
int endIndex = Math.min(i + 9, length);
42-
byte[] subBytes = new byte[endIndex - i];
43-
System.arraycopy(bytes, i, subBytes, 0, endIndex - i);
44-
String subString = new String(subBytes);
38+
String subString = longString.substring(i, endIndex);
4539
checkLLMResponseStreamRequest.setContent(subString);
4640
if (endIndex == length) {
4741
checkLLMResponseStreamRequest.setUseStream(2); // 最后一次调用时,将use_stream设置为2,代表输入流结束
4842
}
4943

5044
try {
5145
// 复制代码运行示例,请自行打印API返回值。
52-
CheckLLMResponseStreamResponse resp = api.checkLLMResponseStream(checkLLMResponseStreamRequest);
46+
CheckLLMResponseStreamResponse resp = api.checkLLMResponseStream(checkLLMResponseStreamRequest , session);
5347
if (resp == null) {
5448
System.out.println("resp is null");
5549
continue;
5650
}
57-
System.out.println(resp.toString());
51+
System.out.println(resp);
5852

5953
} catch (ApiException e) {
6054
// 复制代码运行示例,请自行打印API错误信息。
61-
System.out.println(e.getResponseBody());
55+
System.out.println(e.getResponseBody());
56+
return;
6257
}
6358

6459
}
6560
}
6661

6762
}
6863

69-

0 commit comments

Comments
 (0)