LoginSignup
1
1

【生成AI】GPTsでC++コーディング規約に詳しいアドバイザーを作成

Last updated at Posted at 2023-12-05

PONOS Advent Calendar 2023 6日目の記事です。

はじめに:eyeglasses:

ChatGPTの新機能「GPTs」が新たにリリースされましたね!!さっそく使ってみます!
GPTsとは、ChatGPTを特定の目的にあわせてカスタマイズできる機能です。
※使用&作成条件としてGPT Plusに加入している必要があります(2023/11/28時点)

この記事の対象者:eyeglasses:

・エンジニア全般
・生成AIに興味がある方、全般

今回作成するもの

C++のコーディング規約に詳しいアドバイザー

用意するもの
Google C++ スタイルガイド 日本語全訳
・Google C++ スタイルガイドの内容をコピペしてテキスト保存

GPTs作成

スクリーンショット 2023-11-27 14.02.19.png

1.ChatGPTホーム画面、左側のメニューからExploreを選択

2.My GPTsから「Create a GPT」を選択

3.チャット形式でやりたいこと、アイコンイメージ、参照情報を伝えて完了!簡単!!
・やりたいこと:C++のコーディング規約に特化したアドバイザー
・アイコンイメージ:ChatGPTが提案してくれます、修正もスムーズです
・参照情報:Google C++ スタイルガイドのテキストファイルをアップロード

GPTsの使い方

通常のChatGPTと同じようにやりとりするだけです。
公開、非公開を選べます。
公開方法についてはこちらの記事が参考になります!

警告
Nameで公開はGPT Plus登録名が表示されるので注意してください(実名の可能性あり)

今回作成したGPTs
C++のコーディング規約に詳しいアドバイザーも公開しています!

使用例

「Google C++ スタイルガイドの内容を要約して」と依頼すると以下のように返答してくれます
スクリーンショット 2023-11-27 17.30.20.png

「行列計算のコードを書いてください」と依頼すると以下のように返答してくれます

「行列計算には様々な種類がありますが、基本的な行列の加算と乗算を実行するコードの例を示します。この例では、行列の加算と乗算を行うメソッドを追加します。」

#include <iostream>
#include <vector>

class Matrix {
 public:
  Matrix(int rows, int cols) : rows_(rows), cols_(cols) {
    data_.resize(rows, std::vector<int>(cols, 0));
  }

  void Set(int row, int col, int value) {
    data_[row][col] = value;
  }

  int Get(int row, int col) const {
    return data_[row][col];
  }

  Matrix operator+(const Matrix& other) const {
    if (rows_ != other.rows_ || cols_ != other.cols_) {
      throw std::invalid_argument("Matrices dimensions must match for addition");
    }

    Matrix result(rows_, cols_);
    for (int i = 0; i < rows_; ++i) {
      for (int j = 0; j < cols_; ++j) {
        result.data_[i][j] = data_[i][j] + other.data_[i][j];
      }
    }
    return result;
  }

  Matrix operator*(const Matrix& other) const {
    if (cols_ != other.rows_) {
      throw std::invalid_argument("Matrices dimensions must match for multiplication");
    }

    Matrix result(rows_, other.cols_);
    for (int i = 0; i < rows_; ++i) {
      for (int j = 0; j < other.cols_; ++j) {
        for (int k = 0; k < cols_; ++k) {
          result.data_[i][j] += data_[i][k] * other.data_[k][j];
        }
      }
    }
    return result;
  }

  void Print() const {
    for (int i = 0; i < rows_; ++i) {
      for (int j = 0; j < cols_; ++j) {
        std::cout << data_[i][j] << " ";
      }
      std::cout << std::endl;
    }
  }

 private:
  int rows_, cols_;
  std::vector<std::vector<int>> data_;
};

int main() {
  Matrix mat1(2, 2);
  mat1.Set(0, 0, 1);
  mat1.Set(0, 1, 2);
  mat1.Set(1, 0, 3);
  mat1.Set(1, 1, 4);

  Matrix mat2(2, 2);
  mat2.Set(0, 0, 5);
  mat2.Set(0, 1, 6);
  mat2.Set(1, 0, 7);
  mat2.Set(1, 1, 8);

  Matrix sum = mat1 + mat2;
  Matrix product = mat1 * mat2;

  std::cout << "Matrix 1 + Matrix 2:" << std::endl;
  sum.Print();

  std::cout << "Matrix 1 * Matrix 2:" << std::endl;
  product.Print();

  return 0;
}

まとめ

GPT-4が優秀なので、エラーをそのまま貼り付けるなど雑な使い方してもちゃんと返答してくれます
AIを活用してない方はこの機会に是非、活用してみることをお勧めします

今回作成したGPTsは10分掛からずにできました
(私の場合、公開したかったのでドメインによる公開は少し手間取りました)
「習うより慣れろ」なので是非、皆さんも試してください!

次は、@nissy_gpさんの記事です。
お楽しみに!!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1