test_text_embedding.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import os
  2. import pytest
  3. from core.model_runtime.entities.text_embedding_entities import TextEmbeddingResult
  4. from core.model_runtime.errors.validate import CredentialsValidateFailedError
  5. from core.model_runtime.model_providers.hunyuan.text_embedding.text_embedding import HunyuanTextEmbeddingModel
  6. def test_validate_credentials():
  7. model = HunyuanTextEmbeddingModel()
  8. with pytest.raises(CredentialsValidateFailedError):
  9. model.validate_credentials(
  10. model="hunyuan-embedding", credentials={"secret_id": "invalid_key", "secret_key": "invalid_key"}
  11. )
  12. model.validate_credentials(
  13. model="hunyuan-embedding",
  14. credentials={
  15. "secret_id": os.environ.get("HUNYUAN_SECRET_ID"),
  16. "secret_key": os.environ.get("HUNYUAN_SECRET_KEY"),
  17. },
  18. )
  19. def test_invoke_model():
  20. model = HunyuanTextEmbeddingModel()
  21. result = model.invoke(
  22. model="hunyuan-embedding",
  23. credentials={
  24. "secret_id": os.environ.get("HUNYUAN_SECRET_ID"),
  25. "secret_key": os.environ.get("HUNYUAN_SECRET_KEY"),
  26. },
  27. texts=["hello", "world"],
  28. user="abc-123",
  29. )
  30. assert isinstance(result, TextEmbeddingResult)
  31. assert len(result.embeddings) == 2
  32. assert result.usage.total_tokens == 6
  33. def test_get_num_tokens():
  34. model = HunyuanTextEmbeddingModel()
  35. num_tokens = model.get_num_tokens(
  36. model="hunyuan-embedding",
  37. credentials={
  38. "secret_id": os.environ.get("HUNYUAN_SECRET_ID"),
  39. "secret_key": os.environ.get("HUNYUAN_SECRET_KEY"),
  40. },
  41. texts=["hello", "world"],
  42. )
  43. assert num_tokens == 2
  44. def test_max_chunks():
  45. model = HunyuanTextEmbeddingModel()
  46. result = model.invoke(
  47. model="hunyuan-embedding",
  48. credentials={
  49. "secret_id": os.environ.get("HUNYUAN_SECRET_ID"),
  50. "secret_key": os.environ.get("HUNYUAN_SECRET_KEY"),
  51. },
  52. texts=[
  53. "hello",
  54. "world",
  55. "hello",
  56. "world",
  57. "hello",
  58. "world",
  59. "hello",
  60. "world",
  61. "hello",
  62. "world",
  63. "hello",
  64. "world",
  65. "hello",
  66. "world",
  67. "hello",
  68. "world",
  69. "hello",
  70. "world",
  71. "hello",
  72. "world",
  73. "hello",
  74. "world",
  75. ],
  76. )
  77. assert isinstance(result, TextEmbeddingResult)
  78. assert len(result.embeddings) == 22