Skip to content

Commit

Permalink
[pre-commit.ci] pre-commit suggestions (#353)
Browse files Browse the repository at this point in the history
* [pre-commit.ci] pre-commit suggestions

updates:
- [github.com/pre-commit/pre-commit-hooks: v4.6.0 → v5.0.0](pre-commit/pre-commit-hooks@v4.6.0...v5.0.0)
- [github.com/pre-commit/mirrors-prettier: v3.0.3 → v4.0.0-alpha.8](pre-commit/mirrors-prettier@v3.0.3...v4.0.0-alpha.8)
- [github.com/PyCQA/docformatter: 06907d0267368b49b9180eed423fae5697c1e909 → v1.7.5](PyCQA/docformatter@06907d0...v1.7.5)
- [github.com/astral-sh/ruff-pre-commit: v0.6.3 → v0.7.2](astral-sh/ruff-pre-commit@v0.6.3...v0.7.2)

* Apply suggestions from code review

* Apply suggestions from code review

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jirka Borovec <[email protected]>
  • Loading branch information
pre-commit-ci[bot] and Borda authored Nov 7, 2024
1 parent 23ba5e2 commit 6dbb793
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 73 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ci:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand All @@ -30,7 +30,7 @@ repos:
#args: ["--write-changes"]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
rev: v3.1.0
hooks:
- id: prettier
files: \.(json|yml|yaml|toml)
Expand All @@ -45,7 +45,7 @@ repos:
args: ["--in-place"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
rev: v0.7.2
hooks:
- id: ruff-format
args: ["--preview"]
Expand Down
2 changes: 1 addition & 1 deletion src/litserve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def run(

if sys.platform == "win32":
warnings.warn(
"Windows does not support forking. Using threads" " api_server_worker_type will be set to 'thread'"
"Windows does not support forking. Using threads api_server_worker_type will be set to 'thread'"
)
api_server_worker_type = "thread"
elif api_server_worker_type is None:
Expand Down
2 changes: 1 addition & 1 deletion src/litserve/test_examples/openai_spec_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def predict(self, inputs, context):
for ctx in context:
ctx["temperature"] = 1.0
output = (
"Hi! It's nice to meet you. Is there something I can help you with " "or would you like to chat?"
"Hi! It's nice to meet you. Is there something I can help you with or would you like to chat?"
).split()
for out in output:
time.sleep(0.01) # fake delay
Expand Down
28 changes: 14 additions & 14 deletions tests/e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def test_run_with_port():
client_code = f.read()
assert ":8080" in client_code, "Could not find 8080 in client.py"
output = subprocess.run("python client.py", shell=True, capture_output=True, text=True).stdout
assert (
'{"output":16.0}' in output
), f"tests/simple_server_server_diff_port.py didn't return expected output, got {output}"
assert '{"output":16.0}' in output, (
f"tests/simple_server_server_diff_port.py didn't return expected output, got {output}"
)
os.remove("client.py")


Expand Down Expand Up @@ -121,7 +121,7 @@ def test_openai_parity():
],
)
assert response.choices[0].message.content == "This is a generated output", (
f"Server didn't return expected output" f"\nOpenAI client output: {response}"
f"Server didn't return expected output\nOpenAI client output: {response}"
)

response = client.chat.completions.create(
Expand All @@ -136,7 +136,7 @@ def test_openai_parity():
expected_outputs = ["This is a generated output", None]
for r, expected_out in zip(response, expected_outputs):
assert r.choices[0].delta.content == expected_out, (
f"Server didn't return expected output.\n" f"OpenAI client output: {r}"
f"Server didn't return expected output.\nOpenAI client output: {r}"
)


Expand Down Expand Up @@ -167,7 +167,7 @@ def test_openai_parity_with_image_input():
messages=messages,
)
assert response.choices[0].message.content == "This is a generated output", (
f"Server didn't return expected output" f"\nOpenAI client output: {response}"
f"Server didn't return expected output\nOpenAI client output: {response}"
)

response = client.chat.completions.create(
Expand All @@ -179,7 +179,7 @@ def test_openai_parity_with_image_input():
expected_outputs = ["This is a generated output", None]
for r, expected_out in zip(response, expected_outputs):
assert r.choices[0].delta.content == expected_out, (
f"Server didn't return expected output.\n" f"OpenAI client output: {r}"
f"Server didn't return expected output.\nOpenAI client output: {r}"
)


Expand Down Expand Up @@ -218,10 +218,10 @@ def test_openai_parity_with_tools():
tools=tools,
)
assert response.choices[0].message.content == "", (
f"Server didn't return expected output" f"\nOpenAI client output: {response}"
f"Server didn't return expected output\nOpenAI client output: {response}"
)
assert response.choices[0].message.tool_calls[0].function.name == "get_current_weather", (
f"Server didn't return expected output" f"\nOpenAI client output: {response}"
f"Server didn't return expected output\nOpenAI client output: {response}"
)

response = client.chat.completions.create(
Expand All @@ -233,11 +233,11 @@ def test_openai_parity_with_tools():
expected_outputs = ["", None]
for r, expected_out in zip(response, expected_outputs):
assert r.choices[0].delta.content == expected_out, (
f"Server didn't return expected output.\n" f"OpenAI client output: {r}"
f"Server didn't return expected output.\nOpenAI client output: {r}"
)
if r.choices[0].delta.tool_calls:
assert r.choices[0].delta.tool_calls[0].function.name == "get_current_weather", (
f"Server didn't return expected output.\n" f"OpenAI client output: {r}"
f"Server didn't return expected output.\nOpenAI client output: {r}"
)


Expand All @@ -255,7 +255,7 @@ def test_e2e_openai_with_batching(openai_request_data):
],
)
assert response.choices[0].message.content == (
"Hi! It's nice to meet you. Is there something I can " "help you with or would you like to chat? "
"Hi! It's nice to meet you. Is there something I can help you with or would you like to chat? "
), f"Server didn't return expected output OpenAI client output: {response}"


Expand Down Expand Up @@ -293,7 +293,7 @@ def test_openai_parity_with_response_format():
response_format=response_format,
)
assert response.choices[0].message.content == output, (
f"Server didn't return expected output" f"\nOpenAI client output: {response}"
f"Server didn't return expected output\nOpenAI client output: {response}"
)

response = client.chat.completions.create(
Expand All @@ -306,7 +306,7 @@ def test_openai_parity_with_response_format():
expected_outputs = [output, None]
for r, expected_out in zip(response, expected_outputs):
assert r.choices[0].delta.content == expected_out, (
f"Server didn't return expected output.\n" f"OpenAI client output: {r}"
f"Server didn't return expected output.\nOpenAI client output: {r}"
)


Expand Down
12 changes: 6 additions & 6 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ def test_check_cuda_with_nvidia_smi_mock_gpu(mock_subprocess):
def test_connector(input_accelerator, expected_accelerator, expected_devices):
check_cuda_with_nvidia_smi.cache_clear()
connector = _Connector(accelerator=input_accelerator)
assert (
connector.accelerator == expected_accelerator
), f"accelerator mismatch - expected: {expected_accelerator}, actual: {connector.accelerator}"
assert connector.accelerator == expected_accelerator, (
f"accelerator mismatch - expected: {expected_accelerator}, actual: {connector.accelerator}"
)

assert (
connector.devices == expected_devices
), f"devices mismatch - expected {expected_devices}, actual: {connector.devices}"
assert connector.devices == expected_devices, (
f"devices mismatch - expected {expected_devices}, actual: {connector.devices}"
)

with pytest.raises(ValueError, match="accelerator must be one of 'auto', 'cpu', 'mps', 'cuda', or 'gpu'"):
_Connector(accelerator="SUPER_CHIP")
Expand Down
24 changes: 12 additions & 12 deletions tests/test_lit_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ async def test_stream(simple_stream_api):
resp2 = ac.post("/predict", json={"prompt": "World"}, timeout=10)
resp1, resp2 = await asyncio.gather(resp1, resp2)
assert resp1.status_code == 200, "Check if server is running and the request format is valid."
assert (
resp1.text == expected_output1
), "Server returns input prompt and generated output which didn't match."
assert resp1.text == expected_output1, (
"Server returns input prompt and generated output which didn't match."
)
assert resp2.status_code == 200, "Check if server is running and the request format is valid."
assert (
resp2.text == expected_output2
), "Server returns input prompt and generated output which didn't match."
assert resp2.text == expected_output2, (
"Server returns input prompt and generated output which didn't match."
)


@pytest.mark.asyncio
Expand All @@ -87,12 +87,12 @@ async def test_batched_stream_server(simple_batched_stream_api):
resp1, resp2 = await asyncio.gather(resp1, resp2)
assert resp1.status_code == 200, "Check if server is running and the request format is valid."
assert resp2.status_code == 200, "Check if server is running and the request format is valid."
assert (
resp1.text == expected_output1
), "Server returns input prompt and generated output which didn't match."
assert (
resp2.text == expected_output2
), "Server returns input prompt and generated output which didn't match."
assert resp1.text == expected_output1, (
"Server returns input prompt and generated output which didn't match."
)
assert resp2.text == expected_output2, (
"Server returns input prompt and generated output which didn't match."
)


def test_litapi_with_stream(simple_litapi):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ def put(self, item):
raise StopIteration("interrupt iteration")
if status == LitAPIStatus.ERROR and isinstance(response, StopIteration):
assert self.count // 2 == self.num_streamed_outputs, (
f"Loop count must have incremented for " f"{self.num_streamed_outputs} times."
f"Loop count must have incremented for {self.num_streamed_outputs} times."
)
raise StopIteration("finish streaming")

