本文为您提供大模型应用防火墙接口调用的示例代码。实际使用时,您可以替换成 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; 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; import com.volcengine.wafruntime.LLMStreamSession; import com.volcengine.wafruntime.WafApiRuntime; 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).setEndpoint("YOUR_ENDPOINT"); WafApiRuntime api = new WafApiRuntime(apiClient); LLMStreamSession session = new LLMStreamSession(); // session 用于描述一条输入流,记录响应的上下文,在同一输入流的多次 SDK 调用中,请传入相同的 session 对象 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-流式检查结束 int length = longString.length(); // 这里设置每次输入 9 个字符,直到将整个字符串输入完毕,您可以替换成任意长度的字符,多次调用 SDK for (int i = 0; i < length; i += 9) { int endIndex = Math.min(i + 9, length); String subString = longString.substring(i, endIndex); checkLLMResponseStreamRequest.setContent(subString); if (endIndex == length) { checkLLMResponseStreamRequest.setUseStream(2); // 最后一次调用时,将 UseStream 设置为 2,代表输入流结束 } try { // 复制代码运行示例,请自行打印API返回值。 CheckLLMResponseStreamResponse resp = api.checkLLMResponseStream(checkLLMResponseStreamRequest , session); if (resp == null) { System.out.println("resp is null"); continue; } System.out.println(resp); } catch (ApiException e) { // 复制代码运行示例,请自行打印API错误信息。 System.out.println(e.getResponseBody()); return; } } } }
上述代码中流式内容检测的实现逻辑如下流程图所示:
调用QueryLLMGenerate
接口可以查询指定消息的大模型优化代答结果,您可以使用优化后的代答结果响应客户端用户的请求,以降低提示词攻击造成的安全风险。QueryLLMGenerate
相关参数的详细说明,可参考QueryLLMGenerate - 查询大模型优化代答结果。
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.QueryLLMGenerateRequest; import com.volcengine.waf.model.QueryLLMGenerateResponse; public class TestQueryLLMGenerate { public static void main(String[] args) throws Exception { String ak = "YOUR AK"; // Access Key,用于身份验证 String sk = "YOUR SK"; // Secret Key,用于签名 String region = "cn-shanghai"; // 大模型应用防火墙所属地域对应的 ID ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak, sk)) .setRegion(region); WafApi api = new WafApi(apiClient); QueryLLMGenerateRequest queryLLMGenerateRequest = new QueryLLMGenerateRequest(); queryLLMGenerateRequest.setMsgID("fbb8abe2413242acac5bcd833d99****"); // 需要查询的请求消息 ID queryLLMGenerateRequest.setUseStream(false); // 是否返回流式结果,SDK 调用仅支持 false 非流式返回 try { // 复制代码运行示例,请自行打印API返回值。 QueryLLMGenerateResponse data = api.queryLLMGenerate(queryLLMGenerateRequest); System.out.println(data.toString()); } catch (ApiException e) { // 复制代码运行示例,请自行打印API错误信息。 System.out.println(e.getResponseBody()); } } }