mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* fix(llms): sanitize trailing slashes in provider get_complete_url * test(llms): move trailing-slash url tests into each provider's mapped unit file * fix(llms): strip trailing slashes in Perplexity search and Ollama embedding URLs and run the Topaz tests in CI * fix(llms): strip trailing slashes in search, DashScope, VolcEngine, GigaChat and Gradient AI URL builders * fix(fastcrw): strip a trailing slash from api_base and expect one slash in the GigaChat tests * fix(gigachat): strip a trailing slash from the pass-through api_base * test(integration): cover trailing-slash api_base across provider URL builders Wire cells for chat, streaming, messages, responses, embeddings, search and pass-through with and without a trailing slash, an owned-proxy module for the flag-gated providers, env bases and a worker kill, two chaos bursts, and a Topaz SDK cell --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
138 lines
4.3 KiB
Python
138 lines
4.3 KiB
Python
from typing import Final
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
|
|
import litellm
|
|
from litellm.llms.ollama.completion.handler import ollama_aembeddings, ollama_embeddings
|
|
from litellm.types.utils import EmbeddingResponse
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_response_data():
|
|
return {
|
|
"embeddings": [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]],
|
|
"prompt_eval_count": 5,
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_embedding_response():
|
|
return EmbeddingResponse(object="", data=[], model="", usage=None)
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_encoding():
|
|
mock = MagicMock()
|
|
mock.encode.return_value = [0] * 5
|
|
return mock
|
|
|
|
|
|
def test_ollama_embeddings(mock_response_data, mock_embedding_response, mock_encoding):
|
|
with (
|
|
patch("litellm.module_level_client.post") as mock_post,
|
|
patch("litellm.OllamaConfig.get_config", return_value={"truncate": 512}),
|
|
):
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.json.return_value = mock_response_data
|
|
mock_post.return_value = mock_response
|
|
|
|
response = ollama_embeddings(
|
|
api_base="http://localhost:11434",
|
|
model="test-model",
|
|
prompts=["hello", "world"],
|
|
optional_params={},
|
|
model_response=mock_embedding_response,
|
|
logging_obj=None,
|
|
encoding=mock_encoding,
|
|
)
|
|
|
|
assert response.model == "ollama/test-model"
|
|
assert response.object == "list"
|
|
assert isinstance(response.data, list)
|
|
assert response.usage.total_tokens == 5
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_ollama_aembeddings(
|
|
mock_response_data, mock_embedding_response, mock_encoding
|
|
):
|
|
mock_response = AsyncMock()
|
|
# Make json() a regular synchronous method, not async
|
|
mock_response.json = MagicMock(return_value=mock_response_data)
|
|
with (
|
|
patch(
|
|
"litellm.module_level_aclient.post", return_value=mock_response
|
|
) as mock_post,
|
|
patch("litellm.OllamaConfig.get_config", return_value={"truncate": 512}),
|
|
):
|
|
|
|
response = await ollama_aembeddings(
|
|
api_base="http://localhost:11434",
|
|
model="test-model",
|
|
prompts=["hello", "world"],
|
|
optional_params={},
|
|
model_response=mock_embedding_response,
|
|
logging_obj=None,
|
|
encoding=mock_encoding,
|
|
)
|
|
|
|
assert response.model == "ollama/test-model"
|
|
assert response.object == "list"
|
|
assert isinstance(response.data, list)
|
|
assert response.usage.total_tokens == 5
|
|
|
|
|
|
def test_prompt_eval_fallback_when_missing(mock_embedding_response, mock_encoding):
|
|
response_data = {
|
|
"embeddings": [[0.1, 0.2, 0.3]],
|
|
# No "prompt_eval_count"
|
|
}
|
|
|
|
with (
|
|
patch("litellm.module_level_client.post") as mock_post,
|
|
patch("litellm.OllamaConfig.get_config", return_value={}),
|
|
):
|
|
|
|
mock_response = MagicMock()
|
|
mock_response.json.return_value = response_data
|
|
mock_post.return_value = mock_response
|
|
|
|
response = ollama_embeddings(
|
|
api_base="http://localhost:11434",
|
|
model="test-model",
|
|
prompts=["only-prompt"],
|
|
optional_params={},
|
|
model_response=mock_embedding_response,
|
|
logging_obj=None,
|
|
encoding=mock_encoding,
|
|
)
|
|
|
|
# Fallback should use encoding length (mocked to be 5)
|
|
assert response.usage.prompt_tokens == 5
|
|
assert response.usage.total_tokens == 5
|
|
assert response.usage.completion_tokens == 0
|
|
assert response.data[0]["embedding"] == [0.1, 0.2, 0.3]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"api_base",
|
|
[
|
|
"http://gateway.example.com/ollama",
|
|
"http://gateway.example.com/ollama/",
|
|
"http://gateway.example.com/ollama/api/embed/",
|
|
],
|
|
)
|
|
@respx.mock
|
|
def test_ollama_embeddings_ignore_a_trailing_slash_on_api_base(api_base: str) -> None:
|
|
route: Final = respx.post("http://gateway.example.com/ollama/api/embed").mock(
|
|
return_value=httpx.Response(200, json={"embeddings": [[0.1, 0.2], [0.3, 0.4]], "prompt_eval_count": 2})
|
|
)
|
|
|
|
response: Final = litellm.embedding(model="ollama/all-minilm", api_base=api_base, input=["hello", "world"])
|
|
|
|
assert route.called
|
|
assert [item["embedding"] for item in response.data] == [[0.1, 0.2], [0.3, 0.4]]
|