Skip to content

Commit

Permalink
Merge branch 'feat/model-runtime' into deploy/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
GarfieldDai committed Dec 28, 2023
2 parents 69c9bcc + 299ea51 commit aa543ea
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
from core.model_runtime.entities.llm_entities import LLMMode, LLMResult, \
LLMResultChunk, LLMResultChunkDelta
from core.model_runtime.entities.message_entities import PromptMessageTool, PromptMessage, AssistantPromptMessage, \
PromptMessageFunction, UserPromptMessage, PromptMessageContentType, ImagePromptMessageContent, \
UserPromptMessage, PromptMessageContentType, ImagePromptMessageContent, \
TextPromptMessageContent, SystemPromptMessage, ToolPromptMessage
from core.model_runtime.entities.model_entities import AIModelEntity, ModelPropertyKey
from core.model_runtime.errors.validate import CredentialsValidateFailedError
from core.model_runtime.model_providers.__base.large_language_model import LargeLanguageModel
from core.model_runtime.model_providers.azure_openai._common import _CommonAzureOpenAI
from core.model_runtime.model_providers.azure_openai._constant import LLM_BASE_MODELS, AzureBaseModel
from core.model_runtime.utils import helper

logger = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
provider: huggingface_hub
label:
en_US: HuggingFace Hub
en_US: Hugging Face Model
icon_small:
en_US: icon_s_en.svg
icon_large:
Expand All @@ -25,7 +25,8 @@ model_credential_schema:
credential_form_schemas:
- variable: huggingfacehub_api_type
label:
en_US: HuggingFace Hub API Type
en_US: Endpoint Type
zh_Hans: 端点类型
type: radio
required: true
default: hosted_inference_api
Expand All @@ -38,36 +39,43 @@ model_credential_schema:
en_US: Inference Endpoints
- variable: huggingfacehub_api_token
label:
en_US: HuggingFace Hub Access Token
en_US: API Token
zh_Hans: API Token
type: secret-input
required: true
placeholder:
en_US: Enter your HuggingFace Hub Access Token
en_US: Enter your Hugging Face Hub API Token here
zh_Hans: 在此输入您的 Hugging Face Hub API Token
- variable: huggingface_namespace
label:
en_US: HuggingFace Namespace
en_US: 'User Name / Organization Name'
zh_Hans: '用户名 / 组织名称'
type: text-input
required: true
placeholder:
en_US: Enter your HuggingFace Namespace
en_US: 'Enter your User Name / Organization Name here'
zh_Hans: '在此输入您的用户名 / 组织名称'
show_on:
- variable: __model_type
value: text-embedding
- variable: huggingfacehub_api_type
value: inference_endpoints
- variable: huggingfacehub_endpoint_url
label:
en_US: HuggingFace Hub Endpoint URL
en_US: Endpoint URL
zh_Hans: 端点 URL
type: text-input
required: true
placeholder:
en_US: Enter your HuggingFace Hub Endpoint URL
en_US: Enter your Endpoint URL here
zh_Hans: 在此输入您的端点 URL
show_on:
- variable: huggingfacehub_api_type
value: inference_endpoints
- variable: task_type
label:
en_US: Task Type
en_US: Task
zh_Hans: Task
type: select
options:
- value: text2text-generation
Expand All @@ -79,6 +87,7 @@ model_credential_schema:
- value: text-generation
label:
en_US: Text Generation
zh_Hans: 文本生成
show_on:
- variable: __model_type
value: llm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@ def _get_customizable_model_parameter_rules() -> list[ParameterRule]:
DefaultParameterName.TEMPERATURE).copy()
temperature_rule_dict['name'] = 'temperature'
temperature_rule = ParameterRule(**temperature_rule_dict)
temperature_rule.default = 0.5

top_p_rule_dict = PARAMETER_RULE_TEMPLATE.get(DefaultParameterName.TOP_P).copy()
top_p_rule_dict['name'] = 'top_p'
top_p_rule = ParameterRule(**top_p_rule_dict)
top_p_rule.default = 0.5

