test_rerank.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import pytest
  3. from core.model_runtime.entities.rerank_entities import RerankResult
  4. from core.model_runtime.errors.validate import CredentialsValidateFailedError
  5. from core.model_runtime.model_providers.sagemaker.rerank.rerank import SageMakerRerankModel
  6. def test_validate_credentials():
  7. model = SageMakerRerankModel()
  8. with pytest.raises(CredentialsValidateFailedError):
  9. model.validate_credentials(
  10. model="bge-m3-rerank-v2",
  11. credentials={
  12. "aws_region": os.getenv("AWS_REGION"),
  13. "aws_access_key": os.getenv("AWS_ACCESS_KEY"),
  14. "aws_secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
  15. },
  16. query="What is the capital of the United States?",
  17. docs=[
  18. "Carson City is the capital city of the American state of Nevada. At the 2010 United States "
  19. "Census, Carson City had a population of 55,274.",
  20. "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that "
  21. "are a political division controlled by the United States. Its capital is Saipan.",
  22. ],
  23. score_threshold=0.8,
  24. )
  25. def test_invoke_model():
  26. model = SageMakerRerankModel()
  27. result = model.invoke(
  28. model="bge-m3-rerank-v2",
  29. credentials={
  30. "aws_region": os.getenv("AWS_REGION"),
  31. "aws_access_key": os.getenv("AWS_ACCESS_KEY"),
  32. "aws_secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
  33. },
  34. query="What is the capital of the United States?",
  35. docs=[
  36. "Carson City is the capital city of the American state of Nevada. At the 2010 United States "
  37. "Census, Carson City had a population of 55,274.",
  38. "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that "
  39. "are a political division controlled by the United States. Its capital is Saipan.",
  40. ],
  41. score_threshold=0.8,
  42. )
  43. assert isinstance(result, RerankResult)
  44. assert len(result.docs) == 1
  45. assert result.docs[0].index == 1
  46. assert result.docs[0].score >= 0.8