forked from bravekingzhang/flutter_chat_box
-
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
18c4366
commit 37d98b4
Showing
10 changed files
with
178 additions
and
66 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_chatgpt/data/llm.dart'; | ||
import 'package:flutter_chatgpt/repository/conversation.dart'; | ||
|
||
class ChatGlM extends LLM { | ||
@override | ||
getResponse( | ||
List<Message> messages, | ||
ValueChanged<Message> onResponse, | ||
ValueChanged<Message> errorCallback, | ||
ValueChanged<Message> onSuccess) async { | ||
// TODO: implement getResponse | ||
throw UnimplementedError(); | ||
} | ||
} |
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,15 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_chatgpt/data/llm.dart'; | ||
import 'package:flutter_chatgpt/repository/conversation.dart'; | ||
|
||
class ChatIF extends LLM { | ||
@override | ||
getResponse( | ||
List<Message> messages, | ||
ValueChanged<Message> onResponse, | ||
ValueChanged<Message> errorCallback, | ||
ValueChanged<Message> onSuccess) async { | ||
// TODO: implement getResponse | ||
throw UnimplementedError(); | ||
} | ||
} |
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,76 @@ | ||
import 'package:dart_openai/openai.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter_chatgpt/cubit/setting_cubit.dart'; | ||
import 'package:flutter_chatgpt/repository/conversation.dart'; | ||
import 'package:get_it/get_it.dart'; | ||
|
||
abstract class LLM { | ||
getResponse(List<Message> messages, ValueChanged<Message> onResponse, | ||
ValueChanged<Message> errorCallback, ValueChanged<Message> onSuccess); | ||
} | ||
|
||
class ChatGpt extends LLM { | ||
@override | ||
getResponse( | ||
List<Message> messages, | ||
ValueChanged<Message> onResponse, | ||
ValueChanged<Message> errorCallback, | ||
ValueChanged<Message> onSuccess) async { | ||
List<OpenAIChatCompletionChoiceMessageModel> openAIMessages = []; | ||
//将messages反转 | ||
messages = messages.reversed.toList(); | ||
// 将messages里面的每条消息的内容取出来拼接在一起 | ||
String content = ""; | ||
for (Message message in messages) { | ||
content = content + message.text; | ||
if (content.length < 1800) { | ||
// 插入到 openAIMessages 第一个位置 | ||
openAIMessages.insert( | ||
0, | ||
OpenAIChatCompletionChoiceMessageModel( | ||
content: message.text, | ||
role: message.role.asOpenAIChatMessageRole, | ||
), | ||
); | ||
} | ||
} | ||
var message = Message( | ||
conversationId: messages.first.conversationId, | ||
text: "", | ||
role: Role.assistant); //仅仅第一个返回了角色 | ||
if (GetIt.instance.get<UserSettingCubit>().state.useStream) { | ||
Stream<OpenAIStreamChatCompletionModel> chatStream = OpenAI.instance.chat | ||
.createStream( | ||
model: GetIt.instance.get<UserSettingCubit>().state.gptModel, | ||
messages: openAIMessages); | ||
chatStream.listen( | ||
(chatStreamEvent) { | ||
if (chatStreamEvent.choices.first.delta.content != null) { | ||
message.text = | ||
message.text + chatStreamEvent.choices.first.delta.content!; | ||
onResponse(message); | ||
} | ||
}, | ||
onError: (error) { | ||
message.text = error.message; | ||
errorCallback(message); | ||
}, | ||
onDone: () { | ||
onSuccess(message); | ||
}, | ||
); | ||
} else { | ||
try { | ||
var response = await OpenAI.instance.chat.create( | ||
model: GetIt.instance.get<UserSettingCubit>().state.gptModel, | ||
messages: openAIMessages, | ||
); | ||
message.text = response.choices.first.message.content; | ||
onSuccess(message); | ||
} catch (e) { | ||
message.text = e.toString(); | ||
errorCallback(message); | ||
} | ||
} | ||
} | ||
} |
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