Spaces:
Sleeping
Sleeping
Download services/query_service.py from Qurrah/replyflow-rag: direct link, hf CLI and curl.
- Browser
- Download file 5.82 kB
-
https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/services/query_service.py
- Command line
-
hf download hf://spaces/Qurrah/replyflow-rag/services/query_service.py
-
curl -L -o query_service.py https://huggingface.co/spaces/Qurrah/replyflow-rag/resolve/main/services/query_service.py
5.82 kB
| import logging | |
| import threading | |
| from core.config import query_executor | |
| from core.cache import credentials_cache, instant_reply_cache, rate_limiter | |
| from core.exceptions import WorkspaceNotFoundError | |
| from repositories.comment import insert_comment | |
| from repositories.social_account import ( | |
| get_shopify_credentials as get_shopify_credentials_for_workspace, | |
| get_workspace_by_channel as get_workspace_for_channel, | |
| ) | |
| from rag.generator import generate_rag_answer, generate_reply_from_contexts | |
| from rag.query_router import classify_query_route | |
| from rag.retriever import retrieve | |
| from services.product_search_service import build_inventory_context | |
| REQUEST_GATE = threading.Semaphore(20) | |
| logger = logging.getLogger(__name__) | |
| class RateLimitExceeded(Exception): | |
| pass | |
| def get_workspace_by_channel(channel: str, channel_identifier: str) -> tuple: | |
| try: | |
| workspace, channel_row = get_workspace_for_channel(channel, channel_identifier) | |
| except WorkspaceNotFoundError: | |
| logger.warning(f"No workspace found for {channel}: {channel_identifier}") | |
| raise | |
| logger.debug(f"Resolved channel {channel_identifier} to workspace {workspace['id']}") | |
| return workspace, channel_row | |
| def get_shopify_credentials(workspace_id: str) -> dict: | |
| cache_key = f"shopify_creds:{workspace_id}" | |
| cached = credentials_cache.get(cache_key) | |
| if cached: | |
| logger.debug(f"Cache hit for Shopify credentials workspace={workspace_id}") | |
| return cached | |
| try: | |
| credentials = get_shopify_credentials_for_workspace(workspace_id) | |
| except WorkspaceNotFoundError: | |
| logger.warning(f"No Shopify channel found for workspace {workspace_id}") | |
| raise | |
| credentials_cache.set(cache_key, credentials) | |
| return credentials | |
| def log_comment( | |
| workspace_id: str, | |
| channel: str, | |
| comment_text: str, | |
| reply_text: str, | |
| route: str, | |
| external_id: str = None, | |
| commenter_handle: str = None, | |
| ): | |
| insert_comment( | |
| workspace_id=workspace_id, | |
| channel=channel, | |
| comment_text=comment_text, | |
| reply_text=reply_text, | |
| route=route, | |
| external_id=external_id, | |
| commenter_handle=commenter_handle, | |
| ) | |
| def answer( | |
| query: str, | |
| channel: str, | |
| channel_identifier: str, | |
| external_id: str = None, | |
| commenter_handle: str = None, | |
| ) -> str: | |
| logger.info(f"Incoming query | channel={channel} | identifier={channel_identifier}") | |
| workspace, _ = get_workspace_by_channel(channel, channel_identifier) | |
| workspace_id = workspace["id"] | |
| brand_name = workspace["name"] | |
| shopify = get_shopify_credentials(workspace_id) | |
| shop_domain = shopify["channel_identifier"] | |
| access_token = shopify["credentials"]["access_token"] | |
| decision = classify_query_route(query) | |
| logger.info(f"Router decision: {decision} | query='{query[:60]}'") | |
| if decision == "RAG": | |
| chunks = retrieve(query, workspace_id, top_k=5) | |
| logger.debug(f"Retrieved {len(chunks)} chunks for RAG") | |
| reply = generate_rag_answer(query, chunks, brand_name) | |
| elif decision == "INVENTORY": | |
| product_context = build_inventory_context(shop_domain, access_token, query, channel_identifier) | |
| reply = generate_rag_answer( | |
| query=query, | |
| chunks=[{"source_type": "inventory", "content": product_context}], | |
| brand_name=brand_name, | |
| ) | |
| elif decision == "BOTH": | |
| chunks = retrieve(query, workspace_id, top_k=3) | |
| rag_context = "\n".join([c["content"] for c in chunks]) | |
| product_context = build_inventory_context(shop_domain, access_token, query, channel_identifier) | |
| reply = generate_reply_from_contexts(query, rag_context, product_context, brand_name) | |
| else: | |
| logger.warning(f"Unknown router decision: {decision}") | |
| reply = "I'm not sure how to answer that." | |
| decision = "UNKNOWN" | |
| log_comment( | |
| workspace_id=workspace_id, | |
| channel=channel, | |
| comment_text=query, | |
| reply_text=reply, | |
| route=decision, | |
| external_id=external_id, | |
| commenter_handle=commenter_handle, | |
| ) | |
| logger.info(f"Reply generated | route={decision} | workspace={workspace_id}") | |
| return reply | |
| def answer_query_request( | |
| query: str, | |
| channel: str, | |
| channel_identifier: str, | |
| external_id: str = None, | |
| commenter_handle: str = None, | |
| post_caption: str = None, | |
| ) -> str: | |
| if not query.strip(): | |
| raise ValueError("Query cannot be empty") | |
| if not REQUEST_GATE.acquire(blocking=False): | |
| logger.warning("Request gate full — returning fallback reply | identifier=%s", channel_identifier) | |
| return "We're getting a lot of messages right now — we'll get back to you shortly." | |
| try: | |
| if not rate_limiter.is_allowed(channel_identifier): | |
| logger.warning(f"Rate limit hit for identifier={channel_identifier}") | |
| raise RateLimitExceeded("Too many requests — please slow down") | |
| enriched_query = query | |
| if post_caption: | |
| enriched_query = f"Post context: {post_caption}\nUser question: {query}" | |
| caption_part = post_caption.strip().lower() if post_caption else "" | |
| cache_key = f"comment:{channel_identifier}:{query.strip().lower()}:{caption_part}" | |
| cached_reply = instant_reply_cache.get(cache_key) | |
| if cached_reply: | |
| logger.info(f"Cache hit for query='{query[:60]}'") | |
| return cached_reply | |
| reply = answer( | |
| query=enriched_query, | |
| channel=channel, | |
| channel_identifier=channel_identifier, | |
| external_id=external_id, | |
| commenter_handle=commenter_handle, | |
| ) | |
| instant_reply_cache.set(cache_key, reply) | |
| return reply | |
| finally: | |
| REQUEST_GATE.release() | |