43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# Copyright (c) 2023 Intel Corporation
|
|
#
|
|
# 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.
|
|
"""
|
|
Code source from FastChat's OpenAI protocol:
|
|
https://github.com/lm-sys/FastChat/blob/main/fastchat/protocol/openai_api_protocol.py
|
|
"""
|
|
from typing import Optional, List, Any, Union
|
|
import time
|
|
import shortuuid
|
|
# pylint: disable=E0611
|
|
from pydantic import BaseModel, Field
|
|
|
|
class ChatCompletionRequest(BaseModel):
|
|
prompt: Union[str, List[Any]]
|
|
device: Optional[str] = 'cpu'
|
|
temperature: Optional[float] = 0.7
|
|
top_p: Optional[float] = 1.0
|
|
top_k: Optional[int] = 1
|
|
repetition_penalty: Optional[float] = 1.0
|
|
max_new_tokens: Optional[int] = 128
|
|
stream: Optional[bool] = False
|
|
|
|
|
|
class ChatCompletionResponse(BaseModel):
|
|
id: str = Field(default_factory=lambda: f"chatcmpl-{shortuuid.random()}")
|
|
object: str = "chat.completion"
|
|
created: int = Field(default_factory=lambda: int(time.time()))
|
|
response: str
|