Repository: mlc-ai/mlc-llm
File: python/mlc_llm/serve/entrypoints/openai_entrypoints.py (Python)

Code before:
    ChatFunctionCall,
    ChatToolCall,
    CompletionRequest,
    CompletionResponse,
    CompletionResponseChoice,
    ListResponse,
    LogProbs,
    LogProbsContent,
    ModelResponse,
    UsageInfo,
)
from ..server import ServerContext
from . import entrypoint_utils

app = fastapi.APIRouter()

################ v1/models ################


@app.get("/v1/models")
async def request_models(request: fastapi.Request):
    """OpenAI-compatible served model query API.
    API reference: https://platform.openai.com/docs/api-reference/models
    """
    server_context: ServerContext = request.scope["server_context"]
    return ListResponse(data=[ModelResponse(id=model) for model in server_context.get_model_list()])


################ v1/completions ################


@app.post("/v1/completions")
async def request_completion(request: CompletionRequest, raw_request: fastapi.Request):
    """OpenAI-compatible completion API.
    API reference: https://platform.openai.com/docs/api-reference/completions/create
    """
    # - Check the requested model.
    server_context: ServerContext = raw_request.scope["server_context"]
    async_engine = server_context.get_engine(request.model)
    if async_engine is None:
        return entrypoint_utils.create_error_response(
            HTTPStatus.BAD_REQUEST, message=f'The requested model "{request.model}" is not served.'
        )
    request_id = f"cmpl-{entrypoint_utils.random_uuid()}"
    async_engine.record_event(request_id, event="receive request")

    # - Check if unsupported arguments are specified.
    error = entrypoint_utils.check_unsupported_fields(request)
    if error is not None:
        return error


Review comment (suggestion):
Use `ServerContext.current()` for these places. And we do not need to introduce the `request` parameter.

Code after:
    ChatFunctionCall,
    ChatToolCall,
    CompletionRequest,
    CompletionResponse,
    CompletionResponseChoice,
    ListResponse,
    LogProbs,
    LogProbsContent,
    ModelResponse,
    UsageInfo,
)
from ..server import ServerContext
from . import entrypoint_utils

app = fastapi.APIRouter()

################ v1/models ################


@app.get("/v1/models")
async def request_models():
    """OpenAI-compatible served model query API.
    API reference: https://platform.openai.com/docs/api-reference/models
    """
    server_context: ServerContext = ServerContext.current()
    return ListResponse(data=[ModelResponse(id=model) for model in server_context.get_model_list()])


################ v1/completions ################


@app.post("/v1/completions")
async def request_completion(request: CompletionRequest, raw_request: fastapi.Request):
    """OpenAI-compatible completion API.
    API reference: https://platform.openai.com/docs/api-reference/completions/create
    """
    # - Check the requested model.
    server_context: ServerContext = ServerContext.current()
    async_engine = server_context.get_engine(request.model)
    if async_engine is None:
        return entrypoint_utils.create_error_response(
            HTTPStatus.BAD_REQUEST, message=f'The requested model "{request.model}" is not served.'
        )
    request_id = f"cmpl-{entrypoint_utils.random_uuid()}"
    async_engine.state.record_event(request_id, event="receive request")

    # - Check if unsupported arguments are specified.
    error = entrypoint_utils.check_unsupported_fields(request)
    if error is not None:
        return error