From 8210637bc56cecf6c6e2d9172342eb038812203c Mon Sep 17 00:00:00 2001 From: sino Date: Thu, 13 Jun 2024 16:31:18 +0800 Subject: [PATCH] feat: support jina-clip-v1 embedding model (#5146) --- .../jina/text_embedding/jina-clip-v1.yaml | 9 +++++++ .../jina/text_embedding/text_embedding.py | 26 +++++++++++++------ 2 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 api/core/model_runtime/model_providers/jina/text_embedding/jina-clip-v1.yaml diff --git a/api/core/model_runtime/model_providers/jina/text_embedding/jina-clip-v1.yaml b/api/core/model_runtime/model_providers/jina/text_embedding/jina-clip-v1.yaml new file mode 100644 index 0000000000..c06bfd7ebe --- /dev/null +++ b/api/core/model_runtime/model_providers/jina/text_embedding/jina-clip-v1.yaml @@ -0,0 +1,9 @@ +model: jina-clip-v1 +model_type: text-embedding +model_properties: + context_size: 8192 + max_chunks: 2048 +pricing: + input: '0.001' + unit: '0.001' + currency: USD diff --git a/api/core/model_runtime/model_providers/jina/text_embedding/text_embedding.py b/api/core/model_runtime/model_providers/jina/text_embedding/text_embedding.py index 74a1aabf7a..23203491e6 100644 --- a/api/core/model_runtime/model_providers/jina/text_embedding/text_embedding.py +++ b/api/core/model_runtime/model_providers/jina/text_embedding/text_embedding.py @@ -52,16 +52,21 @@ class JinaTextEmbeddingModel(TextEmbeddingModel): 'Content-Type': 'application/json' } + def transform_jina_input_text(model, text): + if model == 'jina-clip-v1': + return {"text": text} + return text + data = { 'model': model, - 'input': texts + 'input': [transform_jina_input_text(model, text) for text in texts] } try: response = post(url, headers=headers, data=dumps(data)) except Exception as e: raise InvokeConnectionError(str(e)) - + if response.status_code != 200: try: resp = response.json() @@ -75,16 +80,19 @@ class JinaTextEmbeddingModel(TextEmbeddingModel): else: raise InvokeBadRequestError(msg) except JSONDecodeError as e: - raise InvokeServerUnavailableError(f"Failed to convert response to json: {e} with text: {response.text}") + raise InvokeServerUnavailableError( + f"Failed to convert response to json: {e} with text: {response.text}") try: resp = response.json() embeddings = resp['data'] usage = resp['usage'] except Exception as e: - raise InvokeServerUnavailableError(f"Failed to convert response to json: {e} with text: {response.text}") + raise InvokeServerUnavailableError( + f"Failed to convert response to json: {e} with text: {response.text}") - usage = self._calc_response_usage(model=model, credentials=credentials, tokens=usage['total_tokens']) + usage = self._calc_response_usage( + model=model, credentials=credentials, tokens=usage['total_tokens']) result = TextEmbeddingResult( model=model, @@ -122,7 +130,8 @@ class JinaTextEmbeddingModel(TextEmbeddingModel): try: self._invoke(model=model, credentials=credentials, texts=['ping']) except Exception as e: - raise CredentialsValidateFailedError(f'Credentials validation failed: {e}') + raise CredentialsValidateFailedError( + f'Credentials validation failed: {e}') @property def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]: @@ -144,7 +153,7 @@ class JinaTextEmbeddingModel(TextEmbeddingModel): InvokeBadRequestError ] } - + def _calc_response_usage(self, model: str, credentials: dict, tokens: int) -> EmbeddingUsage: """ Calculate response usage @@ -185,7 +194,8 @@ class JinaTextEmbeddingModel(TextEmbeddingModel): model_type=ModelType.TEXT_EMBEDDING, fetch_from=FetchFrom.CUSTOMIZABLE_MODEL, model_properties={ - ModelPropertyKey.CONTEXT_SIZE: int(credentials.get('context_size')) + ModelPropertyKey.CONTEXT_SIZE: int( + credentials.get('context_size')) } )