Skip to content

Commit fbf9e9d

Browse files
committed
Fix Gemini client handling improvements
1 parent 1c56118 commit fbf9e9d

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

ufo/llm/gemini.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def chat_completion(
124124
f"Retrying in {sleep_time:.2f}s..."
125125
)
126126
time.sleep(sleep_time)
127+
if attempt == self.max_retry - 1:
128+
raise
127129

128130
return self.get_text_from_all_candidates(response), cost
129131

@@ -234,12 +236,21 @@ def get_text_from_all_candidates(
234236

235237
return all_texts
236238

237-
@functools.lru_cache()
238-
@staticmethod
239-
def get_gemini_client(api_key: str) -> genai.Client:
240-
"""
241-
Create a Gemini client using the provided API key.
242-
:param api_key: The API key for authentication.
243-
:return: A Gemini client instance.
244-
"""
245-
return genai.Client(api_key=api_key)
239+
240+
# Module-level cache for Gemini clients
241+
_gemini_client_cache: dict = {}
242+
243+
244+
def _get_gemini_client(api_key: str) -> genai.Client:
245+
"""
246+
Create or retrieve a cached Gemini client using the provided API key.
247+
:param api_key: The API key for authentication.
248+
:return: A Gemini client instance.
249+
"""
250+
if api_key not in _gemini_client_cache:
251+
_gemini_client_cache[api_key] = genai.Client(api_key=api_key)
252+
return _gemini_client_cache[api_key]
253+
254+
255+
# Bind as static method for backward compatibility
256+
GeminiService.get_gemini_client = staticmethod(_get_gemini_client)

0 commit comments

Comments
 (0)