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