Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history


# Conflicts:
#	ChatGPT/include/ChatGPT.h
#	ChatGPT/include/Game.h
#	ChatGPT/src/ChatGPT.cpp
#	ChatGPT/src/Game.cpp
#	main.cpp

refactor: move some code from game.h to game.cpp
  • Loading branch information
Nathanliao02 committed Dec 20, 2023
2 parents de9626e + 5ded457 commit dc8a412
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 245 deletions.
15 changes: 4 additions & 11 deletions ChatGPT/include/ChatGPT.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ namespace OpenAI {
class ChatGPT {
public:
explicit ChatGPT(const std::string& token);
OpenAI::ChatCompletion askChatGPT(const std::string& role);
std::string askWhiasper(const std::string& audio_path);
OpenAI::ChatCompletion askChatGPT(const std::string& role, bool isEnding);
std::vector< Message > prompts;
void Add_prompt(const Message& prompt);
void CoutPrompt(){
for (int i=0; i<this->prompts.size();i++){
std::cout << this->prompts[i].role << ":" << this->prompts[i].content << '\n' ;
}
std::cout << '\n';
}
void addPrompt(const Message& prompt);

private:
std::string m_token;
std::string m_link;

std::string PromptsToStringContent();
std::string formatPrompt(const Message& prompt, bool isLast, bool isEnding);
std::string promptsToStringContent(bool isEnding);
};
}
157 changes: 52 additions & 105 deletions ChatGPT/include/Game.h
Original file line number Diff line number Diff line change
@@ -1,111 +1,58 @@
#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 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);
OpenAI::ChatCompletion sendToChatGPT(OpenAI::ChatGPT& chatGpt, bool isEnding);
std::vector<int> picked_story_ids;
void parseGPTResponse(OpenAI::ChatCompletion& chatCompletion, int story_id);
void parseEndingResponse(OpenAI::ChatCompletion& chatCompletion);
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);
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::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);
void PrintFinalResult(OpenAI :: ChatGPT& chatgpt); //印出遊戲結局
private:
std::vector< System::Story > readTextFile(const std::string& filename);
};
}
private:
std::vector<System::Story> readTextFile(const std::string& filename);
};
} // namespace System
Loading

0 comments on commit dc8a412

Please sign in to comment.