( # pyright: ignore[reportUnusedFunction]
http_request: Request,
body: CreateCompletionRequest,
)
| 15940 | |
| 15941 | @app.post("/v1/completions") |
| 15942 | async def create_completion( # pyright: ignore[reportUnusedFunction] |
| 15943 | http_request: Request, |
| 15944 | body: CreateCompletionRequest, |
| 15945 | ): |
| 15946 | service: CompletionService = app.state.service |
| 15947 | formatter = service.formatter |
| 15948 | prompts = body.normalized_prompt() |
| 15949 | if len(prompts) > 1: |
| 15950 | if body.stream: |
| 15951 | raise HTTPException( |
| 15952 | status_code=400, |
| 15953 | detail="streaming does not support multiple prompts", |
| 15954 | ) |
| 15955 | try: |
| 15956 | submissions = [ |
| 15957 | service.submit(body.model_copy(update={"prompt": prompt})) |
| 15958 | for prompt in prompts |
| 15959 | ] |
| 15960 | except CompletionRequestValidationError as exc: |
| 15961 | raise bad_request(exc) from exc |
| 15962 | results = await collect_completion_results( |
| 15963 | formatter, http_request, submissions |
| 15964 | ) |
| 15965 | if isinstance(results, Response): |
| 15966 | return results |
| 15967 | return JSONResponse( |
| 15968 | formatter.aggregate_completion_results(results).model_dump( |
| 15969 | mode="json", |
| 15970 | exclude_none=True, |
| 15971 | ) |
| 15972 | ) |
| 15973 | try: |
| 15974 | stream, cancel = service.submit( |
| 15975 | body.model_copy(update={"prompt": prompts[0]}) |
| 15976 | ) |
| 15977 | except CompletionRequestValidationError as exc: |
| 15978 | raise bad_request(exc) from exc |
| 15979 | if body.stream: |
| 15980 | return StreamingResponse( |
| 15981 | stream_sse_chunks( |
| 15982 | formatter, |
| 15983 | http_request, |
| 15984 | stream, |
| 15985 | cancel, |
| 15986 | lambda chunk: [chunk], |
| 15987 | ), |
| 15988 | media_type="text/event-stream", |
| 15989 | ) |
| 15990 | result = await collect_completion_result( |
| 15991 | formatter, http_request, stream, cancel |
| 15992 | ) |
| 15993 | if isinstance(result, Response): |
| 15994 | return result |
| 15995 | return JSONResponse(result.model_dump(mode="json", exclude_none=True)) |
| 15996 | |
| 15997 | @app.post("/v1/embeddings", response_model=CreateEmbeddingResponse) |
| 15998 | async def create_embedding( # pyright: ignore[reportUnusedFunction] |
nothing calls this directly
no test coverage detected
searching dependent graphs…