Skip to content

Commit

Permalink
Reorder available AI models
Browse files Browse the repository at this point in the history
  • Loading branch information
soramimi committed Jun 16, 2024
1 parent ba65b6a commit ef93d76
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 52 deletions.
9 changes: 4 additions & 5 deletions Guitar.pri
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
44 changes: 39 additions & 5 deletions src/CommitMessageGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. "
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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_));
}
Expand Down
6 changes: 3 additions & 3 deletions src/CommitMessageGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 39 additions & 0 deletions src/GenerativeAI.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
#include "GenerativeAI.h"

std::vector<GenerativeAI::Model> GenerativeAI::available_models()
{
std::vector<Model> 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";
}
44 changes: 6 additions & 38 deletions src/GenerativeAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GENERATIVEAI_H

#include <QString>
#include <vector>

namespace GenerativeAI {

Expand All @@ -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<Model> available_models()
{
std::vector<Model> 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<Model> available_models();

} // namespace
} // namespace GenerativeAI

#endif // GENERATIVEAI_H
3 changes: 2 additions & 1 deletion src/common/jstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ef93d76

Please sign in to comment.