Files
dify-plugin-daemon/cmd/commandline/plugin/templates/python/text-embedding.py
2024-11-13 20:38:47 +08:00

58 lines
1.5 KiB
Python

from typing import Optional
import numpy as np
from dify_plugin.entities.model import EmbeddingInputType
from dify_plugin.errors.model import CredentialsValidateFailedError
from dify_plugin.entities.model.text_embedding import (
TextEmbeddingResult,
)
class {{ .PluginName | SnakeToCamel }}TextEmbeddingModel(TextEmbeddingModel):
"""
Model class for {{ .PluginName }} text embedding model.
"""
def _invoke(
self,
model: str,
credentials: dict,
texts: list[str],
user: Optional[str] = None,
input_type: EmbeddingInputType = EmbeddingInputType.DOCUMENT,
) -> TextEmbeddingResult:
"""
Invoke text embedding model
:param model: model name
:param credentials: model credentials
:param texts: texts to embed
:param user: unique user id
:return: embeddings result
"""
pass
def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int:
"""
Get number of tokens for given prompt messages
:param model: model name
:param credentials: model credentials
:param texts: texts to embed
:return:
"""
return 0
def validate_credentials(self, model: str, credentials: dict) -> None:
"""
Validate model credentials
:param model: model name
:param credentials: model credentials
:return:
"""
try:
pass
except Exception as ex:
raise CredentialsValidateFailedError(str(ex))