Last active
February 4, 2024 01:15
-
-
Save nano-sudo/5dc5f7cd1f805b206bef1041b4d06156 to your computer and use it in GitHub Desktop.
openaiのfine_tuningの動作確認用コード
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
# 動作確認用コード | |
# 適宜printをつけて出力を確認するか、jupyter notebook上で動かしてください。 | |
# https://zenn.dev/nano_sudo/articles/eaf0d77646d7b8 | |
import openai | |
client = openai.OpenAI(api_key="sk-...") | |
client.api_key = "sk-..." # この書き方でもAPIキーを指定できる。(上で書いた場合は不要) | |
# ファイルをアップロード | |
client.files.create( | |
file=open("path/to/file", "rb"), | |
purpose="fine-tune" | |
) | |
# ファイルを一覧表示 | |
client.files.list() | |
# ファインチューニング ジョブを開始 | |
client.fine_tuning.jobs.create( | |
model="gpt-3.5-turbo", | |
training_file="file-id", | |
) | |
# ファインチューン済みモデルの動作確認 | |
completion = client.ChatCompletion.create( | |
model="ft:gpt-3.5-turbo-0613:xxxx", | |
messages=[ | |
{"role": "system", "content": "<システムプロンプト>"}, # データセットで使用したシステムプロンプトを使うことが好ましい | |
{"role": "user", "content": "<ユーザープロンプト>"} | |
] | |
) | |
print(completion.choices[0].message) | |
# おまけ | |
# ファインチューニング ジョブを一覧表示 | |
client.fine_tuning.jobs.list() | |
# ジョブIDからファインチューニングの情報を表示 | |
client.fine_tuning.jobs.retrieve("job_id") | |
# ファインチューニング ジョブをキャンセル | |
client.fine_tuning.jobs.cancel("job_id") | |
# モデル一覧を表示 | |
client.models.list() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment