Zenn
Closed3

GPTsにGitHubコード検索をさせる

hangedmanhangedman

参考にした記事

やりたいこと

  • 個人プロジェクトのソースコード、しばらく離れていると忘れてしまう
  • ChatGPTにコードベースを把握させればいいのではないか
  • あまり手間をかけたくない
  • 専用のGPTsを作って問い合わせてみよう

前提知識

  • GPTsはユーザー個人だけ公開のアプリを作れる
  • GPTsにはActionという外部知識検索用の仕組みが存在する
  • ActionはOpenAPIで定義する -> 【GPTs Actions】GPTsの外部API連携方法
    • OpenAPIの定義でさえも、ChatGPTに聞けば作ってくれる

GitHubコード検索用のAPI

https://docs.github.com/ja/rest/search/search?apiVersion=2022-11-28#search-code

  • curlだと以下でリクエストできる
curl -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer <installation-token>" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  "https://api.github.com/search/code?q=<検索ワード>+in:file+repo:<org or ユーザー名>/<リポジトリ名>"
  • このREST APIは認証なしだと使用できない
  • 認証トークンはGitHub Appsを作成するのがよいだろう、Personal Access Tokenを使用するのは嫌であるため

GitHubコード検索用のOpenAPI定義

作ってもらった

openapi: 3.1.0
info:
  title: GitHub 検索コード API
  description: "このAPIを使用して、GitHubリポジトリ内のコードをクエリパラメータを使って検索できます。"
  version: 1.0.0
servers:
  - url: https://api.github.com
    description: "GitHub API サーバー"
paths:
  /search/code:
    get:
      operationId: searchCode
      summary: "GitHubリポジトリ内のコードを検索"
      description: |
        "指定された検索クエリに基づいて、GitHubリポジトリ内のコードを検索します。
        認証トークンを含める必要があり、`Accept` ヘッダーを指定してください。"
      parameters:
        - name: q
          in: query
          required: true
          description: "検索クエリ。例: `addClass in:file language:js repo:jquery/jquery`"
          schema:
            type: string
      responses:
        '200':
          description: "検索結果が含まれた正常なレスポンス。"
          content:
            application/json:
              schema:
                type: object
                properties:
                  total_count:
                    type: integer
                    description: "クエリに一致する結果の総数。"
                  incomplete_results:
                    type: boolean
                    description: "検索結果が完全かどうかを示します。"
                  items:
                    type: array
                    description: "一致したコード検索結果のリスト。"
                    items:
                      type: object
                      properties:
                        name:
                          type: string
                          description: "ファイル名。"
                        path:
                          type: string
                          description: "リポジトリ内のファイルパス。"
                        repository:
                          type: object
                          description: "コードが存在するリポジトリに関する情報。"
                          properties:
                            id:
                              type: integer
                              description: "リポジトリのID。"
                            name:
                              type: string
                              description: "リポジトリ名。"
                            full_name:
                              type: string
                              description: "リポジトリのフルネーム(例: `owner/repo`)。"
                            private:
                              type: boolean
                              description: "リポジトリがプライベートかどうか。"
      security:
        - githubAuth: []
components:
  schemas: {}
  securitySchemes:
    githubAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

hangedmanhangedman

認証の設定

ざっくり以下の通り

  • GitHub Appsを作成
  • GitHub Appsでclient id, client secretを発行
  • GPTsのアクションを編集するの中で、認証をOAuthに設定する

他の設定は以下の通り

フィールド名 入力例
クライアントID(Client ID) GitHubで作成したOAuth AppのクライアントID
クライアントシークレット(Client Secret) GitHubで作成したOAuth Appのクライアントシークレット
認証URL(Authorization URL) https://github.com/login/oauth/authorize
トークンURL(Token URL) https://github.com/login/oauth/access_token
スコープ(Scope) contents(必要であれば: repo, read:org, userなどをカンマ区切りで記載)

これを設定後、以下のように進める

  • 「Actionsに記載したAPIを叩いてください」とプロンプトに入力

    • ブラウザでGitHub Appsからのサインインを求められるので、サインインボタンを押して進める
    • この操作で、OAuthのコールバックURLがGPTsの中に生成されるのでコピー
  • GitHub Appsのcall back URLの欄にGPTsで発行されたURLをペースト

  • もう一回「Actionsに記載したAPIを叩いてください」とプロンプトに入力して、またエラーになるがここまでで認証は完了する

hangedmanhangedman

後はGPTsでコード検索をして有用かどうか評価する
とりあえずこのスクラップはクローズ

このスクラップは3ヶ月前にクローズされました
作成者以外のコメントは許可されていません