LoginSignup
6
4

More than 1 year has passed since last update.

続:ChatGPTのAPI調査・その2

Last updated at Posted at 2022-12-02

速報:ChatGPTのAPIを叩いてみた
続:ChatGPTのAPI調査

ChatGPTのAPI調査の続き・その2です。

api

curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7}'

curlで投げて確かめます。先日取得したAPIキーをYOUR_API_KEY部分に置き換えます。
Bearerの部分も置き換えたらAPIエラーなったので、Bearerは残して、YOUR_API_KEYの部分のみ置き換えます。

{
    "id": "cmpl-GERzeJQ4lvqPk8SkZu4XMIuR",
    "object": "text_completion",
    "created": 1586839808,
    "model": "text-davinci:003",
    "choices": [
        {
            "text": "\n\nThis is indeed a test",
            "index": 0,
            "logprobs": null,
            "finish_reason": "length"
        }
    ],
    "usage": {
        "prompt_tokens": 5,
        "completion_tokens": 7,
        "total_tokens": 12
    }
}

Say this is a testと言ったので、This is indeed a testと返ってきました。

model list

curl https://api.openai.com/v1/models \
  -H 'Authorization: Bearer YOUR_API_KEY'
{
  "data": [
    {
      "id": "model-id-0",
      "object": "model",
      "owned_by": "organization-owner",
      "permission": [...]
    },
    {
      "id": "model-id-1",
      "object": "model",
      "owned_by": "organization-owner",
      "permission": [...]
    },
    {
      "id": "model-id-2",
      "object": "model",
      "owned_by": "openai",
      "permission": [...]
    },
  ],
  "object": "list"
}

モデルの一覧が返されました。

Completions

https://beta.openai.com/docs/api-reference/completions
最初のやつです。

curl https://api.openai.com/v1/completions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "model": "text-davinci-003",
  "prompt": "Say this is a test",
  "max_tokens": 7,
  "temperature": 0
}'
{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "\n\nThis is indeed a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}

Create edit

curl https://api.openai.com/v1/edits \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "model": "text-davinci-edit-001",
  "input": "What day of the wek is it?",
  "instruction": "Fix the spelling mistakes"
}'
{
  "object": "edit",
  "created": 1589478378,
  "choices": [
    {
      "text": "What day of the week is it?",
      "index": 0,
    }
  ],
  "usage": {
    "prompt_tokens": 25,
    "completion_tokens": 32,
    "total_tokens": 57
  }
}

What day of the wek is it? のwekのスペルミスをFix the spelling mistakesの通り、weekに修正してくれました。

Create image

curl https://api.openai.com/v1/images/generations \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "prompt": "A cute baby sea otter",
  "n": 2,
  "size": "1024x1024"
}'
{
  "created": 1669982742,
  "data": [
    {
      "url": "https://URL1"
    },
    {
      "url": "https://URL2"
    }
  ]
}

A cute baby sea otterと指定したので、かわいい赤ちゃんラッコのURLを返してくれました。
このURLに大量アクセスされたら課金されても困るので、保存してアップしました。
クレジットカードとか登録してませんが、電話番号入れたので念の為。
1.png
2.png
どちらもかわいい赤ちゃんラッコです。

A cute baby dogに変えてやってみたら
3.png
4.png

更にA cute baby picachuとピカチューを指定すると
5.png
6.png
まさかの実写版ピカチューさんも生成してくれます

その他

後、画像合成とかファインチューニングとか色々あります。ファインチューニングは、既にあるモデルに追加で学習させるやり方です。
又、色々と探ってみます。

ChatGPTの中身についてはこちらにまとめました。
必見:ChatGPTのテクノロジーの凄さについて

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