Skip to content

Commit ad9f0ba

Browse files
author
BitsAdmin
committed
Merge branch 'feat/waf-new-feat' into 'integration_2025-04-14_855510378242'
feat: [development task] wafruntime-java (1150616) See merge request iaasng/volcengine-java-sdk!439
2 parents a2fb06a + 5fb6dec commit ad9f0ba

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,5 +275,6 @@
275275
<module>volcengine-java-sdk-translate20250301</module>
276276
<module>volcengine-java-sdk-volccontentplatform</module>
277277
<module>volcengine-java-sdk-edx</module>
278+
<module>volcengine-java-sdk-wafruntime</module>
278279
</modules>
279280
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.volcengine</groupId>
8+
<artifactId>volcengine-java-sdk</artifactId>
9+
<version>0.2.3</version>
10+
</parent>
11+
12+
<artifactId>volcengine-java-sdk-wafruntime</artifactId>
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.volcengine</groupId>
16+
<artifactId>volcengine-java-sdk-waf</artifactId>
17+
<version>0.2.3</version>
18+
<scope>compile</scope>
19+
</dependency>
20+
</dependencies>
21+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* waf
3+
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
4+
*
5+
* OpenAPI spec version: common-version
6+
*
7+
*
8+
* NOTE: This class is auto generated by the swagger code generator program.
9+
* https://github.com/swagger-api/swagger-codegen.git
10+
* Do not edit the class manually.
11+
*/
12+
13+
package com.volcengine.wafruntime;
14+
15+
16+
import com.volcengine.ApiClient;
17+
import com.volcengine.ApiException;
18+
import com.volcengine.ApiResponse;
19+
import com.volcengine.waf.WafApi;
20+
import com.volcengine.waf.model.CheckLLMResponseStreamRequest;
21+
import com.volcengine.waf.model.CheckLLMResponseStreamResponse;
22+
23+
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 的构造函数,显式调用父类的构造函数
31+
public WafApiRuntime(ApiClient apiClient) {
32+
super(apiClient); // 调用父类的带参数构造函数
33+
this.apiClient = apiClient;
34+
}
35+
36+
// 提供一个方法用于更新拼接字符串和发送长度
37+
public void appendStreambuf(String value) {
38+
Streambuf = Streambuf + value;
39+
StreamSendLen += Streambuf.length();
40+
}
41+
42+
@Override
43+
public CheckLLMResponseStreamResponse checkLLMResponseStream(CheckLLMResponseStreamRequest body) throws ApiException {
44+
appendStreambuf(body.getContent());
45+
// 发送长度小于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);
53+
}
54+
55+
56+
if (!body.getContent().isEmpty()) {
57+
ApiResponse<CheckLLMResponseStreamResponse> resp = checkLLMResponseStreamWithHttpInfo(body);
58+
if (MsgID.isEmpty()) {
59+
MsgID = resp.getData().getMsgID();
60+
}
61+
defaultBody = resp.getData();
62+
return resp.getData();
63+
}
64+
return null;
65+
}
66+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
/**
3+
* Example Code generated by Beijing Volcanoengine Technology.
4+
*/
5+
package com.volcengine.wafruntime;
6+
7+
import com.volcengine.ApiClient;
8+
import com.volcengine.ApiException;
9+
import com.volcengine.sign.Credentials;
10+
import com.volcengine.waf.model.CheckLLMResponseStreamRequest;
11+
import com.volcengine.waf.model.CheckLLMResponseStreamResponse;
12+
13+
public class TestCheckLLMResponseStream {
14+
15+
public static void main(String[] args) throws Exception {
16+
17+
// 注意示例代码安全,代码泄漏会导致AK/SK泄漏,有极大的安全风险。
18+
String ak = "YOUR AK";
19+
String sk = "YOUR SK";
20+
String region = "cn-beijing";
21+
//这是一个超长的字符串,您可以替换成从输入流中读取到的任何内容,提交给SDK进行检测
22+
String longString = "这是一个超长的字符串,您可以替换成从输入流中读取到的任何内容,提交给SDK进行检测";
23+
24+
ApiClient apiClient = new ApiClient()
25+
.setCredentials(Credentials.getCredentials(ak, sk))
26+
.setRegion(region).setEndpoint("YOUR ENDPOINT");
27+
28+
WafApiRuntime api = new WafApiRuntime(apiClient);
29+
30+
CheckLLMResponseStreamRequest checkLLMResponseStreamRequest = new CheckLLMResponseStreamRequest();
31+
checkLLMResponseStreamRequest.setContentType(1);
32+
checkLLMResponseStreamRequest.setHost("1eb7e5555abf1791364ee0dfb946c77c.access.omni-shield.volces.com");
33+
checkLLMResponseStreamRequest.setMsgClass(0);
34+
checkLLMResponseStreamRequest.setRegion(region);
35+
checkLLMResponseStreamRequest.setUseStream(1);
36+
37+
byte[] bytes = longString.getBytes();
38+
int length = bytes.length;
39+
// 这里我们每次输入9个字节,直到将整个字符串输入完毕,您可以替换成任意长度的字节数组,多次调用SDK
40+
for (int i = 0; i < length; i += 9) {
41+
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);
45+
checkLLMResponseStreamRequest.setContent(subString);
46+
if (endIndex == length) {
47+
checkLLMResponseStreamRequest.setUseStream(2); // 最后一次调用时,将use_stream设置为2,代表输入流结束
48+
}
49+
50+
try {
51+
// 复制代码运行示例,请自行打印API返回值。
52+
CheckLLMResponseStreamResponse resp = api.checkLLMResponseStream(checkLLMResponseStreamRequest);
53+
if (resp == null) {
54+
System.out.println("resp is null");
55+
continue;
56+
}
57+
System.out.println(resp.toString());
58+
59+
} catch (ApiException e) {
60+
// 复制代码运行示例,请自行打印API错误信息。
61+
System.out.println(e.getResponseBody());
62+
}
63+
64+
}
65+
}
66+
67+
}
68+
69+

0 commit comments

Comments
 (0)