Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion worker_api/llm/llm_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import asyncio
import logging
from typing import Optional
from google import genai
from google.genai import types
from fastapi import HTTPException, status

from worker_api.config import get

logger = logging.getLogger(__name__)

DEFAULT_MODEL = "gemini-2.5-flash"


Expand All @@ -15,6 +18,8 @@ def _chat_with_gemini_sync(
model: Optional[str] = None
) -> dict:
api_key = get("GEMINI_API_KEY")
logger.info(f"GEMINI_API_KEY configured: {bool(api_key)}, key prefix: {api_key[:10] if api_key else 'N/A'}...")

if not api_key:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
Expand All @@ -24,6 +29,7 @@ def _chat_with_gemini_sync(
client = genai.Client(api_key=api_key)

model_name = model or DEFAULT_MODEL
logger.info(f"Requested model: {model}, Using model: {model_name}")

config = types.GenerateContentConfig(
temperature=1.0,
Expand All @@ -40,15 +46,18 @@ def _chat_with_gemini_sync(
]

try:
logger.info(f"Calling Gemini API with prompt length: {len(prompt)}")
response = client.models.generate_content(
model=model_name,
contents=contents,
config=config,
)
logger.info("Gemini API call successful")
except Exception as e:
logger.error(f"Gemini API error: {type(e).__name__}: {str(e)}", exc_info=True)
raise HTTPException(
status_code=status.HTTP_502_BAD_GATEWAY,
detail=f"Gemini API error: {str(e)}"
detail=f"Gemini API error: {type(e).__name__}: {str(e)}"
)

if not response.candidates or not response.candidates[0].content.parts:
Expand Down
Loading
Loading