-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_reranker_base_url.py
More file actions
97 lines (73 loc) · 3.79 KB
/
test_reranker_base_url.py
File metadata and controls
97 lines (73 loc) · 3.79 KB
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
"""Tests for BaseLLMReranker provider-specific default base_url (gh-11952).
When the Ollama provider is configured without an explicit base_url, the
fallback must be http://localhost:11434/v1 — not the OpenAI endpoint.
Before the fix, all Ollama reranker calls silently hit api.openai.com/v1
and failed with 404.
"""
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from mindsdb.integrations.utilities.rag.settings import DEFAULT_LLM_ENDPOINT
from mindsdb.integrations.utilities.rag.rerankers.base_reranker import BaseLLMReranker
OLLAMA_DEFAULT_BASE_URL = "http://localhost:11434/v1"
def _make_reranker(**kwargs):
"""Construct a BaseLLMReranker with _init_client bypassed."""
with patch.object(BaseLLMReranker, "_init_client", return_value=None):
obj = BaseLLMReranker(**kwargs)
return obj
class TestOllamaDefaultBaseUrl:
"""The client must be created with the Ollama-specific URL when base_url is omitted."""
def test_ollama_without_base_url_uses_ollama_default(self):
reranker = _make_reranker(provider="ollama", model="llama3.2", api_key="n/a")
reranker.client = None # force re-init
with patch(
"mindsdb.integrations.utilities.rag.rerankers.base_reranker.AsyncOpenAI"
) as mock_openai:
reranker._init_client()
_, kwargs = mock_openai.call_args
assert kwargs.get("base_url") == OLLAMA_DEFAULT_BASE_URL, (
f"Expected Ollama default {OLLAMA_DEFAULT_BASE_URL!r}, got {kwargs.get('base_url')!r}. "
"Without this fix, Ollama reranker requests hit the OpenAI endpoint and fail with 404."
)
def test_ollama_with_explicit_base_url_respects_it(self):
custom_url = "http://my-ollama-server:11434/v1"
reranker = _make_reranker(provider="ollama", model="llama3.2", api_key="n/a", base_url=custom_url)
reranker.client = None
with patch(
"mindsdb.integrations.utilities.rag.rerankers.base_reranker.AsyncOpenAI"
) as mock_openai:
reranker._init_client()
_, kwargs = mock_openai.call_args
assert kwargs.get("base_url") == custom_url
def test_openai_without_base_url_keeps_openai_default(self):
reranker = _make_reranker(provider="openai", model="gpt-4o", api_key="sk-test")
reranker.client = None
with patch(
"mindsdb.integrations.utilities.rag.rerankers.base_reranker.AsyncOpenAI"
) as mock_openai:
reranker._init_client()
_, kwargs = mock_openai.call_args
assert kwargs.get("base_url") == DEFAULT_LLM_ENDPOINT, (
"OpenAI provider without explicit base_url must still default to DEFAULT_LLM_ENDPOINT."
)
def test_openai_with_explicit_base_url_respects_it(self):
custom_url = "https://my-openai-proxy.example.com/v1"
reranker = _make_reranker(provider="openai", model="gpt-4o", api_key="sk-test", base_url=custom_url)
reranker.client = None
with patch(
"mindsdb.integrations.utilities.rag.rerankers.base_reranker.AsyncOpenAI"
) as mock_openai:
reranker._init_client()
_, kwargs = mock_openai.call_args
assert kwargs.get("base_url") == custom_url
def test_ollama_default_is_not_openai_endpoint(self):
"""Regression: Ollama default must not be the OpenAI endpoint."""
reranker = _make_reranker(provider="ollama", model="llama3.2", api_key="n/a")
reranker.client = None
with patch(
"mindsdb.integrations.utilities.rag.rerankers.base_reranker.AsyncOpenAI"
) as mock_openai:
reranker._init_client()
_, kwargs = mock_openai.call_args
assert kwargs.get("base_url") != DEFAULT_LLM_ENDPOINT, (
"Ollama default base_url must not be the OpenAI API endpoint."
)