LoginSignup
7
9

More than 1 year has passed since last update.

GPT-3を実際に使ってみた。

Last updated at Posted at 2021-06-20

背景

GPT-3のAPIがOpenAIによって一般に公開されたので実際につかってみました。

Open AI APIへ許可申請

OpenAI API (https://openai.com/blog/openai-api/)

JOIN THE WAITLISTを選択します。

英語でいろんな質問に答えていきます。

Submitしたら、登録したメールアドレスに申請の許可が来るのでそれまで待ちです。

ちなみに僕は1週間後に許可が出ました。

pythonでの使い方

openaiモジュールをインストール

$ pip install openai

OPENAI_API_KEYのところに自分のSecret API Key.を入力。

import os
import openai

# Load your API key from an environment variable or secret management service
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(engine="davinci", prompt="This is a test", max_tokens=5)

print(response)

エラー出力。What's up!?

openai.error.AuthenticationError: No API key provided. (HINT: set your API key in code using "openai.api_key = <API-KEY>", or you can set the environment variable OPENAI_API_KEY=<API-KEY>). You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions.

ご丁寧にヒントまでくれてます。

解決方法

#openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_key = "OPENAI_API_KEY"

レスポンスはこんな感じ

{
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "logprobs": null,
      "text": " of the emergency broadcast network"
    }
  ],
  "created": 1624131373,
  "id": "cmpl-3Cb3FU3ipYHjDBm72TkW5ou9uMxGO",
  "model": "davinci:2020-05-03",
  "object": "text_completion"
}

この" of the emergency broadcast network"が自動で生成したテキストです。

GPT-3の使い方

[GPT-3の使い方と概要] https://np-sys.com/gpt-3-introduction/

面白そうなのを以下にまとめてみました。

要約したい内容と、要約の仕方や状況を伝えてやると、それに応じた文章を生成してくれます。

Summarization

  • APIは、テキストのコンテキストを把握し、さまざまな方法で言い換えることが可能
My ten-year-old asked me what this passage means:

"""

A neutron star is the collapsed core of a massive supergiant star, which had a total mass of between 10 and 25 solar masses, possibly more if the star was especially metal-rich.[1] Neutron stars are the smallest and densest stellar objects, excluding black holes and hypothetical white holes, quark stars, and strange stars.[2] Neutron stars have a radius on the order of 10 kilometres (6.2 mi) and a mass of about 1.4 solar masses.[3] They result from the supernova explosion of a massive star, combined with gravitational collapse, that compresses the core past white dwarf star density to that of atomic nuclei.

"""

I rephrased it for him, in plain language a ten-year-old can understand:

"""
  • 要約したい対象をトリプルクオートで囲むのがコツ
import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
engine="davinci",
prompt="My ten-year-old asked me what this passage means:\n\"\"\"\nA neutron star is the collapsed core of a massive supergiant star, which had a total mass of between 10 and 25 solar masses, possibly more if the star was especially metal-rich.[1] Neutron stars are the smallest and densest stellar objects, excluding black holes and hypothetical white holes, quark stars, and strange stars.[2] Neutron stars have a radius on the order of 10 kilometres (6.2 mi) and a mass of about 1.4 solar masses.[3] They result from the supernova explosion of a massive star, combined with gravitational collapse, that compresses the core past white dwarf star density to that of atomic nuclei.\n\"\"\"\n\nI rephrased it for him, in plain language a ten-year-old can understand:\n\"\"\"",
temperature=1,
max_tokens=64,
top_p=0.88,
frequency_penalty=0,
presence_penalty=0,
stop=["\"\"\""]
)

tl;dr:を使って要約することもできる.

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
engine="davinci",
prompt="Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. It is named after the Roman god Jupiter.[19] When viewed from Earth, Jupiter can be bright enough for its reflected light to cast visible shadows,[20] and is on average the third-brightest natural object in the night sky after the Moon and Venus.\n\nJupiter is primarily composed of hydrogen with a quarter of its mass being helium, though helium comprises only about a tenth of the number of molecules. It may also have a rocky core of heavier elements,[21] but like the other giant planets, Jupiter lacks a well-defined solid surface. Because of its rapid rotation, the planet's shape is that of an oblate spheroid (it has a slight but noticeable bulge around the equator).\n\ntl;dr:",
temperature=0.3,
max_tokens=64,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n"]
)
tl;dr: Jupiter is a gas giant, the largest planet in the solar system. It is the fifth planet from the Sun and the largest in the solar system. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in

次は、性格や状況を伝え、Q&Aの例を与えてやると、それっぽい返答が返ってくるというもの。

Conversation

最低限で動かすならこれでもいいが,これは不十分な例.

以下はAIアシスタントとの会話です。アシスタントは親切で、創造的で、賢く、そしてとてもフレンドリーです。

人間:こんにちは、あなたは誰ですか?

AI:私はOpenAIによって作成されたAIです。今日はなんか手伝うことある?

人間:
  • APIに会話をしてくださいということだけではなく,動作方法も伝える
  • API側の名前をAIではなく,現実に存在した専門家の名前にする

上記を考慮するとより適切にはこのような例になる.

Marvは、しぶしぶ質問に答えるチャットボットです。

###

ユーザー:1キログラムは何ポンドですか?

マーブ:これも?キログラムには2.2ポンドあります。これをメモしてください。

###

ユーザー:HTMLは何の略ですか?

Marv:Googleは忙しすぎましたか?ハイパーテキストマークアップ言語。Tは、将来、より良い質問をしようとするためのものです。

###
ユーザー:最初の飛行機が飛ぶのですか?

マーヴ:1903年12月17日、ウィルバーとオーヴィルライトが初飛行を行いました。彼らが来て私を連れ去ってくれたらいいのにと思います。

###

ユーザー:宇宙で最初の人は誰でしたか?

マーブ:
import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
engine="davinci",
prompt="Marv is a chatbot that reluctantly answers questions.\n\n###\nUser: How many pounds are in a kilogram?\nMarv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\n###\nUser: What does HTML stand for?\nMarv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\n###\nUser: When did the first airplane fly?\nMarv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they’d come and take me away.\n###\nUser: Who was the first man in space?\nMarv:",
temperature=0.8,
max_tokens=64,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["###"]
)

入力文を工夫していろいろ引き出してみたい。

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