From 5f9702b91c5bca8ffbc3e348d91d1b176e1ba08f Mon Sep 17 00:00:00 2001
From: Alex
Date: Wed, 12 Mar 2025 13:47:16 +0000
Subject: [PATCH 1/2] fix: /search
---
application/api/answer/routes.py | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/application/api/answer/routes.py b/application/api/answer/routes.py
index 5a221e8d..8488a56d 100644
--- a/application/api/answer/routes.py
+++ b/application/api/answer/routes.py
@@ -706,7 +706,6 @@ class Search(Resource):
retriever = RetrieverCreator.create_retriever(
retriever_name,
- question=question,
source=source,
chat_history=[],
prompt="default",
@@ -716,7 +715,7 @@ class Search(Resource):
user_api_key=user_api_key,
)
- docs = retriever.search()
+ docs = retriever.search(question)
retriever_params = retriever.get_params()
user_logs_collection.insert_one(
From f2b05ad56d701d4d7b55f7e5f7189f6241dd48a8 Mon Sep 17 00:00:00 2001
From: Alex
Date: Wed, 12 Mar 2025 15:13:14 +0000
Subject: [PATCH 2/2] fix: handle cache issues with more grace
---
application/cache.py | 71 +++++++++++++++++++++++++-------------------
1 file changed, 40 insertions(+), 31 deletions(-)
diff --git a/application/cache.py b/application/cache.py
index 80dee4f4..44746434 100644
--- a/application/cache.py
+++ b/application/cache.py
@@ -11,21 +11,25 @@ from application.utils import get_hash
logger = logging.getLogger(__name__)
_redis_instance = None
+_redis_creation_failed = False
_instance_lock = Lock()
-
def get_redis_instance():
- global _redis_instance
- if _redis_instance is None:
+ global _redis_instance, _redis_creation_failed
+ if _redis_instance is None and not _redis_creation_failed:
with _instance_lock:
- if _redis_instance is None:
+ if _redis_instance is None and not _redis_creation_failed:
try:
_redis_instance = redis.Redis.from_url(
settings.CACHE_REDIS_URL, socket_connect_timeout=2
)
+ except ValueError as e:
+ logger.error(f"Invalid Redis URL: {e}")
+ _redis_creation_failed = True # Stop future attempts
+ _redis_instance = None
except redis.ConnectionError as e:
logger.error(f"Redis connection error: {e}")
- _redis_instance = None
+ _redis_instance = None # Keep trying for connection errors
return _redis_instance
@@ -43,34 +47,39 @@ def gen_cache(func):
def wrapper(self, model, messages, stream, tools=None, *args, **kwargs):
try:
cache_key = gen_cache_key(messages, model, tools)
- redis_client = get_redis_instance()
- if redis_client:
- try:
- cached_response = redis_client.get(cache_key)
- if cached_response:
- return cached_response.decode("utf-8")
- except redis.ConnectionError as e:
- logger.error(f"Redis connection error: {e}")
-
- result = func(self, model, messages, stream, tools, *args, **kwargs)
- if redis_client and isinstance(result, str):
- try:
- redis_client.set(cache_key, result, ex=1800)
- except redis.ConnectionError as e:
- logger.error(f"Redis connection error: {e}")
-
- return result
except ValueError as e:
- logger.error(e)
- return "Error: No user message found in the conversation to generate a cache key."
+ logger.error(f"Cache key generation failed: {e}")
+ return func(self, model, messages, stream, tools, *args, **kwargs)
+
+ redis_client = get_redis_instance()
+ if redis_client:
+ try:
+ cached_response = redis_client.get(cache_key)
+ if cached_response:
+ return cached_response.decode("utf-8")
+ except Exception as e:
+ logger.error(f"Error getting cached response: {e}")
+
+ result = func(self, model, messages, stream, tools, *args, **kwargs)
+ if redis_client and isinstance(result, str):
+ try:
+ redis_client.set(cache_key, result, ex=1800)
+ except Exception as e:
+ logger.error(f"Error setting cache: {e}")
+
+ return result
return wrapper
def stream_cache(func):
def wrapper(self, model, messages, stream, tools=None, *args, **kwargs):
- cache_key = gen_cache_key(messages, model, tools)
- logger.info(f"Stream cache key: {cache_key}")
+ try:
+ cache_key = gen_cache_key(messages, model, tools)
+ except ValueError as e:
+ logger.error(f"Cache key generation failed: {e}")
+ result = func(self, model, messages, stream, tools=tools, *args, **kwargs)
+ return result
redis_client = get_redis_instance()
if redis_client:
@@ -83,21 +92,21 @@ def stream_cache(func):
yield chunk
time.sleep(0.03)
return
- except redis.ConnectionError as e:
- logger.error(f"Redis connection error: {e}")
+ except Exception as e:
+ logger.error(f"Error getting cached stream: {e}")
result = func(self, model, messages, stream, tools=tools, *args, **kwargs)
stream_cache_data = []
for chunk in result:
- stream_cache_data.append(chunk)
yield chunk
+ stream_cache_data.append(str(chunk))
if redis_client:
try:
redis_client.set(cache_key, json.dumps(stream_cache_data), ex=1800)
logger.info(f"Stream cache saved for key: {cache_key}")
- except redis.ConnectionError as e:
- logger.error(f"Redis connection error: {e}")
+ except Exception as e:
+ logger.error(f"Error setting stream cache: {e}")
return wrapper