test_rerank.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.gitee_ai.rerank.rerank import GiteeAIRerankModel
  6. def test_validate_credentials():
  7. model = GiteeAIRerankModel()
  8. with pytest.raises(CredentialsValidateFailedError):
  9. model.validate_credentials(
  10. model="bge-reranker-v2-m3",
  11. credentials={"api_key": "invalid_key"},
  12. )
  13. model.validate_credentials(
  14. model="bge-reranker-v2-m3",
  15. credentials={
  16. "api_key": os.environ.get("GITEE_AI_API_KEY"),
  17. },
  18. )
  19. def test_invoke_model():
  20. model = GiteeAIRerankModel()
  21. result = model.invoke(
  22. model="bge-reranker-v2-m3",
  23. credentials={
  24. "api_key": os.environ.get("GITEE_AI_API_KEY"),
  25. },
  26. query="What is the capital of the United States?",
  27. docs=[
  28. "Carson City is the capital city of the American state of Nevada. At the 2010 United States "
  29. "Census, Carson City had a population of 55,274.",
  30. "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that "
  31. "are a political division controlled by the United States. Its capital is Saipan.",
  32. ],
  33. top_n=1,
  34. score_threshold=0.01,
  35. )
  36. assert isinstance(result, RerankResult)
  37. assert len(result.docs) == 1
  38. assert result.docs[0].score >= 0.01