[ChatGPT+DB+LINE]自分のことをずっと覚えていてくれるキャラをノーコードで作ろう！

https://linedevelopercommunity.connpass.com/event/301002/

本資料はこちらのイベントのハンズオン資料となります。
過去の記憶を保持し、ずっと会話が続くBotを作りましょう！

2023/11/26 追記
ハンズオン動画が公開されました！

<iframe width="560" height="315" src="https://www.youtube.com/embed/uj3v_vgVzhc?si=L6yKPMrTlnVBoRS7" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>


【完成図】

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/e6f9deea-9f6b-c3dc-4041-adcdd671feae.png)

【事前準備】

①[LINE Developers](https://developers.line.biz/ja/)のアカウント設定
②[OpenAIのAPI_key](https://platform.openai.com/account/api-keys)取得
③[OpenAIのAPI](https://qiita.com/okinakamasayoshi/items/247c58342e5cf15c7bcd#%E3%82%AF%E3%83%AC%E3%82%B8%E3%83%83%E3%83%88%E7%99%BB%E9%8C%B2%E6%89%8B%E9%A0%86)クレジット登録
④[make(integromat)](https://eu1.make.com/)のアカウント作成
⑤[こちらのスプレッドシート](https://docs.google.com/spreadsheets/d/1G_NzYXLQdZbokQaL7pldrejoKqj1MCkmZVKal4gYx-w/edit?usp=sharing)をコピー
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/8afb9ece-9642-8404-2786-02f545e35631.png)


【説明補足資料】
・[LINE Developers公式アカウント設定手順](https://qiita.com/okinakamasayoshi/private/bf9593b2af20148c4d19)
・[OpenAI API_key取得手順](https://auto-worker.com/blog/?p=6988) 『OpenAIのAPIキーの発行手順』の章
・[OpenAIのAPIクレジット登録確認手順](https://qiita.com/okinakamasayoshi/items/247c58342e5cf15c7bcd)

事前準備後、下記4つのサービスを開いておいてください。

https://developers.line.biz/ja/

https://platform.openai.com/account/api-keys

https://eu1.make.com/

https://docs.google.com/spreadsheets/d/1G_NzYXLQdZbokQaL7pldrejoKqj1MCkmZVKal4gYx-w/edit?usp=sharing

::: note alert
本記事にはチャネルアクセストークン、OPEN APIKEYが記載されていますが、悪用を防ぐために、漏洩には十分注意してください。記事中のキーは全てハンズオン用に取得したものであり、イベント終了後は使用できません。
:::

## 1.イベントに参加いただくにあたり

### 今回の目標

今回のイベントでは
- **入力した文字がそのまま帰ってくるオウム返しLINE Bot**
- **LINEからChatGPTのAPIを叩き プロンプトに応じた返信**
- **プロンプトと会話を記憶させて ずっと会話が続く**

の、3つが目標です。

途中経過も含め、スクリーンショットや動画をXに  `#LINEDC`をつけて発信しよう！

---以下ハンズオンの作業資料---
---


## 2.おうむ返しBotの作成

まずはLINE Botの基本となるおうむ返しBotを作ります。
makeで作成しますので、知っている方はこの章はスキップして下さい。

 <details><summary>おうむ返しBotの作り方はこちら
 <font color="Red">(クリックで表示)</font></summary><div>

### 【事前準備】公式アカウント作成

[まずはこちらからこちらの手順](https://qiita.com/okinakamasayoshi/private/bf9593b2af20148c4d19)から公式アカウントを作成

◆このようなメッセージが返ってきたら成功です
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/c7275f72-e3f5-7a3c-a644-d45451768546.png)

### makeの設定

 [make](https://eu1.make.com/)にて 画面の通りに進めましょう

◆下記の通り進める
 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/fff9b824-b3db-b8ae-7434-e51f2fa53add.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/b9bd5ab4-e843-0be6-ee69-b16354898fe2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/abd19b27-64f0-3c98-a57f-966ce1af0ddf.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/6c63a6b1-91a5-43a5-82a3-1177ab9c6991.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/9209ddd4-c097-978f-6d4c-16f9f71167e0.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/e35909ad-46f6-8d58-a777-01fc7a2466e2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/d2dd762f-78c0-90c2-622d-851a2022047a.png)
 ◆ページを切り替えます
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/0b58ac48-04c9-f4eb-38a8-20c4a478a72d.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/20eb1db4-aca9-e7c1-f741-cb2d4550fd53.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/c6ed1fb4-7dd0-54e7-a3e9-7be67eed3edf.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/edba2dba-adad-5fef-2574-148c56ff6be5.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/7cd335c4-aebf-664f-c705-3c677e771b60.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/36ecdae4-58e0-ffe6-5060-2ff6b54953ce.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/b700528b-913a-bba7-f368-c945ceec3edc.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/4a9e3491-8367-f9e6-2780-8f3ecae1980e.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/cfe4e116-d6df-17a7-ada2-03947603c76c.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/66ea509a-56dc-c6fc-8ecd-a72098aa62ae.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/855baada-1364-b302-16ae-2e303d4ca551.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/3c4d5770-c736-9cec-a89d-284cc90e3908.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/fa2c3246-73ef-11ae-6d32-b0b798a5041d.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/96f50be5-dfb4-1729-d04f-dd773ff47d63.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/18663728-72ae-4c26-9458-438750431c6c.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/a1ab139c-92c5-3018-7018-67634346a746.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/f87ddbb4-74ea-8516-b5da-bffbbbf1c648.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/2c50dcda-9937-74a6-fd08-b392d958f240.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/7eec0999-7260-d78c-a655-ee505a537e81.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/2f100354-9ebc-c4f2-7cbd-320842267e56.png)

 <font color="Red">**◆今のところ毎回「Run Once」をクリックしないと動きません**</font>

 ◆Run Onceのあと「Queue」が表示されたら「Listen for new」 
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/00f0618b-0843-cd46-76d7-2d82f1474eec.png)
</div></details>

## 3.ChatGPTに返答させよう

◆目指す姿
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/4749530d-e2f9-a3c0-0ded-7d0707bf2881.png)

◆画面の通りに進めましょう
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/ae1baed9-7bf4-e98d-155d-156a8e87f923.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/df49ab06-495b-f6ba-c705-4bab45dbf038.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/d256a3a1-27cf-d589-0c4f-38abc9f2e03f.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/57f6b438-2ca8-c85c-13fe-ddb2361a61a9.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/0a665d0b-303a-14f3-67a9-bf290486dc51.png)

◆API Key Organization ID を取得しましょう
[こちら](https://platform.openai.com/api-keys)から取得します
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/b1208d22-3ad0-8073-f0c5-9f811c293543.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/157b5f0d-6ab2-f1c8-cf27-74ba6db978ab.png)

::: note alert
API keyは、他人に絶対に公開しないでください。悪用の恐れがあります。
:::

◆makeに戻って貼り付けます
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/3f46f2cd-fe99-4593-5d79-2b0cf84524e3.png)
◆もう一度API Keysの画面へ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/202f63db-9b72-ede4-12f3-d9b096ff4f10.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/fef801f9-c6e9-2574-08fc-545cf763daa2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/c81de07e-14b9-f3ef-1ec6-76ecbe7ca3e1.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/4e0b88af-f57b-8da3-b38d-8e5842e95e90.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/131b9b48-6d9f-82fc-d671-9f1ab1272cd8.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/f245e7a9-a3d0-d798-d002-629872089745.png)

◆続いて右側のLINEモジュールを開く
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/d8a52c68-c947-a343-7726-9109f951487d.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/da1f98fa-b2c1-c7eb-a03b-c07957fbde64.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/88b6c0d2-d2bc-e357-40ec-fd5feb467a2c.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/6aae34b5-a92f-b34f-996b-376f5c47846a.png)

