Tweaked CORS

This commit is contained in:
2026-02-10 21:21:42 +08:00
parent c00396756c
commit b15e39d011
3 changed files with 82 additions and 21 deletions

View File

@@ -32,15 +32,54 @@ def get_gemini_client():
_client = genai.Client(api_key=settings.GOOGLE_API_KEY, http_options={'api_version': 'v1alpha'})
return _client
@router.post("/chat")
@router.api_route("/chat", methods=["GET", "POST"])
@limiter.limit(settings.RATE_LIMIT)
async def gemini_chat(
request: Request,
chat_data: LLMRequest,
api_key: str = Depends(get_api_key),
module: Module = Depends(get_current_module),
db: Session = Depends(get_db)
):
chat_data = None
if request.method == "GET":
# Handle GET requests (Ultimate Simple Request)
params = request.query_params
if not params.get("prompt"):
return {"status": "error", "detail": "Missing 'prompt' query parameter"}
chat_data = LLMRequest(
prompt=params.get("prompt"),
context=params.get("context", ""),
system_prompt=params.get("system_prompt"),
knowledge_base=params.get("knowledge_base"),
temperature=float(params.get("temperature", 0.7)),
top_p=float(params.get("top_p", 0.95)),
top_k=int(params.get("top_k", 40)),
max_output_tokens=int(params.get("max_output_tokens", 8192))
)
else:
# Handle POST requests
content_type = request.headers.get("Content-Type", "")
if "text/plain" in content_type:
try:
body = await request.body()
import json
data = json.loads(body)
chat_data = LLMRequest(**data)
except Exception as e:
return {"status": "error", "detail": f"Failed to parse text/plain as JSON: {str(e)}"}
else:
# Standard JSON parsing
try:
data = await request.json()
chat_data = LLMRequest(**data)
except Exception as e:
return {"status": "error", "detail": f"Invalid JSON: {str(e)}"}
if not chat_data:
return {"status": "error", "detail": "Could not determine request data"}
client = get_gemini_client()
try: