adapter.py 55.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
# Copyright 2023-2024 SGLang Team
# 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.
# ==============================================================================
14
"""Conversion between OpenAI APIs and native SRT APIs"""
Liangsheng Yin's avatar
Liangsheng Yin committed
15

16
import asyncio
17
import json
18
import logging
19
import os
20
21
import time
import uuid
22
from http import HTTPStatus
23
from typing import Dict, List
24

25
from fastapi import HTTPException, Request, UploadFile
26
from fastapi.responses import ORJSONResponse, StreamingResponse
27
from pydantic import ValidationError
28

29
30
31
32
33
34
35
try:
    from outlines.fsm.json_schema import convert_json_schema_to_str
except ImportError:
    # Before outlines 0.0.47, convert_json_schema_to_str is under
    # outlines.integrations.utils
    from outlines.integrations.utils import convert_json_schema_to_str

36
37
38
39
40
41
42
from sglang.srt.conversation import (
    Conversation,
    SeparatorStyle,
    chat_template_exists,
    generate_chat_conv,
    register_conv_template,
)
Ying Sheng's avatar
Ying Sheng committed
43
from sglang.srt.managers.io_struct import EmbeddingReqInput, GenerateReqInput
Mingyi's avatar
Mingyi committed
44
from sglang.srt.openai_api.protocol import (
45
46
    BatchRequest,
    BatchResponse,
47
48
49
50
51
    ChatCompletionRequest,
    ChatCompletionResponse,
    ChatCompletionResponseChoice,
    ChatCompletionResponseStreamChoice,
    ChatCompletionStreamResponse,
52
    ChatCompletionTokenLogprob,
53
    ChatMessage,
54
    ChoiceLogprobs,
55
56
57
58
59
60
    CompletionRequest,
    CompletionResponse,
    CompletionResponseChoice,
    CompletionResponseStreamChoice,
    CompletionStreamResponse,
    DeltaMessage,
Ying Sheng's avatar
Ying Sheng committed
61
    EmbeddingObject,
62
63
    EmbeddingRequest,
    EmbeddingResponse,
64
    ErrorResponse,
65
    FileDeleteResponse,
66
67
    FileRequest,
    FileResponse,
Tanjiro's avatar
Tanjiro committed
68
    FunctionResponse,
69
    LogProbs,
Tanjiro's avatar
Tanjiro committed
70
    ToolCall,
71
    TopLogprob,
72
73
    UsageInfo,
)
Tanjiro's avatar
Tanjiro committed
74
from sglang.srt.utils import TOOLS_TAG_LIST, parse_tool_response
75
from sglang.utils import get_exception_traceback
76

77
78
logger = logging.getLogger(__name__)

79
80
chat_template_name = None

Liangsheng Yin's avatar
Liangsheng Yin committed
81

82
83
84
85
86
87
88
89
90
91
class FileMetadata:
    def __init__(self, filename: str, purpose: str):
        self.filename = filename
        self.purpose = purpose


# In-memory storage for batch jobs and files
batch_storage: Dict[str, BatchResponse] = {}
file_id_request: Dict[str, FileMetadata] = {}
file_id_response: Dict[str, FileResponse] = {}
92
# map file id to file path in SGLang backend
93
94
95
96
97
98
99
file_id_storage: Dict[str, str] = {}


# backend storage directory
storage_dir = None


100
101
102
def create_error_response(
    message: str,
    err_type: str = "BadRequestError",
103
104
105
    status_code: HTTPStatus = HTTPStatus.BAD_REQUEST,
):
    error = ErrorResponse(message=message, type=err_type, code=status_code.value)
106
    return ORJSONResponse(content=error.model_dump(), status_code=error.code)
107
108
109
110
111


def create_streaming_error_response(
    message: str,
    err_type: str = "BadRequestError",
112
113
114
    status_code: HTTPStatus = HTTPStatus.BAD_REQUEST,
) -> str:
    error = ErrorResponse(message=message, type=err_type, code=status_code.value)
115
116
117
118
    json_str = json.dumps({"error": error.model_dump()})
    return json_str


119
def load_chat_template_for_openai_api(tokenizer_manager, chat_template_arg):
120
121
    global chat_template_name

122
123
124
    logger.info(
        f"Use chat template for the OpenAI-compatible API server: {chat_template_arg}"
    )
125
126
127
128
129
130
    if not chat_template_exists(chat_template_arg):
        if not os.path.exists(chat_template_arg):
            raise RuntimeError(
                f"Chat template {chat_template_arg} is not a built-in template name "
                "or a valid chat template file path."
            )
131
132
133
134
135
        if chat_template_arg.endswith(".jinja"):
            with open(chat_template_arg, "r") as f:
                chat_template = "".join(f.readlines()).strip("\n")
            tokenizer_manager.tokenizer.chat_template = chat_template.replace(
                "\\n", "\n"
136
            )
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
            chat_template_name = None
        else:
            assert chat_template_arg.endswith(
                ".json"
            ), "unrecognized format of chat template file"
            with open(chat_template_arg, "r") as filep:
                template = json.load(filep)
                try:
                    sep_style = SeparatorStyle[template["sep_style"]]
                except KeyError:
                    raise ValueError(
                        f"Unknown separator style: {template['sep_style']}"
                    ) from None
                register_conv_template(
                    Conversation(
                        name=template["name"],
                        system_template=template["system"] + "\n{system_message}",
                        system_message=template.get("system_message", ""),
                        roles=(template["user"], template["assistant"]),
                        sep_style=sep_style,
                        sep=template.get("sep", "\n"),
                        stop_str=template["stop_str"],
                    ),
                    override=True,
                )
            chat_template_name = template["name"]
163
164
165
166
    else:
        chat_template_name = chat_template_arg


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
async def v1_files_create(file: UploadFile, purpose: str, file_storage_pth: str = None):
    try:
        global storage_dir
        if file_storage_pth:
            storage_dir = file_storage_pth
        # Read the file content
        file_content = await file.read()

        # Create an instance of RequestBody
        request_body = FileRequest(file=file_content, purpose=purpose)

        # Save the file to the sglang_oai_storage directory
        os.makedirs(storage_dir, exist_ok=True)
        file_id = f"backend_input_file-{uuid.uuid4()}"
        filename = f"{file_id}.jsonl"
        file_path = os.path.join(storage_dir, filename)

        with open(file_path, "wb") as f:
            f.write(request_body.file)

        # add info to global file map
        file_id_request[file_id] = FileMetadata(filename=file.filename, purpose=purpose)
        file_id_storage[file_id] = file_path

        # Return the response in the required format
        response = FileResponse(
            id=file_id,
            bytes=len(request_body.file),
            created_at=int(time.time()),
            filename=file.filename,
            purpose=request_body.purpose,
        )
        file_id_response[file_id] = response

        return response
    except ValidationError as e:
        return {"error": "Invalid input", "details": e.errors()}


206
207
208
209
210
211
212
213
214
215
216
217
218
219
async def v1_delete_file(file_id: str):
    # Retrieve the file job from the in-memory storage
    file_response = file_id_response.get(file_id)
    if file_response is None:
        raise HTTPException(status_code=404, detail="File not found")
    file_path = file_id_storage.get(file_id)
    if file_path is None:
        raise HTTPException(status_code=404, detail="File not found")
    os.remove(file_path)
    del file_id_response[file_id]
    del file_id_storage[file_id]
    return FileDeleteResponse(id=file_id, deleted=True)


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
async def v1_batches(tokenizer_manager, raw_request: Request):
    try:
        body = await raw_request.json()

        batch_request = BatchRequest(**body)

        batch_id = f"batch_{uuid.uuid4()}"

        # Create an instance of BatchResponse
        batch_response = BatchResponse(
            id=batch_id,
            endpoint=batch_request.endpoint,
            input_file_id=batch_request.input_file_id,
            completion_window=batch_request.completion_window,
            created_at=int(time.time()),
            metadata=batch_request.metadata,
        )

        batch_storage[batch_id] = batch_response

        # Start processing the batch asynchronously
        asyncio.create_task(process_batch(tokenizer_manager, batch_id, batch_request))

        # Return the initial batch_response
        return batch_response

    except ValidationError as e:
        return {"error": "Invalid input", "details": e.errors()}
    except Exception as e:
        return {"error": str(e)}


async def process_batch(tokenizer_manager, batch_id: str, batch_request: BatchRequest):
    try:
        # Update the batch status to "in_progress"
        batch_storage[batch_id].status = "in_progress"
        batch_storage[batch_id].in_progress_at = int(time.time())

        # Retrieve the input file content
        input_file_request = file_id_request.get(batch_request.input_file_id)
        if not input_file_request:
            raise ValueError("Input file not found")

        # Parse the JSONL file and process each request
        input_file_path = file_id_storage.get(batch_request.input_file_id)
        with open(input_file_path, "r", encoding="utf-8") as f:
            lines = f.readlines()

        total_requests = len(lines)
        completed_requests = 0
        failed_requests = 0

        all_ret = []
        end_point = batch_storage[batch_id].endpoint
        file_request_list = []
        all_requests = []
276
        request_ids = []
277
278
279
280
        for line in lines:
            request_data = json.loads(line)
            file_request_list.append(request_data)
            body = request_data["body"]
281
            request_ids.append(request_data["custom_id"])
282
283
284
285
286
287

            # Although streaming is supported for standalone completions, it is not supported in
            # batch mode (multiple completions in single request).
            if body.get("stream", False):
                raise ValueError("Streaming requests are not supported in batch mode")

288
289
290
291
            if end_point == "/v1/chat/completions":
                all_requests.append(ChatCompletionRequest(**body))
            elif end_point == "/v1/completions":
                all_requests.append(CompletionRequest(**body))
292

293
294
        if end_point == "/v1/chat/completions":
            adapted_request, request = v1_chat_generate_request(
295
                all_requests, tokenizer_manager, request_ids=request_ids
296
297
            )
        elif end_point == "/v1/completions":
298
299
300
301
            adapted_request, request = v1_generate_request(
                all_requests, request_ids=request_ids
            )

302
303
304
305
306
        try:
            ret = await tokenizer_manager.generate_request(adapted_request).__anext__()
            if not isinstance(ret, list):
                ret = [ret]
            if end_point == "/v1/chat/completions":
307
308
309
310
311
312
                responses = v1_chat_generate_response(
                    request,
                    ret,
                    to_file=True,
                    cache_report=tokenizer_manager.server_args.enable_cache_report,
                )
313
            else:
yichuan~'s avatar
yichuan~ committed
314
315
316
                responses = v1_generate_response(
                    request, ret, tokenizer_manager, to_file=True
                )
317
318

        except Exception as e:
319
320
            logger.error(f"error: {get_exception_traceback()}")
            responses = []
321
322
323
324
325
326
327
328
329
330
            error_json = {
                "id": f"batch_req_{uuid.uuid4()}",
                "custom_id": request_data.get("custom_id"),
                "response": None,
                "error": {"message": str(e)},
            }
            all_ret.append(error_json)
            failed_requests += len(file_request_list)

        for idx, response in enumerate(responses):
331
            # the batch_req here can be changed to be named within a batch granularity
332
333
334
335
336
337
338
339
            response_json = {
                "id": f"batch_req_{uuid.uuid4()}",
                "custom_id": file_request_list[idx].get("custom_id"),
                "response": response,
                "error": None,
            }
            all_ret.append(response_json)
            completed_requests += 1
340

341
342
343
344
345
346
347
348
349
350
351
352
        # Write results to a new file
        output_file_id = f"backend_result_file-{uuid.uuid4()}"
        global storage_dir
        output_file_path = os.path.join(storage_dir, f"{output_file_id}.jsonl")
        with open(output_file_path, "w", encoding="utf-8") as f:
            for ret in all_ret:
                f.write(json.dumps(ret) + "\n")

        # Update batch response with output file information
        retrieve_batch = batch_storage[batch_id]
        retrieve_batch.output_file_id = output_file_id
        file_id_storage[output_file_id] = output_file_path
353
354
355
356
357
358
359
        file_id_response[output_file_id] = FileResponse(
            id=output_file_id,
            bytes=os.path.getsize(output_file_path),
            created_at=int(time.time()),
            filename=f"{output_file_id}.jsonl",
            purpose="batch_result",
        )
360
361
362
363
364
365
366
367
368
369
        # Update batch status to "completed"
        retrieve_batch.status = "completed"
        retrieve_batch.completed_at = int(time.time())
        retrieve_batch.request_counts = {
            "total": total_requests,
            "completed": completed_requests,
            "failed": failed_requests,
        }

    except Exception as e:
370
        logger.error(f"error: {e}")
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
        # Update batch status to "failed"
        retrieve_batch = batch_storage[batch_id]
        retrieve_batch.status = "failed"
        retrieve_batch.failed_at = int(time.time())
        retrieve_batch.errors = {"message": str(e)}


async def v1_retrieve_batch(batch_id: str):
    # Retrieve the batch job from the in-memory storage
    batch_response = batch_storage.get(batch_id)
    if batch_response is None:
        raise HTTPException(status_code=404, detail="Batch not found")

    return batch_response


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
async def v1_cancel_batch(tokenizer_manager, batch_id: str):
    # Retrieve the batch job from the in-memory storage
    batch_response = batch_storage.get(batch_id)
    if batch_response is None:
        raise HTTPException(status_code=404, detail="Batch not found")

    # Only do cancal when status is "validating" or "in_progress"
    if batch_response.status in ["validating", "in_progress"]:
        # Start cancelling the batch asynchronously
        asyncio.create_task(
            cancel_batch(
                tokenizer_manager=tokenizer_manager,
                batch_id=batch_id,
                input_file_id=batch_response.input_file_id,
            )
        )

        # Update batch status to "cancelling"
        batch_response.status = "cancelling"

        return batch_response
    else:
        raise HTTPException(
            status_code=500,
            detail=f"Current status is {batch_response.status}, no need to cancel",
        )


async def cancel_batch(tokenizer_manager, batch_id: str, input_file_id: str):
    try:
        # Update the batch status to "cancelling"
        batch_storage[batch_id].status = "cancelling"

        # Retrieve the input file content
        input_file_request = file_id_request.get(input_file_id)
        if not input_file_request:
            raise ValueError("Input file not found")

        # Parse the JSONL file and process each request
        input_file_path = file_id_storage.get(input_file_id)
        with open(input_file_path, "r", encoding="utf-8") as f:
            lines = f.readlines()

        file_request_list = []
        request_ids = []
        for line in lines:
            request_data = json.loads(line)
            file_request_list.append(request_data)
            request_ids.append(request_data["custom_id"])

        # Cancel requests by request_ids
        for rid in request_ids:
            tokenizer_manager.abort_request(rid=rid)

        retrieve_batch = batch_storage[batch_id]
        retrieve_batch.status = "cancelled"

    except Exception as e:
        logger.error("error in SGLang:", e)
        # Update batch status to "failed"
        retrieve_batch = batch_storage[batch_id]
        retrieve_batch.status = "failed"
        retrieve_batch.failed_at = int(time.time())
        retrieve_batch.errors = {"message": str(e)}


453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
async def v1_retrieve_file(file_id: str):
    # Retrieve the batch job from the in-memory storage
    file_response = file_id_response.get(file_id)
    if file_response is None:
        raise HTTPException(status_code=404, detail="File not found")
    return file_response


async def v1_retrieve_file_content(file_id: str):
    file_pth = file_id_storage.get(file_id)
    if not file_pth or not os.path.exists(file_pth):
        raise HTTPException(status_code=404, detail="File not found")

    def iter_file():
        with open(file_pth, mode="rb") as file_like:
            yield from file_like

    return StreamingResponse(iter_file(), media_type="application/octet-stream")


473
474
475
def v1_generate_request(
    all_requests: List[CompletionRequest], request_ids: List[str] = None
):
476
477
478
479
480
481
482
483
484
485
486
    if len(all_requests) > 1:
        first_prompt_type = type(all_requests[0].prompt)
        for request in all_requests:
            assert (
                type(request.prompt) is first_prompt_type
            ), "All prompts must be of the same type in file input settings"
            if request.n > 1:
                raise ValueError(
                    "Parallel sampling is not supported for completions from files"
                )

487
488
    prompts = []
    sampling_params_list = []
489
    return_logprobs = []
490
    logprob_start_lens = []
491
    top_logprobs_nums = []
492
    lora_paths = []
yichuan~'s avatar
yichuan~ committed
493

494
    for request in all_requests:
495
        # NOTE: with openai API, the prompt's logprobs are always not computed
496
        if request.echo and request.logprobs:
497
            logger.warning(
498
                "Echo is not compatible with logprobs. "
499
                "To compute logprobs of input prompt, please use the native /generate API."
500
501
502
            )

        prompts.append(request.prompt)
503
        lora_paths.append(request.lora_path)
504
505
506
507
        if request.echo and request.logprobs:
            current_logprob_start_len = 0
        else:
            current_logprob_start_len = -1
508
509
510
511
512
513
514
515
        sampling_params_list.append(
            {
                "temperature": request.temperature,
                "max_new_tokens": request.max_tokens,
                "min_new_tokens": request.min_tokens,
                "stop": request.stop,
                "stop_token_ids": request.stop_token_ids,
                "top_p": request.top_p,
516
517
                "top_k": request.top_k,
                "min_p": request.min_p,
518
519
520
521
522
                "presence_penalty": request.presence_penalty,
                "frequency_penalty": request.frequency_penalty,
                "repetition_penalty": request.repetition_penalty,
                "regex": request.regex,
                "json_schema": request.json_schema,
523
                "ebnf": request.ebnf,
524
525
                "n": request.n,
                "no_stop_trim": request.no_stop_trim,
526
527
                "ignore_eos": request.ignore_eos,
                "skip_special_tokens": request.skip_special_tokens,
528
529
            }
        )
530
        return_logprobs.append(request.logprobs is not None)
531
        logprob_start_lens.append(current_logprob_start_len)
532
533
534
        top_logprobs_nums.append(
            request.logprobs if request.logprobs is not None else 0
        )
535
536

    if len(all_requests) == 1:
537
538
539
540
        if isinstance(prompts[0], str) or isinstance(prompts[0][0], str):
            prompt_kwargs = {"text": prompts[0]}
        else:
            prompt_kwargs = {"input_ids": prompts[0]}
541
        sampling_params_list = sampling_params_list[0]
542
        return_logprobs = return_logprobs[0]
543
        logprob_start_lens = logprob_start_lens[0]
544
        top_logprobs_nums = top_logprobs_nums[0]
545
        lora_paths = lora_paths[0]
546
    else:
547
        if isinstance(prompts[0], str) or isinstance(prompts[0][0], str):
548
549
550
            prompt_kwargs = {"text": prompts}
        else:
            prompt_kwargs = {"input_ids": prompts}
yichuan~'s avatar
yichuan~ committed
551

552
    adapted_request = GenerateReqInput(
553
        **prompt_kwargs,
554
        sampling_params=sampling_params_list,
555
556
        return_logprob=return_logprobs,
        top_logprobs_num=top_logprobs_nums,
557
        logprob_start_len=logprob_start_lens,
558
        return_text_in_logprobs=True,
559
        stream=all_requests[0].stream,
560
        rid=request_ids,
561
        lora_path=lora_paths,
562
    )
yichuan~'s avatar
yichuan~ committed
563

564
    return adapted_request, all_requests if len(all_requests) > 1 else all_requests[0]
565
566


yichuan~'s avatar
yichuan~ committed
567
def v1_generate_response(request, ret, tokenizer_manager, to_file=False):
568
569
570
    choices = []
    echo = False

yichuan~'s avatar
yichuan~ committed
571
    if (not isinstance(request, list)) and request.echo:
572
        # TODO: handle the case propmt is token ids
yichuan~'s avatar
yichuan~ committed
573
574
        if isinstance(request.prompt, list) and isinstance(request.prompt[0], str):
            # for the case of multiple str prompts
575
            prompts = request.prompt
yichuan~'s avatar
yichuan~ committed
576
577
578
579
580
581
582
583
584
585
586
587
588
        elif isinstance(request.prompt, list) and isinstance(request.prompt[0], list):
            # for the case of multiple token ids prompts
            prompts = [
                tokenizer_manager.tokenizer.decode(prompt, skip_special_tokens=True)
                for prompt in request.prompt
            ]
        elif isinstance(request.prompt, list) and isinstance(request.prompt[0], int):
            # for the case of single token ids prompt
            prompts = [
                tokenizer_manager.tokenizer.decode(
                    request.prompt, skip_special_tokens=True
                )
            ]
589
        else:
yichuan~'s avatar
yichuan~ committed
590
            # for the case of single str prompt
591
592
593
594
595
            prompts = [request.prompt]
        echo = True

    for idx, ret_item in enumerate(ret):
        text = ret_item["text"]
yichuan~'s avatar
yichuan~ committed
596
        if isinstance(request, list) and request[idx].echo:
597
598
            echo = True
            text = request[idx].prompt + text
599
        if echo and not isinstance(request, list):
yichuan~'s avatar
yichuan~ committed
600
601
            prompt_index = idx // request.n
            text = prompts[prompt_index] + text
602
603

        logprobs = False
604
        if isinstance(request, list) and request[idx].logprobs is not None:
605
            logprobs = True
606
        elif (not isinstance(request, list)) and request.logprobs is not None:
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
            logprobs = True
        if logprobs:
            if echo:
                input_token_logprobs = ret_item["meta_info"]["input_token_logprobs"]
                input_top_logprobs = ret_item["meta_info"]["input_top_logprobs"]
            else:
                input_token_logprobs = None
                input_top_logprobs = None

            logprobs = to_openai_style_logprobs(
                input_token_logprobs=input_token_logprobs,
                input_top_logprobs=input_top_logprobs,
                output_token_logprobs=ret_item["meta_info"]["output_token_logprobs"],
                output_top_logprobs=ret_item["meta_info"]["output_top_logprobs"],
            )
        else:
            logprobs = None

625
626
        finish_reason = ret_item["meta_info"]["finish_reason"]

627
        if to_file:
628
            # to make the choise data json serializable
629
630
631
632
            choice_data = {
                "index": 0,
                "text": text,
                "logprobs": logprobs,
633
634
635
636
637
                "finish_reason": (finish_reason["type"] if finish_reason else ""),
                "matched_stop": (
                    finish_reason["matched"]
                    if finish_reason and "matched" in finish_reason
                    else None
638
                ),
639
640
641
642
643
644
            }
        else:
            choice_data = CompletionResponseChoice(
                index=idx,
                text=text,
                logprobs=logprobs,
645
646
647
648
649
                finish_reason=(finish_reason["type"] if finish_reason else ""),
                matched_stop=(
                    finish_reason["matched"]
                    if finish_reason and "matched" in finish_reason
                    else None
650
                ),
651
652
653
654
655
656
657
658
659
660
661
            )

        choices.append(choice_data)

    if to_file:
        responses = []
        for i, choice in enumerate(choices):
            response = {
                "status_code": 200,
                "request_id": ret[i]["meta_info"]["id"],
                "body": {
662
                    # remain the same but if needed we can change that
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
                    "id": ret[i]["meta_info"]["id"],
                    "object": "text_completion",
                    "created": int(time.time()),
                    "model": request[i].model,
                    "choices": choice,
                    "usage": {
                        "prompt_tokens": ret[i]["meta_info"]["prompt_tokens"],
                        "completion_tokens": ret[i]["meta_info"]["completion_tokens"],
                        "total_tokens": ret[i]["meta_info"]["prompt_tokens"]
                        + ret[i]["meta_info"]["completion_tokens"],
                    },
                    "system_fingerprint": None,
                },
            }
            responses.append(response)
        return responses
    else:
680
681
682
        prompt_tokens = sum(
            ret[i]["meta_info"]["prompt_tokens"] for i in range(0, len(ret), request.n)
        )
683
684
685
686
687
688
        completion_tokens = sum(item["meta_info"]["completion_tokens"] for item in ret)
        response = CompletionResponse(
            id=ret[0]["meta_info"]["id"],
            model=request.model,
            choices=choices,
            usage=UsageInfo(
yichuan~'s avatar
yichuan~ committed
689
                prompt_tokens=prompt_tokens,
690
                completion_tokens=completion_tokens,
yichuan~'s avatar
yichuan~ committed
691
                total_tokens=prompt_tokens + completion_tokens,
692
693
694
695
696
697
698
699
700
            ),
        )
    return response


async def v1_completions(tokenizer_manager, raw_request: Request):
    request_json = await raw_request.json()
    all_requests = [CompletionRequest(**request_json)]
    adapted_request, request = v1_generate_request(all_requests)
701
702
703
704

    if adapted_request.stream:

        async def generate_stream_resp():
705
706
707
708
            stream_buffers = {}
            n_prev_tokens = {}
            prompt_tokens = {}
            completion_tokens = {}
709
710
            try:
                async for content in tokenizer_manager.generate_request(
711
712
                    adapted_request, raw_request
                ):
713
                    index = content.get("index", 0)
714
715
716
717

                    stream_buffer = stream_buffers.get(index, "")
                    n_prev_token = n_prev_tokens.get(index, 0)

718
                    text = content["text"]
719
720
                    prompt_tokens[index] = content["meta_info"]["prompt_tokens"]
                    completion_tokens[index] = content["meta_info"]["completion_tokens"]
721
722
723

                    if not stream_buffer:  # The first chunk
                        if request.echo:
