openai_api.py 1.56 KB
Newer Older
dengjb's avatar
dengjb committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
coding   : utf-8
@Date    : 2024/7/11
@Author  : Shaobo
@Describe: 
"""
import time
from typing import Literal

import shortuuid
from pydantic import BaseModel


class ChatMessage(BaseModel):
    role: str
    content: str


class ChatCompletionRequest(BaseModel):
    model: str = "codegeex4"
    messages: list[ChatMessage]
    temperature: float = 0.2
    top_p: float = 1.0
    max_tokens: int = 1024
    stop: list[str] = ['<|user|>', '<|assistant|>', '<|observation|>', '<|endoftext|>']
    stream: bool = True
    presence_penalty: float = None


class DeltaMessage(BaseModel):
    role: str
    content: str


class ChatCompletionResponseStreamChoice(BaseModel):
    index: int = 0
    delta: DeltaMessage = DeltaMessage(role='assistant', content='')
    finish_reason: Literal["stop", "length"] = None


class ChatCompletionStreamResponse(BaseModel):
    id: str = f"chatcmpl-{shortuuid.random()}"
    object: str = "chat.completion.chunk"
    created: int = int(time.time())
    model: str = "codegeex4"
    choices: list[ChatCompletionResponseStreamChoice] = [ChatCompletionResponseStreamChoice()]


class ChatCompletionResponseChoice(BaseModel):
    index: int = 0
    message: ChatMessage = ChatMessage(role="assistant", content="")
    finish_reason: Literal["stop", "length"] = None


class ChatCompletionResponse(BaseModel):
    id: str = f"chatcmpl-{shortuuid.random()}"
    object: str = "chat.completion"
    created: int = int(time.time())
    model: str = "codegeex4"
    choices: list[ChatCompletionResponseChoice] = [ChatCompletionResponseChoice()]
    # usage: UsageInfo