top_k_rule = ParameterRule(
name='top_k',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from huggingface_hub import InferenceClient, HfApi
from huggingface_hub.utils import HfHubHTTPError

from core.model_runtime.entities.common_entities import I18nObject
from core.model_runtime.entities.model_entities import AIModelEntity, FetchFrom, ModelType
from core.model_runtime.entities.text_embedding_entities import TextEmbeddingResult, EmbeddingUsage
from core.model_runtime.errors.invoke import InvokeError, InvokeBadRequestError
from core.model_runtime.errors.validate import CredentialsValidateFailedError
Expand Down Expand Up @@ -62,38 +64,53 @@ def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int
return 0

def validate_credentials(self, model: str, credentials: dict) -> None:
if 'huggingfacehub_api_type' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Endpoint Type must be provided.')
try:
if 'huggingfacehub_api_type' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Endpoint Type must be provided.')

if 'huggingfacehub_api_token' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Access Token must be provided.')
if 'huggingfacehub_api_token' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub API Token must be provided.')

if credentials['huggingfacehub_api_type'] == 'inference_endpoints':
if 'huggingface_namespace' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Namespace must be provided.')
if credentials['huggingfacehub_api_type'] == 'inference_endpoints':
if 'huggingface_namespace' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub User Name / Organization Name must be provided.')

if 'huggingfacehub_endpoint_url' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Endpoint URL must be provided.')
if 'huggingfacehub_endpoint_url' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Endpoint URL must be provided.')

if 'task_type' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Task Type must be provided.')
if 'task_type' not in credentials:
raise CredentialsValidateFailedError('Huggingface Hub Task Type must be provided.')

if credentials['task_type'] != 'feature-extraction':
raise CredentialsValidateFailedError('Huggingface Hub Task Type is invalid.')
if credentials['task_type'] != 'feature-extraction':
raise CredentialsValidateFailedError('Huggingface Hub Task Type is invalid.')

model = credentials['huggingfacehub_endpoint_url']
elif credentials['huggingfacehub_api_type'] == 'hosted_inference_api':
self._check_hosted_model_task_type(credentials['huggingfacehub_api_token'],
model)
else:
raise CredentialsValidateFailedError('Huggingface Hub Endpoint Type is invalid.')
model = credentials['huggingfacehub_endpoint_url']
elif credentials['huggingfacehub_api_type'] == 'hosted_inference_api':
self._check_hosted_model_task_type(credentials['huggingfacehub_api_token'],
model)
else:
raise CredentialsValidateFailedError('Huggingface Hub Endpoint Type is invalid.')

client = InferenceClient(token=credentials['huggingfacehub_api_token'])
try:
client = InferenceClient(token=credentials['huggingfacehub_api_token'])
client.feature_extraction(text='hello world', model=model)
except Exception as ex:
raise CredentialsValidateFailedError(str(ex))

def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
entity = AIModelEntity(
model=model,
label=I18nObject(
en_US=model
),
fetch_from=FetchFrom.CUSTOMIZABLE_MODEL,
model_type=ModelType.TEXT_EMBEDDING,
model_properties={
'context_size': 10000,
'max_chunks': 1
}
)
return entity

@property
def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]:
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from replicate import Client as ReplicateClient
from replicate.exceptions import ReplicateError, ModelError

from core.model_runtime.entities.common_entities import I18nObject
from core.model_runtime.entities.model_entities import AIModelEntity, FetchFrom, ModelType
from core.model_runtime.entities.text_embedding_entities import TextEmbeddingResult, EmbeddingUsage
from core.model_runtime.errors.invoke import InvokeError, InvokeBadRequestError
from core.model_runtime.errors.validate import CredentialsValidateFailedError
Expand Down Expand Up @@ -66,6 +68,21 @@ def validate_credentials(self, model: str, credentials: dict) -> None:
except Exception as e:
raise CredentialsValidateFailedError(str(e))

def get_customizable_model_schema(self, model: str, credentials: dict) -> Optional[AIModelEntity]:
entity = AIModelEntity(
model=model,
label=I18nObject(
en_US=model
),
fetch_from=FetchFrom.CUSTOMIZABLE_MODEL,
model_type=ModelType.TEXT_EMBEDDING,
model_properties={
'context_size': 4096,
'max_chunks': 1
}
)
return entity

@property
def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]:
return {
Expand Down

0 comments on commit aa543ea

Please sign in to comment.