yichuan~'s avatar
yichuan~ committed
724
725
726
                            if isinstance(request.prompt, str):
                                # for the case of single str prompts
                                prompts = request.prompt
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
                            elif isinstance(request.prompt, list):
                                if isinstance(request.prompt[0], str):
                                    # for the case of multiple str prompts
                                    prompts = request.prompt[index // request.n]
                                elif isinstance(request.prompt[0], int):
                                    # for the case of single token ids prompt
                                    prompts = tokenizer_manager.tokenizer.decode(
                                        request.prompt, skip_special_tokens=True
                                    )
                                elif isinstance(request.prompt[0], list) and isinstance(
                                    request.prompt[0][0], int
                                ):
                                    # for the case of multiple token ids prompts
                                    prompts = tokenizer_manager.tokenizer.decode(
                                        request.prompt[index // request.n],
                                        skip_special_tokens=True,
                                    )
yichuan~'s avatar
yichuan~ committed
744

745
                            # Prepend prompt in response text.
yichuan~'s avatar
yichuan~ committed
746
                            text = prompts + text
747

748
                    if request.logprobs is not None:
749
750
                        # The first chunk and echo is enabled.
                        if not stream_buffer and request.echo:
751
752
                            input_token_logprobs = content["meta_info"][
                                "input_token_logprobs"
753
                            ]
754
755
                            input_top_logprobs = content["meta_info"][
                                "input_top_logprobs"
756
757
                            ]
                        else:
758
759
                            input_token_logprobs = None
                            input_top_logprobs = None
760
761

                        logprobs = to_openai_style_logprobs(
762
763
764
765
                            input_token_logprobs=input_token_logprobs,
                            input_top_logprobs=input_top_logprobs,
                            output_token_logprobs=content["meta_info"][
                                "output_token_logprobs"
766
                            ][n_prev_token:],
767
768
                            output_top_logprobs=content["meta_info"][
                                "output_top_logprobs"
769
                            ][n_prev_token:],
770
                        )
771
                        n_prev_token = len(
772
                            content["meta_info"]["output_token_logprobs"]
773
                        )
774
                    else:
775
                        logprobs = None
776

777
                    delta = text[len(stream_buffer) :]
Liangsheng Yin's avatar
Liangsheng Yin committed
778
                    stream_buffer = stream_buffer + delta
779
                    finish_reason = content["meta_info"]["finish_reason"]
780
                    choice_data = CompletionResponseStreamChoice(
781
                        index=index,
782
783
                        text=delta,
                        logprobs=logprobs,
784
785
786
787
788
                        finish_reason=(finish_reason["type"] if finish_reason else ""),
                        matched_stop=(
                            finish_reason["matched"]
                            if finish_reason and "matched" in finish_reason
                            else None
789
                        ),
790
791
792
793
794
795
796
                    )
                    chunk = CompletionStreamResponse(
                        id=content["meta_info"]["id"],
                        object="text_completion",
                        choices=[choice_data],
                        model=request.model,
                    )
797
798
799
800

                    stream_buffers[index] = stream_buffer
                    n_prev_tokens[index] = n_prev_token

801
                    yield f"data: {chunk.model_dump_json()}\n\n"
802
                if request.stream_options and request.stream_options.include_usage:
803
804
805
806
807
808
809
810
                    total_prompt_tokens = sum(
                        tokens
                        for i, tokens in prompt_tokens.items()
                        if i % request.n == 0
                    )
                    total_completion_tokens = sum(
                        tokens for tokens in completion_tokens.values()
                    )
811
                    usage = UsageInfo(
812
813
814
                        prompt_tokens=total_prompt_tokens,
                        completion_tokens=total_completion_tokens,
                        total_tokens=total_prompt_tokens + total_completion_tokens,
815
816
817
818
819
820
821
822
823
824
825
826
                    )

                    final_usage_chunk = CompletionStreamResponse(
                        id=str(uuid.uuid4().hex),
                        choices=[],
                        model=request.model,
                        usage=usage,
                    )
                    final_usage_data = final_usage_chunk.model_dump_json(
                        exclude_unset=True, exclude_none=True
                    )
                    yield f"data: {final_usage_data}\n\n"
827
828
829
            except ValueError as e:
                error = create_streaming_error_response(str(e))
                yield f"data: {error}\n\n"
830
831
            yield "data: [DONE]\n\n"

832
833
834
835
836
        return StreamingResponse(
            generate_stream_resp(),
            media_type="text/event-stream",
            background=tokenizer_manager.create_abort_task(adapted_request),
        )
837
838

    # Non-streaming response.
839
840
    try:
        ret = await tokenizer_manager.generate_request(
841
842
            adapted_request, raw_request
        ).__anext__()
843
844
    except ValueError as e:
        return create_error_response(str(e))
845

846
847
848
    if not isinstance(ret, list):
        ret = [ret]

yichuan~'s avatar
yichuan~ committed
849
    response = v1_generate_response(request, ret, tokenizer_manager)
850
    return response
851

852

853
def v1_chat_generate_request(
854
855
856
    all_requests: List[ChatCompletionRequest],
    tokenizer_manager,
    request_ids: List[str] = None,
857
):
858
    input_ids = []
859
860
    sampling_params_list = []
    image_data_list = []
861
    return_logprobs = []
862
    logprob_start_lens = []
863
    top_logprobs_nums = []
864
    modalities_list = []
865
    lora_paths = []
866
867
868

    # NOTE: with openai API, the prompt's logprobs are always not computed

869
870
871
872
873
874
875
876
    for request in all_requests:
        # Prep the data needed for the underlying GenerateReqInput:
        #  - prompt: The full prompt string.
        #  - stop: Custom stop tokens.
        #  - image_data: None or a list of image strings (URLs or base64 strings).
        #    None skips any image processing in GenerateReqInput.
        if not isinstance(request.messages, str):
            # Apply chat template and its stop strings.
Tanjiro's avatar
Tanjiro committed
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
            tools = None
            if request.tools and request.tool_choice != "none":
                request.skip_special_tokens = False
                if request.stream:
                    logger.warning("Streaming is not supported with tools.")
                    request.stream = False
                if not isinstance(request.tool_choice, str):
                    tools = [
                        item.function.model_dump()
                        for item in request.tools
                        if item.function.name == request.tool_choice.function.name
                    ]
                else:
                    tools = [item.function.model_dump() for item in request.tools]

892
            if chat_template_name is None:
893
894
895
896
897
898
899
900
901
902
903
904
905
                openai_compatible_messages = []
                for message in request.messages:
                    if isinstance(message.content, str):
                        openai_compatible_messages.append(
                            {"role": message.role, "content": message.content}
                        )
                    else:
                        content_list = message.dict()["content"]
                        for content in content_list:
                            if content["type"] == "text":
                                openai_compatible_messages.append(
                                    {"role": message.role, "content": content["text"]}
                                )
906
907
908
909
910
                if openai_compatible_messages[-1]["role"] == "assistant":
                    assistant_prefix = openai_compatible_messages[-1]["content"]
                    openai_compatible_messages = openai_compatible_messages[:-1]
                else:
                    assistant_prefix = None
911
                prompt_ids = tokenizer_manager.tokenizer.apply_chat_template(
912
913
914
                    openai_compatible_messages,
                    tokenize=True,
                    add_generation_prompt=True,
Tanjiro's avatar
Tanjiro committed
915
                    tools=tools,
916
                )
917
918
                if assistant_prefix:
                    prompt_ids += tokenizer_manager.tokenizer.encode(assistant_prefix)
919
920
                stop = request.stop
                image_data = None
921
                modalities = []
922
            else:
923
924
925
                conv = generate_chat_conv(request, chat_template_name)
                prompt = conv.get_prompt()
                image_data = conv.image_data
926
                modalities = conv.modalities
927
928
929
930
931
932
                stop = conv.stop_str or []
                if request.stop:
                    if isinstance(request.stop, str):
                        stop.append(request.stop)
                    else:
                        stop.extend(request.stop)
933
                prompt_ids = tokenizer_manager.tokenizer.encode(prompt)
934
        else:
935
            # Use the raw prompt and stop strings if the messages is already a string.
yichuan~'s avatar
yichuan~ committed
936
            prompt_ids = request.messages
937
938
            stop = request.stop
            image_data = None
939
            modalities = []
940
        input_ids.append(prompt_ids)
941
        return_logprobs.append(request.logprobs)
942
        logprob_start_lens.append(-1)
943
        top_logprobs_nums.append(request.top_logprobs or 0)
944
        lora_paths.append(request.lora_path)
945
946
947
948
949
950
951
952

        sampling_params = {
            "temperature": request.temperature,
            "max_new_tokens": request.max_tokens,
            "min_new_tokens": request.min_tokens,
            "stop": stop,
            "stop_token_ids": request.stop_token_ids,
            "top_p": request.top_p,
953
954
            "top_k": request.top_k,
            "min_p": request.min_p,
955
956
957
958
            "presence_penalty": request.presence_penalty,
            "frequency_penalty": request.frequency_penalty,
            "repetition_penalty": request.repetition_penalty,
            "regex": request.regex,
959
            "ebnf": request.ebnf,
960
            "n": request.n,
961
            "no_stop_trim": request.no_stop_trim,
962
            "ignore_eos": request.ignore_eos,
963
            "skip_special_tokens": request.skip_special_tokens,
964
965
966
967
968
969
970
        }
        if request.response_format and request.response_format.type == "json_schema":
            sampling_params["json_schema"] = convert_json_schema_to_str(
                request.response_format.json_schema.schema_
            )
        sampling_params_list.append(sampling_params)

971
        image_data_list.append(image_data)
972
        modalities_list.append(modalities)
973
    if len(all_requests) == 1:
974
975
        if isinstance(input_ids[0], str):
            prompt_kwargs = {"text": input_ids[0]}
yichuan~'s avatar
yichuan~ committed
976
        else:
977
            prompt_kwargs = {"input_ids": input_ids[0]}
978
        sampling_params_list = sampling_params_list[0]
979
        image_data_list = image_data_list[0]
980
        return_logprobs = return_logprobs[0]
981
        logprob_start_lens = logprob_start_lens[0]
982
        top_logprobs_nums = top_logprobs_nums[0]
983
        modalities_list = modalities_list[0]
984
        lora_paths = lora_paths[0]
yichuan~'s avatar
yichuan~ committed
985
986
987
988
989
    else:
        if isinstance(input_ids[0], str):
            prompt_kwargs = {"text": input_ids}
        else:
            prompt_kwargs = {"input_ids": input_ids}
990

991
    adapted_request = GenerateReqInput(
yichuan~'s avatar
yichuan~ committed
992
        **prompt_kwargs,
993
        image_data=image_data_list,
994
        sampling_params=sampling_params_list,
995
        return_logprob=return_logprobs,
996
        logprob_start_len=logprob_start_lens,
997
998
999
        top_logprobs_num=top_logprobs_nums,
        stream=all_requests[0].stream,
        return_text_in_logprobs=True,
1000
        rid=request_ids,
1001
        modalities=modalities_list,
1002
        lora_path=lora_paths,
1003
    )
1004
1005

    return adapted_request, all_requests if len(all_requests) > 1 else all_requests[0]
1006

1007

1008
def v1_chat_generate_response(request, ret, to_file=False, cache_report=False):
1009
1010
1011
    choices = []

    for idx, ret_item in enumerate(ret):
1012
        logprobs = False
yichuan~'s avatar
yichuan~ committed
1013
        if isinstance(request, list) and request[idx].logprobs:
1014
            logprobs = True
yichuan~'s avatar
yichuan~ committed
1015
        elif (not isinstance(request, list)) and request.logprobs:
1016
1017
1018
1019
1020
1021
1022
            logprobs = True
        if logprobs:
            logprobs = to_openai_style_logprobs(
                output_token_logprobs=ret_item["meta_info"]["output_token_logprobs"],
                output_top_logprobs=ret_item["meta_info"]["output_top_logprobs"],
            )
            token_logprobs = []
1023
1024
1025
            for token_idx, (token, logprob) in enumerate(
                zip(logprobs.tokens, logprobs.token_logprobs)
            ):
1026
1027
1028
                token_bytes = list(token.encode("utf-8"))
                top_logprobs = []
                if logprobs.top_logprobs:
1029
1030
1031
                    for top_token, top_logprob in logprobs.top_logprobs[
                        token_idx
                    ].items():
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
                        top_token_bytes = list(top_token.encode("utf-8"))
                        top_logprobs.append(
                            TopLogprob(
                                token=top_token,
                                bytes=top_token_bytes,
                                logprob=top_logprob,
                            )
                        )
                token_logprobs.append(
                    ChatCompletionTokenLogprob(
                        token=token,
                        bytes=token_bytes,
                        logprob=logprob,
                        top_logprobs=top_logprobs,
                    )
                )

            choice_logprobs = ChoiceLogprobs(content=token_logprobs)
        else:
            choice_logprobs = None
1052

1053
1054
        finish_reason = ret_item["meta_info"]["finish_reason"]

Tanjiro's avatar
Tanjiro committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
        tool_calls = None
        text = ret_item["text"]

        if isinstance(request, list):
            tool_choice = request[idx].tool_choice
            tools = request[idx].tools
        else:
            tool_choice = request.tool_choice
            tools = request.tools

        if tool_choice != "none" and any([i in text for i in TOOLS_TAG_LIST]):
            if finish_reason == "stop":
                finish_reason = "tool_calls"
            try:
                text, call_info_list = parse_tool_response(text, tools)  # noqa
                tool_calls = [
                    ToolCall(
                        id=str(call_info[0]),
                        function=FunctionResponse(
                            name=call_info[1], arguments=call_info[2]
                        ),
                    )
                    for call_info in call_info_list
                ]
            except Exception as e:
                logger.error(f"Exception: {e}")
                return create_error_response(
                    HTTPStatus.BAD_REQUEST,
                    "Failed to parse fc related info to json format!",
                )

1086
        if to_file:
1087
            # to make the choice data json serializable
1088
1089
            choice_data = {
                "index": 0,
Tanjiro's avatar
Tanjiro committed
1090
1091
1092
1093
1094
                "message": {
                    "role": "assistant",
                    "content": ret_item["text"] if tool_calls is None else None,
                    "tool_calls": tool_calls,
                },
1095
                "logprobs": choice_logprobs,
1096
1097
1098
1099
1100
                "finish_reason": (finish_reason["type"] if finish_reason else ""),
                "matched_stop": (
                    finish_reason["matched"]
                    if finish_reason and "matched" in finish_reason
                    else None
1101
                ),
1102
            }
1103
        else:
1104
1105
            choice_data = ChatCompletionResponseChoice(
                index=idx,
Tanjiro's avatar
Tanjiro committed
1106
1107
1108
1109
1110
                message=ChatMessage(
                    role="assistant",
                    content=ret_item["text"] if tool_calls is None else None,
                    tool_calls=tool_calls,
                ),
1111
                logprobs=choice_logprobs,
1112
1113
1114
1115
1116
                finish_reason=(finish_reason["type"] if finish_reason else ""),
                matched_stop=(
                    finish_reason["matched"]
                    if finish_reason and "matched" in finish_reason
                    else None
1117
                ),
1118
1119
1120
            )

        choices.append(choice_data)
1121

1122
1123
1124
1125
1126
1127
1128
1129
    if to_file:
        responses = []

        for i, choice in enumerate(choices):
            response = {
                "status_code": 200,
                "request_id": ret[i]["meta_info"]["id"],
                "body": {
1130
                    # remain the same but if needed we can change that
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
                    "id": ret[i]["meta_info"]["id"],
                    "object": "chat.completion",
                    "created": int(time.time()),
                    "model": request[i].model,
                    "choices": choice,
                    "usage": {
                        "prompt_tokens": ret[i]["meta_info"]["prompt_tokens"],
                        "completion_tokens": ret[i]["meta_info"]["completion_tokens"],
                        "total_tokens": ret[i]["meta_info"]["prompt_tokens"]
                        + ret[i]["meta_info"]["completion_tokens"],
                    },
                    "system_fingerprint": None,
                },
            }
            responses.append(response)
        return responses
1147
    else:
1148
1149
1150
1151
        prompt_tokens = sum(
            ret[i]["meta_info"]["prompt_tokens"] for i in range(0, len(ret), request.n)
        )
        completion_tokens = sum(item["meta_info"]["completion_tokens"] for item in ret)
1152
        cached_tokens = sum(item["meta_info"].get("cached_tokens", 0) for item in ret)
1153
1154
1155
1156
1157
        response = ChatCompletionResponse(
            id=ret[0]["meta_info"]["id"],
            model=request.model,
            choices=choices,
            usage=UsageInfo(
1158
1159
1160
                prompt_tokens=prompt_tokens,
                completion_tokens=completion_tokens,
                total_tokens=prompt_tokens + completion_tokens,
1161
1162
1163
                prompt_tokens_details=(
                    {"cached_tokens": cached_tokens} if cache_report else None
                ),
1164
1165
1166
            ),
        )
        return response
1167

1168
1169
1170
1171
1172

async def v1_chat_completions(tokenizer_manager, raw_request: Request):
    request_json = await raw_request.json()
    all_requests = [ChatCompletionRequest(**request_json)]
    adapted_request, request = v1_chat_generate_request(all_requests, tokenizer_manager)
1173
1174
1175
1176

    if adapted_request.stream:

        async def generate_stream_resp():
1177
1178
1179
1180
1181
            is_firsts = {}
            stream_buffers = {}
            n_prev_tokens = {}
            prompt_tokens = {}
            completion_tokens = {}
1182
            try:
1183
1184
1185
                async for content in tokenizer_manager.generate_request(
                    adapted_request, raw_request
                ):
1186
                    index = content.get("index", 0)
1187
1188
1189
1190
1191
1192
1193

                    is_first = is_firsts.get(index, True)
                    stream_buffer = stream_buffers.get(index, "")
                    n_prev_token = n_prev_tokens.get(index, 0)

                    prompt_tokens[index] = content["meta_info"]["prompt_tokens"]
                    completion_tokens[index] = content["meta_info"]["completion_tokens"]
yichuan~'s avatar
yichuan~ committed
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
                    if request.logprobs:
                        logprobs = to_openai_style_logprobs(
                            output_token_logprobs=content["meta_info"][
                                "output_token_logprobs"
                            ][n_prev_token:],
                            output_top_logprobs=content["meta_info"][
                                "output_top_logprobs"
                            ][n_prev_token:],
                        )

                        n_prev_token = len(
                            content["meta_info"]["output_token_logprobs"]
                        )
                        token_logprobs = []
                        for token, logprob in zip(
                            logprobs.tokens, logprobs.token_logprobs
                        ):
                            token_bytes = list(token.encode("utf-8"))
                            top_logprobs = []
                            if logprobs.top_logprobs:
                                for top_token, top_logprob in logprobs.top_logprobs[
                                    0
                                ].items():
                                    top_token_bytes = list(top_token.encode("utf-8"))
                                    top_logprobs.append(
                                        TopLogprob(
                                            token=top_token,
                                            bytes=top_token_bytes,
                                            logprob=top_logprob,
                                        )
                                    )
                            token_logprobs.append(
                                ChatCompletionTokenLogprob(
                                    token=token,
                                    bytes=token_bytes,
                                    logprob=logprob,
                                    top_logprobs=top_logprobs,
                                )
                            )

                        choice_logprobs = ChoiceLogprobs(content=token_logprobs)

                    else:
                        choice_logprobs = None

1239
1240
                    finish_reason = content["meta_info"]["finish_reason"]

1241
1242
1243
1244
                    if is_first:
                        # First chunk with role
                        is_first = False
                        choice_data = ChatCompletionResponseStreamChoice(
1245
                            index=index,
1246
                            delta=DeltaMessage(role="assistant", content=""),
1247
                            finish_reason=(
1248
1249
1250
1251
1252
1253
                                finish_reason["type"] if finish_reason else ""
                            ),
                            matched_stop=(
                                finish_reason["matched"]
                                if finish_reason and "matched" in finish_reason
                                else None
1254
                            ),
yichuan~'s avatar
yichuan~ committed
1255
                            logprobs=choice_logprobs,
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
                        )
                        chunk = ChatCompletionStreamResponse(
                            id=content["meta_info"]["id"],
                            choices=[choice_data],
                            model=request.model,
                        )
                        yield f"data: {chunk.model_dump_json()}\n\n"

                    text = content["text"]
                    delta = text[len(stream_buffer) :]
Liangsheng Yin's avatar
Liangsheng Yin committed
1266
                    stream_buffer = stream_buffer + delta
1267
                    choice_data = ChatCompletionResponseStreamChoice(
1268
                        index=index,
1269
                        delta=DeltaMessage(content=delta),
1270
1271
1272
1273
1274
                        finish_reason=(finish_reason["type"] if finish_reason else ""),
                        matched_stop=(
                            finish_reason["matched"]
                            if finish_reason and "matched" in finish_reason
                            else None
1275
                        ),
yichuan~'s avatar
yichuan~ committed
1276
                        logprobs=choice_logprobs,
1277
1278
1279
1280
1281
1282
                    )
                    chunk = ChatCompletionStreamResponse(
                        id=content["meta_info"]["id"],
                        choices=[choice_data],
                        model=request.model,
                    )
1283
1284
1285
1286
1287

                    is_firsts[index] = is_first
                    stream_buffers[index] = stream_buffer
                    n_prev_tokens[index] = n_prev_token

1288
                    yield f"data: {chunk.model_dump_json()}\n\n"
1289
                if request.stream_options and request.stream_options.include_usage:
1290
1291
1292
1293
1294
1295
1296
1297
                    total_prompt_tokens = sum(
                        tokens
                        for i, tokens in prompt_tokens.items()
                        if i % request.n == 0
                    )
                    total_completion_tokens = sum(
                        tokens for tokens in completion_tokens.values()
                    )
1298
                    usage = UsageInfo(
1299
1300
1301
                        prompt_tokens=total_prompt_tokens,
                        completion_tokens=total_completion_tokens,
                        total_tokens=total_prompt_tokens + total_completion_tokens,
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
                    )

                    final_usage_chunk = ChatCompletionStreamResponse(
                        id=str(uuid.uuid4().hex),
                        choices=[],
                        model=request.model,
                        usage=usage,
                    )
                    final_usage_data = final_usage_chunk.model_dump_json(
                        exclude_unset=True, exclude_none=True
                    )
                    yield f"data: {final_usage_data}\n\n"
1314
1315
1316
            except ValueError as e:
                error = create_streaming_error_response(str(e))
                yield f"data: {error}\n\n"
1317
1318
            yield "data: [DONE]\n\n"

1319
1320
1321
1322
1323
        return StreamingResponse(
            generate_stream_resp(),
            media_type="text/event-stream",
            background=tokenizer_manager.create_abort_task(adapted_request),
        )
1324
1325

    # Non-streaming response.
1326
1327
    try:
        ret = await tokenizer_manager.generate_request(
1328
1329
            adapted_request, raw_request
        ).__anext__()
1330
1331
    except ValueError as e:
        return create_error_response(str(e))
1332
1333
1334
    if not isinstance(ret, list):
        ret = [ret]

1335
1336
1337
    response = v1_chat_generate_response(
        request, ret, cache_report=tokenizer_manager.server_args.enable_cache_report
    )
1338

1339
1340
1341
    return response


1342
1343
1344
def v1_embedding_request(all_requests, tokenizer_manager):
    prompts = []
    sampling_params_list = []
Ying Sheng's avatar
Ying Sheng committed
1345
    first_prompt_type = type(all_requests[0].input)
1346
1347

    for request in all_requests:
Ying Sheng's avatar
Ying Sheng committed
1348
        prompt = request.input
1349
        assert (
1350
            type(prompt) is first_prompt_type
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
        ), "All prompts must be of the same type in file input settings"
        prompts.append(prompt)

    if len(all_requests) == 1:
        prompt = prompts[0]
        if isinstance(prompt, str) or isinstance(prompt[0], str):
            prompt_kwargs = {"text": prompt}
        else:
            prompt_kwargs = {"input_ids": prompt}
    else:
Baoyuan Qi's avatar
Baoyuan Qi committed
1361
        if isinstance(prompts[0], str) or isinstance(prompts[0][0], str):
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
            prompt_kwargs = {"text": prompts}
        else:
            prompt_kwargs = {"input_ids": prompts}

    adapted_request = EmbeddingReqInput(
        **prompt_kwargs,
    )

    if len(all_requests) == 1:
        return adapted_request, all_requests[0]
    return adapted_request, all_requests


Ying Sheng's avatar
Ying Sheng committed
1375
1376
1377
def v1_embedding_response(ret, model_path, to_file=False):
    embedding_objects = []
    prompt_tokens = 0
1378
    for idx, ret_item in enumerate(ret):
Ying Sheng's avatar
Ying Sheng committed
1379
1380
1381
        embedding_objects.append(
            EmbeddingObject(
                embedding=ret[idx]["embedding"],
1382
1383
1384
                index=idx,
            )
        )
Ying Sheng's avatar
Ying Sheng committed
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
        prompt_tokens += ret[idx]["meta_info"]["prompt_tokens"]

    return EmbeddingResponse(
        data=embedding_objects,
        model=model_path,
        usage=UsageInfo(
            prompt_tokens=prompt_tokens,
            total_tokens=prompt_tokens,
        ),
    )
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411


async def v1_embeddings(tokenizer_manager, raw_request: Request):
    request_json = await raw_request.json()
    all_requests = [EmbeddingRequest(**request_json)]
    adapted_request, request = v1_embedding_request(all_requests, tokenizer_manager)

    try:
        ret = await tokenizer_manager.generate_request(
            adapted_request, raw_request
        ).__anext__()
    except ValueError as e:
        return create_error_response(str(e))

    if not isinstance(ret, list):
        ret = [ret]

Ying Sheng's avatar
Ying Sheng committed
1412
    response = v1_embedding_response(ret, tokenizer_manager.model_path)
1413
1414
1415
1416

    return response


1417
def to_openai_style_logprobs(
1418
1419
1420
1421
    input_token_logprobs=None,
    output_token_logprobs=None,
    input_top_logprobs=None,
    output_top_logprobs=None,
1422
1423
1424
1425
1426
1427
1428
1429
):
    ret_logprobs = LogProbs()

    def append_token_logprobs(token_logprobs):
        for logprob, _, token_text in token_logprobs:
            ret_logprobs.tokens.append(token_text)
            ret_logprobs.token_logprobs.append(logprob)

1430
            # Not supported yet
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
            ret_logprobs.text_offset.append(-1)

    def append_top_logprobs(top_logprobs):
        for tokens in top_logprobs:
            if tokens is not None:
                ret_logprobs.top_logprobs.append(
                    {token[2]: token[0] for token in tokens}
                )
            else:
                ret_logprobs.top_logprobs.append(None)

1442
1443
1444
1445
1446
1447
1448
1449
    if input_token_logprobs is not None:
        append_token_logprobs(input_token_logprobs)
    if output_token_logprobs is not None:
        append_token_logprobs(output_token_logprobs)
    if input_top_logprobs is not None:
        append_top_logprobs(input_top_logprobs)
    if output_top_logprobs is not None:
        append_top_logprobs(output_top_logprobs)
1450

Liangsheng Yin's avatar
Liangsheng Yin committed
1451
    return ret_logprobs