Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- fix(server): reject multiple completion prompts with a client error by @remote-controlled-man in #2418
- fix(ci): remove unsupported macOS 14 release runners by @abetlen in #2416

## [0.3.37]
Expand Down
6 changes: 5 additions & 1 deletion llama_cpp/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ async def create_completion(
body: CreateCompletionRequest,
) -> llama_cpp.Completion:
if isinstance(body.prompt, list):
assert len(body.prompt) <= 1
if len(body.prompt) > 1:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Only a single prompt is supported.",
)
body.prompt = body.prompt[0] if len(body.prompt) > 0 else ""

body_model = (
Expand Down
91 changes: 91 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from unittest.mock import Mock

import pytest
from fastapi.testclient import TestClient

from llama_cpp.server import app as server_app
from llama_cpp.server.settings import Settings


@pytest.fixture
def completion_client(monkeypatch):
monkeypatch.delenv("CONFIG_FILE", raising=False)
prompts = []

def complete(self, *, prompt, stream=False, **kwargs):
prompts.append(prompt)
response = {
"id": "cmpl-test",
"object": "text_completion",
"created": 0,
"model": "test-model",
"choices": [
{
"text": "done",
"index": 0,
"logprobs": None,
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": 1,
"completion_tokens": 1,
"total_tokens": 2,
},
}
return iter([response]) if stream else response

class FakeLlama:
__call__ = complete

proxy = Mock(return_value=FakeLlama())
monkeypatch.setattr(server_app, "LlamaProxy", lambda models: proxy)
monkeypatch.setattr(server_app, "_llama_proxy", None)
monkeypatch.setattr(server_app, "_server_settings", None)
monkeypatch.setattr(server_app.llama_cpp.Llama, "__call__", complete)
app = server_app.create_app(Settings(model="unused.gguf", api_key=None))

with TestClient(app) as client:
yield client, proxy, prompts


@pytest.mark.parametrize(
"path", ["/v1/completions", "/v1/engines/copilot-codex/completions"]
)
@pytest.mark.parametrize("stream", [False, True])
@pytest.mark.parametrize("prompt", [["first", "second"], ["", ""], ["a", "b", "c"]])
def test_completion_rejects_multiple_prompts(completion_client, path, stream, prompt):
client, proxy, prompts = completion_client
response = client.post(path, json={"prompt": prompt, "stream": stream})

assert response.status_code == 400
assert response.json() == {"detail": "Only a single prompt is supported."}
proxy.assert_not_called()
assert prompts == []


@pytest.mark.parametrize(
"path", ["/v1/completions", "/v1/engines/copilot-codex/completions"]
)
@pytest.mark.parametrize("stream", [False, True])
@pytest.mark.parametrize(
("prompt", "expected_prompt"),
[("hello", "hello"), ("", ""), (["hello"], "hello"), ([], "")],
)
def test_completion_supported_prompts(
completion_client, path, stream, prompt, expected_prompt
):
client, proxy, prompts = completion_client
response = client.post(path, json={"prompt": prompt, "stream": stream})

assert response.status_code == 200
assert prompts == [expected_prompt]
proxy.assert_called_once_with(
"copilot-codex" if path.startswith("/v1/engines/") else None
)
if stream:
assert response.headers["content-type"].startswith("text/event-stream")
assert '"text": "done"' in response.text
assert "data: [DONE]" in response.text
else:
assert response.json()["choices"][0]["text"] == "done"