From ef93d76ff5bbc6530d4c65a57945c7be9469d34e Mon Sep 17 00:00:00 2001 From: soramimi Date: Sun, 16 Jun 2024 19:15:05 +0900 Subject: [PATCH] Reorder available AI models --- Guitar.pri | 9 ++++--- src/CommitMessageGenerator.cpp | 44 ++++++++++++++++++++++++++++++---- src/CommitMessageGenerator.h | 6 ++--- src/GenerativeAI.cpp | 39 ++++++++++++++++++++++++++++++ src/GenerativeAI.h | 44 +++++----------------------------- src/common/jstream.h | 3 ++- 6 files changed, 93 insertions(+), 52 deletions(-) diff --git a/Guitar.pri b/Guitar.pri index 08dce22b..664d293d 100644 --- a/Guitar.pri +++ b/Guitar.pri @@ -107,11 +107,10 @@ macx { } SOURCES += \ - $$PWD/src/SettingAiForm.cpp \ - $$PWD/src/SettingOptionsForm.cpp \ + src/SettingAiForm.cpp \ + src/SettingOptionsForm.cpp \ src/FileType.cpp \ src/GenerativeAI.cpp \ - src/GenerativeAI.cpp \ src/AbstractProcess.cpp \ src/AbstractSettingForm.cpp \ src/AddRepositoriesCollectivelyDialog.cpp \ @@ -257,8 +256,8 @@ SOURCES += \ src/coloredit/RingSlider.cpp HEADERS += \ - $$PWD/src/SettingAiForm.h \ - $$PWD/src/SettingOptionsForm.h \ + src/SettingAiForm.h \ + src/SettingOptionsForm.h \ src/FileType.h \ src/GenerativeAI.h \ src/GenerativeAI.h \ diff --git a/src/CommitMessageGenerator.cpp b/src/CommitMessageGenerator.cpp index 40e0e27d..57bfb409 100644 --- a/src/CommitMessageGenerator.cpp +++ b/src/CommitMessageGenerator.cpp @@ -11,6 +11,11 @@ namespace { +/** + * @brief Encode a string for JSON. + * @param in The string to encode. + * @return The encoded string. + */ std::string encode_json_string(std::string const &in) { std::string out; @@ -43,6 +48,11 @@ std::string encode_json_string(std::string const &in) return out; } +/** + * @brief Decode a JSON string. + * @param in The JSON string. + * @return The decoded string. + */ std::string decode_json_string(std::string const &in) { QString out; @@ -184,7 +194,13 @@ static std::string example_gemini_response() )---"; } -GeneratedCommitMessage CommitMessageGenerator::parse_openai_response(std::string const &in, GenerativeAI::Type ai_type) +/** + * @brief Parse the response from the AI model. + * @param in The response. + * @param ai_type The AI model type. + * @return The generated commit message. + */ +GeneratedCommitMessage CommitMessageGenerator::parse_response(std::string const &in, GenerativeAI::Type ai_type) { error_message_.clear(); bool ok1 = false; @@ -307,7 +323,13 @@ GeneratedCommitMessage CommitMessageGenerator::parse_openai_response(std::string } } -std::string CommitMessageGenerator::generatePrompt(QString diff, int max) +/** + * @brief Generate a prompt for the given diff. + * @param diff The diff. + * @param max The maximum number of messages to generate. + * @return The prompt. + */ +std::string CommitMessageGenerator::generatePrompt(QString const &diff, int max) { std::string prompt = strformat( "Generate a concise git commit message written in present tense for the following code diff with the given specifications below. " @@ -319,7 +341,14 @@ std::string CommitMessageGenerator::generatePrompt(QString diff, int max) return prompt; } -std::string CommitMessageGenerator::generatePromptJSON(GenerativeAI::Model const &model, QString diff, int max_message_count) +/** + * @brief Generate a JSON string for the given AI model. + * @param model The AI model. + * @param diff The diff to generate the commit message for. + * @param max_message_count The maximum number of messages to generate. + * @return The JSON string. + */ +std::string CommitMessageGenerator::generatePromptJSON(GenerativeAI::Model const &model, QString const &diff, int max_message_count) { std::string prompt = generatePrompt(diff, max_message_count); std::string json; @@ -370,9 +399,14 @@ GeneratedCommitMessage CommitMessageGenerator::test() { std::string s = R"---( )---"; - return parse_openai_response(s, GenerativeAI::CLAUDE); + return parse_response(s, GenerativeAI::CLAUDE); } +/** + * @brief Generate a commit message using the given diff. + * @param g The Git object. + * @return The generated commit message. + */ GeneratedCommitMessage CommitMessageGenerator::generate(GitPtr g) { constexpr int max_message_count = 5; @@ -442,7 +476,7 @@ GeneratedCommitMessage CommitMessageGenerator::generate(GitPtr g) } } std::string text(data, size); - auto list = parse_openai_response(text, model_type); + auto list = parse_response(text, model_type); if (!error_status_.empty()) { return GeneratedCommitMessage::Error(QString::fromStdString(error_status_), QString::fromStdString(error_message_)); } diff --git a/src/CommitMessageGenerator.h b/src/CommitMessageGenerator.h index 77198dab..2830dee5 100644 --- a/src/CommitMessageGenerator.h +++ b/src/CommitMessageGenerator.h @@ -36,9 +36,9 @@ class CommitMessageGenerator { private: std::string error_status_; std::string error_message_; - GeneratedCommitMessage parse_openai_response(const std::string &in, GenerativeAI::Type ai_type); - std::string generatePrompt(QString diff, int max); - std::string generatePromptJSON(const GenerativeAI::Model &model, QString diff, int max); + GeneratedCommitMessage parse_response(const std::string &in, GenerativeAI::Type ai_type); + std::string generatePrompt(const QString &diff, int max); + std::string generatePromptJSON(const GenerativeAI::Model &model, const QString &diff, int max); GeneratedCommitMessage test(); public: CommitMessageGenerator() = default; diff --git a/src/GenerativeAI.cpp b/src/GenerativeAI.cpp index 55603fd9..d032d59f 100644 --- a/src/GenerativeAI.cpp +++ b/src/GenerativeAI.cpp @@ -1,3 +1,42 @@ #include "GenerativeAI.h" +std::vector GenerativeAI::available_models() +{ + std::vector models; + models.emplace_back("gpt-4o"); + models.emplace_back("gpt-4-turbo"); + models.emplace_back("gpt-4"); + models.emplace_back("gpt-3.5-turbo"); + models.emplace_back("claude-3-opus-20240229"); + models.emplace_back("claude-3-sonnet-20240229"); + models.emplace_back("claude-3-haiku-20240307"); + models.emplace_back("gemini-1.0-ultra"); + models.emplace_back("gemini-1.0-pro"); + models.emplace_back("gemini-1.0-flash"); + models.emplace_back("gemini-1.0-nano"); + return models; +} +GenerativeAI::Model::Model(const QString &name) + : name(name) +{ +} + +GenerativeAI::Type GenerativeAI::Model::type() const +{ + if (name.startsWith("gpt-")) { + return GPT; + } + if (name.startsWith("claude-")) { + return CLAUDE; + } + if (name.startsWith("gemini-")) { + return GEMINI; + } + return Unknown; +} + +QString GenerativeAI::Model::anthropic_version() const +{ + return "2023-06-01"; +} diff --git a/src/GenerativeAI.h b/src/GenerativeAI.h index 9f298b01..51d73bc3 100644 --- a/src/GenerativeAI.h +++ b/src/GenerativeAI.h @@ -2,6 +2,7 @@ #define GENERATIVEAI_H #include +#include namespace GenerativeAI { @@ -15,46 +16,13 @@ enum Type { struct Model { QString name; Model() = default; - Model(const QString &name) - : name(name) - { - } - Type type() const - { - if (name.startsWith("gpt-")) { - return GPT; - } - if (name.startsWith("claude-")) { - return CLAUDE; - } - if (name.startsWith("gemini-")) { - return GEMINI; - } - return Unknown; - } - QString anthropic_version() const - { - return "2023-06-01"; - } + Model(const QString &name); + Type type() const; + QString anthropic_version() const; }; -static std::vector available_models() -{ - std::vector models; - models.emplace_back("gpt-3.5-turbo"); - models.emplace_back("gpt-4"); - models.emplace_back("gpt-4-turbo"); - models.emplace_back("gpt-4o"); - models.emplace_back("claude-3-haiku-20240307"); - models.emplace_back("claude-3-sonnet-20240229"); - models.emplace_back("claude-3-opus-20240229"); - models.emplace_back("gemini-1.0-ultra"); - models.emplace_back("gemini-1.0-pro"); - models.emplace_back("gemini-1.0-flash"); - models.emplace_back("gemini-1.0-nano"); - return models; -} +std::vector available_models(); -} // namespace +} // namespace GenerativeAI #endif // GENERATIVEAI_H diff --git a/src/common/jstream.h b/src/common/jstream.h index 62c86990..09390fa9 100644 --- a/src/common/jstream.h +++ b/src/common/jstream.h @@ -85,7 +85,8 @@ class Reader { *out = strtod(vec.data(), nullptr); return ptr - begin; } - + +public: static int parse_string(char const *begin, char const *end, std::string *out) { char const *ptr = begin;