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
|
||||
|
||||
@@ -19,6 +19,7 @@ class AiRequestLog(Base):
|
||||
prompt: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
knowledge_ids: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
retrieve_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
retrieved_chunks: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
input_token: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
output_token: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
total_token: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections.abc import Sequence
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.logs import AiRequestLog
|
||||
from app.services.rag_service import RetrievedChunk
|
||||
|
||||
|
||||
class AiRequestLogService:
|
||||
@@ -20,6 +24,7 @@ class AiRequestLogService:
|
||||
input_token: int,
|
||||
output_token: int,
|
||||
cost_ms: int,
|
||||
retrieved_chunks: Sequence[RetrievedChunk] | None = None,
|
||||
) -> None:
|
||||
db.add(
|
||||
AiRequestLog(
|
||||
@@ -30,6 +35,7 @@ class AiRequestLogService:
|
||||
prompt=prompt,
|
||||
knowledge_ids=knowledge_ids,
|
||||
retrieve_count=retrieve_count,
|
||||
retrieved_chunks=_dump_retrieved_chunks(retrieved_chunks),
|
||||
input_token=input_token,
|
||||
output_token=output_token,
|
||||
total_token=input_token + output_token,
|
||||
@@ -51,6 +57,7 @@ class AiRequestLogService:
|
||||
retrieve_count: int,
|
||||
cost_ms: int,
|
||||
error_message: str,
|
||||
retrieved_chunks: Sequence[RetrievedChunk] | None = None,
|
||||
) -> None:
|
||||
db.add(
|
||||
AiRequestLog(
|
||||
@@ -61,8 +68,26 @@ class AiRequestLogService:
|
||||
prompt=prompt,
|
||||
knowledge_ids=knowledge_ids,
|
||||
retrieve_count=retrieve_count,
|
||||
retrieved_chunks=_dump_retrieved_chunks(retrieved_chunks),
|
||||
cost_ms=cost_ms,
|
||||
status="FAILED",
|
||||
error_message=error_message,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _dump_retrieved_chunks(chunks: Sequence[RetrievedChunk] | None) -> str | None:
|
||||
if not chunks:
|
||||
return None
|
||||
payload = [
|
||||
{
|
||||
"index": index,
|
||||
"knowledgeId": chunk.knowledge_id,
|
||||
"knowledgeName": chunk.knowledge_name,
|
||||
"title": chunk.title,
|
||||
"content": chunk.content,
|
||||
"sourceUrl": chunk.source_url,
|
||||
}
|
||||
for index, chunk in enumerate(chunks, start=1)
|
||||
]
|
||||
return json.dumps(payload, ensure_ascii=False)
|
||||
|
||||
@@ -87,6 +87,7 @@ class ChatService:
|
||||
db.flush()
|
||||
|
||||
started_at = perf_counter()
|
||||
rag_result = None
|
||||
try:
|
||||
# 获取历史消息(不含刚插入的 user_message,它还没 flush id)
|
||||
history = list(
|
||||
@@ -121,11 +122,12 @@ class ChatService:
|
||||
message_id=user_message.id,
|
||||
user_id=user.id,
|
||||
model_name=None,
|
||||
prompt=normalized_question,
|
||||
knowledge_ids=None,
|
||||
retrieve_count=0,
|
||||
prompt=rag_result.prompt if rag_result is not None else normalized_question,
|
||||
knowledge_ids=rag_result.knowledge_ids if rag_result is not None else None,
|
||||
retrieve_count=len(rag_result.chunks) if rag_result is not None else 0,
|
||||
cost_ms=cost_ms,
|
||||
error_message=str(exc),
|
||||
retrieved_chunks=rag_result.chunks if rag_result is not None else None,
|
||||
)
|
||||
db.commit()
|
||||
raise HTTPException(
|
||||
@@ -168,6 +170,7 @@ class ChatService:
|
||||
input_token=completion.input_token,
|
||||
output_token=completion.output_token,
|
||||
cost_ms=cost_ms,
|
||||
retrieved_chunks=rag_result.chunks,
|
||||
)
|
||||
db.commit()
|
||||
return completion.answer
|
||||
|
||||
@@ -80,6 +80,7 @@ class ChatStreamService:
|
||||
retrieve_count=len(rag_result.chunks) if rag_result is not None else 0,
|
||||
cost_ms=cost_ms,
|
||||
error_message=str(exc),
|
||||
retrieved_chunks=rag_result.chunks if rag_result is not None else None,
|
||||
)
|
||||
db.commit()
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc)) from exc
|
||||
@@ -98,6 +99,7 @@ class ChatStreamService:
|
||||
retrieve_count=len(rag_result.chunks) if rag_result is not None else 0,
|
||||
cost_ms=cost_ms,
|
||||
error_message="模型未返回有效内容",
|
||||
retrieved_chunks=rag_result.chunks if rag_result is not None else None,
|
||||
)
|
||||
db.commit()
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="模型未返回有效内容")
|
||||
@@ -137,6 +139,7 @@ class ChatStreamService:
|
||||
input_token=model_response.input_token if model_response is not None else 0,
|
||||
output_token=_rough_token_count(answer),
|
||||
cost_ms=cost_ms,
|
||||
retrieved_chunks=rag_result.chunks if rag_result is not None else None,
|
||||
)
|
||||
db.commit()
|
||||
|
||||
@@ -205,6 +208,7 @@ class ChatStreamService:
|
||||
retrieve_count=len(rag_result.chunks) if rag_result is not None else 0,
|
||||
cost_ms=cost_ms,
|
||||
error_message=str(exc),
|
||||
retrieved_chunks=rag_result.chunks if rag_result is not None else None,
|
||||
)
|
||||
db.commit()
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(exc)) from exc
|
||||
@@ -223,6 +227,7 @@ class ChatStreamService:
|
||||
retrieve_count=len(rag_result.chunks) if rag_result is not None else 0,
|
||||
cost_ms=cost_ms,
|
||||
error_message="模型未返回有效内容",
|
||||
retrieved_chunks=rag_result.chunks if rag_result is not None else None,
|
||||
)
|
||||
db.commit()
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail="模型未返回有效内容")
|
||||
@@ -295,6 +300,7 @@ def _write_success(
|
||||
input_token=model_response.input_token if model_response is not None else 0,
|
||||
output_token=_rough_token_count(answer),
|
||||
cost_ms=cost_ms,
|
||||
retrieved_chunks=rag_result.chunks if rag_result is not None else None,
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user