mirror of
https://github.com/hoshikawa2/first_contas.git
synced 2026-07-09 18:24:20 +00:00
first commit
This commit is contained in:
81
legacy_reference/workflows/actions/rag/helpers.py
Normal file
81
legacy_reference/workflows/actions/rag/helpers.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
def _normalize_rag_queries(params: dict[str, Any]) -> list[str]:
|
||||
raw_queries = params.get("queries")
|
||||
queries: list[str] = []
|
||||
if isinstance(raw_queries, (list, tuple)):
|
||||
for item in raw_queries:
|
||||
text = str(item or "").strip()
|
||||
if text:
|
||||
queries.append(text)
|
||||
elif isinstance(raw_queries, str) and raw_queries.strip():
|
||||
queries.append(raw_queries.strip())
|
||||
|
||||
if not queries:
|
||||
query = str(params.get("query", "")).strip()
|
||||
if query:
|
||||
queries.append(query)
|
||||
return queries
|
||||
|
||||
|
||||
def _extract_rag_documents(payload: Any) -> list[dict[str, Any]]:
|
||||
documents: list[dict[str, Any]] = []
|
||||
if not isinstance(payload, dict):
|
||||
return documents
|
||||
raw_documents = payload.get("documents")
|
||||
if isinstance(raw_documents, (list, tuple)):
|
||||
for item in raw_documents:
|
||||
if isinstance(item, dict):
|
||||
documents.append(dict(item))
|
||||
return documents
|
||||
|
||||
|
||||
def _rag_document_title(doc: dict[str, Any]) -> str:
|
||||
return str(
|
||||
doc.get("title_proc")
|
||||
or doc.get("title")
|
||||
or doc.get("chunk_texto")
|
||||
or ""
|
||||
).strip()
|
||||
|
||||
|
||||
def _rag_document_text(doc: dict[str, Any]) -> str:
|
||||
return str(
|
||||
doc.get("chunk_texto")
|
||||
or doc.get("text")
|
||||
or doc.get("title_proc")
|
||||
or doc.get("title")
|
||||
or ""
|
||||
).strip()
|
||||
|
||||
|
||||
def _is_selected_rag_document(doc: dict[str, Any]) -> bool:
|
||||
try:
|
||||
return float(doc.get("distance", 1.0)) <= 0.6
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def _build_rag_answer_item(query: str, documents: list[dict[str, Any]]) -> str:
|
||||
if not documents:
|
||||
return f"{query}: Nao encontrei trechos relevantes para essa busca."
|
||||
snippets = []
|
||||
for doc in documents:
|
||||
text = _rag_document_text(doc)
|
||||
if text:
|
||||
snippets.append(text)
|
||||
if not snippets:
|
||||
return f"{query}: Encontrei trecho(s), mas sem texto disponivel."
|
||||
return f"{query}: {' '.join(snippets)}"
|
||||
|
||||
|
||||
__all__ = [
|
||||
'_normalize_rag_queries',
|
||||
'_extract_rag_documents',
|
||||
'_rag_document_title',
|
||||
'_rag_document_text',
|
||||
'_is_selected_rag_document',
|
||||
'_build_rag_answer_item',
|
||||
]
|
||||
Reference in New Issue
Block a user