assert (
response == f"{self.count // 2}"
), f"streaming loop generates number from 0 to 9 which is sent via Queue. {args}, count:{self.count}"
assert response == f"{self.count // 2}", (
f"streaming loop generates number from 0 to 9 which is sent via Queue. {args}, count:{self.count}"
)
self.count += 1


Expand Down
7 changes: 2 additions & 5 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,10 @@ def test_readme(tmp_path, killall):
returncode, stdout, stderr = run_script_with_timeout(file, timeout=5, extra_time=extra_time, killall=killall)

if "server.run" in code:
assert uvicorn_msg in stderr, (
f"Expected to run uvicorn server.\n" f"Code:\n {code}\n\nCode output: {stderr}"
)
assert uvicorn_msg in stderr, f"Expected to run uvicorn server.\nCode:\n {code}\n\nCode output: {stderr}"
elif "requests.post" in code:
assert "ConnectionError" in stderr, (
f"Client examples should fail with a ConnectionError because there is no server running."
f"\nCode:\n{code}"
f"Client examples should fail with a ConnectionError because there is no server running.\nCode:\n{code}"
)
else:
assert returncode == 0, (
Expand Down
24 changes: 12 additions & 12 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ async def test_timeout():
await asyncio.sleep(0.0001)
response2 = asyncio.create_task(ac.post("/predict", json={"input": 5.0}))
responses = await asyncio.gather(response1, response2, return_exceptions=True)
assert (
responses[0].status_code == 200
), "First request should complete since it's popped from the request queue."
assert (
responses[1].status_code == 504
), "Server takes longer than specified timeout and request should timeout"
assert responses[0].status_code == 200, (
"First request should complete since it's popped from the request queue."
)
assert responses[1].status_code == 504, (
"Server takes longer than specified timeout and request should timeout"
)


@pytest.mark.asyncio
Expand Down Expand Up @@ -169,12 +169,12 @@ async def test_batch_timeout():
await asyncio.sleep(0.0001)
response3 = asyncio.create_task(ac.post("/predict", json={"input": 6.0}))
responses = await asyncio.gather(response1, response2, response3, return_exceptions=True)
assert (
responses[0].status_code == 200
), "Batch: First request should complete since it's popped from the request queue."
assert (
responses[1].status_code == 200
), "Batch: Second request should complete since it's popped from the request queue."
assert responses[0].status_code == 200, (
"Batch: First request should complete since it's popped from the request queue."
)
assert responses[1].status_code == 200, (
"Batch: Second request should complete since it's popped from the request queue."
)
assert responses[2].status_code == 504, "Batch: Third request was delayed and should fail"

server1 = LitServer(SlowLitAPI(), accelerator="cpu", devices=1, timeout=-1)
Expand Down
30 changes: 15 additions & 15 deletions tests/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ async def test_openai_spec(openai_request_data):
resp = await ac.post("/v1/chat/completions", json=openai_request_data, timeout=10)
assert resp.status_code == 200, "Status code should be 200"

assert (
resp.json()["choices"][0]["message"]["content"] == "This is a generated output"
), "LitAPI predict response should match with the generated output"
assert resp.json()["choices"][0]["message"]["content"] == "This is a generated output", (
"LitAPI predict response should match with the generated output"
)


# OpenAIWithUsage
Expand Down Expand Up @@ -81,9 +81,9 @@ async def test_openai_spec_with_image(openai_request_data_with_image):
resp = await ac.post("/v1/chat/completions", json=openai_request_data_with_image, timeout=10)
assert resp.status_code == 200, "Status code should be 200"

assert (
resp.json()["choices"][0]["message"]["content"] == "This is a generated output"
), "LitAPI predict response should match with the generated output"
assert resp.json()["choices"][0]["message"]["content"] == "This is a generated output", (
"LitAPI predict response should match with the generated output"
)


@pytest.mark.asyncio
Expand All @@ -94,9 +94,9 @@ async def test_override_encode(openai_request_data):
resp = await ac.post("/v1/chat/completions", json=openai_request_data, timeout=10)
assert resp.status_code == 200, "Status code should be 200"

assert (
resp.json()["choices"][0]["message"]["content"] == "This is a custom encoded output"
), "LitAPI predict response should match with the generated output"
assert resp.json()["choices"][0]["message"]["content"] == "This is a custom encoded output", (
"LitAPI predict response should match with the generated output"
)


@pytest.mark.asyncio
Expand All @@ -107,9 +107,9 @@ async def test_openai_spec_with_tools(openai_request_data_with_tools):
async with LifespanManager(server.app) as manager, AsyncClient(app=manager.app, base_url="http://test") as ac:
resp = await ac.post("/v1/chat/completions", json=openai_request_data_with_tools, timeout=10)
assert resp.status_code == 200, "Status code should be 200"
assert (
resp.json()["choices"][0]["message"]["content"] == ""
), "LitAPI predict response should match with the generated output"
assert resp.json()["choices"][0]["message"]["content"] == "", (
"LitAPI predict response should match with the generated output"
)
assert resp.json()["choices"][0]["message"]["tool_calls"] == [
{
"id": "call_1",
Expand Down Expand Up @@ -182,9 +182,9 @@ async def test_oai_prepopulated_context(openai_request_data):
with wrap_litserve_start(server) as server:
async with LifespanManager(server.app) as manager, AsyncClient(app=manager.app, base_url="http://test") as ac:
resp = await ac.post("/v1/chat/completions", json=openai_request_data, timeout=10)
assert (
resp.json()["choices"][0]["message"]["content"] == "This is a"
), "OpenAISpec must return only 3 tokens as specified using `max_tokens` parameter"
assert resp.json()["choices"][0]["message"]["content"] == "This is a", (
"OpenAISpec must return only 3 tokens as specified using `max_tokens` parameter"
)


class WrongLitAPI(ls.LitAPI):
Expand Down

0 comments on commit 6dbb793

Please sign in to comment.