0
# Text Classification
1
2
Text classification functionality with predefined class labels, supporting various pooling strategies and confidence scoring for categorization tasks.
3
4
## Capabilities
5
6
### Text Classification
7
8
Classify text into predefined categories with confidence scores and probability distributions.
9
10
```python { .api }
11
def classify(
12
self,
13
prompts: Union[PromptType, Sequence[PromptType]],
14
*,
15
use_tqdm: Union[bool, Callable[..., tqdm]] = True,
16
pooling_params: Optional[Union[PoolingParams, Sequence[PoolingParams]]] = None,
17
lora_request: Optional[Union[List[LoRARequest], LoRARequest]] = None
18
) -> List[ClassificationRequestOutput]:
19
"""
20
Classify text into predefined categories.
21
22
Parameters:
23
- prompts: Input text to classify
24
- use_tqdm: Show progress bar (keyword-only)
25
- pooling_params: Pooling configuration (keyword-only)
26
- lora_request: LoRA adapter configuration (keyword-only)
27
28
Returns:
29
List of ClassificationRequestOutput with predictions
30
31
Note: This method requires a model that supports classification tasks.
32
The class labels are determined by the model's configuration, not passed as parameters.
33
"""
34
```
35
36
## Usage Examples
37
38
### Basic Text Classification
39
40
```python
41
from vllm import LLM, PoolingParams
42
43
# Use a classification model that has predefined classes
44
llm = LLM(model="cardiffnlp/twitter-roberta-base-sentiment-latest")
45
46
texts = [
47
"I love this product! It's amazing.",
48
"This is terrible, worst purchase ever.",
49
"It's okay, not great but not bad."
50
]
51
52
pooling_params = PoolingParams(pooling_type="MEAN")
53
54
outputs = llm.classify(texts, pooling_params=pooling_params)
55
56
for output in outputs:
57
result = output.outputs
58
print(f"Text: {texts[outputs.index(output)]}")
59
print(f"Predicted probabilities: {result.probs}")
60
```
61
62
## Types
63
64
```python { .api }
65
class ClassificationRequestOutput:
66
id: str
67
outputs: ClassificationOutput
68
prompt_token_ids: List[int]
69
finished: bool
70
71
class ClassificationOutput:
72
probs: List[float] # Class probabilities
73
```