test_embedding.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.volcengine_maas.text_embedding.text_embedding import (
  6. VolcengineMaaSTextEmbeddingModel,
  7. )
  8. def test_validate_credentials():
  9. model = VolcengineMaaSTextEmbeddingModel()
  10. with pytest.raises(CredentialsValidateFailedError):
  11. model.validate_credentials(
  12. model='NOT IMPORTANT',
  13. credentials={
  14. 'api_endpoint_host': 'maas-api.ml-platform-cn-beijing.volces.com',
  15. 'volc_region': 'cn-beijing',
  16. 'volc_access_key_id': 'INVALID',
  17. 'volc_secret_access_key': 'INVALID',
  18. 'endpoint_id': 'INVALID',
  19. 'base_model_name': 'Doubao-embedding',
  20. }
  21. )
  22. model.validate_credentials(
  23. model='NOT IMPORTANT',
  24. credentials={
  25. 'api_endpoint_host': 'maas-api.ml-platform-cn-beijing.volces.com',
  26. 'volc_region': 'cn-beijing',
  27. 'volc_access_key_id': os.environ.get('VOLC_API_KEY'),
  28. 'volc_secret_access_key': os.environ.get('VOLC_SECRET_KEY'),
  29. 'endpoint_id': os.environ.get('VOLC_EMBEDDING_ENDPOINT_ID'),
  30. 'base_model_name': 'Doubao-embedding',
  31. },
  32. )
  33. def test_invoke_model():
  34. model = VolcengineMaaSTextEmbeddingModel()
  35. result = model.invoke(
  36. model='NOT IMPORTANT',
  37. credentials={
  38. 'api_endpoint_host': 'maas-api.ml-platform-cn-beijing.volces.com',
  39. 'volc_region': 'cn-beijing',
  40. 'volc_access_key_id': os.environ.get('VOLC_API_KEY'),
  41. 'volc_secret_access_key': os.environ.get('VOLC_SECRET_KEY'),
  42. 'endpoint_id': os.environ.get('VOLC_EMBEDDING_ENDPOINT_ID'),
  43. 'base_model_name': 'Doubao-embedding',
  44. },
  45. texts=[
  46. "hello",
  47. "world"
  48. ],
  49. user="abc-123"
  50. )
  51. assert isinstance(result, TextEmbeddingResult)
  52. assert len(result.embeddings) == 2
  53. assert result.usage.total_tokens > 0
  54. def test_get_num_tokens():
  55. model = VolcengineMaaSTextEmbeddingModel()
  56. num_tokens = model.get_num_tokens(
  57. model='NOT IMPORTANT',
  58. credentials={
  59. 'api_endpoint_host': 'maas-api.ml-platform-cn-beijing.volces.com',
  60. 'volc_region': 'cn-beijing',
  61. 'volc_access_key_id': os.environ.get('VOLC_API_KEY'),
  62. 'volc_secret_access_key': os.environ.get('VOLC_SECRET_KEY'),
  63. 'endpoint_id': os.environ.get('VOLC_EMBEDDING_ENDPOINT_ID'),
  64. 'base_model_name': 'Doubao-embedding',
  65. },
  66. texts=[
  67. "hello",
  68. "world"
  69. ]
  70. )
  71. assert num_tokens == 2