"docs/design-docs/dynamo-flow.md" did not exist on "03360b84756931e13113656711817094fb97799e"
web.py 2.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16
import json
17
18
19
20
import logging

from components.processor import Processor
from fastapi import FastAPI
21
from fastapi.responses import JSONResponse, StreamingResponse
22
from utils.protocol import MultiModalRequest
23
from utils.vllm import parse_vllm_args
24

25
from dynamo.sdk import DYNAMO_IMAGE, api, depends, service
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

logger = logging.getLogger(__name__)


@service(
    dynamo={
        "namespace": "dynamo",
    },
    resources={"cpu": "10", "memory": "20Gi"},
    workers=1,
    image=DYNAMO_IMAGE,
    app=FastAPI(title="Multimodal Example"),
)
class Frontend:
    processor = depends(Processor)

42
43
44
45
    def __init__(self):
        class_name = self.__class__.__name__
        self.engine_args = parse_vllm_args(class_name, "")

46
    @api(name="v1/chat/completions")
47
    async def generate(self, request: MultiModalRequest):
48
49
50
51
52
53
        if self.engine_args.model != request.model:
            return JSONResponse(
                {"error": f"Model '{request.model}' not found"},
                status_code=404,
            )

54
55
        async def content_generator():
            async for response in self.processor.generate(request.model_dump_json()):
56
57
58
59
60
                try:
                    s = json.loads(response)
                    yield s
                except json.JSONDecodeError as e:
                    raise RuntimeError(f"Failed to parse JSON response: {e}")
61

62
        return StreamingResponse(content_generator(), media_type="text/event-stream")