mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
Merge a288e671a8 into e16d79d635
This commit is contained in:
commit
691769323b
2 changed files with 49 additions and 11 deletions
|
|
@ -34,7 +34,11 @@ def _prepare_ollama_embedding_payload(
|
|||
special_optional_params: Final = ["truncate", "options", "keep_alive", "dimensions"]
|
||||
|
||||
for k, v in optional_params.items():
|
||||
if k in special_optional_params:
|
||||
if k == "options" and isinstance(v, dict):
|
||||
data[k] = (
|
||||
{**v, **accumulated_options} if isinstance(accumulated_options := data.get("options"), dict) else {**v}
|
||||
)
|
||||
elif k in special_optional_params:
|
||||
data[k] = v
|
||||
else:
|
||||
data.setdefault("options", {})
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
from collections.abc import Mapping
|
||||
from typing import Final
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
|
|
@ -6,7 +7,11 @@ import pytest
|
|||
import respx
|
||||
|
||||
import litellm
|
||||
from litellm.llms.ollama.completion.handler import ollama_aembeddings, ollama_embeddings
|
||||
from litellm.llms.ollama.completion.handler import (
|
||||
_prepare_ollama_embedding_payload,
|
||||
ollama_aembeddings,
|
||||
ollama_embeddings,
|
||||
)
|
||||
from litellm.types.utils import EmbeddingResponse
|
||||
|
||||
|
||||
|
|
@ -30,12 +35,47 @@ def mock_encoding():
|
|||
return mock
|
||||
|
||||
|
||||
@pytest.mark.parametrize("options_first", [True, False])
|
||||
def test_embedding_options_merge_without_mutating_caller(options_first: bool) -> None:
|
||||
options: Final = {"num_ctx": 1024, "num_batch": 4}
|
||||
optional_params: Final = (
|
||||
{"options": options, "num_batch": 8, "truncate": False}
|
||||
if options_first
|
||||
else {"num_batch": 8, "options": options, "truncate": False}
|
||||
)
|
||||
|
||||
payload: Final = _prepare_ollama_embedding_payload("test-model", ["hello"], optional_params)
|
||||
|
||||
assert payload == {
|
||||
"model": "test-model",
|
||||
"input": ["hello"],
|
||||
"options": {"num_ctx": 1024, "num_batch": 8},
|
||||
"truncate": False,
|
||||
}
|
||||
assert options == {"num_ctx": 1024, "num_batch": 4}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("optional_params", "expected_options"),
|
||||
[
|
||||
({"options": {"num_ctx": 1024}}, {"num_ctx": 1024}),
|
||||
({"num_batch": 8}, {"num_batch": 8}),
|
||||
({"options": {}, "num_batch": 8}, {"num_batch": 8}),
|
||||
],
|
||||
)
|
||||
def test_embedding_options_single_source(
|
||||
optional_params: Mapping[str, object], expected_options: Mapping[str, object]
|
||||
) -> None:
|
||||
payload: Final = _prepare_ollama_embedding_payload("test-model", ["hello"], optional_params)
|
||||
|
||||
assert payload == {"model": "test-model", "input": ["hello"], "options": expected_options}
|
||||
|
||||
|
||||
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
|
||||
|
|
@ -57,19 +97,14 @@ def test_ollama_embeddings(mock_response_data, mock_embedding_response, mock_enc
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ollama_aembeddings(
|
||||
mock_response_data, mock_embedding_response, mock_encoding
|
||||
):
|
||||
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.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",
|
||||
|
|
@ -96,7 +131,6 @@ def test_prompt_eval_fallback_when_missing(mock_embedding_response, mock_encodin
|
|||
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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue