Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat: support jina-clip-v1 embedding model (#5146)

sino8210637bc56c
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 00000000000..c06bfd7ebeb
--- /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 74a1aabf7a6..23203491e65 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'))
}
)