2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
class Model():
|
2024-02-23 04:35:13 +03:00
|
|
|
...
|
2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
class ChatCompletion(Model):
|
2024-02-23 04:35:13 +03:00
|
|
|
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]
|
|
|
|
}
|
2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
class ChatCompletionChunk(Model):
|
2024-02-23 04:35:13 +03:00
|
|
|
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]
|
|
|
|
}
|
2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
class ChatCompletionMessage(Model):
|
2024-02-23 04:35:13 +03:00
|
|
|
def __init__(self, content: str | None):
|
|
|
|
self.role = "assistant"
|
2024-02-14 11:21:57 +03:00
|
|
|
self.content = content
|
2024-02-23 04:35:13 +03:00
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return self.__dict__
|
2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
class ChatCompletionChoice(Model):
|
2024-02-23 04:35:13 +03:00
|
|
|
def __init__(self, message: ChatCompletionMessage, finish_reason: str):
|
|
|
|
self.index = 0
|
2024-02-14 11:21:57 +03:00
|
|
|
self.message = message
|
2024-02-23 04:35:13 +03:00
|
|
|
self.finish_reason = finish_reason
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
**self.__dict__,
|
|
|
|
"message": self.message.to_json()
|
|
|
|
}
|
2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
class ChatCompletionDelta(Model):
|
2024-02-23 04:35:13 +03:00
|
|
|
def __init__(self, content: str | None):
|
|
|
|
if content is not None:
|
|
|
|
self.content = content
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return self.__dict__
|
2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
class ChatCompletionDeltaChoice(Model):
|
2024-02-23 04:35:13 +03:00
|
|
|
def __init__(self, delta: ChatCompletionDelta, finish_reason: str | None):
|
2024-02-14 11:21:57 +03:00
|
|
|
self.delta = delta
|
2024-02-23 04:35:13 +03:00
|
|
|
self.finish_reason = finish_reason
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
**self.__dict__,
|
|
|
|
"delta": self.delta.to_json()
|
|
|
|
}
|
2024-02-14 11:21:57 +03:00
|
|
|
|
|
|
|
class Image(Model):
|
|
|
|
url: str
|
|
|
|
|
|
|
|
def __init__(self, url: str) -> None:
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
class ImagesResponse(Model):
|
|
|
|
data: list[Image]
|
|
|
|
|
|
|
|
def __init__(self, data: list) -> None:
|
|
|
|
self.data = data
|