2
2

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.

スプレッドシートにChatGPT(gpt-3.5-turbo)

Posted at

スプレッドシートに、ChatGPTでテキスト表示させるやつ。(gpt-3.5-turbo)

備忘録で載せておく


スプレッドシートの拡張機能からGASを開く

以下コードを書く

const SECRET_KEY = "OPENAIで取得したAPI";
const MODEL_NAME = "gpt-3.5-turbo"; // more structured and deterministic: for data

function GPT(prompt="どうもChatGPTです", max_tokens=30) {
  const url = "https://api.openai.com/v1/chat/completions";
  const payload = {
    model: MODEL_NAME,
    messages: [{role: "user", content: prompt}]
  };

  const options = {
    contentType: "application/json",
    headers: { Authorization: "Bearer " + SECRET_KEY },
    payload: JSON.stringify(payload),
  };

  const res = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
  return res.choices[0].message.content.trim();
}

prompt は、おはよう、なら朝っぽい返事になる。とか、そういうことらしい by ChatGPT


スプレッドシートのセル

以下の場合

C2 = IF(ISBLANK($B2),"",GPT($B2&"の"&C$1&"を教えてください:",3000))

D2 = IF(ISBLANK($B2),"",GPT($B2&"の"&D$1&"を教えてください:",3000))

のようになってます

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?