Label Based Classification
The classify()
function is used to classify text inputs based on a set of labels. The function can be used for both single label and multi label classification.
Single Label Classification
import zyx
inputs = [
"I love programming in Python",
"I like french fries"
]
labels = [
"code",
"food"
]
result = zyx.classify(
inputs = inputs,
labels = labels,
)
print(result)
Multi Label Classification
from zyx import classify
inputs = [
"I love programming in Python and food",
"I like french fries"
]
labels = [
"code",
"food"
]
result = classify(
inputs = inputs,
labels = labels,
classification="multi",
model="openai/gpt-4o",
)
print(result)
MultiClassificationResult(text='I love programming in Python and food', labels=['code', 'food']),
MultiClassificationResult(text='I like french fries', labels=['food'])
API Reference
Classifies given input(s) into one or more of the provided labels.
Examples:
>>> classify(["I love programming in Python", "I like french fries", "I love programming in Julia"], ["code", "food"], classification = "single", batch_size = 2, verbose = True)
[
ClassificationResult(text="I love programming in Python", label="code"),
ClassificationResult(text="I like french fries", label="food")
]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Union[str, List[str]]
|
The input text to classify. |
required |
labels
|
List[str]
|
The list of labels to classify the input text into. |
required |
classification
|
Literal['single', 'multi']
|
The type of classification to perform. Defaults to "single". |
'single'
|
n
|
int
|
The number of classifications to generate. Defaults to 1. |
1
|
batch_size
|
int
|
The number of inputs to classify at a time. Defaults to 3. |
3
|
model
|
str
|
The model to use for classification. Defaults to "gpt-4o-mini". |
'gpt-4o-mini'
|
api_key
|
Optional[str]
|
The API key to use for OpenAI. Defaults to None. |
None
|
base_url
|
Optional[str]
|
The base URL for the OpenAI API. Defaults to None. |
None
|
organization
|
Optional[str]
|
The organization to use for OpenAI. Defaults to None. |
None
|
mode
|
InstructorMode
|
The mode to use for classification. Defaults to "tool_call". |
'tool_call'
|
temperature
|
Optional[float]
|
The temperature to use for classification. Defaults to None. |
None
|
client
|
Optional[Literal['openai', 'litellm']]
|
The client to use for classification. Defaults to None. |
None
|
verbose
|
bool
|
Whether to print verbose output. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
List
|
List[Union[ClassificationResult, MultiClassificationResult]]: The classifications generated. |
Source code in zyx/resources/completions/base/classify.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|