Update chat_model.py (#9318)

### What problem does this PR solve?
https://github.com/infiniflow/ragflow/issues/9317
base on
https://discuss.ai.google.dev/t/valueerror-invalid-operation-the-response-text-quick-accessor-requires-the-response-to-contain-a-valid-part-but-none-were-returned/42866
should can be handled by retry 
### Type of change

- [x] Refactoring
This commit is contained in:
Stephen Hu
2025-08-08 14:13:07 +08:00
committed by GitHub
parent 392f5f4ce9
commit 7713e14d6a

View File

@ -1099,9 +1099,20 @@ class GeminiChat(Base):
if system: if system:
self.model._system_instruction = content_types.to_content(system) self.model._system_instruction = content_types.to_content(system)
response = self.model.generate_content(hist, generation_config=gen_conf) retry_count = 0
ans = response.text max_retries = 3
return ans, response.usage_metadata.total_token_count while retry_count < max_retries:
try:
response = self.model.generate_content(hist, generation_config=gen_conf)
ans = response.text
return ans, response.usage_metadata.total_token_count
except Exception as e:
retry_count += 1
if retry_count >= max_retries:
raise e
else:
import time
time.sleep(50)
def chat_streamly(self, system, history, gen_conf={}, **kwargs): def chat_streamly(self, system, history, gen_conf={}, **kwargs):
from google.generativeai.types import content_types from google.generativeai.types import content_types