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

本文为您介绍大模型应用防火墙 Python SDK 的下载地址、安装方式以及代码示例,帮助您快速了解如何使用 SDK 调用 OpenAPI。

前提条件

环境要求

  • Python 2.7 或以上版本。执行python --version可以检查当前 Python 的版本信息。
  • 由于 Windows 系统有最长路径限制,可能会导致 Python 安装失败。可以按照以下方式设置:
    1. 按下Win+R,输入regedit打开注册表编辑器。
    2. 设置\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem 路径下的变量LongPathsEnabled1即可。

步骤一 下载 SDK

前往volcengine-python-sdk下载大模型应用防火墙 Python SDK 安装包。

步骤二 安装 SDK

您可以使用以下任意一种方式安装 SDK。

方式一

使用 pip 命令安装 SDK。

pip install volcengine-python-sdk

方式二

  • 执行python setup.py install --user命令,为指定 user 安装 SDK。
  • 执行sudo python setup.py install命令,为全部 user 安装 SDK。

步骤三 初始化配置

配置全局默认参数:

configuration = volcenginesdkcore.Configuration()
configuration.client_side_validation = True  # 客户端是否进行参数校验
configuration.schema = "http"                # https or http
configuration.debug = False                  # 是否开启调试
configuration.logger_file = "sdk.log"

volcenginesdkcore.Configuration.set_default(configuration)

步骤四 调用接口

以下提供调用CheckLLMPrompt判别大模型提示词是否合规的示例代码,实际使用时,您可以替换成 SDK 中已经支持的任意接口。CheckLLMPrompt相关参数的详细说明,可参考CheckLLMPrompt - 判别大模型提示词是否合规

from __future__ import print_function
import volcenginesdkcore
import volcenginesdkwaf
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    configuration = volcenginesdkcore.Configuration()
    configuration.ak = "YOUR AK"
    configuration.sk = "YOUR SK"
    configuration.region = "cn-beijing" 
    # set default configuration
    volcenginesdkcore.Configuration.set_default(configuration)

    # use global default configuration
    api_instance = volcenginesdkwaf.WAFApi()
    check_llm_prompt_request = volcenginesdkwaf.CheckLLMPromptRequest(
        content="需要判别的提示词",
        content_type=1,
        host="example.com",
        msg_class=0,
        region="cn-beijing",
    )
    
    try:
        # 复制代码运行示例,请自行打印API返回值。
        data = api_instance.check_llm_prompt(check_llm_prompt_request)
        print("response:" , data )

    except ApiException as e:
        # 复制代码运行示例,请自行打印API错误信息。
        print("Exception when calling api: %s\n" % e)
        pass