(
request: Request,
data: ChatCompletionRequest,
auth_info: Dict = Depends(auth_info_required),
)
| 36 | response_model=BaseDataResponse, |
| 37 | ) |
| 38 | async def api_chat_completion( |
| 39 | request: Request, |
| 40 | data: ChatCompletionRequest, |
| 41 | auth_info: Dict = Depends(auth_info_required), |
| 42 | ): |
| 43 | if is_model_id(model_id=data.model_id): |
| 44 | # validate model |
| 45 | model: Model = await model_ops.get(model_id=data.model_id) |
| 46 | |
| 47 | # check function call ability |
| 48 | functions = [function.model_dump() for function in data.functions] if data.functions is not None else None |
| 49 | if functions and not model.allow_function_call(): |
| 50 | raise_request_validation_error(f"Model {model.model_id} does not support function calls.") |
| 51 | |
| 52 | # prepare messages |
| 53 | messages = [message.model_dump() for message in data.messages] |
| 54 | |
| 55 | # perform chat completion with model |
| 56 | if data.stream: |
| 57 | if not model.allow_streaming(): |
| 58 | raise_request_validation_error(f"Model {model.model_id} does not support streaming.") |
| 59 | |
| 60 | async def generator(sse_chunk_dicts): |
| 61 | async for chunk_dict in sse_chunk_dicts: |
| 62 | yield f"data: {json.dumps(chunk_dict)}\n\n" |
| 63 | yield SSE_DONE_MSG |
| 64 | |
| 65 | sse_chunk_dicts = await stream_chat_completion( |
| 66 | model=model, |
| 67 | messages=messages, |
| 68 | configs=data.configs, |
| 69 | function_call=data.function_call, |
| 70 | functions=functions, |
| 71 | ) |
| 72 | |
| 73 | return StreamingResponse( |
| 74 | generator(sse_chunk_dicts), |
| 75 | media_type="text/event-stream", |
| 76 | ) |
| 77 | |
| 78 | else: |
| 79 | # generate none stream response |
| 80 | response_data = await chat_completion( |
| 81 | model=model, |
| 82 | messages=messages, |
| 83 | configs=data.configs, |
| 84 | function_call=data.function_call, |
| 85 | functions=functions, |
| 86 | ) |
| 87 | return BaseDataResponse(data=response_data) |
| 88 | |
| 89 | elif is_assistant_id(assistant_id=data.model_id): |
| 90 | # validate assistant |
| 91 | assistant: Assistant = await assistant_ops.get(assistant_id=data.model_id) |
| 92 | |
| 93 | # perform chat completion with assistant |
| 94 | if data.stream: |
| 95 | session = StatelessStreamSession( |
nothing calls this directly
no test coverage detected