本文为您提供模型应用防火墙接口调用的示例代码。实际使用时,您可以替换成 SDK 中已经支持的任意接口。
调用前,请确保已经完成C++ 安装和初始化。
调用CheckLLMPrompt
接口可以判别输入的提示词是否合规。大模型应用防火墙检测并分析提示词内容,并返回对应内容的判别结果、处置动作和命中的内容。CheckLLMPrompt
相关参数的详细说明,可参考CheckLLMPrompt - 判别大模型提示词是否合规。
#include "api_request.h" // 定义常量,实际使用时请替换为真实值 const char *AccessKeyID = "Your AK"; // Access Key,用于身份验证 const char *SecretAccessKey = "Your SK"; // Secret Key,用于签名 const char *Region = "cn-beijing"; // 大模型应用防火墙所属地域对应的 ID const char *Service = "waf"; // 大模型应用防火墙的服务名称,固定为 waf const char *Endpoint = "open.volcengineapi.com"; // 大模型应用防火墙的服务地址,固定为 open.volcengineapi.com const char *Action = "CheckLLMPrompt"; // API 名称 const char *Version = "2023-12-25"; // API 版本 const char *Content = "请输入您想要校验的内容,文本内容或资源URL"; // 需要检测的内容 int main() { CURL *curl; CURLcode res; struct curl_slist *headers = NULL; char queryString[MAX_LEN] = {0}; char requestAddr[MAX_LEN] = {0}; char date[64] = {0}; char authDate[9] = {0}; char payload[MAX_LEN] = {0}; char canonicalString[MAX_LEN * 2] = {0}; char hashedCanonicalString[MAX_LEN] = {0}; char signString[MAX_LEN * 2] = {0}; char authorization[MAX_LEN] = {0}; char requestBody[MAX_LEN] = {0}; char x_date_header[MAX_LEN] = {0}; char x_content_sha256_header[MAX_LEN] = {0}; // 构建签名材料 time_t now = time(NULL); struct tm *tm_info = gmtime(&now); if (strftime(date, sizeof(date), DATE_FORMAT, tm_info) == 0) { fprintf(stderr, "Failed to format date\n"); return 1; } strncpy(authDate, date, 8); authDate[8] = '\0'; // 构建请求体 buildCheckLLMPromptBody(Content, 1, "config.sdk.cn", 0, Region, requestBody); // 构建查询字符串 buildQueryString(Action, Version, queryString, MAX_LEN); // 构建请求地址 buildRequestAddress(Endpoint, queryString, requestAddr, MAX_LEN); // 构建签名相关材料 buildSignatureMaterials(date, authDate, requestBody, payload, MAX_LEN); // 构建规范字符串 buildCanonicalString("POST", queryString, Endpoint, date, payload, canonicalString); strcpy(hashedCanonicalString, hashSHA256((const unsigned char *)canonicalString, strlen(canonicalString))); // 构建签名字符串 buildSignString(date, authDate, hashedCanonicalString, signString); // 构建认证请求头 buildAuthorization(authDate, signString, authorization); // 初始化libcurl curl = initCurl(); if (curl) { // 设置请求头 headers = curl_slist_append(headers, "Content-Type: application/json"); if (snprintf(x_date_header, MAX_LEN, "X-Date: %s", date) >= MAX_LEN) { fprintf(stderr, "x_date_header buffer overflow\n"); curl_slist_free_all(headers); curl_easy_cleanup(curl); return 1; } headers = curl_slist_append(headers, x_date_header); if (snprintf(x_content_sha256_header, MAX_LEN, "X-Content-Sha256: %s", payload) >= MAX_LEN) { fprintf(stderr, "x_content_sha256_header buffer overflow\n"); curl_slist_free_all(headers); curl_easy_cleanup(curl); return 1; } headers = curl_slist_append(headers, x_content_sha256_header); char Authorization[4096] = {0}; sprintf(Authorization, "Authorization: %s", authorization); headers = curl_slist_append(headers, Authorization); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 执行请求 res = performCurlRequest(curl, requestAddr, requestBody, headers); // 清理请求头列表 curl_slist_free_all(headers); // 清理libcurl句柄 curl_easy_cleanup(curl); } return 0; }
调用QueryLLMGenerate
接口可以查询指定消息的大模型优化代答结果,您可以使用优化后的代答结果响应客户端用户的请求,以降低提示词攻击造成的安全风险。QueryLLMGenerate
相关参数的详细说明,可参考QueryLLMGenerate - 实时查询大模型生成结果。
#include "api_request.h" // 定义常量,实际使用时请替换为真实值 const char *AccessKeyID = "Your AK"; // Access Key,用于身份验证 const char *SecretAccessKey = "Your SK"; // Secret Key,用于签名 const char *Region = "cn-beijing"; // 大模型应用防火墙所属地域对应的 ID const char *Service = "waf"; // 大模型应用防火墙的服务名称,固定为 waf const char *Endpoint = "open.volcengineapi.com"; // 大模型应用防火墙的服务地址,固定为 open.volcengineapi.com const char *Action = "QueryLLMGenerate"; // API 名称 const char *Version = "2023-12-25"; // API 版本 const char *Msgid = "5c51f409261a436dabfdfc8504809e65"; // 需要查询的请求消息 ID const int UseStream = 0 ; // 设置是否返回流式结果,SDK 调用仅支持 false 非流式返回 int main() { CURL *curl; CURLcode res; struct curl_slist *headers = NULL; char queryString[MAX_LEN] = {0}; char requestAddr[MAX_LEN] = {0}; char date[64] = {0}; char authDate[9] = {0}; char payload[MAX_LEN] = {0}; char canonicalString[MAX_LEN * 2] = {0}; char hashedCanonicalString[MAX_LEN] = {0}; char signString[MAX_LEN * 2] = {0}; char authorization[MAX_LEN] = {0}; char requestBody[MAX_LEN] = {0}; char x_date_header[MAX_LEN] = {0}; char x_content_sha256_header[MAX_LEN] = {0}; // 构建签名材料 time_t now = time(NULL); struct tm *tm_info = gmtime(&now); if (strftime(date, sizeof(date), DATE_FORMAT, tm_info) == 0) { fprintf(stderr, "Failed to format date\n"); return 1; } strncpy(authDate, date, 8); authDate[8] = '\0'; // 构建请求体 buildQueryLLMGenerateBody(Msgid , UseStream , requestBody); // 构建查询字符串 buildQueryString(Action, Version, queryString, MAX_LEN); // 构建请求地址 buildRequestAddress(Endpoint, queryString, requestAddr, MAX_LEN); // 构建签名相关材料 buildSignatureMaterials(date, authDate, requestBody, payload, MAX_LEN); // 构建规范字符串 buildCanonicalString("POST", queryString, Endpoint, date, payload, canonicalString); strcpy(hashedCanonicalString, hashSHA256((const unsigned char *)canonicalString, strlen(canonicalString))); // 构建签名字符串 buildSignString(date, authDate, hashedCanonicalString, signString); // 构建认证请求头 buildAuthorization(authDate, signString, authorization); // 初始化libcurl curl = initCurl(); if (curl) { // 设置请求头 headers = curl_slist_append(headers, "Content-Type: application/json"); if (snprintf(x_date_header, MAX_LEN, "X-Date: %s", date) >= MAX_LEN) { fprintf(stderr, "x_date_header buffer overflow\n"); curl_slist_free_all(headers); curl_easy_cleanup(curl); return 1; } headers = curl_slist_append(headers, x_date_header); if (snprintf(x_content_sha256_header, MAX_LEN, "X-Content-Sha256: %s", payload) >= MAX_LEN) { fprintf(stderr, "x_content_sha256_header buffer overflow\n"); curl_slist_free_all(headers); curl_easy_cleanup(curl); return 1; } headers = curl_slist_append(headers, x_content_sha256_header); char Authorization[4096] = {0}; sprintf(Authorization, "Authorization: %s", authorization); headers = curl_slist_append(headers, Authorization); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 执行请求 res = performCurlRequest(curl, requestAddr, requestBody, headers); // 清理请求头列表 curl_slist_free_all(headers); // 清理libcurl句柄 curl_easy_cleanup(curl); } return 0; }