◆Run Onceを押し、LINEにメッセージを送り、返信が来ればOKです！
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/4749530d-e2f9-a3c0-0ded-7d0707bf2881.png)

## 4.スプレッドシートに会話を記録しよう
◆目指す姿
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/67159624-dec0-ad81-74ec-dcdc8dada077.png)

◆画面の通りに進めましょう
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/9b16efa2-17b8-ce32-8aaa-2c19264ae84b.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/a992ea0e-417f-9d63-03b3-c81fb3e5f5a2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/18f7d2fa-9193-a514-07ab-ee7abda8b0c4.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/effbaf14-57d9-0c18-c785-f6872d32934c.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/cfd533ec-01e6-7265-8dc0-d2238439632e.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/fee27744-d43e-affa-511a-3fb51d4b0236.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/48a00db6-64a1-624d-2f27-79bb58fa7572.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/a4cb9369-d3c0-4530-9c49-a5e3fc66553c.png)

◆Run Onceを押し、LINEにメッセージを送り、返信が記録されていればOK！
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/49845081-3c4e-1d35-e9f3-a17320bc6fc0.png)

## 5.記録を呼び出して、会話がずっと続くBotを作ろう！
◆目指す姿
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/e6f9deea-9f6b-c3dc-4041-adcdd671feae.png)
◆画面の通りに進めましょう
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/694a907e-ffeb-655b-d738-33dd9aeeeed8.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/6bae0f34-1171-ab15-4657-1a40d10e2d51.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/e5aec487-3919-d0e9-d651-11b547e37b19.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/c65638c7-20ee-c39a-38f8-2d23321aeaa6.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/734c81b2-9032-cbeb-2930-29d4b8eee297.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/2db3732d-fda6-66b0-f729-bfb32978ff57.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/fe21b691-fca3-637e-1fa6-1a4206661c3e.png)
◆同じ要領でもう1セット作ります(中身は違うので注意)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/1854af98-6797-9269-9374-dbd6908f04b8.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/dcaa25bb-8f42-c7a8-d22c-2c91826428e3.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/a0560fd7-5c40-cc80-a4c2-9e8687b19891.png)
◆ここまで来たら一度Run onceしてメッセージを送ります
◆モジュール内の``text``の中身を分かりやすく表示するためです
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/55956a92-7336-b126-dcb7-0a2d5526a94e.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/050d4af3-bb6f-f6c2-d8db-5c96ee80fafc.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/38d27d0f-6e67-3989-77b7-16377381d1ab.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/ce538dd8-89b1-7465-b9be-3df5b3c4978a.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/abae85be-dd54-6ce1-e7fb-c2c1852dfbac.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/f95d84d5-c49c-8340-cfa8-0e8f6f6be873.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/5d876aa8-3650-5cd8-a96e-575f3f76ddb8.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/549fb296-8316-f953-a150-dd0daecdb63d.png)
◆これで完成！過去の記憶を覚えているBotが完成！
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/e6f9deea-9f6b-c3dc-4041-adcdd671feae.png)

