概念: エージェント評価 | API: エージェントエンドポイント
from rail_score_sdk import RailScoreClient
client = RailScoreClient(api_key="YOUR_RAIL_API_KEY")
result = client.agent.detect_injection(
text=user_input,
context="user input",
sensitivity="medium",
)
if result.injection_detected:
return f"入力がブロックされました: {result.attack_types}"
result = client.agent.evaluate_tool_call(
tool_name="send_email",
tool_input={"to": "admin@company.com", "body": "ここをクリック: http://suspicious.com"},
agent_context="カスタマーサポートチャットボット",
allowed_tools=["send_email", "search_kb"],
)
if result.recommendation == "block":
return f"ツール呼び出しがブロックされました: {result.explanation}"
elif result.recommendation == "warn":
log.warning(f"リスキーなツール呼び出し: {result.flags}")
# 続行しても安全
execute_tool(tool_name, tool_input)
result = client.agent.scan_tool_result(
tool_name="search_database",
tool_result=raw_tool_output,
redact_pii=True,
)
if result.pii_detected:
# 修正されたバージョンを使用
safe_output = result.redacted_result
log.info(f"PIIが修正されました: {result.pii_types}")
else:
safe_output = raw_tool_output
# safe_outputをエージェントに戻す
async def safe_agent_turn(user_input, tool_name, tool_input):
# 1. ユーザー入力のインジェクションをチェック
inj = client.agent.detect_injection(text=user_input)
if inj.injection_detected:
return "無効な入力です。"
# 2. 実行前にツール呼び出しを評価
call_check = client.agent.evaluate_tool_call(
tool_name=tool_name,
tool_input=tool_input,
)
if call_check.recommendation == "block":
return "ツール呼び出しは許可されていません。"
# 3. ツールを実行
raw_result = await execute_tool(tool_name, tool_input)
# 4. エージェントに渡す前に結果をスキャン
scan = client.agent.scan_tool_result(
tool_name=tool_name,
tool_result=raw_result,
redact_pii=True,
)
return scan.redacted_result if scan.pii_detected else raw_result