Skip to content

Commit

Permalink
fix: resolve not memorying choices error
Browse files Browse the repository at this point in the history
  • Loading branch information
jschang19 committed Dec 19, 2023
1 parent 7a427d2 commit c43efe3
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 110 deletions.
235 changes: 128 additions & 107 deletions ChatGPT/include/Game.h
Original file line number Diff line number Diff line change
@@ -1,113 +1,134 @@
#include <vector>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>

#include "ChatGPT.h"
#include <nlohmann/json.hpp>

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<System::Option> 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<int> 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<int> 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;
}
};
void parseEndingResponse(OpenAI::ChatCompletion& chatCompletion) {
nlohmann::json j2;
try {
for(auto &choice: chatCompletion.choices){
std::cout<<choice.message.content<<std::endl;
}
} catch (std::exception& e) {
std::cerr << "Game.h ending 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);
OpenAI::Message generateEndingPrompt();
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);

class Game {
public:
int count;
int current_count = 0;
std::vector<int> 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<<std::endl;
auto response = chatGpt.askChatGPT("user");
return response;
};
std::vector<int> 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);
};
}
private:
std::vector<System::Story> readTextFile(const std::string& filename);
};
} // namespace System
17 changes: 14 additions & 3 deletions ChatGPT/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <algorithm> // std::shuffle
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clock
#include <string>
#include <tuple>
#include <iostream>
#include <thread>
Expand Down Expand Up @@ -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"<<std::endl;
return false;
}
std::transform(user_choice_id.begin(), user_choice_id.end(), user_choice_id.begin(), ::tolower);
Expand Down Expand Up @@ -196,11 +198,20 @@ OpenAI::Message System::Game::generateStoryPrompt(int story_index) {
std::string prompt = "";
// get previous story
if(this->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});
}
}

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});
}

0 comments on commit c43efe3

Please sign in to comment.