Last active
March 22, 2023 23:44
-
-
Save MasahiroKawahara/7b86cef8fd182e920bb720825d82b0b6 to your computer and use it in GitHub Desktop.
【ChatGPT】GitHubプルリクエスト作成時に差分内容を要約して自動コメントする仕組みを作ってみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import openai | |
from pathlib import Path | |
content_path = sys.argv[1] | |
diff_path = sys.argv[2] | |
# APIキーの設定 | |
openai.api_key = os.environ.get("OPENAI_API_KEY") | |
chatgpt_system_content = '''これから以下の入力を与えます。 | |
- 入力(コンテンツのファイル名) | |
- 入力(「修正前のコンテンツ」と「修正後のコンテンツ」のdiffコマンドの結果) | |
- "< " から始まる行は修正前のコンテンツに該当します | |
- "> " から始まる行は修正後のコンテンツに該当します | |
あなたは与えられた入力から「修正前のコンテンツ」と「修正後のコンテンツ」の差分を調べてください。 | |
「修正前のコンテンツ」と「修正後のコンテンツ」の差分から、修正内容を要約して出力してください。 | |
### 制約 | |
- Markdown形式で出力してください。 | |
- 出力の先頭は「更新内容は以下のとおりです。」としてください。 | |
- リスト形式で更新内容を出力してください。 | |
- 1つのリストアイテムには1つの更新内容を出力してください。 | |
- 日本語で出力してください。 | |
- "です・ます調" で出力してください。 | |
''' | |
chatgpt_user_content = "### 入力(コンテンツのファイル名)\n\n" + content_path \ | |
+ "\n\n### 入力(「修正前のコンテンツ」と「修正後のコンテンツ」のdiffコマンドの結果)\n\n" + Path(diff_path).read_text() | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{"role": "system", "content": chatgpt_system_content}, | |
{"role": "user", "content": chatgpt_user_content}, | |
], | |
) | |
print(response.choices[0]["message"]["content"].strip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment