Skip to content

Commit edd202f

Browse files
author
BitsAdmin
committed
Merge branch 'llmshield/dev-online-1.14.0' into 'integration_2025-09-09_1047981984514'
feat: [development task] waf runtime (1637662) See merge request iaasng/volcengine-java-sdk!641
2 parents cc2f490 + 0c65aa0 commit edd202f

32 files changed

+1583
-1
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@
301301
<module>volcengine-java-sdk-speechsaasprod</module>
302302
<module>volcengine-java-sdk-graph20250815</module>
303303
<module>volcengine-java-sdk-speechsaasprod20250521</module>
304+
<module>volcengine-java-sdk-llmshield</module>
304305
<module>volcengine-java-sdk-graph</module>
305306
<module>volcengine-java-sdk-bmq</module>
306307
<module>volcengine-java-sdk-cloudidentity</module>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.28</version>
10+
</parent>
11+
12+
<artifactId>volcengine-java-sdk-llmshield</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.apache.httpcomponents</groupId>
22+
<artifactId>httpclient</artifactId>
23+
<version>4.5.13</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.fasterxml.jackson.core</groupId>
27+
<artifactId>jackson-databind</artifactId>
28+
<version>2.13.4.2</version>
29+
</dependency>
30+
<!-- 其他依赖 -->
31+
<dependency>
32+
<groupId>com.google.code.gson</groupId>
33+
<artifactId>gson</artifactId>
34+
<!-- 可根据需要修改为最新稳定版本 -->
35+
<version>2.10.1</version>
36+
</dependency>
37+
</dependencies>
38+
</project>
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package com.volcengine.llmshield;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.apache.http.HttpResponse;
5+
import org.apache.http.client.HttpClient;
6+
import org.apache.http.client.methods.HttpPost;
7+
import org.apache.http.client.utils.URIBuilder;
8+
import org.apache.http.entity.StringEntity;
9+
import org.apache.http.impl.client.HttpClientBuilder;
10+
import org.apache.http.util.EntityUtils;
11+
12+
import java.io.IOException;
13+
import java.net.URI;
14+
import java.net.URISyntaxException;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
import java.util.concurrent.TimeUnit;
18+
19+
20+
// 客户端类
21+
22+
public class ApiClient {
23+
private final String CONTENT_TYPE_HEADER = "application/json";
24+
25+
26+
private final String url;
27+
private final String ak;
28+
private final String sk;
29+
private final String region;
30+
private HttpClient httpClient;
31+
32+
private ApiClient(String url, String ak,String sk,String region, long timeout) {
33+
this.url = url;
34+
this.ak = ak;
35+
this.sk = sk;
36+
this.region = region;
37+
this.httpClient = HttpClientBuilder.create()
38+
.setConnectionTimeToLive(timeout, TimeUnit.MILLISECONDS)
39+
.build();
40+
}
41+
42+
/**
43+
* 创建新的客户端实例
44+
*
45+
* @param url API 请求的基础 URL
46+
* @param ak 访问密钥
47+
* @param sk 密钥
48+
* @param region 区域
49+
* @param timeout 连接超时时间(毫秒)
50+
* @return 客户端实例
51+
*/
52+
public static ApiClient New(String url, String ak,String sk , String region, long timeout) {
53+
return new ApiClient(url, ak,sk,region, timeout);
54+
}
55+
56+
/**
57+
* 多模态、多轮对话审核
58+
*
59+
* @param request 审核请求对象
60+
* @return 审核响应对象
61+
* @throws IOException 网络请求或解析响应时发生错误
62+
* @throws IllegalArgumentException 当传入的 request.getUseStream()为0,或 session 参数为 null 时,会抛出此异常。
63+
*/
64+
public ModerateV2Response Moderate(ModerateV2Request request) throws Exception {
65+
if (request == null) {
66+
67+
request = new ModerateV2Request();
68+
}
69+
70+
ObjectMapper objectMapper = new ObjectMapper();
71+
objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
72+
String requestBody = objectMapper.writeValueAsString(request);
73+
74+
URIBuilder uriBuilder = new URIBuilder(url +"/v2/moderate");
75+
uriBuilder.addParameter("Action", "Moderate");
76+
uriBuilder.addParameter("Version", "2025-08-31"); // 支持多个参数
77+
URI uri = uriBuilder.build();
78+
HttpPost httpPost = new HttpPost(uri);
79+
httpPost.setHeader("Content-Type", CONTENT_TYPE_HEADER);
80+
81+
httpPost.setEntity(new StringEntity(requestBody, "UTF-8"));
82+
Sign sign = new Sign();
83+
sign.DoSignRequest(httpPost, uri , "Moderate" , ak , sk , region);
84+
HttpResponse response = httpClient.execute(httpPost);
85+
try {
86+
int statusCode = response.getStatusLine().getStatusCode();
87+
if (statusCode != 200) {
88+
// throw new IOException("bad response code: " + statusCode);
89+
}
90+
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
91+
ModerateV2Response moderateResponse = objectMapper.readValue(responseBody, ModerateV2Response.class);
92+
return moderateResponse;
93+
} finally {
94+
EntityUtils.consumeQuietly(response.getEntity());
95+
}
96+
}
97+
98+
public ModerateV2Response ModerateStream(ModerateV2Request request, ModerateV2StreamSession session) throws Exception {
99+
if (request == null) {
100+
101+
request = new ModerateV2Request();
102+
}
103+
// 本接口不支持非流式调用
104+
if (request.getUseStream() == 0 || session == null) {
105+
throw new IllegalArgumentException("useStream cannot 0 ,session cannot null");
106+
}
107+
108+
if (session.getRequest() == null) {
109+
session.setRequest(new ModerateV2Request(request));
110+
} else {
111+
session.appendRequestContent(request);
112+
if ( session.getStreamSendLen() >= session.getCurrentSendWindow() || session.getRequest().getUseStream() == 2 ) {
113+
session.growSendWindow();
114+
}else {
115+
return session.getDefaultOut();
116+
}
117+
}
118+
119+
ObjectMapper objectMapper = new ObjectMapper();
120+
objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
121+
String requestBody = objectMapper.writeValueAsString(session.getRequest());
122+
123+
URIBuilder uriBuilder = new URIBuilder(url +"/v2/moderate");
124+
uriBuilder.addParameter("Action", "Moderate");
125+
uriBuilder.addParameter("Version", "2025-08-31"); // 支持多个参数
126+
URI uri = uriBuilder.build();
127+
HttpPost httpPost = new HttpPost(uri);
128+
httpPost.setHeader("Content-Type", CONTENT_TYPE_HEADER);
129+
130+
httpPost.setEntity(new StringEntity(requestBody, "UTF-8"));
131+
Sign sign = new Sign();
132+
sign.DoSignRequest(httpPost, uri , "Moderate" , ak , sk , region);
133+
HttpResponse response = httpClient.execute(httpPost);
134+
try {
135+
int statusCode = response.getStatusLine().getStatusCode();
136+
if (statusCode != 200) {
137+
// throw new IOException("bad response code: " + statusCode);
138+
}
139+
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
140+
ObjectMapper objectMapperresp = new ObjectMapper();
141+
ModerateV2Response moderateResponse = objectMapperresp.readValue(responseBody, ModerateV2Response.class);
142+
143+
session.setDefaultOut(moderateResponse);
144+
session.setStreamSendLen(0);
145+
// if(session.getRequest().getUseStream() == 2 ){
146+
// System.out.println("最终检测内容: "+session.getRequest().getMessage().getContent());
147+
// }
148+
149+
return moderateResponse;
150+
} finally {
151+
EntityUtils.consumeQuietly(response.getEntity());
152+
}
153+
}
154+
155+
public GenerateStreamV2Response GenerateV2Stream(GenerateStreamV2Request request) throws Exception {
156+
if (request == null) {
157+
request = new GenerateStreamV2Request();
158+
}
159+
// 创建 ObjectMapper 用于 JSON 序列化
160+
ObjectMapper objectMapper = new ObjectMapper();
161+
objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
162+
// 将请求对象序列化为 JSON 字符串
163+
String requestBody = objectMapper.writeValueAsString(request);
164+
URIBuilder uriBuilder = new URIBuilder(url +"/v2/generate");
165+
uriBuilder.addParameter("Action", "Generate");
166+
uriBuilder.addParameter("Version", "2025-08-31"); // 支持多个参数
167+
URI uri = uriBuilder.build();
168+
HttpPost httpPost = new HttpPost(uri);
169+
httpPost.setHeader("Content-Type", CONTENT_TYPE_HEADER);
170+
171+
httpPost.setEntity(new StringEntity(requestBody, "UTF-8"));
172+
Sign sign = new Sign();
173+
sign.DoSignRequest(httpPost, uri , "Generate" , ak , sk , region);
174+
HttpResponse response = httpClient.execute(httpPost);
175+
int statusCode = response.getStatusLine().getStatusCode();
176+
177+
if (statusCode != 200) {
178+
EntityUtils.consumeQuietly(response.getEntity());
179+
// throw new Exception("Bad response code: " + statusCode);
180+
return new GenerateStreamV2Response(response.getEntity().getContent());
181+
} else {
182+
return new GenerateStreamV2Response(response.getEntity().getContent());
183+
}
184+
}
185+
186+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.volcengine.llmshield;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
5+
// 拦截详情
6+
// 忽略“无属性”的问题,允许序列化
7+
@JsonIgnoreProperties(ignoreUnknown = true)
8+
public class BlockDetailV2 {
9+
// 空结构
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.volcengine.llmshield;
2+
import com.fasterxml.jackson.annotation.JsonValue;
3+
// 内容类型枚举
4+
public enum ContentTypeV2 {
5+
TEXT(1), // 文本
6+
AUDIO(2), // 音频
7+
IMAGE(3), // 图片
8+
VIDEO(4); // 视频
9+
10+
private final long value;
11+
12+
ContentTypeV2(long value) {
13+
this.value = value;
14+
}
15+
16+
@JsonValue
17+
public long getValue() {
18+
return value;
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.volcengine.llmshield;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
// 决策详情
6+
public class DecisionDetailV2 {
7+
@JsonProperty("BlockDetail")
8+
private BlockDetailV2 blockDetail; // 拦截详情
9+
10+
@JsonProperty("ReplaceDetail")
11+
private ReplaceDetailV2 replaceDetail; // 替换详情
12+
13+
// Getters and Setters
14+
public BlockDetailV2 getBlockDetail() {
15+
return blockDetail;
16+
}
17+
18+
public void setBlockDetail(BlockDetailV2 blockDetail) {
19+
this.blockDetail = blockDetail;
20+
}
21+
22+
public ReplaceDetailV2 getReplaceDetail() {
23+
return replaceDetail;
24+
}
25+
26+
public void setReplaceDetail(ReplaceDetailV2 replaceDetail) {
27+
this.replaceDetail = replaceDetail;
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.volcengine.llmshield;
2+
import com.fasterxml.jackson.annotation.JsonValue;
3+
// 决策类型枚举
4+
public enum DecisionTypeV2 {
5+
PASS(1), // 通过
6+
BLOCK(2), // 拦截
7+
MARK(3), // 标记
8+
REPLACE(4) , // 替换
9+
OPTIMIZE(5); // 替换
10+
11+
private final long value;
12+
13+
DecisionTypeV2(long value) {
14+
this.value = value;
15+
}
16+
@JsonValue
17+
public long getValue() {
18+
return value;
19+
}
20+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.volcengine.llmshield;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import java.util.List;
6+
7+
// 决策信息
8+
public class DecisionV2 {
9+
@JsonProperty("DecisionType")
10+
private DecisionTypeV2 decisionType; // 决策类型
11+
12+
@JsonProperty("DecisionDetail")
13+
private DecisionDetailV2 detail; // 决策详情
14+
15+
@JsonProperty("DecisionStrategyID")
16+
private String decisionStrategyID; // 决策策略ID
17+
18+
@JsonProperty("HitStrategyIDs")
19+
private List<String> hitStrategyIDs; // 命中策略ID列表
20+
21+
// Getters and Setters
22+
public DecisionTypeV2 getDecisionType() {
23+
return decisionType;
24+
}
25+
26+
public void setDecisionType(DecisionTypeV2 decisionType) {
27+
this.decisionType = decisionType;
28+
}
29+
30+
public DecisionDetailV2 getDetail() {
31+
return detail;
32+
}
33+
34+
public void setDetail(DecisionDetailV2 detail) {
35+
this.detail = detail;
36+
}
37+
38+
public String getDecisionStrategyID() {
39+
return decisionStrategyID;
40+
}
41+
42+
public void setDecisionStrategyID(String decisionStrategyID) {
43+
this.decisionStrategyID = decisionStrategyID;
44+
}
45+
46+
public List<String> getHitStrategyIDs() {
47+
return hitStrategyIDs;
48+
}
49+
50+
public void setHitStrategyIDs(List<String> hitStrategyIDs) {
51+
this.hitStrategyIDs = hitStrategyIDs;
52+
}
53+
}

0 commit comments

Comments
 (0)