LoginSignup
0
0

More than 1 year has passed since last update.

GoogleスプレッドシートでChatGPTを使う(GASソースコード部分)

Posted at

GoogleスプレッドシートでChatGPTを利用する

code.gs
// ChatGPTに必要な情報の設定
var apikey = "こちらにOpenAIのAPI Keyを貼り付ける";
var model = "gpt-3.5-turbo";
var temperature = 0;
var url = "https://api.openai.com/v1/chat/completions";

function GPT(content, maxTokens=2048) {
  if(content){
    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)
    };

    var response = JSON.parse(UrlFetchApp.fetch(url, requestOptions).getContentText());

    return response.choices[0].message.content.trim();

  }else{
    return "no result";
  }
}
0
0
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
0