Skip to content

Commit 3524899

Browse files
committed
fix: cache
1 parent b76e820 commit 3524899

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

application/cache.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def gen_cache_key(messages, model="docgpt", tools=None):
4545

4646
def gen_cache(func):
4747
def wrapper(self, model, messages, stream, tools=None, *args, **kwargs):
48+
if tools is not None:
49+
return func(self, model, messages, stream, tools, *args, **kwargs)
50+
4851
try:
4952
cache_key = gen_cache_key(messages, model, tools)
5053
except ValueError as e:
@@ -74,12 +77,16 @@ def wrapper(self, model, messages, stream, tools=None, *args, **kwargs):
7477

7578
def stream_cache(func):
7679
def wrapper(self, model, messages, stream, tools=None, *args, **kwargs):
80+
if tools is not None:
81+
yield from func(self, model, messages, stream, tools, *args, **kwargs)
82+
return
83+
7784
try:
7885
cache_key = gen_cache_key(messages, model, tools)
7986
except ValueError as e:
8087
logger.error(f"Cache key generation failed: {e}")
81-
result = func(self, model, messages, stream, tools=tools, *args, **kwargs)
82-
return result
88+
yield from func(self, model, messages, stream, tools, *args, **kwargs)
89+
return
8390

8491
redis_client = get_redis_instance()
8592
if redis_client:
@@ -90,15 +97,13 @@ def wrapper(self, model, messages, stream, tools=None, *args, **kwargs):
9097
cached_response = json.loads(cached_response.decode("utf-8"))
9198
for chunk in cached_response:
9299
yield chunk
93-
time.sleep(0.03)
100+
time.sleep(0.03) # Simulate streaming delay
94101
return
95102
except Exception as e:
96103
logger.error(f"Error getting cached stream: {e}")
97104

98-
result = func(self, model, messages, stream, tools=tools, *args, **kwargs)
99105
stream_cache_data = []
100-
101-
for chunk in result:
106+
for chunk in func(self, model, messages, stream, tools, *args, **kwargs):
102107
yield chunk
103108
stream_cache_data.append(str(chunk))
104109

0 commit comments

Comments
 (0)