LoginSignup
3
4

More than 1 year has passed since last update.

PythonでOpenAI APIを実行する方法

Last updated at Posted at 2023-04-16

この記事の目的

  • Python環境を作成して、OpenAI APIを実行できるようになる

所要時間

大体10分くらい

前提

  • pythonの実行環境が構築されていること

Pythonの開発環境を用意しよう!(Mac)

この記事を書いた際の動作環境

Python 3.10.4
pip 22.3.1 

OpenAIのAPIKeyを発行する

このページで発行できる

image.png

ライブラリをインストール

pip install openai

コード書いて実行

chatgpt-api.py

import openai

openai.api_key = '[発行したAPIKEY]'

response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=[
                {'role': 'user', 'content': 'ハローワールド!!'}],
                temperature=0.0,
)

print(response['choices'][0]['message']['content'])

実行結果

こんにちは、私はAIアシスタントです。どうかお気軽にお話ししてください。

ChatGPTくんこれから君を使い倒します!よろしくお願いします!!
という感じで、API実行できました。
次の記事でAPIの中身を見ていこうと思います!

参考

3
4
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
3
4