text-embedding.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from typing import Optional
  2. import numpy as np
  3. from dify_plugin.entities.model import EmbeddingInputType
  4. from dify_plugin.errors.model import CredentialsValidateFailedError
  5. from dify_plugin.entities.model.text_embedding import (
  6. TextEmbeddingResult,
  7. )
  8. class {{ .PluginName | SnakeToCamel }}TextEmbeddingModel(TextEmbeddingModel):
  9. """
  10. Model class for {{ .PluginName }} text embedding model.
  11. """
  12. def _invoke(
  13. self,
  14. model: str,
  15. credentials: dict,
  16. texts: list[str],
  17. user: Optional[str] = None,
  18. input_type: EmbeddingInputType = EmbeddingInputType.DOCUMENT,
  19. ) -> TextEmbeddingResult:
  20. """
  21. Invoke text embedding model
  22. :param model: model name
  23. :param credentials: model credentials
  24. :param texts: texts to embed
  25. :param user: unique user id
  26. :return: embeddings result
  27. """
  28. pass
  29. def get_num_tokens(self, model: str, credentials: dict, texts: list[str]) -> int:
  30. """
  31. Get number of tokens for given prompt messages
  32. :param model: model name
  33. :param credentials: model credentials
  34. :param texts: texts to embed
  35. :return:
  36. """
  37. return 0
  38. def validate_credentials(self, model: str, credentials: dict) -> None:
  39. """
  40. Validate model credentials
  41. :param model: model name
  42. :param credentials: model credentials
  43. :return:
  44. """
  45. try:
  46. pass
  47. except Exception as ex:
  48. raise CredentialsValidateFailedError(str(ex))