LoginSignup
0
1

More than 1 year has passed since last update.

Google ドキュメントでChatGPTを使う(Google Apps Scriptを使ってChatGPTを操作する) 1

Last updated at Posted at 2023-03-11

Google Apps ScriptでChatGPT APIを使う

code.gs
function ChatGPT() {
  // API keyとmodelを設定
  var apiKey = "こちらにOpenAIのAPI Keyを貼り付ける";
  var model = "gpt-3.5-turbo";
  

  // ChatGPTに投げる質問/必要な情報を設定
  var content = "APIについての記事を300文字以内で書いてください";
  var temperature= 0; // 生成する応答の多様性
  var maxTokens = 2048; // 生成する応答の最大トークン数(文字数)
  var url = "https://api.openai.com/v1/chat/completions";

  const requestBody = {
    "model": model,
    "messages": [{'role': 'user', 'content': content}],
    "temperature": temperature,
    "max_tokens": maxTokens,
  };
  const requestOptions = {
    "method": "POST",
    "headers": {
      "Content-Type": "application/json",
      "Authorization": "Bearer "+apiKey
    },
    "payload": JSON.stringify(requestBody)
  }

  // APIを叩いてChatGPTから回答を取得
  var response = UrlFetchApp.fetch(url, requestOptions);
  var responseText = response.getContentText();
  var json = JSON.parse(responseText);
  var result_text = json.choices[0].message.content.trim();

  console.log(result_text)
}
0
1
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
0
1