rag snippet生成入库和写rag

This commit is contained in:
zhaoawd
2025-12-09 00:15:22 +08:00
parent ebd79b75bd
commit 3218e51bad
11 changed files with 1231 additions and 8 deletions

View File

@ -16,6 +16,8 @@ from fastapi.responses import JSONResponse
from app.exceptions import ProviderAPICallError, ProviderConfigurationError
from app.models import (
ActionStatus,
ActionType,
DataImportAnalysisJobAck,
DataImportAnalysisJobRequest,
LLMRequest,
@ -25,10 +27,11 @@ from app.models import (
TableSnippetUpsertRequest,
TableSnippetUpsertResponse,
)
from app.routers import chat_router, metrics_router
from app.services import LLMGateway
from app.services.import_analysis import process_import_analysis_job
from app.services.table_profiling import process_table_profiling_job
from app.services.table_snippet import upsert_action_result
from app.services.table_snippet import ingest_snippet_rag_from_db, upsert_action_result
def _ensure_log_directories(config: dict[str, Any]) -> None:
@ -135,6 +138,9 @@ def create_app() -> FastAPI:
version="0.1.0",
lifespan=lifespan,
)
# Chat/metric management APIs
application.include_router(chat_router)
application.include_router(metrics_router)
@application.exception_handler(RequestValidationError)
async def request_validation_exception_handler(
@ -230,11 +236,12 @@ def create_app() -> FastAPI:
)
async def upsert_table_snippet(
payload: TableSnippetUpsertRequest,
client: httpx.AsyncClient = Depends(get_http_client),
) -> TableSnippetUpsertResponse:
request_copy = payload.model_copy(deep=True)
try:
return await asyncio.to_thread(upsert_action_result, request_copy)
response = await asyncio.to_thread(upsert_action_result, request_copy)
except Exception as exc:
logger.error(
"Failed to upsert table snippet: table_id=%s version_ts=%s action_type=%s",
@ -244,6 +251,29 @@ def create_app() -> FastAPI:
exc_info=True,
)
raise HTTPException(status_code=500, detail=str(exc)) from exc
else:
if (
payload.action_type == ActionType.SNIPPET_ALIAS
and payload.status == ActionStatus.SUCCESS
and payload.rag_workspace_id is not None
):
try:
await ingest_snippet_rag_from_db(
table_id=payload.table_id,
version_ts=payload.version_ts,
workspace_id=payload.rag_workspace_id,
rag_item_type=payload.rag_item_type or "SNIPPET",
client=client,
)
except Exception:
logger.exception(
"Failed to ingest snippet RAG artifacts",
extra={
"table_id": payload.table_id,
"version_ts": payload.version_ts,
},
)
return response
@application.post("/__mock__/import-callback")
async def mock_import_callback(payload: dict[str, Any]) -> dict[str, str]: