2024-04-06 21:37:07 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-24 19:43:45 +03:00
|
|
|
from typing import Optional, List, Dict
|
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
|
|
|
|
2024-11-24 19:43:45 +03:00
|
|
|
from .helper import filter_none
|
|
|
|
|
|
|
|
try:
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
except ImportError:
|
|
|
|
class BaseModel():
|
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(cls, **data):
|
2024-11-24 19:43:45 +03:00
|
|
|
new = cls()
|
|
|
|
for key, value in data.items():
|
|
|
|
setattr(new, key, value)
|
|
|
|
return new
|
|
|
|
class Field():
|
|
|
|
def __init__(self, **config):
|
|
|
|
pass
|
|
|
|
|
2024-12-07 22:07:11 +03:00
|
|
|
class BaseModel(BaseModel):
|
|
|
|
@classmethod
|
|
|
|
def model_construct(cls, **data):
|
|
|
|
if hasattr(super(), "model_construct"):
|
|
|
|
return super().model_construct(**data)
|
|
|
|
return cls.construct(**data)
|
|
|
|
|
2024-11-24 19:43:45 +03:00
|
|
|
class ChatCompletionChunk(BaseModel):
|
|
|
|
id: str
|
|
|
|
object: str
|
|
|
|
created: int
|
|
|
|
model: str
|
|
|
|
provider: Optional[str]
|
|
|
|
choices: List[ChatCompletionDeltaChoice]
|
2024-04-06 21:37:07 +03:00
|
|
|
|
2024-11-24 19:43:45 +03:00
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(
|
2024-11-24 19:43:45 +03:00
|
|
|
cls,
|
2024-04-06 21:37:07 +03:00
|
|
|
content: str,
|
|
|
|
finish_reason: str,
|
|
|
|
completion_id: str = None,
|
|
|
|
created: int = None
|
|
|
|
):
|
2024-12-07 22:07:11 +03:00
|
|
|
return super().model_construct(
|
2024-11-24 19:43:45 +03:00
|
|
|
id=f"chatcmpl-{completion_id}" if completion_id else None,
|
|
|
|
object="chat.completion.cunk",
|
|
|
|
created=created,
|
|
|
|
model=None,
|
|
|
|
provider=None,
|
2024-12-07 22:07:11 +03:00
|
|
|
choices=[ChatCompletionDeltaChoice.model_construct(
|
|
|
|
ChatCompletionDelta.model_construct(content),
|
2024-11-24 19:43:45 +03:00
|
|
|
finish_reason
|
|
|
|
)]
|
|
|
|
)
|
|
|
|
|
|
|
|
class ChatCompletionMessage(BaseModel):
|
|
|
|
role: str
|
|
|
|
content: str
|
|
|
|
|
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(cls, content: str):
|
|
|
|
return super().model_construct(role="assistant", content=content)
|
2024-11-24 19:43:45 +03:00
|
|
|
|
|
|
|
class ChatCompletionChoice(BaseModel):
|
|
|
|
index: int
|
|
|
|
message: ChatCompletionMessage
|
|
|
|
finish_reason: str
|
|
|
|
|
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(cls, message: ChatCompletionMessage, finish_reason: str):
|
|
|
|
return super().model_construct(index=0, message=message, finish_reason=finish_reason)
|
2024-11-24 19:43:45 +03:00
|
|
|
|
|
|
|
class ChatCompletion(BaseModel):
|
|
|
|
id: str
|
|
|
|
object: str
|
|
|
|
created: int
|
|
|
|
model: str
|
|
|
|
provider: Optional[str]
|
|
|
|
choices: List[ChatCompletionChoice]
|
|
|
|
usage: Dict[str, int] = Field(examples=[{
|
|
|
|
"prompt_tokens": 0, #prompt_tokens,
|
|
|
|
"completion_tokens": 0, #completion_tokens,
|
|
|
|
"total_tokens": 0, #prompt_tokens + completion_tokens,
|
|
|
|
}])
|
|
|
|
|
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(
|
2024-11-24 19:43:45 +03:00
|
|
|
cls,
|
2024-04-06 21:37:07 +03:00
|
|
|
content: str,
|
|
|
|
finish_reason: str,
|
|
|
|
completion_id: str = None,
|
|
|
|
created: int = None
|
|
|
|
):
|
2024-12-07 22:07:11 +03:00
|
|
|
return super().model_construct(
|
2024-11-24 19:43:45 +03:00
|
|
|
id=f"chatcmpl-{completion_id}" if completion_id else None,
|
|
|
|
object="chat.completion",
|
|
|
|
created=created,
|
|
|
|
model=None,
|
|
|
|
provider=None,
|
2024-12-07 22:07:11 +03:00
|
|
|
choices=[ChatCompletionChoice.model_construct(
|
|
|
|
ChatCompletionMessage.model_construct(content),
|
2024-11-24 19:43:45 +03:00
|
|
|
finish_reason
|
|
|
|
)],
|
|
|
|
usage={
|
|
|
|
"prompt_tokens": 0, #prompt_tokens,
|
|
|
|
"completion_tokens": 0, #completion_tokens,
|
|
|
|
"total_tokens": 0, #prompt_tokens + completion_tokens,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
class ChatCompletionDelta(BaseModel):
|
|
|
|
role: str
|
|
|
|
content: str
|
|
|
|
|
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(cls, content: Optional[str]):
|
|
|
|
return super().model_construct(role="assistant", content=content)
|
2024-11-24 19:43:45 +03:00
|
|
|
|
|
|
|
class ChatCompletionDeltaChoice(BaseModel):
|
|
|
|
index: int
|
|
|
|
delta: ChatCompletionDelta
|
|
|
|
finish_reason: Optional[str]
|
|
|
|
|
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(cls, delta: ChatCompletionDelta, finish_reason: Optional[str]):
|
|
|
|
return super().model_construct(index=0, delta=delta, finish_reason=finish_reason)
|
2024-11-24 19:43:45 +03:00
|
|
|
|
|
|
|
class Image(BaseModel):
|
|
|
|
url: Optional[str]
|
|
|
|
b64_json: Optional[str]
|
|
|
|
revised_prompt: Optional[str]
|
|
|
|
|
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(cls, url: str = None, b64_json: str = None, revised_prompt: str = None):
|
|
|
|
return super().model_construct(**filter_none(
|
2024-11-24 19:43:45 +03:00
|
|
|
url=url,
|
|
|
|
b64_json=b64_json,
|
|
|
|
revised_prompt=revised_prompt
|
|
|
|
))
|
|
|
|
|
|
|
|
class ImagesResponse(BaseModel):
|
2024-11-25 02:36:38 +03:00
|
|
|
data: List[Image]
|
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
|
|
|
model: str
|
|
|
|
provider: str
|
|
|
|
created: int
|
|
|
|
|
2024-11-24 19:43:45 +03:00
|
|
|
@classmethod
|
2024-12-07 22:07:11 +03:00
|
|
|
def model_construct(cls, data: List[Image], created: int = None, model: str = None, provider: str = None):
|
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())
|
2024-12-07 22:07:11 +03:00
|
|
|
return super().model_construct(
|
2024-11-24 19:43:45 +03:00
|
|
|
data=data,
|
|
|
|
model=model,
|
|
|
|
provider=provider,
|
|
|
|
created=created
|
2024-11-27 01:38:48 +03:00
|
|
|
)
|