(
request: Request,
body: CreateCompletionRequest,
)
| 301 | tags=[openai_v1_tag], |
| 302 | ) |
| 303 | async def create_completion( |
| 304 | request: Request, |
| 305 | body: CreateCompletionRequest, |
| 306 | ) -> llama_cpp.Completion: |
| 307 | if isinstance(body.prompt, list): |
| 308 | assert len(body.prompt) <= 1 |
| 309 | body.prompt = body.prompt[0] if len(body.prompt) > 0 else "" |
| 310 | |
| 311 | body_model = ( |
| 312 | body.model |
| 313 | if request.url.path != "/v1/engines/copilot-codex/completions" |
| 314 | else "copilot-codex" |
| 315 | ) |
| 316 | |
| 317 | exclude = { |
| 318 | "n", |
| 319 | "best_of", |
| 320 | "logit_bias_type", |
| 321 | "user", |
| 322 | "min_tokens", |
| 323 | } |
| 324 | kwargs = body.model_dump(exclude=exclude) |
| 325 | |
| 326 | # handle streaming request |
| 327 | if kwargs.get("stream", False): |
| 328 | send_chan, recv_chan = anyio.create_memory_object_stream(10) |
| 329 | return EventSourceResponse( |
| 330 | recv_chan, |
| 331 | data_sender_callable=partial( # type: ignore |
| 332 | get_event_publisher, |
| 333 | request=request, |
| 334 | inner_send_chan=send_chan, |
| 335 | body=body, |
| 336 | body_model=body_model, |
| 337 | llama_call=llama_cpp.Llama.__call__, |
| 338 | kwargs=kwargs, |
| 339 | ), |
| 340 | sep="\n", |
| 341 | ping_message_factory=_ping_message_factory, |
| 342 | ) |
| 343 | |
| 344 | # handle regular request |
| 345 | async with contextlib.asynccontextmanager(get_llama_proxy)() as llama_proxy: |
| 346 | llama = prepare_request_resources(body, llama_proxy, body_model, kwargs) |
| 347 | |
| 348 | if await request.is_disconnected(): |
| 349 | print( |
| 350 | f"Disconnected from client (via refresh/close) before llm invoked {request.client}" |
| 351 | ) |
| 352 | raise HTTPException( |
| 353 | status_code=status.HTTP_400_BAD_REQUEST, |
| 354 | detail="Client closed request", |
| 355 | ) |
| 356 | |
| 357 | return await run_in_threadpool(llama, **kwargs) |
| 358 | |
| 359 | |
| 360 | @router.post( |
nothing calls this directly
no test coverage detected
searching dependent graphs…