#include <FLstd.h>
#include <cppstd.h>
#include <ChatGPT.h>
using json = nlohmann::json;
extern string requestData;
json jsonData;
Fl_Multiline_Input* input;
Fl_Text_Display* output;
Fl_Input* sysInput;
Fl_Input* jsonInput;
Fl_Browser* browser;
int sendNum;
string startTime;
string jsonFileName;
string systemStr;
void sendCB(Fl_Widget*, void*) {
// 開始時間取得
if (sendNum == 0){
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
std::tm tm = *std::localtime(&time_t);
// 日時を指定されたフォーマットで文字列に変換する
std::stringstream ss;
ss << std::put_time(&tm, "%y%m%d_%H%M%S");
startTime = ss.str();
sendNum++;
}
const char* system = sysInput -> value();
systemStr = string(system);
const char* question0 = nullptr;
question0 = input -> value();
string question(question0);
json response = chatWithOpenAI(question);
string resDump = response.dump();
cout << resDump << endl;
string content = response["choices"][0]["message"]["content"].get<string>();
cout << content << endl;
while (!content.empty() && content.front() == '\n') {
content.erase(0, 1);
}
size_t pos = 0;
while ((pos = content.find('"', pos)) != string::npos) {
content.replace(pos, 1, "\\\"");
pos += 2;
}
pos = 0;
while ((pos = content.find('\n', pos)) != string::npos) {
content.replace(pos, 1, "\\n");
pos += 2;
}
string res = R"({"role":"assistant","content":")" + content + R"("})";
json resJson = json::parse(res);
jsonData = json::parse(requestData);
jsonData["messages"].push_back(resJson);
cout << "回答追加後jsonData: " << jsonData.dump() << endl;
// jsonファイル化
jsonFileName = "/Users/[ユーザID]/ChatGPT/" + startTime + "_ChatGPT.json";
std::ofstream ofs(jsonFileName, std::ios::out | std::ios::trunc);
ofs << std::setw(2) << jsonData << std::endl;
ofs.close();
Fl_Text_Buffer*buffer = new Fl_Text_Buffer();
buffer -> text(content.c_str());
output -> buffer(buffer);
output -> wrap_mode(Fl_Text_Display::WRAP_AT_BOUNDS, 5);
browser -> load(jsonFileName.c_str());
int line_num = browser->size();
browser -> bottomline(line_num);
browser -> has_scrollbar(Fl_Browser_::BOTH);
}
int main(int argc, char **argv) {
Fl_Window* window = new Fl_Window (100, 100, 960, 640, "ChatGPT");
window->color(fl_rgb_color(208,207,207));
// Question inputLabel
Fl_Box* inputLabel = new Fl_Box(14,22,26,23,"Q");
inputLabel->labelsize(18);
inputLabel->labelcolor(fl_rgb_color(62,66,60));
inputLabel->align(Fl_Align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT));
// Question input
input = new Fl_Multiline_Input(47, 15, 360, 105);
input->textsize(12);
// input->wrap_mode(Fl_Multiline_Input::WRAP_AT_BOUNDS, 0); // 折り返し設定
Fl_Scroll *scroll = new Fl_Scroll(47, 15, 360, 105);
scroll->end();
scroll->type(Fl_Scroll::VERTICAL_ALWAYS);
scroll->scrollbar_size(20);
scroll->add(input);
// sendBtn
Fl_Button* sendBtn = new Fl_Button(420,15,60,40,"送信");
sendBtn->color(fl_rgb_color(112,128,144));
sendBtn->labelcolor(fl_rgb_color(208,207,207));
sendBtn->labelsize(16);
sendBtn->callback(sendCB);
// Response
output = new Fl_Text_Display(19,130,450,500,"Response");
output->labelcolor(fl_rgb_color(62,66,60));
output->textsize(12);
output->align(Fl_Align(FL_ALIGN_TOP_RIGHT));
output->labelsize(10);
// Sys設定 sysLabel
Fl_Box* sysLabel = new Fl_Box(499,22,60,17,"Sys設定");
sysLabel->labelsize(16);
sysLabel->labelcolor(fl_rgb_color(62,66,60));
sysLabel->align(Fl_Align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT));
// Sys設定 sysInput
sysInput = new Fl_Input(573, 15, 370, 30);
sysInput->textsize(12);
// JSON jsonLabel
Fl_Box* jsonLabel = new Fl_Box(507,62,44,16,"JSON");
jsonLabel->labelsize(16);
jsonLabel->labelcolor(fl_rgb_color(62,66,60));
jsonLabel->align(Fl_Align(FL_ALIGN_INSIDE|FL_ALIGN_LEFT));
// JSON jsonInput
jsonInput = new Fl_Input(573, 55, 335, 30);
jsonInput->textsize(12);
// jsonBtn
Fl_Button* jsonBtn = new Fl_Button(913,60,30,20,"...");
jsonBtn->color(fl_rgb_color(112,128,144));
jsonBtn->labelcolor(fl_rgb_color(208,207,207));
jsonBtn->labelsize(16);
// jsonBtn->callback(sendCB);
// Browser
browser = new Fl_Browser(493, 100, 450, 532);
browser->textsize(12);
browser->type(FL_HOLD_BROWSER);
window->end();
window->show(argc, argv);
return Fl::run();
}