Skip to content

Commit

Permalink
feat: Better documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddhPurohit committed May 22, 2024
1 parent 2157951 commit 9218833
Show file tree
Hide file tree
Showing 24 changed files with 1,559 additions and 574 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@

## 1.3.1

- Added documentation for TextChat
- Added documentation for TextChat

## 1.3.2

- Added documentation

## 1.3.3

- Added better documentation
207 changes: 143 additions & 64 deletions example/cloudflare_ai_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,121 +4,196 @@ import 'package:cloudflare_ai/cloudflare_ai.dart';
import 'package:cloudflare_ai/src/text_chat/text_chat.dart';

void main() async {
String accountId = "Your Account ID"; // Your Account ID
String apiKey = "Your API Key"; // Your API Key
String accountId = "Your Account ID";

// Text Generation
// Initialize a TextGenerationModel
/// Your Account ID
String apiKey = "Your API Key";

/// Your API Key
/// Text Generation
/// Initialize a TextGenerationModel
TextGenerationModel textGenModel = TextGenerationModel(
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextGenerationModels.GEMMA_7B_IT, // Model to use
accountId: accountId,

/// Account ID
apiKey: apiKey,

/// API Key
model: TextGenerationModels.GEMMA_7B_IT,

/// Model to use
);

// Generate Text for a prompt
/// Generate Text for a prompt
TextGenerationResponse textGenRes = await textGenModel
.generateText("Write a story about a robot, living on the moon"); // Prompt
.generateText("Write a story about a robot, living on the moon");

/// Prompt
if (textGenRes.success && textGenRes.result != null) {
print(textGenRes.result?.response); // Print the generated text if request is successful
print(textGenRes.result?.response);

/// Print the generated text if request is successful
} else {
print(textGenRes..errors.map((e) => e.toJson()).toList()); // Print the errors if request is unsuccessful
print(textGenRes..errors.map((e) => e.toJson()).toList());

/// Print the errors if request is unsuccessful
}

// Text Summarization
// Initialize a TextSummarizationModel
/// Text Summarization
/// Initialize a TextSummarizationModel
TextSummarizationModel textSummarizationModel = TextSummarizationModel(
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextSummarizationModels
.BART_LARGE_CNN, // Bart Large CNN is default, hence this parameter is optional
accountId: accountId,

/// Account ID
apiKey: apiKey,

/// API Key
model: TextSummarizationModels.BART_LARGE_CNN,

/// Bart Large CNN is default, hence this parameter is optional
);

// Summarize Text
/// Summarize Text
TextSummarizationResponse textSummarizationRes =
await textSummarizationModel.summarize(
"Your very long text....", // Text to summarize
maxLength: 1024, // 1024 is default, hence this parameter is optional
"Your very long text....",

/// Text to summarize
maxLength: 1024,

/// 1024 is default, hence this parameter is optional
);

if (textSummarizationRes.success && textSummarizationRes.result != null) {
print(textSummarizationRes.result?.response); // Print the summarized text
print(textSummarizationRes.result?.response);

/// Print the summarized text
} else {
print(textSummarizationRes..errors.map((e) => e.toJson()).toList()); // Print the errors
print(textSummarizationRes..errors.map((e) => e.toJson()).toList());

/// Print the errors
}

// Text to Image
// Initialize a TextToImageModel
/// Text to Image
/// Initialize a TextToImageModel
TextToImageModel textToImageModel = TextToImageModel(
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextToImageModels.DREAMSHAPER_8_LCM, // Define the model
accountId: accountId,

/// Account ID
apiKey: apiKey,

/// API Key
model: TextToImageModels.DREAMSHAPER_8_LCM,

/// Define the model
);

// Generate Image
/// Generate Image
Uint8List textToImageResult =
await textToImageModel.generateImage("An alien on the moon"); // Prompt
await textToImageModel.generateImage("An alien on the moon");

/// Prompt
// Save the image to a file
File("image.png").writeAsBytes(textToImageResult); // Save the image to a file
/// Save the image to a file
File("image.png").writeAsBytes(textToImageResult);

// Text Classification
// Initialize a TextClassificationModel
/// Save the image to a file
/// Text Classification
/// Initialize a TextClassificationModel
TextClassificationModel textClassificationModel = TextClassificationModel(
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextClassificationModels
.DISTILBERT_SST_2_INT8 // DISTILBERT_SST_2_INT8 is default, hence this parameter is optional
accountId: accountId,

/// Account ID
apiKey: apiKey,

/// API Key
model: TextClassificationModels.DISTILBERT_SST_2_INT8

/// DISTILBERT_SST_2_INT8 is default, hence this parameter is optional
);

// Classify Text
/// Classify Text
TextClassificationResponse textClassificationResponse =
await textClassificationModel.classifyText(
"Test Prompt", // Text to classify
); // Classify the text
"Test Prompt",

/// Text to classify
);

/// Classify the text
if (textClassificationResponse.success &&
textClassificationResponse.result != null) {
print(
'Positive Confidence level: ${textClassificationResponse.result?.positive}'); // Print the positive confidence level if request is successful
'Positive Confidence level: ${textClassificationResponse.result?.positive}');

/// Print the positive confidence level if request is successful
print(
'Negative Confidence level: ${textClassificationResponse.result?.negative}'); // Print the negative confidence level if request is successful
'Negative Confidence level: ${textClassificationResponse.result?.negative}');

