Spaces:
Sleeping
Sleeping
REFINE: update cache references from inventory_cache to instant_reply_cache for consistency in query handling
Browse files
services/query_service.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import logging
|
| 2 |
|
| 3 |
-
from core.cache import credentials_cache,
|
| 4 |
from repositories.comment import insert_comment
|
| 5 |
from repositories.social_account import (
|
| 6 |
get_shopify_credentials as get_shopify_credentials_for_workspace,
|
|
@@ -92,7 +92,7 @@ def answer(
|
|
| 92 |
reply = generate_rag_answer(query, chunks, brand_name)
|
| 93 |
|
| 94 |
elif decision == "INVENTORY":
|
| 95 |
-
product_context = build_inventory_context(shop_domain, access_token, query)
|
| 96 |
reply = generate_rag_answer(
|
| 97 |
query=query,
|
| 98 |
chunks=[{"source_type": "inventory", "content": product_context}],
|
|
@@ -102,7 +102,7 @@ def answer(
|
|
| 102 |
elif decision == "BOTH":
|
| 103 |
chunks = retrieve(query, workspace_id, top_k=3)
|
| 104 |
rag_context = "\n".join([c["content"] for c in chunks])
|
| 105 |
-
product_context = build_inventory_context(shop_domain, access_token, query)
|
| 106 |
reply = generate_reply_from_contexts(query, rag_context, product_context, brand_name)
|
| 107 |
|
| 108 |
else:
|
|
@@ -144,8 +144,8 @@ def answer_query_request(
|
|
| 144 |
enriched_query = f"Post context: {post_caption}\nUser question: {query}"
|
| 145 |
|
| 146 |
caption_part = post_caption.strip().lower() if post_caption else ""
|
| 147 |
-
cache_key = f"
|
| 148 |
-
cached_reply =
|
| 149 |
if cached_reply:
|
| 150 |
logger.info(f"Cache hit for query='{query[:60]}'")
|
| 151 |
return cached_reply
|
|
@@ -157,5 +157,5 @@ def answer_query_request(
|
|
| 157 |
external_id=external_id,
|
| 158 |
commenter_handle=commenter_handle,
|
| 159 |
)
|
| 160 |
-
|
| 161 |
return reply
|
|
|
|
| 1 |
import logging
|
| 2 |
|
| 3 |
+
from core.cache import credentials_cache, instant_reply_cache, rate_limiter
|
| 4 |
from repositories.comment import insert_comment
|
| 5 |
from repositories.social_account import (
|
| 6 |
get_shopify_credentials as get_shopify_credentials_for_workspace,
|
|
|
|
| 92 |
reply = generate_rag_answer(query, chunks, brand_name)
|
| 93 |
|
| 94 |
elif decision == "INVENTORY":
|
| 95 |
+
product_context = build_inventory_context(shop_domain, access_token, query, channel_identifier)
|
| 96 |
reply = generate_rag_answer(
|
| 97 |
query=query,
|
| 98 |
chunks=[{"source_type": "inventory", "content": product_context}],
|
|
|
|
| 102 |
elif decision == "BOTH":
|
| 103 |
chunks = retrieve(query, workspace_id, top_k=3)
|
| 104 |
rag_context = "\n".join([c["content"] for c in chunks])
|
| 105 |
+
product_context = build_inventory_context(shop_domain, access_token, query, channel_identifier)
|
| 106 |
reply = generate_reply_from_contexts(query, rag_context, product_context, brand_name)
|
| 107 |
|
| 108 |
else:
|
|
|
|
| 144 |
enriched_query = f"Post context: {post_caption}\nUser question: {query}"
|
| 145 |
|
| 146 |
caption_part = post_caption.strip().lower() if post_caption else ""
|
| 147 |
+
cache_key = f"comment:{channel_identifier}:{query.strip().lower()}:{caption_part}"
|
| 148 |
+
cached_reply = instant_reply_cache.get(cache_key)
|
| 149 |
if cached_reply:
|
| 150 |
logger.info(f"Cache hit for query='{query[:60]}'")
|
| 151 |
return cached_reply
|
|
|
|
| 157 |
external_id=external_id,
|
| 158 |
commenter_handle=commenter_handle,
|
| 159 |
)
|
| 160 |
+
instant_reply_cache.set(cache_key, reply)
|
| 161 |
return reply
|