mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
* test: drop the cwd-relative sys.path.insert calls from the test suite
TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.
Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.
Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.
* test: drop the duplicate imports the sys.path sweep exposed to F811
* test(pre-call-utils): restore the os import the new bedrock tests need
149 lines
4.4 KiB
Python
149 lines
4.4 KiB
Python
import json
|
|
from datetime import datetime
|
|
|
|
|
|
import litellm
|
|
import pytest
|
|
|
|
from litellm.utils import (
|
|
LiteLLMResponseObjectHandler,
|
|
)
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
from litellm.types.utils import (
|
|
ModelResponse,
|
|
TextCompletionResponse,
|
|
TextChoices,
|
|
Logprobs as TextCompletionLogprobs,
|
|
Usage,
|
|
)
|
|
|
|
|
|
def test_convert_chat_to_text_completion():
|
|
"""Test converting chat completion to text completion"""
|
|
chat_response = ModelResponse(
|
|
id="chat123",
|
|
created=1234567890,
|
|
model="gpt-3.5-turbo",
|
|
choices=[
|
|
{
|
|
"index": 0,
|
|
"message": {"content": "Hello, world!"},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
usage={"total_tokens": 10, "completion_tokens": 10},
|
|
_hidden_params={"api_key": "test"},
|
|
)
|
|
|
|
text_completion = TextCompletionResponse()
|
|
result = LiteLLMResponseObjectHandler.convert_chat_to_text_completion(
|
|
response=chat_response, text_completion_response=text_completion
|
|
)
|
|
|
|
assert isinstance(result, TextCompletionResponse)
|
|
assert result.id == "chat123"
|
|
assert result.object == "text_completion"
|
|
assert result.created == 1234567890
|
|
assert result.model == "gpt-3.5-turbo"
|
|
assert result.choices[0].text == "Hello, world!"
|
|
assert result.choices[0].finish_reason == "stop"
|
|
assert result.usage == Usage(
|
|
completion_tokens=10,
|
|
prompt_tokens=0,
|
|
total_tokens=10,
|
|
completion_tokens_details=None,
|
|
prompt_tokens_details=None,
|
|
)
|
|
|
|
|
|
def test_convert_provider_response_logprobs_non_huggingface():
|
|
"""Test converting provider logprobs for non-huggingface provider"""
|
|
response = ModelResponse(id="test123", _hidden_params={})
|
|
|
|
result = LiteLLMResponseObjectHandler._convert_provider_response_logprobs_to_text_completion_logprobs(
|
|
response=response, custom_llm_provider="openai"
|
|
)
|
|
|
|
assert result is None
|
|
|
|
|
|
def test_convert_chat_to_text_completion_multiple_choices():
|
|
"""Test converting chat completion to text completion with multiple choices"""
|
|
chat_response = ModelResponse(
|
|
id="chat456",
|
|
created=1234567890,
|
|
model="gpt-3.5-turbo",
|
|
choices=[
|
|
{
|
|
"index": 0,
|
|
"message": {"content": "First response"},
|
|
"finish_reason": "stop",
|
|
},
|
|
{
|
|
"index": 1,
|
|
"message": {"content": "Second response"},
|
|
"finish_reason": "length",
|
|
},
|
|
],
|
|
usage={"total_tokens": 20},
|
|
_hidden_params={"api_key": "test"},
|
|
)
|
|
|
|
text_completion = TextCompletionResponse()
|
|
result = LiteLLMResponseObjectHandler.convert_chat_to_text_completion(
|
|
response=chat_response, text_completion_response=text_completion
|
|
)
|
|
|
|
assert isinstance(result, TextCompletionResponse)
|
|
assert result.id == "chat456"
|
|
assert result.object == "text_completion"
|
|
assert len(result.choices) == 2
|
|
assert result.choices[0].text == "First response"
|
|
assert result.choices[0].finish_reason == "stop"
|
|
assert result.choices[1].text == "Second response"
|
|
assert result.choices[1].finish_reason == "length"
|
|
assert result.usage == Usage(
|
|
completion_tokens=0,
|
|
prompt_tokens=0,
|
|
total_tokens=20,
|
|
completion_tokens_details=None,
|
|
prompt_tokens_details=None,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
async def test_text_completion_include_usage(sync_mode):
|
|
"""Test text completion with include_usage"""
|
|
last_chunk = None
|
|
if sync_mode:
|
|
response = await litellm.atext_completion(
|
|
model="gpt-3.5-turbo",
|
|
prompt="Hello, world!",
|
|
stream=True,
|
|
stream_options={"include_usage": True},
|
|
)
|
|
|
|
async for chunk in response:
|
|
print(chunk)
|
|
last_chunk = chunk
|
|
else:
|
|
response = litellm.text_completion(
|
|
model="gpt-3.5-turbo",
|
|
prompt="Hello, world!",
|
|
stream=True,
|
|
stream_options={"include_usage": True},
|
|
)
|
|
|
|
for chunk in response:
|
|
print(chunk)
|
|
last_chunk = chunk
|
|
|
|
assert last_chunk is not None
|
|
assert last_chunk.usage is not None
|
|
assert last_chunk.usage.prompt_tokens > 0
|
|
assert last_chunk.usage.completion_tokens > 0
|
|
assert last_chunk.usage.total_tokens > 0
|