LoginSignup
2
1

More than 1 year has passed since last update.

ChatGPTをPythonから呼び出す

Posted at

PythonからChatGPTを含むOpenAIの各種モデルのAPIを叩く方法。

APIは課金用の請求先を登録する必要がある. APIは月額課金ではなく従量課金になっており、gpt-4で750 word=0.03USD と普通に使う分にはそこまで高くない。
https://openai.com/pricing

  1. Set up paid accountから請求先のクレジットカード情報を登録する。
    https://platform.openai.com/account/billing/overview

  2. APIキーを作成する。
    https://platform.openai.com/account/api-keys

  3. ライブラリをインストールする。
    pip install openai

  4. 以下を実行する。

import openai

openai.api_key = "<YOUR API KEY>"

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    # model="gpt-4",
    messages=[
        {"role": "user", "content": "How to use chatgpt?"},
    ],
)
print(response.choices[0]["message"]["content"].strip())

使用できるモデルの一覧

openai.Model.list()

APIリファレンス
https://platform.openai.com/docs/api-reference

2
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
2
1