/// Print the negative confidence level if request is successful
} else {
print(textSummarizationRes..errors.map((e) => e.toJson()).toList()); // Print the errors if request is unsuccessful
print(textSummarizationRes..errors.map((e) => e.toJson()).toList());

/// Print the errors if request is unsuccessful
}

// Language Translation
// Initialize a LanguageTranslationModel
/// Language Translation
/// Initialize a LanguageTranslationModel
LanguageTranslationModel languageTranslationModel = LanguageTranslationModel(
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: LanguageTranslationModels
.M2M100_1_2B, // M2M100_1_2B is default, hence this parameter is optional
accountId: accountId,

/// Account ID
apiKey: apiKey,

/// API Key
model: LanguageTranslationModels.M2M100_1_2B,

/// M2M100_1_2B is default, hence this parameter is optional
);

// Translate Text
/// Translate Text
LanguageTranslationResponse languageTranslationRes =
await languageTranslationModel.translate(
"Hello", // Text to translate
Languages.English, // Language to translate from
Languages.Hindi, // Language to translate to
"Hello",

/// Text to translate
Languages.English,

/// Language to translate from
Languages.Hindi,

/// Language to translate to
);

if (languageTranslationRes.success && languageTranslationRes.result != null) {
print(languageTranslationRes.result?.response); // Print the translated text if request is successful
print(languageTranslationRes.result?.response);

/// Print the translated text if request is successful
} else {
print(languageTranslationRes..errors.map((e) => e.toJson()).toList()); // Print the errors if request is unsuccessful
print(languageTranslationRes..errors.map((e) => e.toJson()).toList());

/// Print the errors if request is unsuccessful
}

// Text Chat
// Initialize a TextChatModel
/// Text Chat
/// Initialize a TextChatModel
TextChatModel textChatModel = TextChatModel(
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextChatModels.GEMMA_7B_IT, // Model to use
accountId: accountId,

/// Account ID
apiKey: apiKey,

/// API Key
model: TextChatModels.GEMMA_7B_IT,

/// Model to use
);

// Load any previous conversations
/// Load any previous conversations
textChatModel.loadMessages([
{
"role": "user",
Expand All @@ -130,8 +205,12 @@ void main() async {
},
]);

// Send a new message
ChatMessage chatRes = await textChatModel.chat("Who are you?"); // Message
/// Send a new message
ChatMessage chatRes = await textChatModel.chat("Who are you?");

/// Message
print(chatRes.content);

print(chatRes.content); // Print the response
/// Print the response
}
73 changes: 53 additions & 20 deletions lib/src/language_translation/language_translation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,73 @@ export 'response.dart';
export 'languages.dart';

class LanguageTranslationModel {
late String accountId; // Account ID, available on Cloudflare daskboard
late String apiKey; // API Key, can by generated from the Cloudflare dashboard
late LanguageTranslationModels model; // The model to use
NetworkService networkService = NetworkService(); // Network service object
late String baseUrl; // Base URL for the API
late String accountId;

/// Account ID, available on Cloudflare daskboard
late String apiKey;

/// API Key, can by generated from the Cloudflare dashboard
late LanguageTranslationModels model;

/// The model to use
NetworkService networkService = NetworkService();

/// Network service object
late String baseUrl;

/// Base URL for the API
LanguageTranslationModel({
required this.accountId,
required this.apiKey,
required this.accountId,
required this.apiKey,
this.model = LanguageTranslationModels.M2M100_1_2B,
}) {
baseUrl = "https://api.cloudflare.com/client/v4/accounts/$accountId/ai/run";
baseUrl =
"https:///api.cloudflare.com/client/v4/accounts/$accountId/ai/run";
if (accountId.trim() == "") {
throw Exception("Account ID cannot be empty"); // Throw an exception if account ID is empty
throw Exception("Account ID cannot be empty");

/// Throw an exception if account ID is empty
}
if (apiKey.trim() == "") {
throw Exception("API Key cannot be empty"); // Throw an exception if API key is empty
throw Exception("API Key cannot be empty");

/// Throw an exception if API key is empty
}
}

// Asynchronous function which returns summarized text through the TextSummarizationResponse object
/// Asynchronous function which returns summarized text through the TextSummarizationResponse object
Future<LanguageTranslationResponse> translate(
String text, // Text to translate
Languages from, // Language to translate from
Languages to, // Language to translate to
String text,

/// Text to translate
Languages from,

/// Language to translate from
Languages to,

/// Language to translate to
) async {
Map<String, dynamic> res =
await networkService.post("$baseUrl/${model.value}", apiKey, {
"source_lang": from.value, // Source language
"target_lang": to.value, // Target language
"text": text, // Text to translate
}); // Post request to the API
"source_lang": from.value,

/// Source language
"target_lang": to.value,

/// Target language
"text": text,

/// Text to translate
});

/// Post request to the API
LanguageTranslationResponse response =
LanguageTranslationResponse.fromJson(res['data']); // Create a response object from the JSON data
return response; // Return the response object
LanguageTranslationResponse.fromJson(res['data']);

/// Create a response object from the JSON data
return response;

/// Return the response object
}
}
Loading

0 comments on commit 9218833

Please sign in to comment.