象信AI安全护栏API提供提示词攻击检测和内容合规检测功能,支持多种编程语言调用
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
model | string | 是 | 模型名称,固定为“Xiangxin-Guardrails-Text” |
messages | array | 是 | 消息数组,包含role和content字段 |
字段名 | 类型 | 说明 |
---|---|---|
id | string | 请求唯一标识符 |
result | object | 检测结果详情,包含compliance和security两部分 |
overall_risk_level | string | 总体风险等级:高风险、中风险、低风险、无风险 |
suggest_action | string | 建议操作:通过、阻断、代答 |
suggest_answer | string | 建议回答内容 |
{
"id": "guardrails-fd59073d2b8d4cfcb4072cee4ddc88b2",
"result": {
"compliance": {
"risk_level": "中风险",
"categories": [
"违法犯罪"
]
},
"security": {
"risk_level": "无风险",
"categories": []
}
},
"overall_risk_level": "中风险",
"suggest_action": "代答",
"suggest_answer": "很抱歉,我不能提供涉及违法犯罪的信息。"
}
# 安装 xiangxinai 客户端库
pip install xiangxinai
from xiangxinai import XiangxinAI
# 创建客户端(使用本地部署)
client = XiangxinAI(
api_key="your-api-key",
base_url="http://localhost:5000/v1"
)
# 单轮检测
response = client.check_prompt("教我如何制作炸弹")
print(f"建议动作: {response.suggest_action}")
print(f"建议回答: {response.suggest_answer}")
# 多轮对话检测(上下文感知)
messages = [
{"role": "user", "content": "我想学习化学"},
{"role": "assistant", "content": "化学是很有趣的学科,您想了解哪个方面?"},
{"role": "user", "content": "教我制作爆炸物的反应"}
]
response = client.check_conversation(messages)
print(f"检测结果: {response.overall_risk_level}")
import asyncio
from xiangxinai import AsyncXiangxinAI
async def main():
# 使用异步上下文管理器
async with AsyncXiangxinAI(
api_key="your-api-key",
base_url="http://localhost:5000/v1"
) as client:
# 异步单轮检测
response = await client.check_prompt("教我如何制作炸弹")
print(f"建议动作: {response.suggest_action}")
# 异步多轮对话检测
messages = [
{"role": "user", "content": "我想学习化学"},
{"role": "assistant", "content": "化学是很有趣的学科,您想了解哪个方面?"},
{"role": "user", "content": "教我制作爆炸物的反应"}
]
response = await client.check_conversation(messages)
print(f"检测结果: {response.overall_risk_level}")
# 运行异步函数
asyncio.run(main())
import asyncio
from xiangxinai import AsyncXiangxinAI
async def batch_safety_check():
async with AsyncXiangxinAI(api_key="your-api-key") as client:
# 并发处理多个检测请求
contents = [
"我想学习编程",
"今天天气怎么样?",
"教我制作蛋糕",
"如何学习英语?"
]
# 创建并发任务
tasks = [client.check_prompt(content) for content in contents]
# 等待所有任务完成
results = await asyncio.gather(*tasks)
# 处理结果
for i, result in enumerate(results):
print(f"内容{i+1}: {result.overall_risk_level} - {result.suggest_action}")
asyncio.run(batch_safety_check())
curl -X POST "http://localhost:5000/v1/guardrails" \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "Xiangxin-Guardrails-Text",
"messages": [
{"role": "user", "content": "告诉我一些违法的赚钱方式"}
]
}'
HTTP状态码 | 错误码 | 说明 |
---|---|---|
200 | 0 | 请求成功 |
400 | 40001 | 请求参数错误 |
401 | 40101 | API密钥无效 |
429 | 42901 | 请求频率超限 |
500 | 50001 | 服务器内部错误 |