chore(api/core): apply ruff reformatting (#7624)

This commit is contained in:
Bowen Liang
2024-09-10 17:00:20 +08:00
committed by GitHub
parent 178730266d
commit 2cf1187b32
724 changed files with 21180 additions and 21123 deletions

View File

@@ -7,6 +7,7 @@ class I18nObject(BaseModel):
"""
Model class for i18n object.
"""
zh_Hans: Optional[str] = None
en_US: str

View File

@@ -2,123 +2,123 @@ from core.model_runtime.entities.model_entities import DefaultParameterName
PARAMETER_RULE_TEMPLATE: dict[DefaultParameterName, dict] = {
DefaultParameterName.TEMPERATURE: {
'label': {
'en_US': 'Temperature',
'zh_Hans': '温度',
"label": {
"en_US": "Temperature",
"zh_Hans": "温度",
},
'type': 'float',
'help': {
'en_US': 'Controls randomness. Lower temperature results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive. Higher temperature results in more random completions.',
'zh_Hans': '温度控制随机性。较低的温度会导致较少的随机完成。随着温度接近零,模型将变得确定性和重复性。较高的温度会导致更多的随机完成。',
"type": "float",
"help": {
"en_US": "Controls randomness. Lower temperature results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive. Higher temperature results in more random completions.",
"zh_Hans": "温度控制随机性。较低的温度会导致较少的随机完成。随着温度接近零,模型将变得确定性和重复性。较高的温度会导致更多的随机完成。",
},
'required': False,
'default': 0.0,
'min': 0.0,
'max': 1.0,
'precision': 2,
"required": False,
"default": 0.0,
"min": 0.0,
"max": 1.0,
"precision": 2,
},
DefaultParameterName.TOP_P: {
'label': {
'en_US': 'Top P',
'zh_Hans': 'Top P',
"label": {
"en_US": "Top P",
"zh_Hans": "Top P",
},
'type': 'float',
'help': {
'en_US': 'Controls diversity via nucleus sampling: 0.5 means half of all likelihood-weighted options are considered.',
'zh_Hans': '通过核心采样控制多样性0.5表示考虑了一半的所有可能性加权选项。',
"type": "float",
"help": {
"en_US": "Controls diversity via nucleus sampling: 0.5 means half of all likelihood-weighted options are considered.",
"zh_Hans": "通过核心采样控制多样性0.5表示考虑了一半的所有可能性加权选项。",
},
'required': False,
'default': 1.0,
'min': 0.0,
'max': 1.0,
'precision': 2,
"required": False,
"default": 1.0,
"min": 0.0,
"max": 1.0,
"precision": 2,
},
DefaultParameterName.TOP_K: {
'label': {
'en_US': 'Top K',
'zh_Hans': 'Top K',
"label": {
"en_US": "Top K",
"zh_Hans": "Top K",
},
'type': 'int',
'help': {
'en_US': 'Limits the number of tokens to consider for each step by keeping only the k most likely tokens.',
'zh_Hans': '通过只保留每一步中最可能的 k 个标记来限制要考虑的标记数量。',
"type": "int",
"help": {
"en_US": "Limits the number of tokens to consider for each step by keeping only the k most likely tokens.",
"zh_Hans": "通过只保留每一步中最可能的 k 个标记来限制要考虑的标记数量。",
},
'required': False,
'default': 50,
'min': 1,
'max': 100,
'precision': 0,
"required": False,
"default": 50,
"min": 1,
"max": 100,
"precision": 0,
},
DefaultParameterName.PRESENCE_PENALTY: {
'label': {
'en_US': 'Presence Penalty',
'zh_Hans': '存在惩罚',
"label": {
"en_US": "Presence Penalty",
"zh_Hans": "存在惩罚",
},
'type': 'float',
'help': {
'en_US': 'Applies a penalty to the log-probability of tokens already in the text.',
'zh_Hans': '对文本中已有的标记的对数概率施加惩罚。',
"type": "float",
"help": {
"en_US": "Applies a penalty to the log-probability of tokens already in the text.",
"zh_Hans": "对文本中已有的标记的对数概率施加惩罚。",
},
'required': False,
'default': 0.0,
'min': 0.0,
'max': 1.0,
'precision': 2,
"required": False,
"default": 0.0,
"min": 0.0,
"max": 1.0,
"precision": 2,
},
DefaultParameterName.FREQUENCY_PENALTY: {
'label': {
'en_US': 'Frequency Penalty',
'zh_Hans': '频率惩罚',
"label": {
"en_US": "Frequency Penalty",
"zh_Hans": "频率惩罚",
},
'type': 'float',
'help': {
'en_US': 'Applies a penalty to the log-probability of tokens that appear in the text.',
'zh_Hans': '对文本中出现的标记的对数概率施加惩罚。',
"type": "float",
"help": {
"en_US": "Applies a penalty to the log-probability of tokens that appear in the text.",
"zh_Hans": "对文本中出现的标记的对数概率施加惩罚。",
},
'required': False,
'default': 0.0,
'min': 0.0,
'max': 1.0,
'precision': 2,
"required": False,
"default": 0.0,
"min": 0.0,
"max": 1.0,
"precision": 2,
},
DefaultParameterName.MAX_TOKENS: {
'label': {
'en_US': 'Max Tokens',
'zh_Hans': '最大标记',
"label": {
"en_US": "Max Tokens",
"zh_Hans": "最大标记",
},
'type': 'int',
'help': {
'en_US': 'Specifies the upper limit on the length of generated results. If the generated results are truncated, you can increase this parameter.',
'zh_Hans': '指定生成结果长度的上限。如果生成结果截断,可以调大该参数。',
"type": "int",
"help": {
"en_US": "Specifies the upper limit on the length of generated results. If the generated results are truncated, you can increase this parameter.",
"zh_Hans": "指定生成结果长度的上限。如果生成结果截断,可以调大该参数。",
},
'required': False,
'default': 64,
'min': 1,
'max': 2048,
'precision': 0,
"required": False,
"default": 64,
"min": 1,
"max": 2048,
"precision": 0,
},
DefaultParameterName.RESPONSE_FORMAT: {
'label': {
'en_US': 'Response Format',
'zh_Hans': '回复格式',
"label": {
"en_US": "Response Format",
"zh_Hans": "回复格式",
},
'type': 'string',
'help': {
'en_US': 'Set a response format, ensure the output from llm is a valid code block as possible, such as JSON, XML, etc.',
'zh_Hans': '设置一个返回格式确保llm的输出尽可能是有效的代码块如JSON、XML等',
"type": "string",
"help": {
"en_US": "Set a response format, ensure the output from llm is a valid code block as possible, such as JSON, XML, etc.",
"zh_Hans": "设置一个返回格式确保llm的输出尽可能是有效的代码块如JSON、XML等",
},
'required': False,
'options': ['JSON', 'XML'],
"required": False,
"options": ["JSON", "XML"],
},
DefaultParameterName.JSON_SCHEMA: {
'label': {
'en_US': 'JSON Schema',
"label": {
"en_US": "JSON Schema",
},
'type': 'text',
'help': {
'en_US': 'Set a response json schema will ensure LLM to adhere it.',
'zh_Hans': '设置返回的json schemallm将按照它返回',
"type": "text",
"help": {
"en_US": "Set a response json schema will ensure LLM to adhere it.",
"zh_Hans": "设置返回的json schemallm将按照它返回",
},
'required': False,
"required": False,
},
}

View File

@@ -12,11 +12,12 @@ class LLMMode(Enum):
"""
Enum class for large language model mode.
"""
COMPLETION = "completion"
CHAT = "chat"
@classmethod
def value_of(cls, value: str) -> 'LLMMode':
def value_of(cls, value: str) -> "LLMMode":
"""
Get value of given mode.
@@ -26,13 +27,14 @@ class LLMMode(Enum):
for mode in cls:
if mode.value == value:
return mode
raise ValueError(f'invalid mode value {value}')
raise ValueError(f"invalid mode value {value}")
class LLMUsage(ModelUsage):
"""
Model class for llm usage.
"""
prompt_tokens: int
prompt_unit_price: Decimal
prompt_price_unit: Decimal
@@ -50,20 +52,20 @@ class LLMUsage(ModelUsage):
def empty_usage(cls):
return cls(
prompt_tokens=0,
prompt_unit_price=Decimal('0.0'),
prompt_price_unit=Decimal('0.0'),
prompt_price=Decimal('0.0'),
prompt_unit_price=Decimal("0.0"),
prompt_price_unit=Decimal("0.0"),
prompt_price=Decimal("0.0"),
completion_tokens=0,
completion_unit_price=Decimal('0.0'),
completion_price_unit=Decimal('0.0'),
completion_price=Decimal('0.0'),
completion_unit_price=Decimal("0.0"),
completion_price_unit=Decimal("0.0"),
completion_price=Decimal("0.0"),
total_tokens=0,
total_price=Decimal('0.0'),
currency='USD',
latency=0.0
total_price=Decimal("0.0"),
currency="USD",
latency=0.0,
)
def plus(self, other: 'LLMUsage') -> 'LLMUsage':
def plus(self, other: "LLMUsage") -> "LLMUsage":
"""
Add two LLMUsage instances together.
@@ -85,10 +87,10 @@ class LLMUsage(ModelUsage):
total_tokens=self.total_tokens + other.total_tokens,
total_price=self.total_price + other.total_price,
currency=other.currency,
latency=self.latency + other.latency
latency=self.latency + other.latency,
)
def __add__(self, other: 'LLMUsage') -> 'LLMUsage':
def __add__(self, other: "LLMUsage") -> "LLMUsage":
"""
Overload the + operator to add two LLMUsage instances.
@@ -97,10 +99,12 @@ class LLMUsage(ModelUsage):
"""
return self.plus(other)
class LLMResult(BaseModel):
"""
Model class for llm result.
"""
model: str
prompt_messages: list[PromptMessage]
message: AssistantPromptMessage
@@ -112,6 +116,7 @@ class LLMResultChunkDelta(BaseModel):
"""
Model class for llm result chunk delta.
"""
index: int
message: AssistantPromptMessage
usage: Optional[LLMUsage] = None
@@ -122,6 +127,7 @@ class LLMResultChunk(BaseModel):
"""
Model class for llm result chunk.
"""
model: str
prompt_messages: list[PromptMessage]
system_fingerprint: Optional[str] = None
@@ -132,4 +138,5 @@ class NumTokensResult(PriceInfo):
"""
Model class for number of tokens result.
"""
tokens: int

View File

@@ -9,13 +9,14 @@ class PromptMessageRole(Enum):
"""
Enum class for prompt message.
"""
SYSTEM = "system"
USER = "user"
ASSISTANT = "assistant"
TOOL = "tool"
@classmethod
def value_of(cls, value: str) -> 'PromptMessageRole':
def value_of(cls, value: str) -> "PromptMessageRole":
"""
Get value of given mode.
@@ -25,13 +26,14 @@ class PromptMessageRole(Enum):
for mode in cls:
if mode.value == value:
return mode
raise ValueError(f'invalid prompt message type value {value}')
raise ValueError(f"invalid prompt message type value {value}")
class PromptMessageTool(BaseModel):
"""
Model class for prompt message tool.
"""
name: str
description: str
parameters: dict
@@ -41,7 +43,8 @@ class PromptMessageFunction(BaseModel):
"""
Model class for prompt message function.
"""
type: str = 'function'
type: str = "function"
function: PromptMessageTool
@@ -49,14 +52,16 @@ class PromptMessageContentType(Enum):
"""
Enum class for prompt message content type.
"""
TEXT = 'text'
IMAGE = 'image'
TEXT = "text"
IMAGE = "image"
class PromptMessageContent(BaseModel):
"""
Model class for prompt message content.
"""
type: PromptMessageContentType
data: str
@@ -65,6 +70,7 @@ class TextPromptMessageContent(PromptMessageContent):
"""
Model class for text prompt message content.
"""
type: PromptMessageContentType = PromptMessageContentType.TEXT
@@ -72,9 +78,10 @@ class ImagePromptMessageContent(PromptMessageContent):
"""
Model class for image prompt message content.
"""
class DETAIL(Enum):
LOW = 'low'
HIGH = 'high'
LOW = "low"
HIGH = "high"
type: PromptMessageContentType = PromptMessageContentType.IMAGE
detail: DETAIL = DETAIL.LOW
@@ -84,6 +91,7 @@ class PromptMessage(ABC, BaseModel):
"""
Model class for prompt message.
"""
role: PromptMessageRole
content: Optional[str | list[PromptMessageContent]] = None
name: Optional[str] = None
@@ -101,6 +109,7 @@ class UserPromptMessage(PromptMessage):
"""
Model class for user prompt message.
"""
role: PromptMessageRole = PromptMessageRole.USER
@@ -108,14 +117,17 @@ class AssistantPromptMessage(PromptMessage):
"""
Model class for assistant prompt message.
"""
class ToolCall(BaseModel):
"""
Model class for assistant prompt message tool call.
"""
class ToolCallFunction(BaseModel):
"""
Model class for assistant prompt message tool call function.
"""
name: str
arguments: str
@@ -123,7 +135,7 @@ class AssistantPromptMessage(PromptMessage):
type: str
function: ToolCallFunction
@field_validator('id', mode='before')
@field_validator("id", mode="before")
@classmethod
def transform_id_to_str(cls, value) -> str:
if not isinstance(value, str):
@@ -145,10 +157,12 @@ class AssistantPromptMessage(PromptMessage):
return True
class SystemPromptMessage(PromptMessage):
"""
Model class for system prompt message.
"""
role: PromptMessageRole = PromptMessageRole.SYSTEM
@@ -156,6 +170,7 @@ class ToolPromptMessage(PromptMessage):
"""
Model class for tool prompt message.
"""
role: PromptMessageRole = PromptMessageRole.TOOL
tool_call_id: str

View File

@@ -11,6 +11,7 @@ class ModelType(Enum):
"""
Enum class for model type.
"""
LLM = "llm"
TEXT_EMBEDDING = "text-embedding"
RERANK = "rerank"
@@ -26,22 +27,22 @@ class ModelType(Enum):
:return: model type
"""
if origin_model_type == 'text-generation' or origin_model_type == cls.LLM.value:
if origin_model_type == "text-generation" or origin_model_type == cls.LLM.value:
return cls.LLM
elif origin_model_type == 'embeddings' or origin_model_type == cls.TEXT_EMBEDDING.value:
elif origin_model_type == "embeddings" or origin_model_type == cls.TEXT_EMBEDDING.value:
return cls.TEXT_EMBEDDING
elif origin_model_type == 'reranking' or origin_model_type == cls.RERANK.value:
elif origin_model_type == "reranking" or origin_model_type == cls.RERANK.value:
return cls.RERANK
elif origin_model_type == 'speech2text' or origin_model_type == cls.SPEECH2TEXT.value:
elif origin_model_type == "speech2text" or origin_model_type == cls.SPEECH2TEXT.value:
return cls.SPEECH2TEXT
elif origin_model_type == 'tts' or origin_model_type == cls.TTS.value:
elif origin_model_type == "tts" or origin_model_type == cls.TTS.value:
return cls.TTS
elif origin_model_type == 'text2img' or origin_model_type == cls.TEXT2IMG.value:
elif origin_model_type == "text2img" or origin_model_type == cls.TEXT2IMG.value:
return cls.TEXT2IMG
elif origin_model_type == cls.MODERATION.value:
return cls.MODERATION
else:
raise ValueError(f'invalid origin model type {origin_model_type}')
raise ValueError(f"invalid origin model type {origin_model_type}")
def to_origin_model_type(self) -> str:
"""
@@ -50,26 +51,28 @@ class ModelType(Enum):
:return: origin model type
"""
if self == self.LLM:
return 'text-generation'
return "text-generation"
elif self == self.TEXT_EMBEDDING:
return 'embeddings'
return "embeddings"
elif self == self.RERANK:
return 'reranking'
return "reranking"
elif self == self.SPEECH2TEXT:
return 'speech2text'
return "speech2text"
elif self == self.TTS:
return 'tts'
return "tts"
elif self == self.MODERATION:
return 'moderation'
return "moderation"
elif self == self.TEXT2IMG:
return 'text2img'
return "text2img"
else:
raise ValueError(f'invalid model type {self}')
raise ValueError(f"invalid model type {self}")
class FetchFrom(Enum):
"""
Enum class for fetch from.
"""
PREDEFINED_MODEL = "predefined-model"
CUSTOMIZABLE_MODEL = "customizable-model"
@@ -78,6 +81,7 @@ class ModelFeature(Enum):
"""
Enum class for llm feature.
"""
TOOL_CALL = "tool-call"
MULTI_TOOL_CALL = "multi-tool-call"
AGENT_THOUGHT = "agent-thought"
@@ -89,6 +93,7 @@ class DefaultParameterName(str, Enum):
"""
Enum class for parameter template variable.
"""
TEMPERATURE = "temperature"
TOP_P = "top_p"
TOP_K = "top_k"
@@ -99,7 +104,7 @@ class DefaultParameterName(str, Enum):
JSON_SCHEMA = "json_schema"
@classmethod
def value_of(cls, value: Any) -> 'DefaultParameterName':
def value_of(cls, value: Any) -> "DefaultParameterName":
"""
Get parameter name from value.
@@ -109,13 +114,14 @@ class DefaultParameterName(str, Enum):
for name in cls:
if name.value == value:
return name
raise ValueError(f'invalid parameter name {value}')
raise ValueError(f"invalid parameter name {value}")
class ParameterType(Enum):
"""
Enum class for parameter type.
"""
FLOAT = "float"
INT = "int"
STRING = "string"
@@ -127,6 +133,7 @@ class ModelPropertyKey(Enum):
"""
Enum class for model property key.
"""
MODE = "mode"
CONTEXT_SIZE = "context_size"
MAX_CHUNKS = "max_chunks"
@@ -144,6 +151,7 @@ class ProviderModel(BaseModel):
"""
Model class for provider model.
"""
model: str
label: I18nObject
model_type: ModelType
@@ -158,6 +166,7 @@ class ParameterRule(BaseModel):
"""
Model class for parameter rule.
"""
name: str
use_template: Optional[str] = None
label: I18nObject
@@ -175,6 +184,7 @@ class PriceConfig(BaseModel):
"""
Model class for pricing info.
"""
input: Decimal
output: Optional[Decimal] = None
unit: Decimal
@@ -185,6 +195,7 @@ class AIModelEntity(ProviderModel):
"""
Model class for AI model.
"""
parameter_rules: list[ParameterRule] = []
pricing: Optional[PriceConfig] = None
@@ -197,6 +208,7 @@ class PriceType(Enum):
"""
Enum class for price type.
"""
INPUT = "input"
OUTPUT = "output"
@@ -205,6 +217,7 @@ class PriceInfo(BaseModel):
"""
Model class for price info.
"""
unit_price: Decimal
unit: Decimal
total_amount: Decimal

View File

@@ -12,6 +12,7 @@ class ConfigurateMethod(Enum):
"""
Enum class for configurate method of provider model.
"""
PREDEFINED_MODEL = "predefined-model"
CUSTOMIZABLE_MODEL = "customizable-model"
@@ -20,6 +21,7 @@ class FormType(Enum):
"""
Enum class for form type.
"""
TEXT_INPUT = "text-input"
SECRET_INPUT = "secret-input"
SELECT = "select"
@@ -31,6 +33,7 @@ class FormShowOnObject(BaseModel):
"""
Model class for form show on.
"""
variable: str
value: str
@@ -39,6 +42,7 @@ class FormOption(BaseModel):
"""
Model class for form option.
"""
label: I18nObject
value: str
show_on: list[FormShowOnObject] = []
@@ -46,15 +50,14 @@ class FormOption(BaseModel):
def __init__(self, **data):
super().__init__(**data)
if not self.label:
self.label = I18nObject(
en_US=self.value
)
self.label = I18nObject(en_US=self.value)
class CredentialFormSchema(BaseModel):
"""
Model class for credential form schema.
"""
variable: str
label: I18nObject
type: FormType
@@ -70,6 +73,7 @@ class ProviderCredentialSchema(BaseModel):
"""
Model class for provider credential schema.
"""
credential_form_schemas: list[CredentialFormSchema]
@@ -82,6 +86,7 @@ class ModelCredentialSchema(BaseModel):
"""
Model class for model credential schema.
"""
model: FieldModelSchema
credential_form_schemas: list[CredentialFormSchema]
@@ -90,6 +95,7 @@ class SimpleProviderEntity(BaseModel):
"""
Simple model class for provider.
"""
provider: str
label: I18nObject
icon_small: Optional[I18nObject] = None
@@ -102,6 +108,7 @@ class ProviderHelpEntity(BaseModel):
"""
Model class for provider help.
"""
title: I18nObject
url: I18nObject
@@ -110,6 +117,7 @@ class ProviderEntity(BaseModel):
"""
Model class for provider.
"""
provider: str
label: I18nObject
description: Optional[I18nObject] = None
@@ -138,7 +146,7 @@ class ProviderEntity(BaseModel):
icon_small=self.icon_small,
icon_large=self.icon_large,
supported_model_types=self.supported_model_types,
models=self.models
models=self.models,
)
@@ -146,5 +154,6 @@ class ProviderConfig(BaseModel):
"""
Model class for provider config.
"""
provider: str
credentials: dict

View File

@@ -5,6 +5,7 @@ class RerankDocument(BaseModel):
"""
Model class for rerank document.
"""
index: int
text: str
score: float
@@ -14,5 +15,6 @@ class RerankResult(BaseModel):
"""
Model class for rerank result.
"""
model: str
docs: list[RerankDocument]

View File

@@ -9,6 +9,7 @@ class EmbeddingUsage(ModelUsage):
"""
Model class for embedding usage.
"""
tokens: int
total_tokens: int
unit_price: Decimal
@@ -22,7 +23,7 @@ class TextEmbeddingResult(BaseModel):
"""
Model class for text embedding result.
"""
model: str
embeddings: list[list[float]]
usage: EmbeddingUsage