55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
from fastapi import APIRouter, Depends, Request
|
|
from app.api.deps import get_api_key
|
|
from app.core.limiter import limiter
|
|
from app.core.config import settings
|
|
from pydantic import BaseModel
|
|
from google import genai
|
|
import asyncio
|
|
|
|
router = APIRouter()
|
|
|
|
class LLMRequest(BaseModel):
|
|
prompt: str
|
|
context: str = ""
|
|
|
|
# Shared client instance (global)
|
|
_client = None
|
|
|
|
def get_gemini_client():
|
|
global _client
|
|
if _client is None and settings.GOOGLE_API_KEY and settings.GOOGLE_API_KEY != "your-google-api-key":
|
|
_client = genai.Client(api_key=settings.GOOGLE_API_KEY, http_options={'api_version': 'v1alpha'})
|
|
return _client
|
|
|
|
@router.post("/chat")
|
|
@limiter.limit(settings.RATE_LIMIT)
|
|
async def gemini_chat(
|
|
request: Request,
|
|
chat_data: LLMRequest,
|
|
api_key: str = Depends(get_api_key)
|
|
):
|
|
client = get_gemini_client()
|
|
|
|
try:
|
|
if not client:
|
|
return {
|
|
"status": "mock",
|
|
"model": "gemini",
|
|
"response": f"MOCK: Gemini response to '{chat_data.prompt}'"
|
|
}
|
|
|
|
# Using the async generation method provided by the new google-genai library
|
|
# We use await to ensure we don't block the event loop
|
|
response = await client.aio.models.generate_content(
|
|
model="gemini-2.0-flash",
|
|
contents=chat_data.prompt
|
|
)
|
|
|
|
return {
|
|
"status": "success",
|
|
"model": "gemini",
|
|
"response": response.text
|
|
}
|
|
except Exception as e:
|
|
return {"status": "error", "detail": str(e)}
|