Files
fckbot/rag/rag_api.py
Markov Andrey 59feffc190 Update 9 files
- /rag/config_models.py
- /rag/utils/config_loader.py
- /rag/auth.py
- /rag/rag_server.py
- /rag/rag_api.py
- /rag/rag_orchestrator.py
- /rag/__init__.py
- /bots/xmpp/client.py
- /bots/rag_client.py
2026-06-30 14:27:34 +00:00

122 lines
4.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
Единый API-интерфейс для работы с RAG-оркестратором.
Используется ботами для вызова RAG-логики без необходимости
знать внутреннее устройство оркестратора.
Этот класс является прослойкой (facade) между ботами и RAGOrchestrator.
"""
import logging
from typing import Optional, Dict, Any
from .rag_orchestrator import RAGOrchestrator
from .services.kb_service import KBService
from .services.giga_client import GigaClient
from .services.file_service import FileService
from .services.postgres_service import PostgresService
from .config_models import AppConfig
logger = logging.getLogger(__name__)
class RAGAPI:
"""
Единый API-интерфейс для RAG-функциональности.
Создаёт и содержит экземпляр RAGOrchestrator.
"""
def __init__(
self,
db: PostgresService,
qdrant,
embedding,
kb: KBService,
giga: GigaClient,
files: FileService,
config: AppConfig,
default_prompts: Optional[Dict[str, str]] = None
):
self.config = config
self.default_prompts = default_prompts or {}
self.orchestrator = RAGOrchestrator(
db=db,
qdrant=qdrant,
embedding=embedding,
kb=kb,
giga=giga,
files=files,
config=config,
default_prompts=self.default_prompts
)
logger.info("RAGAPI инициализирован")
async def query(
self,
query: str,
user_jid: str,
room_jid: Optional[str],
prompts: Optional[Dict[str, str]] = None,
intent_override: Optional[str] = None,
last_file_path: Optional[str] = None,
last_file_text: Optional[str] = None,
) -> Dict[str, Any]:
try:
if prompts is None:
prompts = self.default_prompts.copy()
result = await self.orchestrator.process_query(
query=query,
user_jid=user_jid,
room_jid=room_jid,
prompts=prompts,
intent_override=intent_override,
last_file_path=last_file_path,
last_file_text=last_file_text
)
logger.debug(f"RAGAPI.query успешно выполнен для {user_jid}, intent={result.get('intent')}")
return result
except Exception as e:
logger.error(f"Ошибка в RAGAPI.query: {e}", exc_info=True)
return {
"answer": f"⚠️ Произошла ошибка при обработке запроса: {str(e)}",
"intent": "ERROR",
"context": "",
"sources": [],
"confidence": None,
"error": str(e)
}
async def index_document(
self,
file_name: str,
file_text: str,
user_jid: str,
room_jid: Optional[str],
is_global: bool = False,
title: Optional[str] = None,
metadata: Optional[Dict] = None,
file_hash: Optional[str] = None,
update_if_exists: bool = True
) -> Dict[str, Any]:
try:
result = await self.orchestrator.index_document(
file_name=file_name,
file_text=file_text,
user_jid=user_jid,
room_jid=room_jid,
is_global=is_global,
title=title,
metadata=metadata,
file_hash=file_hash,
update_if_exists=update_if_exists
)
logger.info(f"Документ {file_name} проиндексирован, doc_id={result.get('doc_id')}")
return result
except Exception as e:
logger.error(f"Ошибка в RAGAPI.index_document: {e}", exc_info=True)
return {"doc_id": None, "chunk_count": 0, "error": str(e)}
def close(self):
logger.debug("RAGAPI закрывается")