fix max token error
Browse files- app/gemini_client.py +14 -3
app/gemini_client.py
CHANGED
|
@@ -2,8 +2,8 @@ from google.generativeai.embedding import embed_content
|
|
| 2 |
from google.generativeai.client import configure
|
| 3 |
from google.generativeai.generative_models import GenerativeModel
|
| 4 |
from loguru import logger
|
| 5 |
-
from typing import List, Optional
|
| 6 |
-
from google.generativeai.types import GenerationConfig
|
| 7 |
|
| 8 |
from .request_limit_manager import RequestLimitManager
|
| 9 |
from .utils import (
|
|
@@ -27,6 +27,13 @@ class GeminiClient:
|
|
| 27 |
self._cached_model = None
|
| 28 |
self._cached_key = None
|
| 29 |
self._cached_model_instance = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def _get_model_instance(self, key: str, model: str):
|
| 32 |
"""
|
|
@@ -68,7 +75,11 @@ class GeminiClient:
|
|
| 68 |
# Sử dụng cached model instance
|
| 69 |
_model = self._get_model_instance(key, model)
|
| 70 |
|
| 71 |
-
response = _model.generate_content(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
# Log toàn bộ nội dung response ở mức INFO để tiện gỡ lỗi
|
| 74 |
logger.info(f"[GEMINI][RAW_RESPONSE] {response}")
|
|
|
|
| 2 |
from google.generativeai.client import configure
|
| 3 |
from google.generativeai.generative_models import GenerativeModel
|
| 4 |
from loguru import logger
|
| 5 |
+
from typing import Dict, List, Optional
|
| 6 |
+
from google.generativeai.types import GenerationConfig, HarmCategory, HarmBlockThreshold
|
| 7 |
|
| 8 |
from .request_limit_manager import RequestLimitManager
|
| 9 |
from .utils import (
|
|
|
|
| 27 |
self._cached_model = None
|
| 28 |
self._cached_key = None
|
| 29 |
self._cached_model_instance = None
|
| 30 |
+
# Thêm cấu hình safety_settings để bỏ chặn các phản hồi bị coi là không an toàn
|
| 31 |
+
self.safety_settings: Dict[HarmCategory, HarmBlockThreshold] = {
|
| 32 |
+
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
| 33 |
+
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
|
| 34 |
+
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
| 35 |
+
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
| 36 |
+
}
|
| 37 |
|
| 38 |
def _get_model_instance(self, key: str, model: str):
|
| 39 |
"""
|
|
|
|
| 75 |
# Sử dụng cached model instance
|
| 76 |
_model = self._get_model_instance(key, model)
|
| 77 |
|
| 78 |
+
response = _model.generate_content(
|
| 79 |
+
prompt,
|
| 80 |
+
safety_settings=self.safety_settings,
|
| 81 |
+
**kwargs
|
| 82 |
+
)
|
| 83 |
|
| 84 |
# Log toàn bộ nội dung response ở mức INFO để tiện gỡ lỗi
|
| 85 |
logger.info(f"[GEMINI][RAW_RESPONSE] {response}")
|