Skip to content

Commit 3e7eed7

Browse files
fix: handle gemini config
1 parent 182e387 commit 3e7eed7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

app/services/gemini.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ class Catalog(BaseModel):
1515

1616
class GeminiService:
1717
def __init__(self, model: str = settings.DEFAULT_GEMINI_MODEL):
18-
self.client = genai.Client(api_key=settings.GEMINI_API_KEY)
1918
self.model = model
19+
self.client = None
20+
if api_key := settings.GEMINI_API_KEY:
21+
try:
22+
self.client = genai.Client(api_key=api_key)
23+
except Exception as e:
24+
logger.warning(f"Failed to initialize Gemini client: {e}")
25+
else:
26+
logger.warning("GEMINI_API_KEY not set. Gemini features will be disabled.")
2027

2128
@staticmethod
2229
def get_prompt():
@@ -41,18 +48,15 @@ def get_prompt():
4148

4249
def generate_content(self, prompt: str) -> str:
4350
system_prompt = self.get_prompt()
51+
if not self.client:
52+
logger.warning("Gemini client not initialized. Gemini features will be disabled.")
53+
return ""
4454
try:
4555
response = self.client.models.generate_content(
4656
model=self.model,
4757
contents=system_prompt + "\n\n" + prompt,
48-
# config={
49-
# # "response_mime_type": "application/json",
50-
# # "response_json_schema": Catalog.model_json_schema(),
51-
# "system_instruction": system_prompt,
52-
# },
5358
)
54-
print(response.text)
55-
return response.text
59+
return response.text.strip()
5660
except Exception as e:
5761
logger.error(f"Error generating content: {e}")
5862
return ""

0 commit comments

Comments
 (0)