test_text_embedding.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.oci.text_embedding.text_embedding import OCITextEmbeddingModel
  6. def test_validate_credentials():
  7. model = OCITextEmbeddingModel()
  8. with pytest.raises(CredentialsValidateFailedError):
  9. model.validate_credentials(
  10. model="cohere.embed-multilingual-v3.0",
  11. credentials={"oci_config_content": "invalid_key", "oci_key_content": "invalid_key"},
  12. )
  13. model.validate_credentials(
  14. model="cohere.embed-multilingual-v3.0",
  15. credentials={
  16. "oci_config_content": os.environ.get("OCI_CONFIG_CONTENT"),
  17. "oci_key_content": os.environ.get("OCI_KEY_CONTENT"),
  18. },
  19. )
  20. def test_invoke_model():
  21. model = OCITextEmbeddingModel()
  22. result = model.invoke(
  23. model="cohere.embed-multilingual-v3.0",
  24. credentials={
  25. "oci_config_content": os.environ.get("OCI_CONFIG_CONTENT"),
  26. "oci_key_content": os.environ.get("OCI_KEY_CONTENT"),
  27. },
  28. texts=["hello", "world", " ".join(["long_text"] * 100), " ".join(["another_long_text"] * 100)],
  29. user="abc-123",
  30. )
  31. assert isinstance(result, TextEmbeddingResult)
  32. assert len(result.embeddings) == 4
  33. # assert result.usage.total_tokens == 811
  34. def test_get_num_tokens():
  35. model = OCITextEmbeddingModel()
  36. num_tokens = model.get_num_tokens(
  37. model="cohere.embed-multilingual-v3.0",
  38. credentials={
  39. "oci_config_content": os.environ.get("OCI_CONFIG_CONTENT"),
  40. "oci_key_content": os.environ.get("OCI_KEY_CONTENT"),
  41. },
  42. texts=["hello", "world"],
  43. )
  44. assert num_tokens == 2