## 6.補足説明・追加機能
### GPTに送り込んでいる情報
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/b556dabd-d50b-33e7-60c2-399eb5f08592.png)
``user``の中にLINEに過去にLINEに打ち込んだ情報をひとまとめに
``assistant``の中にGPTからの返答が毎回全て送り込まれるため
スプレッドシート内の過去の記憶を保持したBotが完成します。

### LINEの操作で記憶消そう
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/eefb272f-f017-dc14-297c-beb68f2336bd.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/b80250a7-1325-5efd-87dd-1c47b370e8c8.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/a0d85b81-0b34-6120-8b74-623e7147ea98.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/6d76094e-753d-3c0f-b584-ddbf478ad10e.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/b73e5eb0-2aec-2d14-c0cb-f8a4f29a65d0.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/6d31c6f7-3934-5bda-75e1-d241d19a6794.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/b7534546-f242-baf9-e7ee-a62678b0bb7d.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/7132fef8-c4e3-12bc-c1db-cdffcd5e45f2.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/39b70e64-cfa0-0238-4cd3-aadba442812a.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/8cff1b9f-9258-5c3e-5753-5c8fe2b7d54b.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/57536c5a-dd2a-7705-ceab-3b367b578670.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/52a0275a-64b3-1f09-2a0e-4786fddf2fa4.png)

### 永遠に会話が続くの？
``gpt-3.5-turbo``モデルでは 4097 トークンがリミットです
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/aac99714-93a2-4957-3246-bf4dc85e0b14.png)
``gpt-4-1106-preview``などに変更すると 読み込める会話の枠が拡大します
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1793443/5f2889aa-94bb-bb57-97a0-a94dc9565884.png)


## 7.LINE Botライフを楽しもう!!

 プライベートでもビジネスでも大活躍のLINE！

 企業用LINE Botを完全外注すると〇〇百万円なることもあります。
 ChatGPTも搭載した簡単なLINE Botくらいはまずは自分で作り、
 外注部分を最小化すれば、色々と可能性が広がりそうですよね。

 [LINEDCコミュニティ](https://linedevelopercommunity.connpass.com/)でも楽しいイベントが沢山ありますし、
 [YouTubeチャンネル](https://www.youtube.com/channel/UCZkYYwmvSA6y7WWLxM5x9IA)にはすぐ真似できる動画も盛り沢山!

 みなさんがオリジナルのLINE Botをどんどん生み出してくれると嬉しいです!
 またぜひどこかのイベントでお会いしましょう。ありがとうございました☻

 ◆おきなか[Twitterはこちら](https://twitter.com/okinakamasayos1)

 ◆LINE DC コミュニティ

https://linedevelopercommunity.connpass.com/

 ◆YouTubeチャンネル

https://www.youtube.com/channel/UCZkYYwmvSA6y7WWLxM5x9IA

 ◆私が通ったプロトタイピングスクール(おすすめ)

 https://protoout.studio/

 ◆最近色々なコミュニティも立ち上がりました！

 https://note.com/protoout/n/nebe3e2651bda
 
 https://note.com/m_okinaka/n/nf0cd83fba186

 過去の記事いろいろ

https://qiita.com/okinakamasayoshi/items/08d36273112000b7cd87

https://qiita.com/okinakamasayoshi/items/b3349556062fa7d3d8ab

https://qiita.com/okinakamasayoshi/items/478c9df40b608bc9ac09

https://qiita.com/okinakamasayoshi/items/01e0d454caea3a200b8e

https://protopedia.net/prototype/3769

https://protopedia.net/prototype/2636

Xもよければフォローしてね！

https://twitter.com/okinakamasayos1