API 教程
Python 示例
使用 Python requests 获取提示词,并为 BenchDX 或 RobustDX 提交响应。
以下函数覆盖本文档中的四个端点。BenchDX 设置 adversarial=False;RobustDX 设置 adversarial=True。
获取提示词
fetch_promptsPython
from typing import Any
import requests
BASE_URL = "https://trial.aidx.pro"
def fetch_prompts(
evaluation_id: str,
token: str,
adversarial: bool = False,
) -> list[dict[str, Any]]:
endpoint = "all-adv-prompts" if adversarial else "promptsAll"
response = requests.get(
f"{BASE_URL}/api/evaluation/{endpoint}",
headers={"Authorization": f"Bearer {token}"},
params={"evaluationId": evaluation_id},
timeout=30,
)
response.raise_for_status()
result = response.json()
if not result.get("success"):
raise RuntimeError(result.get("message", "Failed to fetch prompts"))
return result["data"]此函数会同时检查 HTTP 状态和响应封装,然后再返回提示词列表。
提交响应
submit_responsePython
import requests
BASE_URL = "https://trial.aidx.pro"
def submit_response(
evaluation_id: str,
trace_id: str,
model_response: str,
token: str,
adversarial: bool = False,
) -> None:
endpoint = "adv-response" if adversarial else "response"
response_field = "advResponse" if adversarial else "llmResponse"
response = requests.post(
f"{BASE_URL}/api/evaluation/{endpoint}",
headers={"Authorization": f"Bearer {token}"},
data={
"evaluationId": evaluation_id,
"traceId": trace_id,
response_field: model_response,
},
timeout=30,
)
response.raise_for_status()
result = response.json()
if not result.get("success"):
raise RuntimeError(result.get("message", "Failed to submit response"))| 评估 | GET 端点 | POST 端点 | 响应字段 |
|---|---|---|---|
| BenchDX | promptsAll | response | llmResponse |
| RobustDX | all-adv-prompts | adv-response | advResponse |
连接待测目标调用
为自己的模型或应用 API 实现 call_target_model,然后在循环中保留每个提示词的追踪信息:
BenchDX 执行循环Python
EVALUATION_ID = "your_evaluation_id"
AIDX_PAT = "your_personal_access_token"
prompts = fetch_prompts(
evaluation_id=EVALUATION_ID,
token=AIDX_PAT,
adversarial=False,
)
for item in prompts:
# Implement this function for your model or application API.
model_response = call_target_model(item["content"])
submit_response(
evaluation_id=EVALUATION_ID,
trace_id=item["traceId"],
model_response=model_response,
token=AIDX_PAT,
adversarial=False,
)