test_rerank.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import os
  2. import pytest
  3. from api.core.model_runtime.entities.rerank_entities import 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. import os
  49. import pytest
  50. from api.core.model_runtime.entities.rerank_entities import RerankDocument, RerankResult
  51. from core.model_runtime.errors.validate import CredentialsValidateFailedError
  52. from core.model_runtime.model_providers.localai.rerank.rerank import LocalaiRerankModel
  53. def test_validate_credentials_for_chat_model():
  54. model = LocalaiRerankModel()
  55. with pytest.raises(CredentialsValidateFailedError):
  56. model.validate_credentials(
  57. model='bge-reranker-v2-m3',
  58. credentials={
  59. 'server_url': 'hahahaha',
  60. 'completion_type': 'completion',
  61. }
  62. )
  63. model.validate_credentials(
  64. model='bge-reranker-base',
  65. credentials={
  66. 'server_url': os.environ.get('LOCALAI_SERVER_URL'),
  67. 'completion_type': 'completion',
  68. }
  69. )
  70. def test_invoke_rerank_model():
  71. model = LocalaiRerankModel()
  72. response = model.invoke(
  73. model='bge-reranker-base',
  74. credentials={
  75. 'server_url': os.environ.get('LOCALAI_SERVER_URL')
  76. },
  77. query='Organic skincare products for sensitive skin',
  78. docs=[
  79. "Eco-friendly kitchenware for modern homes",
  80. "Biodegradable cleaning supplies for eco-conscious consumers",
  81. "Organic cotton baby clothes for sensitive skin",
  82. "Natural organic skincare range for sensitive skin",
  83. "Tech gadgets for smart homes: 2024 edition",
  84. "Sustainable gardening tools and compost solutions",
  85. "Sensitive skin-friendly facial cleansers and toners",
  86. "Organic food wraps and storage solutions",
  87. "Yoga mats made from recycled materials"
  88. ],
  89. top_n=3,
  90. score_threshold=0.75,
  91. user="abc-123"
  92. )
  93. assert isinstance(response, RerankResult)
  94. assert len(response.docs) == 3
  95. def test__invoke():
  96. model = LocalaiRerankModel()
  97. # Test case 1: Empty docs
  98. result = model._invoke(
  99. model='bge-reranker-base',
  100. credentials={
  101. 'server_url': 'https://example.com',
  102. 'api_key': '1234567890'
  103. },
  104. query='Organic skincare products for sensitive skin',
  105. docs=[],
  106. top_n=3,
  107. score_threshold=0.75,
  108. user="abc-123"
  109. )
  110. assert isinstance(result, RerankResult)
  111. assert len(result.docs) == 0
  112. # Test case 2: Valid invocation
  113. result = model._invoke(
  114. model='bge-reranker-base',
  115. credentials={
  116. 'server_url': 'https://example.com',
  117. 'api_key': '1234567890'
  118. },
  119. query='Organic skincare products for sensitive skin',
  120. docs=[
  121. "Eco-friendly kitchenware for modern homes",
  122. "Biodegradable cleaning supplies for eco-conscious consumers",
  123. "Organic cotton baby clothes for sensitive skin",
  124. "Natural organic skincare range for sensitive skin",
  125. "Tech gadgets for smart homes: 2024 edition",
  126. "Sustainable gardening tools and compost solutions",
  127. "Sensitive skin-friendly facial cleansers and toners",
  128. "Organic food wraps and storage solutions",
  129. "Yoga mats made from recycled materials"
  130. ],
  131. top_n=3,
  132. score_threshold=0.75,
  133. user="abc-123"
  134. )
  135. assert isinstance(result, RerankResult)
  136. assert len(result.docs) == 3
  137. assert all(isinstance(doc, RerankDocument) for doc in result.docs)