feat: adiciona formatos e metadados ao RAG
This commit is contained in:
parent
e7850f0416
commit
c2c2fc716c
10 changed files with 400 additions and 21 deletions
|
|
@ -9,7 +9,7 @@ WORKDIR /app
|
|||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY app.py .
|
||||
COPY app.py source_metadata.py ./
|
||||
|
||||
RUN useradd --system --uid 10001 ativa-rag && \
|
||||
chown -R 10001:10001 /app
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ from fastapi import FastAPI, Header, HTTPException
|
|||
from fastapi.responses import JSONResponse, StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from source_metadata import source_reference
|
||||
|
||||
|
||||
QDRANT_URL = os.getenv("QDRANT_URL", "http://192.168.0.153:6333").rstrip("/")
|
||||
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://192.168.0.106:11434").rstrip("/")
|
||||
|
|
@ -129,12 +131,7 @@ def build_context(results: list[dict[str, Any]]) -> tuple[str, list[dict[str, An
|
|||
context_parts.append(block)
|
||||
used_chars += len(block)
|
||||
|
||||
sources.append(
|
||||
{
|
||||
"source": source,
|
||||
"score": round(float(item.get("score", 0.0)), 6),
|
||||
}
|
||||
)
|
||||
sources.append(source_reference(payload, item.get("score", 0.0)))
|
||||
|
||||
return "\n---\n".join(context_parts), sources
|
||||
|
||||
|
|
|
|||
21
rag-api/source_metadata.py
Normal file
21
rag-api/source_metadata.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
"""Helpers to expose Qdrant source metadata without inventing citations."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
SOURCE_FIELDS = ("file_type", "section", "page", "document_id", "chunk_index")
|
||||
|
||||
|
||||
def source_reference(payload: dict[str, Any], score: Any) -> dict[str, Any]:
|
||||
source = payload.get("source") or payload.get("path") or "fonte_desconhecida"
|
||||
reference: dict[str, Any] = {
|
||||
"source": source,
|
||||
"score": round(float(score or 0.0), 6),
|
||||
}
|
||||
for field in SOURCE_FIELDS:
|
||||
value = payload.get(field)
|
||||
if value is not None:
|
||||
reference[field] = value
|
||||
return reference
|
||||
Loading…
Add table
Add a link
Reference in a new issue