LoginSignup
6
5

More than 1 year has passed since last update.

【ChatGPT】話題のChatGPT APIを10分で使ってみる

Last updated at Posted at 2023-03-27

QiitaもZennもTwitterなどのエンジニア界隈だけでなく、
Yahoo!ニュースやテレビで取り上げられるほどChat GPTで盛り上がっていますね。

(個人的には鬱陶しいと思いつつも) 気になったのでChat GPT APIを使ってみました。

参考にしたもの

とりあえず何かサンプルが欲しかったので、
Youtubeで「Chatgpt api python how to」で調べて一番最初に出たShortsを参考にしました。

APIキーを取得

OpenAIでAPI KEYを取得

アカウントは既に作成済みだったので作成方法は割愛
ページにアクセスしてポップアップに従って進めるだけ。簡単!!

プログラム書く

一応APIKEYはプログラムファイルとは別で管理

apikey.py
APIKEY = "取得したAPIKEYはここに書いてね"

で、プログラムファイルを作成

example.py
import openai
from apikey import APIKEY

openai.api_key = APIKEY

output = openai.ChatCompletion.create(
    model="gpt-3.5-turbo", 
    messages = [
        {
            "role" : "user",
            "content" : "Write me a script for hosting a conference on technology" # 技術に関する会議を開催するための台本を書いてください
        }
    ]
)

print(output)

失態

example.py実行してみたらエラーが出た

AttributeError: module 'openapi' has no attribute 'ChatCompletion'

原因はexample.pyに書いているopenaiを「openapi」とタイポしていた。

できた!

直して再実行

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "Welcome everybody to the annual technology conference. It's a pleasure to see such a diverse group of experts come together to share their knowledge and experience to shape the future of technology.\n\nAs we all know, technology is advancing rapidly day by day, and it is essential to keep ourselves updated with the latest trends and innovations. This conference aims to provide a platform for all the participants to discuss technological advancements in various domains and to collaborate towards finding solutions to challenges.\n\nWe have an exciting lineup of speakers who will be sharing their insights, experiences, and thoughts on technology, and how it's shaping our world today. Our keynote speakers will cover essential topics such as AI, cybersecurity, blockchain, and many others.\n\nWe also have panel discussions where experts will come together and discuss some of the most pressing issues related to technology, and how we can tackle them together. This conference will also provide an opportunity for networking, making new connections, and catching up with old ones.\n\nAs we continue with the conference, we urge all the participants to take an active role in the discussions and share their thoughts and ideas. We believe that this will help us gain more insights, spark innovative ideas, and help us collectively drive technology advancements that will shape our world's future.\n\nWe hope that you all will find this conference informative and engaging. Let's work together to create a better future through technology. Thank you.",
        "role": "assistant"
      }
    }
  ],
  "created": 1679926083,
  "id": "chatcmpl-6yhnvB3SIkrzAtRQ4ieuHm3dp3uKh",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 275,
    "prompt_tokens": 18,
    "total_tokens": 293
  }
}

出力が長いせいか実行完了するまで30秒ほどかかりました。
gpt-4.0なら改善されるのかな?

ちなみにドキュメントは以下っぽい
https://platform.openai.com/docs/api-reference/models/retrieve

GPT-4はいつ使えるのか?

modelをgpt-4にしてみたが以下の結果。

openai.error.InvalidRequestError: The model: `gpt-4` does not exist

Waitlistで予約しておくと、順次使えるようになるっぽい

気長に待ちます

ここまでかかった時間、約10分!!

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