[C++] 272 FLTK : ChatGPTアプリの製作 その1 ChatGPT API

[M1 Mac, Monterey 12.6.3, clang 13.0.0, FLTK 1.3.8, ChatGPT Plus, NO IDE]

FLTKでChatGPTアプリを製作します。あくまで私個人用です。

とりあえずたたき台を作成しました。返信されたJSONをそのまま表示しています。

#include <FLstd.h>
#include <cppstd.h>
#include <ChatGPT.h>

Fl_Input* input;
Fl_Text_Display* output;

void sendCB(Fl_Widget*, void*) {
	const char* question0 = input->value();
	string question = string(question0);
	json response = chatWithOpenAI(question);
	string resDump = response.dump();
	cout << resDump << endl;

	Fl_Text_Buffer *buffer = new Fl_Text_Buffer();
	buffer -> text(resDump.c_str());

	output -> buffer(buffer);
	output -> wrap_mode(Fl_Text_Display::WRAP_AT_BOUNDS, 5);
}

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 label
	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_Input(47,18,360,30,"");
	input->textsize(12);
    	
	// sendBtn
	Fl_Button* sendBtn = new Fl_Button(420,13,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(30,70,448,552,"Response");
	output->labelcolor(fl_rgb_color(62,66,60));
	output->textsize(12);
	output->align(Fl_Align(FL_ALIGN_TOP_RIGHT));
	output->labelsize(10);

	window->end();  
	window->show(argc, argv);

	return Fl::run();
}