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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
from __future__ import annotations
from typing import Union
from time import time
class Model():
...
class ChatCompletion(Model):
def __init__(
self,
content: str,
finish_reason: str,
completion_id: str = None,
created: int = None
):
self.id: str = f"chatcmpl-{completion_id}" if completion_id else None
self.object: str = "chat.completion"
self.created: int = created
self.model: str = None
self.provider: str = None
self.choices = [ChatCompletionChoice(ChatCompletionMessage(content), finish_reason)]
self.usage: dict[str, int] = {
"prompt_tokens": 0, #prompt_tokens,
"completion_tokens": 0, #completion_tokens,
"total_tokens": 0, #prompt_tokens + completion_tokens,
}
def to_json(self):
return {
**self.__dict__,
"choices": [choice.to_json() for choice in self.choices]
}
class ChatCompletionChunk(Model):
def __init__(
self,
content: str,
finish_reason: str,
completion_id: str = None,
created: int = None
):
self.id: str = f"chatcmpl-{completion_id}" if completion_id else None
self.object: str = "chat.completion.chunk"
self.created: int = created
self.model: str = None
self.provider: str = None
self.choices = [ChatCompletionDeltaChoice(ChatCompletionDelta(content), finish_reason)]
def to_json(self):
return {
**self.__dict__,
"choices": [choice.to_json() for choice in self.choices]
}
class ChatCompletionMessage(Model):
def __init__(self, content: Union[str, None]):
self.role = "assistant"
self.content = content
def to_json(self):
return self.__dict__
class ChatCompletionChoice(Model):
def __init__(self, message: ChatCompletionMessage, finish_reason: str):
self.index = 0
self.message = message
self.finish_reason = finish_reason
def to_json(self):
return {
**self.__dict__,
"message": self.message.to_json()
}
class ChatCompletionDelta(Model):
content: Union[str, None] = None
def __init__(self, content: Union[str, None]):
if content is not None:
self.content = content
self.role = "assistant"
def to_json(self):
return self.__dict__
class ChatCompletionDeltaChoice(Model):
def __init__(self, delta: ChatCompletionDelta, finish_reason: Union[str, None]):
self.index = 0
self.delta = delta
self.finish_reason = finish_reason
def to_json(self):
return {
**self.__dict__,
"delta": self.delta.to_json()
}
class Image(Model):
def __init__(self, url: str = None, b64_json: str = None, revised_prompt: str = None) -> None:
if url is not None:
self.url = url
if b64_json is not None:
self.b64_json = b64_json
if revised_prompt is not None:
self.revised_prompt = revised_prompt
def to_json(self):
return self.__dict__
class ImagesResponse(Model):
data: list[Image]
model: str
provider: str
created: int
def __init__(self, data: list[Image], created: int = None, model: str = None, provider: str = None) -> None:
self.data = data
if created is None:
created = int(time())
self.model = model
if provider is not None:
self.provider = provider
self.created = created
def to_json(self):
return {
**self.__dict__,
"data": [image.to_json() for image in self.data]
}
|