LoginSignup
4
2

【GPT-4o+GAS+LINEbot】GPT-4oの画像入力対応のLINEbot

Last updated at Posted at 2024-05-15

今、大いに話題になっているGPT-4oを使ったLINEbotを作成してみました!

マルチモーダルなので以前作成したGemini対応版を改良して作成しました。

GPT-4o体験ボット
https://lin.ee/0WcZ3HG2
image.png

下記が、作成方法です!

作成したものがこちら!
(デプロイ⇒webアプリのURLをLINEdeveloperのwebhookに張り付けると完成!)

画像を渡す前にImgAppライブラリで画像を縮小して渡しております。
(値段節約のため。。。)

Geminiと違って、画像をURLで渡せるのでそこをちょっと変えております。

GAS
function GPT4oImg(message, fileUrl,fileID,i) {
  const options = {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
    authorization: 'Bearer ' + ChatGPT_APIKEY
  },
  muteHttpExceptions: true,
  payload: JSON.stringify({
    "model": "gpt-4o",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": message
          },
          {
            "type": "image_url",
            "image_url": {
              "url": fileUrl
            }
          }
        ]
      }
    ],
    "max_tokens": 300
  })
};

 const response = UrlFetchApp.fetch('https://api.openai.com/v1/chat/completions', options);
 const chatreply = JSON.parse(response.getContentText());

  //回答の抽出はドキュメントに方法が記載されてます
  //https://platform.openai.com/docs/guides/chat/introduction
       UserSheet.getRange(i+1, 10, 1, 2).clearContent();
       var image = DriveApp.getFileById(fileID);
       image.setTrashed(true);
  return chatreply['choices'][0]['message']['content'].trim();
}

画像を送ったときは一問一答のゼロショットプロンプトにしております。

もうちょっときれいにできそうですが、現状はここまでです💦

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