test_rerank.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import os
  2. import pytest
  3. from core.model_runtime.entities.rerank_entities import RerankDocument, RerankResult
  4. from core.model_runtime.errors.validate import CredentialsValidateFailedError
  5. from core.model_runtime.model_providers.localai.rerank.rerank import LocalaiRerankModel
  6. def test_validate_credentials_for_chat_model():
  7. model = LocalaiRerankModel()
  8. with pytest.raises(CredentialsValidateFailedError):
  9. model.validate_credentials(
  10. model='bge-reranker-v2-m3',
  11. credentials={
  12. 'server_url': 'hahahaha',
  13. 'completion_type': 'completion',
  14. }
  15. )
  16. model.validate_credentials(
  17. model='bge-reranker-base',
  18. credentials={
  19. 'server_url': os.environ.get('LOCALAI_SERVER_URL'),
  20. 'completion_type': 'completion',
  21. }
  22. )
  23. def test_invoke_rerank_model():
  24. model = LocalaiRerankModel()
  25. response = model.invoke(
  26. model='bge-reranker-base',
  27. credentials={
  28. 'server_url': os.environ.get('LOCALAI_SERVER_URL')
  29. },
  30. query='Organic skincare products for sensitive skin',
  31. docs=[
  32. "Eco-friendly kitchenware for modern homes",
  33. "Biodegradable cleaning supplies for eco-conscious consumers",
  34. "Organic cotton baby clothes for sensitive skin",
  35. "Natural organic skincare range for sensitive skin",
  36. "Tech gadgets for smart homes: 2024 edition",
  37. "Sustainable gardening tools and compost solutions",
  38. "Sensitive skin-friendly facial cleansers and toners",
  39. "Organic food wraps and storage solutions",
  40. "Yoga mats made from recycled materials"
  41. ],
  42. top_n=3,
  43. score_threshold=0.75,
  44. user="abc-123"
  45. )
  46. assert isinstance(response, RerankResult)
  47. assert len(response.docs) == 3
  48. def test__invoke():
  49. model = LocalaiRerankModel()
  50. # Test case 1: Empty docs
  51. result = model._invoke(
  52. model='bge-reranker-base',
  53. credentials={
  54. 'server_url': 'https://example.com',
  55. 'api_key': '1234567890'
  56. },
  57. query='Organic skincare products for sensitive skin',
  58. docs=[],
  59. top_n=3,
  60. score_threshold=0.75,
  61. user="abc-123"
  62. )
  63. assert isinstance(result, RerankResult)
  64. assert len(result.docs) == 0
  65. # Test case 2: Valid invocation
  66. result = model._invoke(
  67. model='bge-reranker-base',
  68. credentials={
  69. 'server_url': 'https://example.com',
  70. 'api_key': '1234567890'
  71. },
  72. query='Organic skincare products for sensitive skin',
  73. docs=[
  74. "Eco-friendly kitchenware for modern homes",
  75. "Biodegradable cleaning supplies for eco-conscious consumers",
  76. "Organic cotton baby clothes for sensitive skin",
  77. "Natural organic skincare range for sensitive skin",
  78. "Tech gadgets for smart homes: 2024 edition",
  79. "Sustainable gardening tools and compost solutions",
  80. "Sensitive skin-friendly facial cleansers and toners",
  81. "Organic food wraps and storage solutions",
  82. "Yoga mats made from recycled materials"
  83. ],
  84. top_n=3,
  85. score_threshold=0.75,
  86. user="abc-123"
  87. )
  88. assert isinstance(result, RerankResult)
  89. assert len(result.docs) == 3
  90. assert all(isinstance(doc, RerankDocument) for doc in result.docs)