or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

agents.mdaudio.mdbatch.mdbeta.mdchat-completions.mdclassification.mdembeddings.mdfiles.mdfim.mdfine-tuning.mdindex.mdmodels.mdocr.md

classification.mddocs/

0

# Content Classification

1

2

Moderate content and classify text using Mistral's safety and classification models. These capabilities help ensure content safety and enable automated text categorization.

3

4

## Capabilities

5

6

### Content Moderation

7

8

Analyze content for safety and policy compliance.

9

10

```python { .api }

11

def moderate(

12

inputs: List[Union[str, Dict]],

13

model: Optional[str] = None,

14

**kwargs

15

) -> ModerationResponse:

16

"""

17

Moderate content for safety compliance.

18

19

Parameters:

20

- inputs: List of text strings or message objects to moderate

21

- model: Optional moderation model identifier

22

23

Returns:

24

ModerationResponse with safety analysis results

25

"""

26

```

27

28

### Text Classification

29

30

Classify text into predefined categories or custom classes.

31

32

```python { .api }

33

def classify(

34

inputs: List[str],

35

model: str,

36

**kwargs

37

) -> ClassificationResponse:

38

"""

39

Classify text inputs.

40

41

Parameters:

42

- inputs: List of text strings to classify

43

- model: Classification model identifier

44

45

Returns:

46

ClassificationResponse with classification results

47

"""

48

```

49

50

## Usage Examples

51

52

### Content Moderation

53

54

```python

55

from mistralai import Mistral

56

57

client = Mistral(api_key="your-api-key")

58

59

# Moderate text content

60

texts_to_moderate = [

61

"This is a normal message about cooking recipes.",

62

"Let me share some travel tips for your vacation.",

63

"Here's how to build a simple web application."

64

]

65

66

response = client.classifiers.moderate(

67

inputs=texts_to_moderate

68

)

69

70

# Check moderation results

71

for i, result in enumerate(response.results):

72

print(f"Text {i + 1}:")

73

print(f" Safe: {not result.flagged}")

74

75

if result.flagged:

76

print(f" Flagged categories: {[cat.category for cat in result.categories if cat.flagged]}")

77

78

print(f" Text: {texts_to_moderate[i][:50]}...")

79

print()

80

```

81

82

### Chat Message Moderation

83

84

```python

85

from mistralai.models import UserMessage, AssistantMessage

86

87

# Moderate conversation messages

88

messages = [

89

{"role": "user", "content": "How do I bake a chocolate cake?"},

90

{"role": "assistant", "content": "Here's a simple chocolate cake recipe..."},

91

{"role": "user", "content": "Can you suggest healthy cooking alternatives?"}

92

]

93

94

response = client.classifiers.moderate(inputs=messages)

95

96

# Process results

97

for i, result in enumerate(response.results):

98

message = messages[i]

99

print(f"Message from {message['role']}:")

100

print(f" Content safe: {not result.flagged}")

101

102

if result.flagged:

103

print(f" Issues found: {[cat.category for cat in result.categories if cat.flagged]}")

104

```

105

106

### Text Classification

107

108

```python

109

# Classify text into categories

110

texts = [

111

"The stock market showed significant gains today with tech shares leading.",

112

"Scientists discovered a new species of butterfly in the Amazon rainforest.",

113

"The latest smartphone model features improved camera and battery life.",

114

"Local community center offers free coding classes for children."

115

]

116

117

response = client.classifiers.classify(

118

inputs=texts,

119

model="classification-model"

120

)

121

122

# Review classifications

123

for i, prediction in enumerate(response.predictions):

124

print(f"Text {i + 1}: {texts[i][:50]}...")

125

print(f" Category: {prediction.category}")

126

print(f" Confidence: {prediction.confidence:.3f}")

127

print()

128

```

129

130

## Types

131

132

### Moderation Types

133

134

```python { .api }

135

class ChatModerationRequest:

136

inputs: List[Union[str, Dict]]

137

model: Optional[str]

138

139

class ModerationResponse:

140

id: str

141

model: str

142

results: List[ModerationObject]

143

144

class ModerationObject:

145

flagged: bool

146

categories: List[ModerationCategory]

147

148

class ModerationCategory:

149

category: str

150

flagged: bool

151

score: float

152

```

153

154

### Classification Types

155

156

```python { .api }

157

class ClassificationRequest:

158

inputs: List[str]

159

model: str

160

161

class ClassificationResponse:

162

id: str

163

object: str

164

model: str

165

predictions: List[Prediction]

166

167

class Prediction:

168

category: str

169

confidence: float

170

171

class ClassificationTargetResult:

172

category: str

173

score: float

174

```

175

176

### Content Categories

177

178

Common moderation categories include:

179

- **harassment**: Harassment and bullying content

180

- **hate**: Hate speech and discrimination

181

- **self-harm**: Self-harm and suicide content

182

- **sexual**: Sexual content

183

- **violence**: Violence and graphic content

184

- **illegal**: Illegal activities

185

186

## Usage Guidelines

187

188

### Content Moderation Best Practices

189

190

- **Batch Processing**: Process multiple texts together for efficiency

191

- **Threshold Tuning**: Adjust sensitivity based on your application needs

192

- **Human Review**: Consider human review for edge cases

193

- **Context Awareness**: Account for context in moderation decisions

194

195

### Classification Best Practices

196

197

- **Model Selection**: Choose appropriate models for your domain

198

- **Confidence Thresholds**: Set appropriate confidence thresholds for decisions

199

- **Category Definitions**: Ensure clear category definitions for consistent results

200

- **Evaluation**: Regularly evaluate classification accuracy on your data

201

202

### Integration Patterns

203

204

- **Pre-publishing**: Moderate content before publication

205

- **Real-time**: Moderate content in real-time conversations

206

- **Batch Review**: Process existing content in batches

207

- **Automated Workflows**: Integrate with content management systems