Blame view

tests/test_reranker_dashscope_backend.py 3.44 KB
d31c7f65   tangwang   补充云服务reranker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  from __future__ import annotations
  
  from reranker.backends import get_rerank_backend
  from reranker.backends.dashscope_rerank import DashScopeRerankBackend
  
  
  def test_dashscope_backend_factory_loads():
      backend = get_rerank_backend(
          "dashscope_rerank",
          {
              "model_name": "qwen3-rerank",
              "endpoint": "https://dashscope.aliyuncs.com/compatible-api/v1/reranks",
              "api_key": "test-key",
          },
      )
      assert isinstance(backend, DashScopeRerankBackend)
  
  
  def test_dashscope_backend_score_with_meta_dedup_and_restore(monkeypatch):
      backend = DashScopeRerankBackend(
          {
              "model_name": "qwen3-rerank",
              "endpoint": "https://dashscope.aliyuncs.com/compatible-api/v1/reranks",
              "api_key": "test-key",
              "top_n_cap": 0,
          }
      )
  
      def _fake_post(query: str, docs: list[str], top_n: int):
          assert query == "wireless mouse"
          # deduplicated docs
          assert docs == ["doc-a", "doc-b"]
          assert top_n == 2
          return {
              "results": [
                  {"index": 1, "relevance_score": 0.9},
                  {"index": 0, "relevance_score": 0.2},
              ]
          }
  
      monkeypatch.setattr(backend, "_post_rerank", _fake_post)
      scores, meta = backend.score_with_meta(
          query="wireless mouse",
          docs=["doc-a", "doc-b", "doc-a", "", "   ", None],
          normalize=True,
      )
  
      assert scores == [0.2, 0.9, 0.2, 0.0, 0.0, 0.0]
      assert meta["input_docs"] == 6
      assert meta["usable_docs"] == 3
      assert meta["unique_docs"] == 2
      assert meta["top_n"] == 2
      assert meta["response_results"] == 2
      assert meta["backend"] == "dashscope_rerank"
  
  
  def test_dashscope_backend_top_n_cap_and_normalize_fallback(monkeypatch):
      backend = DashScopeRerankBackend(
          {
              "model_name": "qwen3-rerank",
              "endpoint": "https://dashscope.aliyuncs.com/compatible-api/v1/reranks",
              "api_key": "test-key",
              "top_n_cap": 1,
          }
      )
  
      def _fake_post(query: str, docs: list[str], top_n: int):
          assert query == "q"
          assert len(docs) == 2
          assert top_n == 1
          # Only top-1 returned, score outside [0,1] to trigger sigmoid fallback
          return {"results": [{"index": 1, "score": 3.0}]}
  
      monkeypatch.setattr(backend, "_post_rerank", _fake_post)
      scores_norm, _ = backend.score_with_meta(query="q", docs=["a", "b"], normalize=True)
      scores_raw, _ = backend.score_with_meta(query="q", docs=["a", "b"], normalize=False)
  
      assert scores_norm[0] == 0.0
      assert 0.95 < scores_norm[1] < 0.96
      assert scores_raw == [0.0, 3.0]
  
  
  def test_dashscope_backend_score_with_meta_topn_request(monkeypatch):
      backend = DashScopeRerankBackend(
          {
              "model_name": "qwen3-rerank",
              "endpoint": "https://dashscope.aliyuncs.com/compatible-api/v1/reranks",
              "api_key": "test-key",
              "top_n_cap": 0,
          }
      )
  
      def _fake_post(query: str, docs: list[str], top_n: int):
          assert query == "q"
          assert docs == ["d1", "d2", "d3"]
          assert top_n == 2
          return {"results": [{"index": 2, "relevance_score": 0.8}, {"index": 0, "relevance_score": 0.3}]}
  
      monkeypatch.setattr(backend, "_post_rerank", _fake_post)
      scores, meta = backend.score_with_meta_topn(query="q", docs=["d1", "d2", "d3"], top_n=2)
      assert scores == [0.3, 0.0, 0.8]
      assert meta["top_n"] == 2
      assert meta["requested_top_n"] == 2