From a48dfd163c2eadad86ecc9380000e1024c26e493 Mon Sep 17 00:00:00 2001 From: takatost Date: Tue, 2 Jan 2024 14:53:01 +0800 Subject: [PATCH 1/3] fix gemini pro vision bug --- api/controllers/console/app/completion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/controllers/console/app/completion.py b/api/controllers/console/app/completion.py index d01a785aa27cd9..b1deb100da03ba 100644 --- a/api/controllers/console/app/completion.py +++ b/api/controllers/console/app/completion.py @@ -183,7 +183,7 @@ def generate() -> Generator: except ModelCurrentlyNotSupportError: yield "data: " + json.dumps(api.handle_error(ProviderModelCurrentlyNotSupportError()).get_json()) + "\n\n" except InvokeError as e: - yield "data: " + json.dumps(api.handle_error(CompletionRequestError(str(e))).get_json()) + "\n\n" + yield "data: " + json.dumps(api.handle_error(CompletionRequestError(e.description)).get_json()) + "\n\n" except ValueError as e: yield "data: " + json.dumps(api.handle_error(e).get_json()) + "\n\n" except Exception: From f774f5d25fbdc0576d0e231466877794d5287361 Mon Sep 17 00:00:00 2001 From: takatost Date: Tue, 2 Jan 2024 15:07:55 +0800 Subject: [PATCH 2/3] fix bug --- api/core/entities/provider_configuration.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/core/entities/provider_configuration.py b/api/core/entities/provider_configuration.py index f43db7992485ea..2228560a87ff97 100644 --- a/api/core/entities/provider_configuration.py +++ b/api/core/entities/provider_configuration.py @@ -249,11 +249,12 @@ def custom_model_credentials_validate(self, model_type: ModelType, model: str, c except JSONDecodeError: original_credentials = {} - # encrypt credentials + # decrypt credentials for key, value in credentials.items(): if key in provider_credential_secret_variables: # if send [__HIDDEN__] in secret input, it will be same as original value - credentials[key] = encrypter.decrypt_token(self.tenant_id, original_credentials[key]) + if value == '[__HIDDEN__]' and key in original_credentials: + credentials[key] = encrypter.decrypt_token(self.tenant_id, original_credentials[key]) model_provider_factory.model_credentials_validate( provider=self.provider.provider, From 0a085532b7abe05923026c510758820fd3cfa783 Mon Sep 17 00:00:00 2001 From: takatost Date: Tue, 2 Jan 2024 15:22:26 +0800 Subject: [PATCH 3/3] fix openai ft models --- api/core/indexing_runner.py | 15 ++++---- .../model_providers/openai/llm/llm.py | 5 ++- ...442fc_modify_provider_model_name_length.py | 38 +++++++++++++++++++ api/models/provider.py | 2 +- 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 api/migrations/versions/187385f442fc_modify_provider_model_name_length.py diff --git a/api/core/indexing_runner.py b/api/core/indexing_runner.py index e86a0e27dc88a6..d7e0843c8460c2 100644 --- a/api/core/indexing_runner.py +++ b/api/core/indexing_runner.py @@ -297,13 +297,14 @@ def file_indexing_estimate(self, tenant_id: str, file_details: List[UploadFile], "qa_preview": document_qa_list, "preview": preview_texts } - embedding_model_type_instance = cast(TextEmbeddingModel, embedding_model_instance.model_type_instance) - embedding_price_info = embedding_model_type_instance.get_price( - model=embedding_model_instance.model, - credentials=embedding_model_instance.credentials, - price_type=PriceType.INPUT, - tokens=tokens - ) + if embedding_model_instance: + embedding_model_type_instance = cast(TextEmbeddingModel, embedding_model_instance.model_type_instance) + embedding_price_info = embedding_model_type_instance.get_price( + model=embedding_model_instance.model, + credentials=embedding_model_instance.credentials, + price_type=PriceType.INPUT, + tokens=tokens + ) return { "total_segments": total_segments, "tokens": tokens, diff --git a/api/core/model_runtime/model_providers/openai/llm/llm.py b/api/core/model_runtime/model_providers/openai/llm/llm.py index 452fc351c0a35f..17e5910b927551 100644 --- a/api/core/model_runtime/model_providers/openai/llm/llm.py +++ b/api/core/model_runtime/model_providers/openai/llm/llm.py @@ -125,7 +125,7 @@ def validate_credentials(self, model: str, credentials: dict) -> None: base_model = model.split(':')[1] # check if model exists - remote_models = self.remote_models() + remote_models = self.remote_models(credentials) remote_model_map = {model.model: model for model in remote_models} if model not in remote_model_map: raise CredentialsValidateFailedError(f'Fine-tuned model {model} not found') @@ -687,6 +687,9 @@ def _num_tokens_from_messages(self, model: str, messages: List[PromptMessage], Official documentation: https://github.com/openai/openai-cookbook/blob/ main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb""" + if model.startswith('ft:'): + model = model.split(':')[1] + try: encoding = tiktoken.encoding_for_model(model) except KeyError: diff --git a/api/migrations/versions/187385f442fc_modify_provider_model_name_length.py b/api/migrations/versions/187385f442fc_modify_provider_model_name_length.py new file mode 100644 index 00000000000000..ac10564299a23d --- /dev/null +++ b/api/migrations/versions/187385f442fc_modify_provider_model_name_length.py @@ -0,0 +1,38 @@ +"""modify provider model name length + +Revision ID: 187385f442fc +Revises: 88072f0caa04 +Create Date: 2024-01-02 07:18:43.887428 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '187385f442fc' +down_revision = '88072f0caa04' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('provider_models', schema=None) as batch_op: + batch_op.alter_column('model_name', + existing_type=sa.VARCHAR(length=40), + type_=sa.String(length=255), + existing_nullable=False) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('provider_models', schema=None) as batch_op: + batch_op.alter_column('model_name', + existing_type=sa.String(length=255), + type_=sa.VARCHAR(length=40), + existing_nullable=False) + + # ### end Alembic commands ### diff --git a/api/models/provider.py b/api/models/provider.py index 1ce8e35614ea48..4c9fd793cc828c 100644 --- a/api/models/provider.py +++ b/api/models/provider.py @@ -96,7 +96,7 @@ class ProviderModel(db.Model): id = db.Column(UUID, server_default=db.text('uuid_generate_v4()')) tenant_id = db.Column(UUID, nullable=False) provider_name = db.Column(db.String(40), nullable=False) - model_name = db.Column(db.String(40), nullable=False) + model_name = db.Column(db.String(255), nullable=False) model_type = db.Column(db.String(40), nullable=False) encrypted_config = db.Column(db.Text, nullable=True) is_valid = db.Column(db.Boolean, nullable=False, server_default=db.text('false'))