-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
681ec26
commit 90b12de
Showing
10 changed files
with
253 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,3 +189,4 @@ Supported Models: | |
- [x] Image Generation | ||
- [x] Text Classification | ||
- [x] Language Translation | ||
- [x] Text Chat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
enum Role { | ||
system, | ||
user, | ||
assistant; | ||
} | ||
|
||
class ChatMessage { | ||
late Role role; | ||
late String content; | ||
|
||
ChatMessage({ | ||
required this.role, | ||
required this.content, | ||
}); | ||
|
||
ChatMessage.fromJson(json) { | ||
switch (json['role']) { | ||
case 'system': | ||
role = Role.assistant; | ||
break; | ||
case 'user': | ||
role = Role.user; | ||
break; | ||
case 'assistant': | ||
role = Role.assistant; | ||
break; | ||
default: | ||
throw Exception("Invalid role"); | ||
} | ||
content = json['content']; | ||
} | ||
|
||
Map toJson() { | ||
Map res = {}; | ||
res['role'] = role.name; | ||
res['content'] = content; | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Defines the models, and specifies path to them in the API Endpoint | ||
enum TextChatModels { | ||
LLAMA_2_7B("@cf/meta/llama-2-7b-chat-fp16"), | ||
LLAMA_2_7B_INT8("@cf/meta/llama-2-7b-chat-int8"), | ||
MISTRAL_7B("@cf/mistral/mistral-7b-instruct-v0.1"), | ||
CODE_LLAMA_7B("@hf/thebloke/codellama-7b-instruct-awq"), | ||
CODE_LLAMA_2_13B("@hf/thebloke/llama-2-13b-chat-awq"), | ||
ZEPHYR_7B("@hf/thebloke/zephyr-7b-beta-awq"), | ||
MISTRAL_7B_AWQ_V01("@hf/thebloke/mistral-7b-instruct-v0.1-awq"), | ||
MISTRAL_7B_AWQ_V02("@hf/mistral/mistral-7b-instruct-v0.2"), | ||
OPENHERMES_MISTRAL_7B("@hf/thebloke/openhermes-2.5-mistral-7b-awq"), | ||
NEURAL_CHAT_7B("@hf/thebloke/neural-chat-7b-v3-1-awq"), | ||
LLAMA_GUARD_7B("@hf/thebloke/llamaguard-7b-awq"), | ||
DEEPSEEK_CODER_6_7_BASE("@hf/thebloke/deepseek-coder-6.7b-base-awq"), | ||
DEEPSEEK_CODER_6_7_INSTRUCT("@hf/thebloke/deepseek-coder-6.7b-instruct-awq"), | ||
DEEPSEEK_MATH_7B_BASE("@cf/deepseek-ai/deepseek-math-7b-base"), | ||
DEEPSEEK_MATH_7B_INSTRUCT("@cf/deepseek-ai/deepseek-math-7b-instruct"), | ||
OPENCHAT_3_5("@cf/openchat/openchat-3.5-0106"), | ||
PHI_2("@cf/phi/phi-2"), | ||
TINYLAMA_1_1B("@cf/tinyllama/tinyllama-1.1b-chat-v1.0"), | ||
DISCOLM_GERMAN_7B("@cf/thebloke/discolm-german-7b-v1-awq"), | ||
QWEN_1_5_0_5B_CHAT("@cf/qwen/qwen-1.5.0.5b-chat"), | ||
QWEN1_5_1_8B_CHAT("@cf/qwen/qwen1.5-1.8b-chat"), | ||
QWEN_1_5_7B_CHAT_AWQ("@cf/qwen/qwen1.5-7b-chat-awq"), | ||
QWEN_1_5_14B_CHAT_AWQ("@cf/qwen/qwen1.5-14b-chat-awq"), | ||
FALCON_7B_INSTRUCT("@cf/tiiuae/falcon-7b-instruct"), | ||
GEMMA_2B_IT_LORA("@cf/google/gemma-2b-it-lora"), | ||
GEMMA_7B_IT("@hf/google/gemma-7b-it"), | ||
GEMMA_7B_IT_LORA("@cf/google/gemma-7b-it-lora"), | ||
HERMES_2_PRO_7B("@hf/nousresearch/hermes-2-pro-mistral-7b"), | ||
LLAMA_2_7B_CHAT_HF_LORA("@cf/meta-llama/llama-2-7b-chat-hf-lora"), | ||
LLAMA_3_8B_INSTRUCT("@hf/meta-llama/meta-llama-3-8b-instruct"), | ||
UNA_CYBERTRON_7B_V2_BF16("@cf/fblgit/una-cybertron-7b-v2-bf16"), | ||
STARLING_LM_7B_BETA("@hf/nexusflow/starling-lm-7b-beta"), | ||
SQL_CODER_7B_2("@cf/defog/sqlcoder-7b-2"); | ||
|
||
const TextChatModels(this.value); | ||
final String value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Defines Repsonse received from the API | ||
import '../models/error.dart'; | ||
|
||
class TextChatResponse { | ||
late TextChatResult? result; | ||
late bool success; | ||
late List<ErrorModel> errors; | ||
late List messages; | ||
|
||
TextChatResponse({ | ||
required this.result, | ||
required this.success, | ||
errors, | ||
messages, | ||
}) { | ||
this.errors = errors ?? []; | ||
this.messages = messages ?? []; | ||
} | ||
|
||
TextChatResponse.fromJson(Map<String, dynamic> json) { | ||
result = TextChatResult.fromJson(json['result']); | ||
success = json['success']; | ||
errors = | ||
(json['errors'] as List).map((e) => ErrorModel.fromJson(e)).toList(); | ||
messages = json['messages']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = <String, dynamic>{}; | ||
data['result'] = result?.toJson() ?? {}; | ||
data['success'] = success; | ||
data['errors'] = errors.map((e) => e.toJson()).toList(); | ||
data['messages'] = messages; | ||
return data; | ||
} | ||
} | ||
|
||
class TextChatResult { | ||
String? response; | ||
|
||
TextChatResult({this.response}); | ||
|
||
TextChatResult.fromJson(Map<String, dynamic> json) { | ||
response = json['response']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = <String, dynamic>{}; | ||
data['response'] = response; | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'chat_message.dart'; | ||
import 'models.dart'; | ||
import 'response.dart'; | ||
import '../services/network_service.dart'; | ||
export 'models.dart'; | ||
export 'response.dart'; | ||
export 'chat_message.dart'; | ||
|
||
class TextChatModel { | ||
late String accountId; | ||
late String apiKey; | ||
late TextChatModels model; | ||
late bool raw; | ||
List<ChatMessage> _messages = []; | ||
NetworkService networkService = NetworkService(); | ||
late String baseUrl; | ||
|
||
TextChatModel({ | ||
required this.accountId, | ||
required this.apiKey, | ||
required this.model, | ||
this.raw = true, | ||
}) { | ||
baseUrl = "https://api.cloudflare.com/client/v4/accounts/$accountId/ai/run"; | ||
if (accountId.trim() == "") { | ||
throw Exception("Account ID cannot be empty"); | ||
} | ||
if (apiKey.trim() == "") { | ||
throw Exception("API Key cannot be empty"); | ||
} | ||
} | ||
|
||
// Get all chat messages | ||
List<ChatMessage> get allMessages => _messages; | ||
|
||
// Load a previous chat | ||
void loadMessages(List<Map<String, dynamic>> messages) { | ||
_messages = | ||
messages.map((message) => ChatMessage.fromJson(message)).toList(); | ||
} | ||
|
||
// Asynchronous function which returns the classification labels with their confidence of the text | ||
Future<ChatMessage> chat(String message) async { | ||
_messages.add( | ||
ChatMessage( | ||
role: Role.user, | ||
content: message, | ||
), | ||
); | ||
Map<String, dynamic> res = | ||
await networkService.post("$baseUrl/${model.value}", apiKey, { | ||
"messages": _messages, | ||
}); | ||
TextChatResponse response = TextChatResponse.fromJson(res['data']); | ||
if (!response.success || response.result == null) { | ||
throw Exception(response.errors); | ||
} | ||
_messages.add( | ||
ChatMessage( | ||
role: Role.assistant, | ||
content: response.result?.response ?? "", | ||
), | ||
); | ||
return _messages.last; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters