feat(audit): 增加审计日志与演示数据管理
This commit is contained in:
36
apps/audit/services.py
Normal file
36
apps/audit/services.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from agent_core.results import AgentResult
|
||||
|
||||
from .models import AgentAuditLog
|
||||
|
||||
|
||||
def _mask_sensitive_text(value: str) -> str:
|
||||
masked = value
|
||||
for marker in ("LLM_API_KEY=", "EMBEDDING_API_KEY="):
|
||||
if marker in masked:
|
||||
prefix, _, suffix = masked.partition(marker)
|
||||
secret, separator, rest = suffix.partition(" ")
|
||||
masked_secret = "sk-***" if secret.startswith("sk-") else "***"
|
||||
masked = f"{prefix}{marker}{masked_secret}{separator}{rest}"
|
||||
return masked
|
||||
|
||||
|
||||
def create_audit_log(
|
||||
scenario_id: str,
|
||||
scenario_name: str,
|
||||
user_input: str,
|
||||
agent_result: AgentResult,
|
||||
) -> AgentAuditLog:
|
||||
return AgentAuditLog.objects.create(
|
||||
scenario_id=scenario_id,
|
||||
scenario_name=scenario_name,
|
||||
user_input=user_input,
|
||||
retrieved_chunks=agent_result.references,
|
||||
tool_calls=agent_result.tool_calls,
|
||||
structured_output=agent_result.structured_output,
|
||||
final_answer=agent_result.answer,
|
||||
raw_output=agent_result.raw_output,
|
||||
model_name=agent_result.model_name,
|
||||
latency_ms=max(agent_result.latency_ms, 0),
|
||||
status=agent_result.status,
|
||||
error_message=_mask_sensitive_text(agent_result.error),
|
||||
)
|
||||
Reference in New Issue
Block a user