test_rerank.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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={"server_url": os.environ.get("LOCALAI_SERVER_URL")},
  28. query="Organic skincare products for sensitive skin",
  29. docs=[
  30. "Eco-friendly kitchenware for modern homes",
  31. "Biodegradable cleaning supplies for eco-conscious consumers",
  32. "Organic cotton baby clothes for sensitive skin",
  33. "Natural organic skincare range for sensitive skin",
  34. "Tech gadgets for smart homes: 2024 edition",
  35. "Sustainable gardening tools and compost solutions",
  36. "Sensitive skin-friendly facial cleansers and toners",
  37. "Organic food wraps and storage solutions",
  38. "Yoga mats made from recycled materials",
  39. ],
  40. top_n=3,
  41. score_threshold=0.75,
  42. user="abc-123",
  43. )
  44. assert isinstance(response, RerankResult)
  45. assert len(response.docs) == 3
  46. def test__invoke():
  47. model = LocalaiRerankModel()
  48. # Test case 1: Empty docs
  49. result = model._invoke(
  50. model="bge-reranker-base",
  51. credentials={"server_url": "https://example.com", "api_key": "1234567890"},
  52. query="Organic skincare products for sensitive skin",
  53. docs=[],
  54. top_n=3,
  55. score_threshold=0.75,
  56. user="abc-123",
  57. )
  58. assert isinstance(result, RerankResult)
  59. assert len(result.docs) == 0
  60. # Test case 2: Valid invocation
  61. result = model._invoke(
  62. model="bge-reranker-base",
  63. credentials={"server_url": "https://example.com", "api_key": "1234567890"},
  64. query="Organic skincare products for sensitive skin",
  65. docs=[
  66. "Eco-friendly kitchenware for modern homes",
  67. "Biodegradable cleaning supplies for eco-conscious consumers",
  68. "Organic cotton baby clothes for sensitive skin",
  69. "Natural organic skincare range for sensitive skin",
  70. "Tech gadgets for smart homes: 2024 edition",
  71. "Sustainable gardening tools and compost solutions",
  72. "Sensitive skin-friendly facial cleansers and toners",
  73. "Organic food wraps and storage solutions",
  74. "Yoga mats made from recycled materials",
  75. ],
  76. top_n=3,
  77. score_threshold=0.75,
  78. user="abc-123",
  79. )
  80. assert isinstance(result, RerankResult)
  81. assert len(result.docs) == 3
  82. assert all(isinstance(doc, RerankDocument) for doc in result.docs)