2024-04-06 21:37:07 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Union
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
from time import time
|
2024-04-06 21:37:07 +03:00
|
|
|
|
|
|
|
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
|
2024-05-17 19:43:17 +03:00
|
|
|
self.role = "assistant"
|
2024-04-06 21:37:07 +03:00
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return self.__dict__
|
|
|
|
|
|
|
|
class ChatCompletionDeltaChoice(Model):
|
|
|
|
def __init__(self, delta: ChatCompletionDelta, finish_reason: Union[str, None]):
|
2024-05-17 19:43:17 +03:00
|
|
|
self.index = 0
|
2024-04-06 21:37:07 +03:00
|
|
|
self.delta = delta
|
|
|
|
self.finish_reason = finish_reason
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
**self.__dict__,
|
|
|
|
"delta": self.delta.to_json()
|
|
|
|
}
|
|
|
|
|
|
|
|
class Image(Model):
|
2024-05-18 08:37:37 +03:00
|
|
|
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
|
2024-04-06 21:37:07 +03:00
|
|
|
|
2024-05-18 08:37:37 +03:00
|
|
|
def to_json(self):
|
|
|
|
return self.__dict__
|
2024-04-06 21:37:07 +03:00
|
|
|
|
|
|
|
class ImagesResponse(Model):
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
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:
|
2024-04-06 21:37:07 +03:00
|
|
|
self.data = data
|
Fix api streaming, fix AsyncClient (#2357)
* Fix api streaming, fix AsyncClient, Improve Client class, Some providers fixes, Update models list, Fix some tests, Update model list in Airforce provid
er, Add OpenAi image generation url to api, Fix reload and debug in api arguments, Fix websearch in gui
* Fix Cloadflare and Pi and AmigoChat provider
* Fix conversation support in DDG provider, Add cloudflare bypass with nodriver
* Fix unittests without curl_cffi
2024-11-16 15:19:51 +03:00
|
|
|
if created is None:
|
|
|
|
created = int(time())
|
|
|
|
self.model = model
|
|
|
|
if provider is not None:
|
|
|
|
self.provider = provider
|
2024-05-18 08:37:37 +03:00
|
|
|
self.created = created
|
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
**self.__dict__,
|
|
|
|
"data": [image.to_json() for image in self.data]
|
|
|
|
}
|