/typetalk-bot-chatgpt

Create a Typetalk bot using OpenAI's ChatGPT API.

Primary LanguagePythonMIT LicenseMIT

Typetalk bot using ChatGPT API

Create a Typetalk bot using OpenAI's ChatGPT API.

This Typetalk bot operate according to the following specifications.

  • Follow the tone and answers according to the pre-defined character settings.
  • Summarise the message as a start point of conversation in approximately 30 words and set tag.
  • Create conversation flow by passing the part of conversation thread to the ChatGPT API each time you speak.
  • The conversaton history pass to ChatGPT API limit latest four messages.
    • Because of the API sepcification that the maximum number of token is 4,096 for the conversation history,questions, and answers combined.
    • You can handle this bot specfication by changing max_tokens parameter of ChatGPT API and pre-defined character settings.

Wait a few seconds to receive a response from the ChatGPT API. Typetalk bot using ChatGPT API

Setup document

Japanese version is on Qiita.

Recommended System Requirements

  • Python 3.11.3 (Python 3.9, 3.10 is also OK.)
    • bottle 0.12.25
    • openai 0.27.6
  • ngrok 3.3.0

Requirement API Keys or Tokens

OpenAI API Key

Create your OpenAI API account and API Key.

Typetalk Token

Create your Nulab account and singin to the Nulab account from Typetakl.com.

Create Typetalk bot

  1. Create a new topic
  2. Add a bot and get Typetalki token
    1. Check both API Scope
    2. Not Check Using Outgoing Webhook
    3. After clicking on the Create button, you will receive a Typetalk Token on the bot detailed view.
  3. Copy the Typetalk token from the bot detailed view, and paste it TYPETALK_TOPIC_ID in const.py.

ngrok token

Create your ngrok account and download ngrok binary, crate configuration file following setup document. You can also download ngrok binary for each OS from Install ngrok page.

Setting environment variables

Add environment variables to your shell dotfile like .zshenv. You can find the Typetalk topic ID on the bot detailed view page.

export OPENAI_API_KEY="yyyy"
export TYPETALK_TOKEN="xxxx"
export TYPETALK_TOPIC_ID="zzzzzz'

Apply shell configuration.

exec $SHELL

Installing Python Packages

pip3 install --upgrade pip
pip3 install -r requirements.txt

Starting bottle

python3 index.py
Server Start
Bottle v0.12.25 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8080/
Hit Ctrl-C to quit.

Starting ngrok

I reccomend you to open this on the another console window or tab.

ngrok http 8080
Session Status                online
Account                       [YOUR EMAIL_ADDRESS] (Plan: Free)
Version                       3.3.0
Region                        Japan (jp)
Latency                       34ms
Web Interface                 http://127.0.0.1:4040
Forwarding                    https://xxxx-xxx-xxx-xxx-xxx.ngrok-free.app -> http://localhost:8080