Skip to content

Commit

Permalink
优化图片生成
Browse files Browse the repository at this point in the history
  • Loading branch information
NingNing0111 committed May 4, 2024
1 parent 84271af commit c159270
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 147 deletions.
8 changes: 7 additions & 1 deletion lib/controller/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ class ChatPageController extends GetxController {
currHistoryMessage = Get.find<SidebarPageController>().histories.last;
update();
}

void setHistory(HistoryMessage history) {
currHistoryMessage = history;
update();
}

@override
void onClose() {
Get.find<SidebarPageController>().saveAll();
super.onClose();

}
}
16 changes: 11 additions & 5 deletions lib/controller/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class SettingPageController extends GetxController {
final key = "".obs;
// 对话配置
final chatModel = "gpt-3.5-turbo".obs;
final drawModel = "dall-e-2".obs;
final imageModel = "dall-e-2".obs;
final temperature = 0.5.obs;
final presencePenalty = 0.0.obs;
final frequencyPenalty = 0.0.obs;
final historyLength = 16.obs;
final disabledSystemPrompt = true.obs;
final enabledSystemPrompt = true.obs;
final enabledImageChat = false.obs;
final topP = 0.5.obs;

final db = SettingDatabase();
Expand All @@ -33,11 +34,13 @@ class SettingPageController extends GetxController {
api.value = db.getApi()??api.value;
key.value = db.getKey()??key.value;
chatModel.value = db.getChatModel()??chatModel.value;
imageModel.value = db.getImageModel()??imageModel.value;
temperature.value = db.getTemperature()??temperature.value;
presencePenalty.value = db.getPresencePenalty()??presencePenalty.value;
frequencyPenalty.value = db.getFrequencyPenalty()??frequencyPenalty.value;
historyLength.value = db.getHistoryLength()??historyLength.value;
disabledSystemPrompt.value = db.getDisabledSystemPrompt()??disabledSystemPrompt.value;
enabledSystemPrompt.value = db.getDisabledSystemPrompt()??enabledSystemPrompt.value;
enabledImageChat.value = db.getEnabledImageChat()??enabledImageChat.value;
topP.value = db.getTopP()??topP.value;
setLanguage(language.value!);
super.onInit();
Expand All @@ -54,7 +57,8 @@ class SettingPageController extends GetxController {
presencePenalty.value = 0.0;
frequencyPenalty.value = 0.0;
historyLength.value = 16;
disabledSystemPrompt(true);
enabledSystemPrompt(true);
enabledImageChat(false);
topP.value = 0.5;
update();
}
Expand All @@ -71,11 +75,13 @@ class SettingPageController extends GetxController {
db.saveApi(api.value);
db.saveKey(key.value);
db.saveChatModel(chatModel.value);
db.saveImageModel(imageModel.value);
db.saveTemperature(temperature.value);
db.savePresencePenalty(presencePenalty.value);
db.saveFrequencyPenalty(frequencyPenalty.value);
db.saveHistoryLength(historyLength.value);
db.saveDisabledSystemPrompt(disabledSystemPrompt.value);
db.saveDisabledSystemPrompt(enabledSystemPrompt.value);
db.saveEnabledImageChat(enabledImageChat.value);
db.saveTopP(topP.value);
log("配置信息存储完成");
}
Expand Down
5 changes: 4 additions & 1 deletion lib/controller/sidebar.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:developer';

import 'package:chat_all/db/message_database.dart';
import 'package:chat_all/model/message.dart';
import 'package:get/get.dart';
Expand Down Expand Up @@ -77,11 +79,12 @@ class SidebarPageController extends GetxController {

@override
void onClose() {
db.saveHistory(histories);
saveAll();
super.onClose();
}

void saveAll() {
log("聊天数据保存成功");
db.saveHistory(histories);
}

Expand Down
7 changes: 4 additions & 3 deletions lib/db/message_database.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

import 'dart:developer';

import 'package:chat_all/db/boxes.dart';
import 'package:chat_all/model/message.dart';

Expand Down Expand Up @@ -30,13 +32,12 @@ class MessageDatabase {
for (var element in values) {
histories.add(HistoryMessage(id: element.id, title: element.title, messages: element.messages, createTime: element.createTime));
}
print(histories);
return histories;

}

//
// 添加HistoryMessage
void addHistoryMessage(HistoryMessage message){
void addHistoryMessage(HistoryMessage message){ //
var histories = readAll();
histories.add(message);
historyBox.put("key_histories", histories);
Expand Down
15 changes: 15 additions & 0 deletions lib/db/setting_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class SettingDatabase {
return settingBox.get("key_setting_chatModel");
}

void saveImageModel(String imageModel){
settingBox.put("key_setting_imageModel", imageModel);
}
String? getImageModel(){
return settingBox.get("key_setting_imageModel");
}

void saveTemperature(double temperature){
settingBox.put("key_setting_temperature", temperature);
}
Expand Down Expand Up @@ -115,6 +122,14 @@ class SettingDatabase {
return settingBox.get("key_setting_disabledSystemPrompt");
}

void saveEnabledImageChat(bool enabledImageChat){
settingBox.put("key_setting_enabledImageChat", enabledImageChat);
}

bool? getEnabledImageChat() {
return settingBox.get("key_setting_enabledImageChat");
}

void saveTopP(double topP){
settingBox.put("key_setting_topP", topP);
}
Expand Down
13 changes: 8 additions & 5 deletions lib/i18n/record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ class MessageRecord extends Translations {
"basic_setting_language": "语言",
"api_setting": "接口设置",
"chat_setting": "对话设置",
"chat_setting_model": "模型",
"chat_setting_chat_model": "对话模型",
"chat_setting_image_model": "图片生成模型",
"chat_setting_temperature": "随机性",
"chat_setting_top_p": "核采样",
"chat_setting_presence_penalty": "话题新鲜度",
"chat_setting_frequency_penalty": "频率惩罚",
"chat_setting_history_length": "携带的消息长度",
"chat_setting_disabled_system_prompt": "开启系统词注入",
"chat_setting_enabled_system_prompt": "开启系统词注入",
"chat_setting_enabled_image_chat": "开启图片绘制",
"chat_other_reset_setting": "重置配置",
"chat_other_clear_history": "清空对话",
"chat_page_title": "聊天对话",
Expand All @@ -33,7 +35,6 @@ class MessageRecord extends Translations {
"chat_page_home_tip_tour_content": "请给我一些关于拉萨旅行的计划",
"chat_page_home_tip_social_title": "学习社交技巧",
"chat_page_home_tip_social_content": "如何结交到真心朋友?",

"default_history_chat_title": "开启新对话",
"default_history_chat_overview": "新的聊天",
"default_dialog_delete_title": "确定删除",
Expand All @@ -52,13 +53,15 @@ class MessageRecord extends Translations {
"basic_setting_language": "Language",
"api_setting": "API Settings",
"chat_setting": "Chat Settings",
"chat_setting_model": "Model",
"chat_setting_chat_model": "Chat Model",
"chat_setting_image_model": "Image Model",
"chat_setting_temperature": "temperature",
"chat_setting_top_p": "top_p",
"chat_setting_presence_penalty": "Presence Penalty",
"chat_setting_frequency_penalty": "Frequency Penalty",
"chat_setting_history_length": "History Length",
"chat_setting_disabled_system_prompt": "Enable System Prompt",
"chat_setting_enabled_system_prompt": "Enabled System Prompt",
"chat_setting_enabled_image_chat": "Enabled Image Chat",
"chat_other_reset_setting": "Reset Settings",
"chat_other_clear_history": "Clear History",
"chat_page_title": "Chat Conversation",
Expand Down
22 changes: 15 additions & 7 deletions lib/page/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';



import '../controller/sidebar.dart';

class ChatPage extends StatefulWidget {
Expand All @@ -32,13 +30,21 @@ class _ChatPageState extends State<ChatPage> {
final _chatService = OpenAIService();
final _textEditingController = TextEditingController();
final ScrollController _scrollController = ScrollController();
final isWaiting = false.obs; // 给我一张小狗狗的照片
final isWaiting = false.obs;

@override
void initState() {
super.initState();
}

@override
void dispose() {
_sidebarController.saveAll();
super.dispose();
}



@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -142,6 +148,7 @@ class _ChatPageState extends State<ChatPage> {
child: currMessage.content.isEmpty
? const CircularProgressIndicator(
backgroundColor: Colors.blue,

)
: MdCodeMath(currMessage.content),
),
Expand Down Expand Up @@ -210,13 +217,13 @@ class _ChatPageState extends State<ChatPage> {

// 获取对话Message
final chatMessage = getChatMessageByLen();
log("$chatMessage------**********");

// 发起对话
await _chatService.chat(
drawModel: _settingController.drawModel.value,
imageModel: _settingController.imageModel.value,
messages: chatMessage,
model: _settingController.chatModel.value,
isImageChat: _settingController.enabledImageChat.value,
chatModel: _settingController.chatModel.value,
temperature: _settingController.temperature.value,
topP: _settingController.topP.value,
presencePenalty: _settingController.presencePenalty.value,
Expand All @@ -228,7 +235,8 @@ class _ChatPageState extends State<ChatPage> {
_chatController.updateMessageContent(
_chatController.currHistoryMessage.messages.length - 1, event);
updateToBottom();
}); //
});
_sidebarController.saveAll();
}

void updateToBottom() {
Expand Down
95 changes: 73 additions & 22 deletions lib/page/setting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,9 @@ class _SettingPageState extends State<SettingPage> {
children: [
Row(
children: [
SvgPicture.asset(
AssetsManage.apiIcon,
width: 30,
height: 30,
color: AdaptiveTheme.of(context).mode.isLight
? Colors.black
: Colors.white,
const Icon(
Icons.api,
size: 30,
),
const SizedBox(
width: 10,
Expand Down Expand Up @@ -269,7 +265,7 @@ class _SettingPageState extends State<SettingPage> {
const Divider(),

///
/// 对话设置:模型、随机性、核采样、话题新鲜度、频率惩罚、是否注入系统提示词、附带的历史消息总数
/// 对话设置:对话模型、图片模型、随机性、核采样、话题新鲜度、频率惩罚、是否注入系统提示词、附带的历史消息总数
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand All @@ -282,19 +278,11 @@ class _SettingPageState extends State<SettingPage> {
children: [
Row(
children: [
SvgPicture.asset(
AssetsManage.modelIcon,
width: 30,
height: 30,
color: AdaptiveTheme.of(context).mode.isLight
? Colors.black
: Colors.white,
),
const SizedBox(
width: 10,
width: 40,
),
Text(
"chat_setting_model".tr,
"chat_setting_chat_model".tr,
style: AdaptiveTheme.of(context)
.theme
.textTheme
Expand All @@ -317,6 +305,42 @@ class _SettingPageState extends State<SettingPage> {
})
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
const SizedBox(
width: 40,
),
Text(
"chat_setting_image_model".tr,
style: AdaptiveTheme.of(context)
.theme
.textTheme
.titleMedium,
)
],
),
DropdownButton(
padding: const EdgeInsets.all(5),
borderRadius: BorderRadius.circular(10),
value: "dall-e-2",
items: const [
DropdownMenuItem<String>(
value: "dall-e-2",
child: Text("dall-e-2"),
),
DropdownMenuItem<String>(
value: "dall-e-3",
child: Text("dall-e-3"),
)
],
onChanged: (String? value) {
_settingController.imageModel.value = value!;
})
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expand Down Expand Up @@ -520,7 +544,34 @@ class _SettingPageState extends State<SettingPage> {
width: 40,
),
Text(
"chat_setting_disabled_system_prompt".tr,
"chat_setting_enabled_system_prompt".tr,
style: AdaptiveTheme.of(context)
.theme
.textTheme
.titleMedium,
)
],
),
Obx(() => Switch(
activeColor: Colors.blue,
inactiveThumbColor: Colors.grey,
value: _settingController.enabledSystemPrompt.value,
onChanged: (value) {
_settingController.enabledSystemPrompt.value =
!_settingController.enabledSystemPrompt.value;
}))
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
const SizedBox(
width: 40,
),
Text(
"chat_setting_enabled_image_chat".tr,
style: AdaptiveTheme.of(context)
.theme
.textTheme
Expand All @@ -531,10 +582,10 @@ class _SettingPageState extends State<SettingPage> {
Obx(() => Switch(
activeColor: Colors.blue,
inactiveThumbColor: Colors.grey,
value: _settingController.disabledSystemPrompt.value,
value: _settingController.enabledImageChat.value,
onChanged: (value) {
_settingController.disabledSystemPrompt.value =
!_settingController.disabledSystemPrompt.value;
_settingController.enabledImageChat.value =
!_settingController.enabledImageChat.value;
}))
],
)
Expand Down
Loading

0 comments on commit c159270

Please sign in to comment.