5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Chat GPTをPythonから呼び出すプログラム(API)

Posted at

◯ライブラリのインストール

!pip install openai

◯APIキーの取得
以下のウェブサイトでアカウント登録をした上で、ユーザーアカウント管理画面から、API KEYを取得。

◯Pythonプログラム
(クエリを入力すると、ChatGPTに接続して、レスポンスを表示)

import openai
openai.api_key = "------取得した自分のAPIキーをここに入れる--------"

query = input()
completion = openai.ChatCompletion.create(
    model = "gpt-3.5-turbo",
    messages=[
        {"role":"user", "content": query }
    ]
  )
chat_response = completion.choices[0].message.content
print(str(chat_response))

※上記プログラムの利用は、自己責任でお願いします。

5
9
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
5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?