Files
Ai/rag/retrieve.py
T

39 lines
1.3 KiB
Python
Raw Normal View History

"""
بازیابی RAG: embed کوئری و جستجو در vector store
"""
from .config import load_rag_config, RAGConfig
from .embedding import embed_single
from .vector_store import QdrantVectorStore
def search_with_query(
query: str,
sensor_uuid: str,
limit: int = 5,
score_threshold: float | None = None,
config: RAGConfig | None = None,
2026-03-19 22:54:29 +03:30
kb_name: str | None = None,
) -> list[dict]:
"""
کوئری را embed می‌کند و در vector store جستجو می‌کند.
فقط chunks مربوط به sensor_uuid یا __global__ برمی‌گردد (ایزوله‌سازی کاربر).
2026-03-19 22:54:29 +03:30
kb_name: اختیاری — فیلتر بر اساس پایگاه دانش.
Args:
sensor_uuid: شناسه سنسور کاربر — اجباری برای امنیت
2026-03-19 22:54:29 +03:30
kb_name: نام پایگاه دانش (chat/irrigation/fertilization)
Returns:
لیست نتایج با id, score, text, metadata
"""
cfg = config or load_rag_config()
query_vector = embed_single(query, config=cfg)
store = QdrantVectorStore(config=cfg)
return store.search(
query_vector=query_vector,
limit=limit,
score_threshold=score_threshold,
sensor_uuid=sensor_uuid,
2026-03-19 22:54:29 +03:30
kb_name=kb_name,
)