From c43efe3af6cde69d894aaeccf81d5590210817e1 Mon Sep 17 00:00:00 2001 From: jschang19 Date: Tue, 19 Dec 2023 22:47:27 +0800 Subject: [PATCH] fix: resolve not memorying choices error --- ChatGPT/include/Game.h | 235 ++++++++++++++++++++++------------------- ChatGPT/src/Game.cpp | 17 ++- 2 files changed, 142 insertions(+), 110 deletions(-) diff --git a/ChatGPT/include/Game.h b/ChatGPT/include/Game.h index 64165ca..6acc3cc 100644 --- a/ChatGPT/include/Game.h +++ b/ChatGPT/include/Game.h @@ -1,113 +1,134 @@ -#include +#include #include +#include + #include "ChatGPT.h" -#include namespace System { - struct Option{ - std::string id; - std::string text; - Option(std::string id, std::string text) : id(id), text(text) {}; // constructor - }; +struct Option { + std::string id; + std::string text; + Option(std::string id, std::string text) + : id(id), text(text){}; // constructor +}; + +class Story { + public: + int id; + std::string place; + std::string content; + std::string user_choice; + bool is_answered = false; + std::vector choices; + Story(int id, std::string place, std::string content) + : id(id), place(place), content(content) {} + ~Story(); // destructor + private: +}; + +class Game { + public: + int count; + int current_count = 0; + std::vector story_ids; + void addPrompt(OpenAI::ChatGPT& chatGpt, const OpenAI::Message& prompt, + bool isLast) { + + if (isLast) { + OpenAI::Message prompt = this->generateEndingPrompt(); + chatGpt.addPrompt(prompt); + } else { + chatGpt.addPrompt(prompt); + } - class Story { - public: - int id; - std::string place; - std::string content; - std::string user_choice; - bool is_answered = false; - std::vector< System::Option > choices; - Story(int id, std::string place, std::string content) - : id(id), place(place), content(content) {} - ~Story(); // destructor - private: - }; - + }; + OpenAI::ChatCompletion sendToChatGPT(OpenAI::ChatGPT& chatGpt, bool isEnding) { + this->print("ChatGPT 正在思考中...", "l"); + std::cout << std::endl; + auto response = chatGpt.askChatGPT("user", isEnding); + return response; + }; + std::vector picked_story_ids; + void parseGPTResponse(OpenAI::ChatCompletion& chatCompletion, int story_id) { + System::Story* story_ptr = this->getStoryPtrById(story_id); + nlohmann::json j2; + try { + j2 = nlohmann::json::parse(chatCompletion.choices[0].message.content); + std::vector choices; + for (auto& choice : j2["options"]) { + choices.push_back(System::Option(choice["id"], choice["text"])); + } + this->setOptions(story_id, choices); + } catch (std::exception& e) { + std::cerr << "Game.h parsing Error: " + + chatCompletion.choices[0].message.content; + } + }; + void parseEndingResponse(OpenAI::ChatCompletion& chatCompletion) { + nlohmann::json j2; + try { + for(auto &choice: chatCompletion.choices){ + std::cout< stories; + std::vector getRandStoryIds(int num); + System::Story* getStoryPtrById(int id); + OpenAI::Message generateStoryPrompt(int id); + OpenAI::Message generateEndingPrompt(); + void setOptions(int id, const std::vector& choices); + void printOptions(int id); + void print(const std::string& str, const std::string& color = "w", + bool bold = false) { + std::string color_code; + switch (color[0]) { + case 'r': + color_code = "31"; + break; + case 'g': + color_code = "32"; + break; + case 'y': + color_code = "33"; + break; + case 'b': + color_code = "34"; + break; + case 'p': + color_code = "35"; + break; + case 'c': + color_code = "36"; + break; + case 'w': + color_code = "37"; + break; + case 'l': + color_code = "90"; + break; + default: + color_code = "37"; + break; + } + if (bold) { + std::cout << "\033[1;" << color_code << "m" << str << "\033[0m\n"; + } else { + std::cout << "\033[" << color_code << "m" << str << "\033[0m\n"; + } + }; + void printWelcome(); + void checkStatus(const std::string& key); + bool verifyOpenAiKey(const std::string& key); + bool checkConnection(); + bool setUserChoice(int story_id, std::string user_choice_id); - class Game { - public: - int count; - int current_count = 0; - std::vector story_ids; - void addPrompt(OpenAI::ChatGPT& chatGpt, const int story_id){ - OpenAI::Message prompt = this->generateStoryPrompt(story_id); - chatGpt.Add_prompt(prompt); - }; - OpenAI::ChatCompletion sendToChatGPT(OpenAI::ChatGPT& chatGpt){ - this->print("ChatGPT 正在思考中...", "l"); - std::cout< picked_story_ids; - void parseGPTResponse(OpenAI::ChatCompletion& chatCompletion, int story_id){ - System::Story* story_ptr = this->getStoryPtrById(story_id); - nlohmann::json j2; - try { - j2 = nlohmann::json::parse(chatCompletion.choices[0].message.content); - std::vector< System::Option > choices; - for (auto& choice : j2["options"]) { - choices.push_back(System::Option(choice["id"], choice["text"])); - } - this->setOptions(story_id, choices); - }catch(std::exception& e){ - std::cerr<<"Game.h parsing Error: "+chatCompletion.choices[0].message.content; - } - }; - Game(); - ~Game(); // destructor - std::vector< System::Story > stories; - std::vector< int > getRandStoryIds(int num); - System::Story* getStoryPtrById(int id); - OpenAI::Message generateStoryPrompt(int id); - void setOptions(int id, const std::vector< System::Option > &choices); - void printOptions(int id); - void print(const std::string& str, const std::string& color = "w", bool bold=false){ - std::string color_code; - switch (color[0]) - { - case 'r': - color_code = "31"; - break; - case 'g': - color_code = "32"; - break; - case 'y': - color_code = "33"; - break; - case 'b': - color_code = "34"; - break; - case 'p': - color_code = "35"; - break; - case 'c': - color_code = "36"; - break; - case 'w': - color_code = "37"; - break; - case 'l': - color_code = "90"; - break; - default: - color_code = "37"; - break; - } - if(bold){ - std::cout << "\033[1;" << color_code << "m" << str << "\033[0m\n"; - } - else{ - std::cout << "\033[" << color_code << "m" << str << "\033[0m\n"; - } - }; - void printWelcome(); - void checkStatus(const std::string& key); - bool verifyOpenAiKey(const std::string& key); - bool checkConnection(); - bool setUserChoice(int story_id, std::string user_choice_id); - private: - std::vector< System::Story > readTextFile(const std::string& filename); - }; -} \ No newline at end of file + private: + std::vector readTextFile(const std::string& filename); +}; +} // namespace System \ No newline at end of file diff --git a/ChatGPT/src/Game.cpp b/ChatGPT/src/Game.cpp index b4e2b7d..8f5c79e 100644 --- a/ChatGPT/src/Game.cpp +++ b/ChatGPT/src/Game.cpp @@ -10,6 +10,7 @@ #include // std::shuffle #include // std::default_random_engine #include // std::chrono::system_clock +#include #include #include #include @@ -109,6 +110,7 @@ void System::Game::printOptions(int id) { bool System::Game::setUserChoice(int story_id, std::string user_choice_id) { System::Story* story_ptr = this->getStoryPtrById(story_id); if (story_ptr == nullptr) { + std::cout<<"story_ptr is nullptr"<current_count == 0){ - prompt = "我在" + this->stories[story_index].place + ",我" + this->stories[story_index].content; + prompt = "我在" + this->stories[story_index].place + this->stories[story_index].content + "。"; }else{ int previous_id = this->story_ids[story_index - 1]; - prompt = "我在「" + this->stories[story_index].place + "," + this->stories[story_index].content + "」"; + prompt +=( "我剛剛在" + this->stories[previous_id].place + this->stories[previous_id].content + ",我選擇「" + this->stories[previous_id].user_choice + "。」"); + prompt += "現在我在" + this->stories[story_index].place + "," + this->stories[story_index].content + "。"; } return OpenAI::Message({"user", prompt}); -} \ No newline at end of file +} + +OpenAI::Message System::Game::generateEndingPrompt(){ + std::string final_prompt = ""; + final_prompt = "我在「" + this->stories[this->current_count].place + "," + this->stories[this->current_count].content + "」"; + + return OpenAI::Message({"user", final_prompt}); +} +