Show retrieved knowledge chunks in audit logs
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import json
|
||||
from datetime import datetime
|
||||
from io import StringIO
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi.responses import Response
|
||||
@@ -236,6 +238,7 @@ def _ai_log_dict(log: AiRequestLog, *, include_prompt: bool = False) -> dict:
|
||||
"modelName": log.model_name,
|
||||
"knowledgeIds": log.knowledge_ids,
|
||||
"retrieveCount": log.retrieve_count,
|
||||
"retrievedChunks": _parse_retrieved_chunks(log.retrieved_chunks),
|
||||
"inputToken": log.input_token,
|
||||
"outputToken": log.output_token,
|
||||
"totalToken": log.total_token,
|
||||
@@ -247,3 +250,29 @@ def _ai_log_dict(log: AiRequestLog, *, include_prompt: bool = False) -> dict:
|
||||
if include_prompt:
|
||||
data["prompt"] = log.prompt
|
||||
return data
|
||||
|
||||
|
||||
def _parse_retrieved_chunks(raw: str | None) -> list[dict[str, Any]]:
|
||||
if not raw:
|
||||
return []
|
||||
try:
|
||||
chunks = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
return []
|
||||
if not isinstance(chunks, list):
|
||||
return []
|
||||
normalized: list[dict[str, Any]] = []
|
||||
for index, item in enumerate(chunks, start=1):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
normalized.append(
|
||||
{
|
||||
"index": item.get("index") or index,
|
||||
"knowledgeId": item.get("knowledgeId"),
|
||||
"knowledgeName": item.get("knowledgeName") or "",
|
||||
"title": item.get("title") or "",
|
||||
"content": item.get("content") or "",
|
||||
"sourceUrl": item.get("sourceUrl") or None,
|
||||
}
|
||||
)
|
||||
return normalized
|
||||
|
||||
Reference in New Issue
Block a user