A dart client for using the Cloudflare Workers AI APIs
BSD-3-CLAUSE License
A Dart client for accessing the Cloudflare AI API.
Run this command:
dart pub add cloudflare_ai
or
Add this to your package's pubspec.yaml file:
dependencies:
cloudflare_ai: ^1.3.5
import 'package:cloudflare_ai/cloudflare_ai.dart';
void main() async {
// Initialize a TextGenerationModel
TextGenerationModel model = TextGenerationModel(
accountId: "Your Account ID",
apiKey: "Your API Key",
model: TextGenerationModels.GEMMA_7B_IT,
);
// Generate Text for a prompt
TextGenerationResponse res = await model
.generateText("Write a story about a robot, living on the moon");
if (res.success && res.result != null) {
print(res.result?.response);
} else {
print(res.errors);
}
}
Supported Models:
import 'package:cloudflare_ai/cloudflare_ai.dart';
void main() async {
// Initialize a TextSummarizationModel
TextSummarizationModel model = TextSummarizationModel(
accountId: "Your Account ID",
apiKey: "Your API Key",
model: TextSummarizationModels.BART_LARGE_CNN,
);
// Summarize Text
TextSummarizationResponse res = await model.summarize(
"The quick brown fox jumps over the lazy dog",
maxLength: 10,
);
if (res.success && res.result != null) {
print(res.result?.response);
} else {
print(res.errors);
}
}
Supported Models:
import 'package:cloudflare_ai/cloudflare_ai.dart';
void main() async {
// Initialize a TextToImageModel
TextToImageModel model = TextToImageModel(
accountId: "Your Account ID",
apiKey: "Your API Key"
model: TextToImageModels.DREAMSHAPER_8_LCM,
);
// Generate Image from Text
TextToImageResponse res = await model.generateImage("A beautiful sunset over the ocean");
// Save Image to File
res.saveImage("sunset.jpg");
}
Supported Models:
import 'package:cloudflare_ai/cloudflare_ai.dart';
void main() async {
// Initialize a TextClassificationModel
TextClassificationModel model = TextClassificationModel(
accountId: "Your Account ID",
apiKey: "Your API Key"
model: TextClassificationModels.DISTILBERT_SST_2_INT8,
);
// Classify Text
TextClassificationResponse res = await model.classifyText("A beautiful sunset over the ocean");
if (res.success && res.result != null) {
print(
'Positive Confidence level: ${res.result?.positive}');
print(
'Negative Confidence level: ${res.result?.negative}');
} else {
print(res.errors);
}
}
Supported Models:
import 'package:cloudflare_ai/cloudflare_ai.dart';
void main() async {
// Initialize a LanguageTranslationModel
LanguageTranslationModel model = LanguageTranslationModel(
accountId: "Your Account ID",
apiKey: "Your API Key",
model: LanguageTranslationModels.M2M100_1_2B,
);
// Translate Text
LanguageTranslationResponse res = await model.translate("A beautiful sunset over the ocean", Languages.English, Languages.French);
if (res.success && res.result != null) {
print(res.result?.response);
} else {
print(res.errors);
}
}
Supported Models:
import 'package:cloudflare_ai/cloudflare_ai.dart';
void main() async {
// Initialize a TextChatModel
TextChatModel model = TextChatModel(
accountId: "Your Account ID",
apiKey: "Your API Key",
model: TextChatModels.GEMMA_7B_IT,
);
// Load previous messages
model.loadMessages([
{
"role": "user",
"content": "Hello!",
},
{
"role": "assistant",
"content": "Hello! How may I help you?",
},
]);
// Start Chat
ChatMessage message = await model.chat("Hello");
print(message.content);
}
Supported Models: Same as Text Generation Models