-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
[Frontend] Add /classify endpoint #17032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
aaeab80
Add classification endpoint along with tests and example
frieda-huang 110e04b
feat(classification): Add /classify endpoint, tests, and documentation
frieda-huang 35e46c6
Reorder Classification API section & handle missing hf_config
frieda-huang 0a2789d
Refactor to standardize preprocessing for classify and embed endpoints
frieda-huang 8775df1
Fix mypy issue
frieda-huang 3d26a4c
Resolve mypy type issues
frieda-huang 214558a
Reorder /classify and /score
frieda-huang 3b0b390
Reorder /classify and /score
frieda-huang f66f027
Refactor to use mixin
frieda-huang 1788d9e
Refactor to use mixin
frieda-huang 2c641cc
Merge branch 'vllm-project:main' into classify
frieda-huang 55dcba1
Fix formatting
frieda-huang 2ba02e5
Merge branch 'classify' of https://github.com/frieda-huang/vllm into …
frieda-huang d88967c
Fix test_max_truncation_size failing
frieda-huang d4bb1b5
Fix test_embeddings; replace Config with ConfigDict
frieda-huang f7d85a7
Merge branch 'vllm-project:main' into classify
frieda-huang 4c7a0dc
Merge branch 'vllm-project:main' into classify
frieda-huang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import argparse | ||
| import pprint | ||
|
|
||
| import requests | ||
|
|
||
|
|
||
| def post_http_request(payload: dict, api_url: str) -> requests.Response: | ||
| headers = {"User-Agent": "Test Client"} | ||
| response = requests.post(api_url, headers=headers, json=payload) | ||
| return response | ||
|
|
||
|
|
||
| def parse_args(): | ||
| parse = argparse.ArgumentParser() | ||
| parse.add_argument("--host", type=str, default="localhost") | ||
| parse.add_argument("--port", type=int, default=8000) | ||
| parse.add_argument("--model", | ||
| type=str, | ||
| default="jason9693/Qwen2.5-1.5B-apeach") | ||
| return parse.parse_args() | ||
|
|
||
|
|
||
| def main(args): | ||
| host = args.host | ||
| port = args.port | ||
| model_name = args.model | ||
|
|
||
| api_url = f"http://{host}:{port}/classify" | ||
| prompts = [ | ||
| "Hello, my name is", | ||
| "The president of the United States is", | ||
| "The capital of France is", | ||
| "The future of AI is", | ||
| ] | ||
|
|
||
| payload = { | ||
| "model": model_name, | ||
| "input": prompts, | ||
| } | ||
|
|
||
| classify_response = post_http_request(payload=payload, api_url=api_url) | ||
| pprint.pprint(classify_response.json()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = parse_args() | ||
| main(args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import pytest | ||
| import requests | ||
|
|
||
| from vllm.entrypoints.openai.protocol import ClassificationResponse | ||
|
|
||
| from ...utils import RemoteOpenAIServer | ||
|
|
||
| MODEL_NAME = "jason9693/Qwen2.5-1.5B-apeach" | ||
| DTYPE = "float32" # Use float32 to avoid NaN issue | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def server(): | ||
| args = [ | ||
| "--enforce-eager", | ||
| "--max-model-len", | ||
| "512", | ||
| "--dtype", | ||
| DTYPE, | ||
| ] | ||
|
|
||
| with RemoteOpenAIServer(MODEL_NAME, args) as remote_server: | ||
| yield remote_server | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_single_input_classification(server: RemoteOpenAIServer, | ||
| model_name: str): | ||
| input_text = "This product was excellent and exceeded my expectations" | ||
|
|
||
| classification_response = requests.post( | ||
| server.url_for("classify"), | ||
| json={ | ||
| "model": model_name, | ||
| "input": input_text | ||
| }, | ||
| ) | ||
|
|
||
| classification_response.raise_for_status() | ||
| output = ClassificationResponse.model_validate( | ||
| classification_response.json()) | ||
|
|
||
| assert output.object == "list" | ||
| assert output.model == MODEL_NAME | ||
| assert len(output.data) == 1 | ||
| assert hasattr(output.data[0], "label") | ||
| assert hasattr(output.data[0], "probs") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_multiple_inputs_classification(server: RemoteOpenAIServer, | ||
| model_name: str): | ||
| input_texts = [ | ||
| "The product arrived on time and works perfectly", | ||
| "I'm very satisfied with my purchase, would buy again", | ||
| "The customer service was helpful and resolved my issue quickly", | ||
| "This product broke after one week, terrible quality", | ||
| "I'm very disappointed with this purchase, complete waste of money", | ||
| "The customer service was rude and unhelpful", | ||
| ] | ||
|
|
||
| classification_response = requests.post( | ||
| server.url_for("classify"), | ||
| json={ | ||
| "model": model_name, | ||
| "input": input_texts | ||
| }, | ||
| ) | ||
| output = ClassificationResponse.model_validate( | ||
| classification_response.json()) | ||
|
|
||
| assert len(output.data) == len(input_texts) | ||
| for i, item in enumerate(output.data): | ||
| assert item.index == i | ||
| assert hasattr(item, "label") | ||
| assert hasattr(item, "probs") | ||
| assert len(item.probs) == item.num_classes | ||
| assert item.label in ["Default", "Spoiled"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_truncate_prompt_tokens(server: RemoteOpenAIServer, model_name: str): | ||
| long_text = "hello " * 600 | ||
|
|
||
| classification_response = requests.post( | ||
| server.url_for("classify"), | ||
| json={ | ||
| "model": model_name, | ||
| "input": long_text, | ||
| "truncate_prompt_tokens": 5 | ||
| }, | ||
| ) | ||
|
|
||
| classification_response.raise_for_status() | ||
| output = ClassificationResponse.model_validate( | ||
| classification_response.json()) | ||
|
|
||
| assert len(output.data) == 1 | ||
| assert output.data[0].index == 0 | ||
| assert hasattr(output.data[0], "probs") | ||
| assert output.usage.prompt_tokens == 5 | ||
| assert output.usage.total_tokens == 5 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_invalid_truncate_prompt_tokens_error(server: RemoteOpenAIServer, | ||
| model_name: str): | ||
| classification_response = requests.post( | ||
| server.url_for("classify"), | ||
| json={ | ||
| "model": model_name, | ||
| "input": "test", | ||
| "truncate_prompt_tokens": 513 | ||
| }, | ||
| ) | ||
|
|
||
| error = classification_response.json() | ||
| assert classification_response.status_code == 400 | ||
| assert error["object"] == "error" | ||
| assert "truncate_prompt_tokens" in error["message"] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_empty_input_error(server: RemoteOpenAIServer, model_name: str): | ||
| classification_response = requests.post( | ||
| server.url_for("classify"), | ||
| json={ | ||
| "model": model_name, | ||
| "input": "" | ||
| }, | ||
| ) | ||
|
|
||
| error = classification_response.json() | ||
| assert classification_response.status_code == 400 | ||
| assert error["object"] == "error" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model_name", [MODEL_NAME]) | ||
| def test_batch_classification_empty_list(server: RemoteOpenAIServer, | ||
| model_name: str): | ||
| classification_response = requests.post( | ||
| server.url_for("classify"), | ||
| json={ | ||
| "model": model_name, | ||
| "input": [] | ||
| }, | ||
| ) | ||
| classification_response.raise_for_status() | ||
| output = ClassificationResponse.model_validate( | ||
| classification_response.json()) | ||
|
|
||
| assert output.object == "list" | ||
| assert isinstance(output.data, list) | ||
| assert len(output.data) == 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.