fix(ollama): prefer explicit api_base

This commit is contained in:
mukunda katta 2026-04-21 08:21:00 -07:00
parent b9bedc8153
commit 00011f4bd4
2 changed files with 37 additions and 6 deletions

View file

@ -3969,8 +3969,8 @@ def completion( # type: ignore # noqa: PLR0915
response = model_response
elif custom_llm_provider == "ollama":
api_base = (
litellm.api_base
or api_base
api_base
or litellm.api_base
or get_secret("OLLAMA_API_BASE")
or "http://localhost:11434"
)
@ -3998,8 +3998,8 @@ def completion( # type: ignore # noqa: PLR0915
elif custom_llm_provider == "ollama_chat":
api_base = (
litellm.api_base
or api_base
api_base
or litellm.api_base
or get_secret("OLLAMA_API_BASE")
or "http://localhost:11434"
)
@ -5317,8 +5317,8 @@ def embedding( # noqa: PLR0915
)
elif custom_llm_provider == "ollama":
api_base = (
litellm.api_base
or api_base
api_base
or litellm.api_base
or get_secret_str("OLLAMA_API_BASE")
or "http://localhost:11434"
) # type: ignore

View file

@ -1323,6 +1323,37 @@ def test_lm_studio_completion(monkeypatch):
print(e)
@pytest.mark.parametrize("model", ["ollama/phi", "ollama_chat/phi"])
def test_ollama_completion_explicit_api_base_takes_precedence(monkeypatch, model):
monkeypatch.setattr(litellm, "api_base", "https://api.deepseek.com")
with patch("litellm.main.base_llm_http_handler.completion") as mock_completion:
mock_completion.return_value = MagicMock()
litellm.completion(
model=model,
messages=[{"role": "user", "content": "Hello"}],
api_base="http://localhost:11434",
)
assert mock_completion.call_args.kwargs["api_base"] == "http://localhost:11434"
def test_ollama_embedding_explicit_api_base_takes_precedence(monkeypatch):
monkeypatch.setattr(litellm, "api_base", "https://api.deepseek.com")
with patch("litellm.main.ollama.ollama_embeddings") as mock_embeddings:
mock_embeddings.return_value = MagicMock()
litellm.embedding(
model="ollama/qwen3-embedding:0.6b",
input="hello",
api_base="http://localhost:11434",
)
assert mock_embeddings.call_args.kwargs["api_base"] == "http://localhost:11434"
# ################### Hugging Face Conversational models ########################
# def hf_test_completion_conv():
# try: