🙃

ChatGPT API の使い方(Google Apps Script)

2023/03/02に公開

ChatGPT APIをGoogle Apps Script(Javascript)で動かしてみた

const SECRET_KEY = "sk-xxxxxxxxxx"; // OpenAIからとってきてね

function Gpt35turbo(role,prompt) {
  const url = "https://api.openai.com/v1/chat/completions";
  const payload = {
      model: 'gpt-3.5-turbo',
      "messages": [
        {"role": "system", "content": role},
        {"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
}

ChatGPTに対して役割を強めに強制できるのでCoTが捗りますね。

関連記事
https://platform.openai.com/docs/guides/chat
https://note.com/npaka/n/n0dd2b226e8ae
https://qiita.com/sakasegawa/items/db2cff79bd14faf2c8e0

Discussion