test_text_embedding.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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',
  11. credentials={
  12. 'secret_id': 'invalid_key',
  13. 'secret_key': 'invalid_key'
  14. }
  15. )
  16. model.validate_credentials(
  17. model='hunyuan-embedding',
  18. credentials={
  19. 'secret_id': os.environ.get('HUNYUAN_SECRET_ID'),
  20. 'secret_key': os.environ.get('HUNYUAN_SECRET_KEY')
  21. }
  22. )
  23. def test_invoke_model():
  24. model = HunyuanTextEmbeddingModel()
  25. result = model.invoke(
  26. model='hunyuan-embedding',
  27. credentials={
  28. 'secret_id': os.environ.get('HUNYUAN_SECRET_ID'),
  29. 'secret_key': os.environ.get('HUNYUAN_SECRET_KEY')
  30. },
  31. texts=[
  32. "hello",
  33. "world"
  34. ],
  35. user="abc-123"
  36. )
  37. assert isinstance(result, TextEmbeddingResult)
  38. assert len(result.embeddings) == 2
  39. assert result.usage.total_tokens == 6
  40. def test_get_num_tokens():
  41. model = HunyuanTextEmbeddingModel()
  42. num_tokens = model.get_num_tokens(
  43. model='hunyuan-embedding',
  44. credentials={
  45. 'secret_id': os.environ.get('HUNYUAN_SECRET_ID'),
  46. 'secret_key': os.environ.get('HUNYUAN_SECRET_KEY')
  47. },
  48. texts=[
  49. "hello",
  50. "world"
  51. ]
  52. )
  53. assert num_tokens == 2
  54. def test_max_chunks():
  55. model = HunyuanTextEmbeddingModel()
  56. result = model.invoke(
  57. model='hunyuan-embedding',
  58. credentials={
  59. 'secret_id': os.environ.get('HUNYUAN_SECRET_ID'),
  60. 'secret_key': os.environ.get('HUNYUAN_SECRET_KEY')
  61. },
  62. texts=[
  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. "hello",
  76. "world",
  77. "hello",
  78. "world",
  79. "hello",
  80. "world",
  81. "hello",
  82. "world",
  83. "hello",
  84. "world",
  85. ]
  86. )
  87. assert isinstance(result, TextEmbeddingResult)
  88. assert len(result.embeddings) == 22