OpenAI-compatible unified generation endpoint.
(
request: ChatCompletionRequest,
raw_request: Request,
api_key: str = Depends(verify_api_key_flexible),
)
| 852 | |
| 853 | @router.post("/v1/chat/completions") |
| 854 | async def create_chat_completion( |
| 855 | request: ChatCompletionRequest, |
| 856 | raw_request: Request, |
| 857 | api_key: str = Depends(verify_api_key_flexible), |
| 858 | ): |
| 859 | """OpenAI-compatible unified generation endpoint.""" |
| 860 | try: |
| 861 | normalized = await _normalize_openai_request(request) |
| 862 | if not normalized.prompt: |
| 863 | raise HTTPException(status_code=400, detail="Prompt cannot be empty") |
| 864 | |
| 865 | request_base_url = _get_request_base_url(raw_request) |
| 866 | |
| 867 | if request.stream: |
| 868 | return StreamingResponse( |
| 869 | _iterate_openai_stream(normalized, request_base_url), |
| 870 | media_type="text/event-stream", |
| 871 | headers={ |
| 872 | "Cache-Control": "no-cache", |
| 873 | "Connection": "keep-alive", |
| 874 | "X-Accel-Buffering": "no", |
| 875 | }, |
| 876 | ) |
| 877 | |
| 878 | payload = _parse_handler_result( |
| 879 | await _collect_non_stream_result( |
| 880 | normalized.model, |
| 881 | normalized.prompt, |
| 882 | normalized.images, |
| 883 | base_url_override=request_base_url, |
| 884 | video_media_id=normalized.video_media_id, |
| 885 | ) |
| 886 | ) |
| 887 | return _build_openai_json_response(payload) |
| 888 | |
| 889 | except HTTPException: |
| 890 | raise |
| 891 | except Exception as exc: |
| 892 | raise HTTPException(status_code=500, detail=str(exc)) |
| 893 | |
| 894 | |
| 895 | @router.post("/v1beta/models/{model}:generateContent") |
nothing calls this directly
no test coverage detected