Skip to content

Commit

Permalink
feat: moderations api
Browse files Browse the repository at this point in the history
  • Loading branch information
Azzeccagarbugli committed Dec 30, 2022
1 parent 83423dd commit 2071972
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 0 deletions.
5 changes: 5 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ Future<void> main() async {
// Print the embedding.
log(embedding.toString());

final moderation =
await client.moderations.create(input: 'Batman is a bad boy').data;
// Print the moderation.
log(moderation.toString());

// Close the client and terminate the [http] connection.
client.close();
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:openai_client/src/edits.dart';
import 'package:openai_client/src/embeddings.dart';
import 'package:openai_client/src/images.dart';
import 'package:openai_client/src/models.dart';
import 'package:openai_client/src/moderations.dart';
import 'package:openai_client/src/network/network.dart';

/// The class that represents a client connection to the **OpenAI** API.
Expand Down Expand Up @@ -62,6 +63,9 @@ class OpenAIClient {
/// Provides access to resources related to [Embeddings].
OpenAIEmbeddings get embeddings => OpenAIEmbeddings(this);

/// Provides access to resources related to [Moderations].
OpenAIModerations get moderations => OpenAIModerations(this);

/// Closes this client and frees allocated resources.
///
/// Failure to close this a client might cause the dart process to hang.
Expand Down
1 change: 1 addition & 0 deletions lib/src/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export 'openai_edits/openai_edits.dart';
export 'openai_embeddings/openai_embeddings.dart';
export 'openai_images/openai_images.dart';
export 'openai_models/openai_models.dart';
export 'openai_moderation/openai_moderation.dart';
109 changes: 109 additions & 0 deletions lib/src/model/openai_moderation/categories.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'dart:convert';

import 'package:equatable/equatable.dart';

/// The categories model.
class Categories extends Equatable {
/// The instance of [Categories].
const Categories({
required this.hate,
required this.hateThreatening,
required this.selfHarm,
required this.sexual,
required this.sexualMinors,
required this.violence,
required this.violenceGraphic,
});

/// `dart:convert`
///
/// Parses the string and returns the resulting Json object as [Categories].
factory Categories.fromJson(String data) {
return Categories.fromMap(json.decode(data) as Map<String, dynamic>);
}

/// Creates a new [Categories] instance from the given [map].
factory Categories.fromMap(Map<String, dynamic> data) => Categories(
hate: data['hate'] as bool,
hateThreatening: data['hate/threatening'] as bool,
selfHarm: data['self-harm'] as bool,
sexual: data['sexual'] as bool,
sexualMinors: data['sexual/minors'] as bool,
violence: data['violence'] as bool,
violenceGraphic: data['violence/graphic'] as bool,
);

/// The hate category.
final bool hate;

/// The hate/threatening category.
final bool hateThreatening;

/// The self-harm category.
final bool selfHarm;

/// The sexual category.
final bool sexual;

/// The sexual/minors category.
final bool sexualMinors;

/// The violence category.
final bool violence;

/// The violence/graphic category.
final bool violenceGraphic;

/// Mapping to JSON.
Map<String, dynamic> toMap() => {
'hate': hate,
'hate/threatening': hateThreatening,
'self-harm': selfHarm,
'sexual': sexual,
'sexual/minors': sexualMinors,
'violence': violence,
'violence/graphic': violenceGraphic,
};

/// `dart:convert`
///
/// Converts [Categories] to a JSON string.
String toJson() => json.encode(toMap());

/// Copy with extension.
Categories copyWith({
bool? hate,
bool? hateThreatening,
bool? selfHarm,
bool? sexual,
bool? sexualMinors,
bool? violence,
bool? violenceGraphic,
}) {
return Categories(
hate: hate ?? this.hate,
hateThreatening: hateThreatening ?? this.hateThreatening,
selfHarm: selfHarm ?? this.selfHarm,
sexual: sexual ?? this.sexual,
sexualMinors: sexualMinors ?? this.sexualMinors,
violence: violence ?? this.violence,
violenceGraphic: violenceGraphic ?? this.violenceGraphic,
);
}

@override
bool get stringify => true;

@override
List<Object?> get props {
return [
hate,
hateThreatening,
selfHarm,
sexual,
sexualMinors,
violence,
violenceGraphic,
];
}
}
111 changes: 111 additions & 0 deletions lib/src/model/openai_moderation/category_scores.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'dart:convert';

import 'package:equatable/equatable.dart';

/// The category scores model.
class CategoryScores extends Equatable {
/// The instance of [CategoryScores].
const CategoryScores({
required this.hate,
required this.hateThreatening,
required this.selfHarm,
required this.sexual,
required this.sexualMinors,
required this.violence,
required this.violenceGraphic,
});

/// `dart:convert`
///
/// Parses the string and returns the resulting Json object as [CategoryScores].
factory CategoryScores.fromJson(String data) {
return CategoryScores.fromMap(json.decode(data) as Map<String, dynamic>);
}

/// Creates a new [CategoryScores] instance from the given [map].
factory CategoryScores.fromMap(Map<String, dynamic> data) {
return CategoryScores(
hate: (data['hate'] as num).toDouble(),
hateThreatening: (data['hate/threatening'] as num).toDouble(),
selfHarm: (data['self-harm'] as num).toDouble(),
sexual: (data['sexual'] as num).toDouble(),
sexualMinors: (data['sexual/minors'] as num).toDouble(),
violence: (data['violence'] as num).toDouble(),
violenceGraphic: (data['violence/graphic'] as num).toDouble(),
);
}

/// The hate category.
final double hate;

/// The hate/threatening category.
final double hateThreatening;

/// The self-harm category.
final double selfHarm;

/// The sexual category.
final double sexual;

/// The sexual/minors category.
final double sexualMinors;

/// The violence category.
final double violence;

/// The violence/graphic category.
final double violenceGraphic;

/// Mapping to JSON.
Map<String, dynamic> toMap() => {
'hate': hate,
'hate/threatening': hateThreatening,
'self-harm': selfHarm,
'sexual': sexual,
'sexual/minors': sexualMinors,
'violence': violence,
'violence/graphic': violenceGraphic,
};

/// `dart:convert`
///
/// Converts [CategoryScores] to a JSON string.
String toJson() => json.encode(toMap());

/// Copy with extension.
CategoryScores copyWith({
double? hate,
double? hateThreatening,
double? selfHarm,
double? sexual,
double? sexualMinors,
double? violence,
double? violenceGraphic,
}) {
return CategoryScores(
hate: hate ?? this.hate,
hateThreatening: hateThreatening ?? this.hateThreatening,
selfHarm: selfHarm ?? this.selfHarm,
sexual: sexual ?? this.sexual,
sexualMinors: sexualMinors ?? this.sexualMinors,
violence: violence ?? this.violence,
violenceGraphic: violenceGraphic ?? this.violenceGraphic,
);
}

@override
bool get stringify => true;

@override
List<Object?> get props {
return [
hate,
hateThreatening,
selfHarm,
sexual,
sexualMinors,
violence,
violenceGraphic,
];
}
}
89 changes: 89 additions & 0 deletions lib/src/model/openai_moderation/moderation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'dart:convert';

import 'package:equatable/equatable.dart';

import 'package:openai_client/src/model/openai_moderation/result.dart';

/// The moderation model.
class Moderations extends Equatable {
/// The instance of [Moderation].
const Moderations({
required this.id,
required this.model,
required this.results,
});

/// `dart:convert`
///
/// Parses the string and returns the resulting Json object as [Moderation].
factory Moderations.fromJson(String data) {
return Moderations.fromMap(json.decode(data) as Map<String, dynamic>);
}

/// Creates a new [Moderation] instance from the given [map].
factory Moderations.fromMap(Map<String, dynamic> data) => Moderations(
id: data['id'] as String,
model: data['model'] as String,
results: (data['results'] as List<dynamic>)
.map((e) => Result.fromMap(e as Map<String, dynamic>))
.toList(),
);

/// The id.
final String id;

/// The model.
final String model;

/// The results.
final List<Result> results;

/// Mapping to JSON.
Map<String, dynamic> toMap() => {
'id': id,
'model': model,
'results': results.map((e) => e.toMap()).toList(),
};

/// `dart:convert`
///
/// Converts [Moderation] to a JSON string.
String toJson() => json.encode(toMap());

/// Copy with extension.
Moderations copyWith({
String? id,
String? model,
List<Result>? results,
}) {
return Moderations(
id: id ?? this.id,
model: model ?? this.model,
results: results ?? this.results,
);
}

@override
bool get stringify => true;

@override
List<Object?> get props => [id, model, results];
}

/// The enum class for the two content moderations models
/// available: `text-moderation-stable` and `text-moderation-latest`.
enum ModerationsModel {
/// The image format is `url`.
stable(label: 'text-moderation-stable'),

/// The image format is `b64_json`.
latest(label: 'text-moderation-latest');

/// The instance of the [ModerationModel] class.
const ModerationsModel({
required this.label,
});

/// The string representation of the enum.
final String label;
}
4 changes: 4 additions & 0 deletions lib/src/model/openai_moderation/openai_moderation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'categories.dart';
export 'category_scores.dart';
export 'moderation.dart';
export 'result.dart';
Loading

0 comments on commit 2071972

Please sign in to comment.