feat: extend api params for Jina Embeddings V3 (#8657)
Some checks are pending
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions

This commit is contained in:
Aaron Ji 2024-09-23 13:45:09 +08:00 committed by GitHub
parent 03fdf5e7f8
commit 3618a97c20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 76 additions and 9 deletions

View File

@ -67,3 +67,46 @@ model_credential_schema:
required: false
type: text-input
default: '8192'
- variable: task
label:
zh_Hans: 下游任务
en_US: Downstream task
placeholder:
zh_Hans: 选择将使用向量模型的下游任务。模型将返回针对该任务优化的向量。
en_US: Select the downstream task for which the embeddings will be used. The model will return the optimized embeddings for that task.
required: false
type: select
options:
- value: retrieval.query
label:
en_US: retrieval.query
- value: retrieval.passage
label:
en_US: retrieval.passage
- value: separation
label:
en_US: separation
- value: classification
label:
en_US: classification
- value: text-matching
label:
en_US: text-matching
- variable: dimensions
label:
zh_Hans: 输出维度
en_US: Output dimensions
placeholder:
zh_Hans: 输入您的输出维度
en_US: Enter output dimensions
required: false
type: text-input
- variable: late_chunking
label:
zh_Hans: 后期分块
en_US: Late chunking
placeholder:
zh_Hans: 应用后期分块技术来利用模型的长上下文功能来生成上下文块向量化。
en_US: Apply the late chunking technique to leverage the model's long-context capabilities for generating contextual chunk embeddings.
required: false
type: switch

View File

@ -27,6 +27,38 @@ class JinaTextEmbeddingModel(TextEmbeddingModel):
api_base: str = "https://api.jina.ai/v1"
def _to_payload(self, model: str, texts: list[str], credentials: dict) -> dict:
"""
Parse model credentials
:param model: model name
:param credentials: model credentials
:param texts: texts to embed
:return: parsed credentials
"""
def transform_jina_input_text(model, text):
if model == "jina-clip-v1":
return {"text": text}
return text
data = {"model": model, "input": [transform_jina_input_text(model, text) for text in texts]}
task = credentials.get("task")
dimensions = credentials.get("dimensions")
late_chunking = credentials.get("late_chunking")
if task is not None:
data["task"] = task
if dimensions is not None:
data["dimensions"] = int(dimensions)
if late_chunking is not None:
data["late_chunking"] = late_chunking
return data
def _invoke(
self, model: str, credentials: dict, texts: list[str], user: Optional[str] = None
) -> TextEmbeddingResult:
@ -49,15 +81,7 @@ class JinaTextEmbeddingModel(TextEmbeddingModel):
url = base_url + "/embeddings"
headers = {"Authorization": "Bearer " + api_key, "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": [transform_jina_input_text(model, text) for text in texts]}
if model == "jina-embeddings-v3":
data["task"] = "text-matching"
data = self._to_payload(model=model, texts=texts, credentials=credentials)
try:
response = post(url, headers=headers, data=dumps(data))