You need to enable JavaScript to run this app.
导航
Java 使用示例
最近更新时间:2025.04.23 16:51:49首次发布时间:2025.03.28 19:07:12
我的收藏
有用
有用
无用
无用

本文为您提供大模型应用防火墙接口调用的示例代码。实际使用时,您可以替换成 SDK 中已经支持的任意接口。

前提条件

调用前,请确保已经完成Java 安装和初始化

调用接口

CheckLLMPrompt

CheckLLMPrompt相关参数的详细说明,可参考CheckLLMPrompt - 判别大模型提示词是否合规

package com.volcengine.sdk.example;

import com.volcengine.ApiClient;
import com.volcengine.ApiException;
import com.volcengine.sign.Credentials;
import com.volcengine.waf.WafApi;
import com.volcengine.waf.model.CheckLLMPromptRequest;
import com.volcengine.waf.model.CheckLLMPromptResponse;

public class TestCheckLLMPrompt {
    public static void main(String[] args) throws Exception {
       
        String ak = "YOUR AK";  // Access Key,用于身份验证
        String sk = "YOUR SK";  // Secret Key,用于签名
        String region = "cn-beijing";  // 大模型应用防火墙所属地域对应的 ID

        ApiClient apiClient = new ApiClient()
                .setCredentials(Credentials.getCredentials(ak, sk))
                .setRegion(region);

        WafApi api = new WafApi(apiClient);
        
        CheckLLMPromptRequest checkLLMPromptRequest = new CheckLLMPromptRequest();
        checkLLMPromptRequest.setContent("请输入您想要校验的内容,文本内容或资源URL"); // 需要检测的内容
        checkLLMPromptRequest.setContentType(1);  // 内容类型: 1-文本, 2-音频, 3-图片
        checkLLMPromptRequest.setHost("fd****.access.omni-shield.volces.com");  // 大模型应用防火墙生成的接入点域名
        checkLLMPromptRequest.setMsgClass(0);  // 场景类型,当前默认为标准类型,填入 0 即可
        checkLLMPromptRequest.setRegion("cn-beijing"); // 大模型应用防火墙所属地域对应的 ID
        
        try {
            // 复制代码运行示例,请自行打印API返回值。
            CheckLLMPromptResponse data = api.checkLLMPrompt(checkLLMPromptRequest);
            System.out.println(data.toString()); 

        } catch (ApiException e) {
            // 复制代码运行示例,请自行打印API错误信息。
            System.out.println(e.getResponseBody());
        }
    }
}

CheckLLMResponseStream

CheckLLMResponseStream相关参数的详细说明,可参考CheckLLMResponseStream - 判别大模型响应内容是否合规

package com.volcengine.wafruntime;

import com.volcengine.ApiClient;
import com.volcengine.ApiException;
import com.volcengine.sign.Credentials;
import com.volcengine.waf.model.CheckLLMResponseStreamRequest;
import com.volcengine.waf.model.CheckLLMResponseStreamResponse;
public class TestCheckLLMResponseStream {
    public static void main(String[] args) throws Exception {
       
        String ak = "YOUR AK";  // Access Key,用于身份验证
        String sk = "YOUR SK";  // Secret Key,用于签名
        String region = "cn-beijing";  // 大模型应用防火墙所属地域对应的 ID
        String longString = "这是一个超长的字符串,您可以替换成从输入流中读取到的任何内容,提交给SDK进行检测";   // 需要检测的文本内容
        ApiClient apiClient = new ApiClient()
                .setCredentials(Credentials.getCredentials(ak, sk))
                .setRegion(region);
        WafApiRuntime api = new WafApiRuntime(apiClient);
        CheckLLMResponseStreamRequest checkLLMResponseStreamRequest = new CheckLLMResponseStreamRequest();
        checkLLMResponseStreamRequest.setContentType(1);  // 内容类型: 1-文本, 2-音频, 3-图片
        checkLLMResponseStreamRequest.setHost("fd****.access.omni-shield.volces.com");  // 大模型应用防火墙生成的接入点域名
        checkLLMResponseStreamRequest.setMsgClass(0);  // 场景类型,当前默认为标准类型,填入 0 即可
        checkLLMResponseStreamRequest.setRegion(region);  // 大模型应用防火墙所属地域对应的 ID
        checkLLMResponseStreamRequest.setUseStream(1);  // 是否开启流式计算:0-非流式检查,1-流式检查,2-流式检查结束
        byte[] bytes = longString.getBytes();
        int length = bytes.length;
        // 这里每次输入 9 个字节,直到将整个字符串输入完毕,您可以替换成任意长度的字节数组,多次调用SDK
        for (int i = 0; i < length; i += 9) {
            int endIndex = Math.min(i + 9, length);
            byte[] subBytes = new byte[endIndex - i];
            System.arraycopy(bytes, i, subBytes, 0, endIndex - i);
            String subString = new String(subBytes);
            checkLLMResponseStreamRequest.setContent(subString);
            if (endIndex == length) {
                checkLLMResponseStreamRequest.setUseStream(2); // 最后一次调用时,将use_stream设置为2,代表输入流结束
            }
            try {
                // 复制代码运行示例,请自行打印API返回值。
                CheckLLMResponseStreamResponse resp = api.checkLLMResponseStream(checkLLMResponseStreamRequest);
                if (resp == null) {
                    System.out.println("resp is null");
                    continue;
                }
                System.out.println(resp.toString());
            } catch (ApiException e) {
                // 复制代码运行示例,请自行打印API错误信息。
                 System.out.println(e.getResponseBody());
            }
        }
    }
}