Skip to content

Commit

Permalink
chore: format get_customizable_model_schema return value (langgenius#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoice authored Oct 21, 2024
1 parent 79fe175 commit 1e829ce
Show file tree
Hide file tree
Showing 28 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ For instance, Xinference supports `max_tokens`, `temperature`, and `top_p` param
However, some vendors may support different parameters for different models. For example, the `OpenLLM` vendor supports `top_k`, but not all models provided by this vendor support `top_k`. Let's say model A supports `top_k` but model B does not. In such cases, we need to dynamically generate the model parameter schema, as illustrated below:

```python
def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ provider_credential_schema:
但是有的供应商根据不同的模型支持不同的参数,如供应商`OpenLLM`支持`top_k`,但是并不是这个供应商提供的所有模型都支持`top_k`,我们这里举例A模型支持`top_k`,B模型不支持`top_k`,那么我们需要在这里动态生成模型参数的Schema,如下所示:

```python
def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
Used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError, json.JSONDecodeError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _calc_response_usage(self, model: str, credentials: dict, tokens: int) -> Em

return usage

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
4 changes: 2 additions & 2 deletions api/core/model_runtime/model_providers/localai/llm/llm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Generator
from typing import cast
from typing import Optional, cast

from httpx import Timeout
from openai import (
Expand Down Expand Up @@ -212,7 +212,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
except Exception as ex:
raise CredentialsValidateFailedError(f"Invalid credentials {str(ex)}")

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
completion_model = None
if credentials["completion_type"] == "chat_completion":
completion_model = LLMMode.CHAT.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [InvokeBadRequestError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int
num_tokens += self._get_num_tokens_by_gpt2(text)
return num_tokens

def _get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def _get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
Get customizable model schema
Expand Down
2 changes: 1 addition & 1 deletion api/core/model_runtime/model_providers/moonshot/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
self._add_custom_parameters(credentials)
super().validate_credentials(model, credentials)

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
return AIModelEntity(
model=model,
label=I18nObject(en_US=model, zh_Hans=model),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _speech2text_invoke(self, model: str, credentials: dict, file: IO[bytes]) ->

return response.text

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
except Exception as ex:
raise CredentialsValidateFailedError(str(ex))

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
3 changes: 2 additions & 1 deletion api/core/model_runtime/model_providers/openllm/llm/llm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Generator
from typing import Optional

from core.model_runtime.entities.common_entities import I18nObject
from core.model_runtime.entities.llm_entities import LLMMode, LLMResult, LLMResultChunk, LLMResultChunkDelta
Expand Down Expand Up @@ -193,7 +194,7 @@ def _handle_chat_generate_stream_response(
),
)

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [KeyError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _invoke(

return self._tts_invoke_streaming(model_type, payload, sagemaker_endpoint)

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _add_custom_parameters(cls, credentials: dict) -> None:
credentials["mode"] = "chat"
credentials["endpoint_url"] = "https://api.siliconflow.cn/v1"

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
return AIModelEntity(
model=model,
label=I18nObject(en_US=model, zh_Hans=model),
Expand Down
2 changes: 1 addition & 1 deletion api/core/model_runtime/model_providers/stepfun/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
self._add_custom_parameters(credentials)
super().validate_credentials(model, credentials)

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
return AIModelEntity(
model=model,
label=I18nObject(en_US=model, zh_Hans=model),
Expand Down
2 changes: 1 addition & 1 deletion api/core/model_runtime/model_providers/tongyi/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
Architecture for defining customizable models
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Generator
from typing import Optional

from httpx import Response, post
from yarl import URL
Expand Down Expand Up @@ -109,7 +110,7 @@ def _convert_prompt_message_to_text(self, message: list[PromptMessage]) -> str:
raise NotImplementedError(f"PromptMessage type {type(item)} is not supported")
return text

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from collections.abc import Generator
from typing import Optional

from volcenginesdkarkruntime.types.chat import ChatCompletion, ChatCompletionChunk

Expand Down Expand Up @@ -298,7 +299,7 @@ def _handle_chat_response(resp: ChatCompletion) -> LLMResult:
chunks = client.stream_chat(prompt_messages, **req_params)
return _handle_stream_chat_response(chunks)

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
4 changes: 2 additions & 2 deletions api/core/model_runtime/model_providers/xinference/llm/llm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Generator, Iterator
from typing import cast
from typing import Optional, cast

from openai import (
APIConnectionError,
Expand Down Expand Up @@ -321,7 +321,7 @@ def _convert_prompt_message_to_dict(self, message: PromptMessage) -> dict:

return message_dict

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]
InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError],
}

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _speech2text_invoke(

return response["text"]

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def _calc_response_usage(self, model: str, credentials: dict, tokens: int) -> Em

return usage

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _invoke(
"""
return self._tts_invoke_streaming(model, credentials, content_text, voice)

def get_customizable_model_schema(self, model: str, credentials: dict) -> AIModelEntity | None:
def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
"""
used to define customizable model schema
"""
Expand Down

0 comments on commit 1e829ce

Please sign in to comment.