Home technology How to run all the applications of GPT3 from a single telegram bot of your own

How to run all the applications of GPT3 from a single telegram bot of your own

by satcit

Hi everyone.. I recently gained access to GPT-3 API. GPT-3, which is the world’s most powerful language generator AI is nothing short of a miracle. It can write poems, stories and even computer code. However, I found it is a bit difficult for laymen like me to play with it.

So, with the help of Node Red which is running 24*7 on my Linux server, I built a telegram bot from which I can use all the applications of GPT-3 (I mean all of these https://beta.openai.com/examples) by simple on and off commands for each function. Here is the Node Red flow for two of the applications – chat bot and Q&A bot. I can add all the applications to this flow, but I didn’t do that for the sake of simplicity. Moreover, adding any GPT-3 application to this flow will be just be repititions of what is given here.

Screenshot from 2021-10-21 21-45-32

First of all, install Node Red on you computer or server.

Then you have to add these nodes to your Node Red palette.

node-red-contrib-telegrambot ( node-red-contrib-telegrambot (node) – Node-RED )
node-red-contrib-simple-gate ( node-red-contrib-simple-gate (node) – Node-RED )

Go to Manage Palette>Install and search for these modules namely node-red-contrib-telegrambot and node-red-contrib-simple-gate

After that, just copy the below code and import it in Node Red.

[{"id":"679aeaba.495934","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"abf78001a603df16","type":"http request","z":"679aeaba.495934","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.openai.com/v1/engines/davinci/completions ","tls":"","persist":false,"proxy":"","authType":"","x":890,"y":40,"wires":[["a1c5bc51c3239088"]]},{"id":"a1c5bc51c3239088","type":"change","z":"679aeaba.495934","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.choices[0].text","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":40,"wires":[["b9e2aec377884ea5"]]},{"id":"88aabea7319d9816","type":"function","z":"679aeaba.495934","name":"Chat bot","func":"msg.payload = ` The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\\n\\nHuman: Hello, who are you?\\nAI: I am an AI created by OpenAI. How can I help you today?\\nHuman:${msg.payload}\\nAI:`;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":180,"wires":[["74a1d8b330ceb8d4"]]},{"id":"74a1d8b330ceb8d4","type":"function","z":"679aeaba.495934","name":"set payload and headers","func":"msg.payload = {\n  \"prompt\": msg.payload,\n  \"temperature\": 0.9,\n  \"max_tokens\": 150,\n  \"top_p\": 1,\n  \"frequency_penalty\": 0.0,\n  \"presence_penalty\": 0.6,\n  \"stop\": [\"\\n\", \" Human:\", \" AI:\"]\n};\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.headers['Authorization'] = 'Bearer APIKEY';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":650,"y":100,"wires":[["abf78001a603df16"]]},{"id":"bb97445894cbd350","type":"change","z":"679aeaba.495934","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":220,"wires":[["94525358.b338a","fe1beb85.909348","1ec27295.42bfcd","635eca65.b4eb64"]]},{"id":"b9e2aec377884ea5","type":"function","z":"679aeaba.495934","name":"Unnamed function","func":"msg.payload= {\n    chatId: '100786515',\n    type: 'message',\n    content: msg.payload\n   \n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1210,"y":340,"wires":[["65c957a34cd33f54"]]},{"id":"65c957a34cd33f54","type":"telegram sender","z":"679aeaba.495934","name":"","bot":"","haserroroutput":false,"outputs":1,"x":1210,"y":440,"wires":[[]]},{"id":"bed65cf37977c4aa","type":"function","z":"679aeaba.495934","name":"Q&A","func":"msg.payload = ` I am a highly intelligent question answering bot identifying myself as a female. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with \\\"Unknown\\\".\\n\\nQ: What is human life expectancy in the United States?\\nA: Human life expectancy in the United States is 78 years.\\n\\nQ: Who was president of the United States in 1955?\\nA: Dwight D. Eisenhower was president of the United States in 1955.\\n\\nQ: Which party did he belong to?\\nA: He belonged to the Republican Party.\\n\\nQ: What is the square root of banana?\\nA: Unknown\\n\\nQ: How does a telescope work?\\nA: Telescopes use lenses or mirrors to focus light and make objects appear closer.\\n\\nQ: Where were the 1992 Olympics held?\\nA: The 1992 Olympics were held in Barcelona, Spain.\\n\\nQ: How many squigs are in a bonk?\\nA: Unknown\\n\\nQ:${msg.payload}\\nA:`;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":40,"wires":[["7538cabfe1f16da4"]]},{"id":"7538cabfe1f16da4","type":"function","z":"679aeaba.495934","name":"set payload and headers","func":"msg.payload = {\n  \"prompt\": msg.payload,\n  \"temperature\": 1,\n  \"max_tokens\": 100,\n  \"top_p\": 1,\n  \"frequency_penalty\": 0.0,\n  \"presence_penalty\": 0.0,\n  \"stop\": [\"\\n\"]\n};\nmsg.headers = {};\nmsg.headers['Content-Type'] = 'application/json';\nmsg.headers['Authorization'] = 'Bearer APIKEY';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":650,"y":40,"wires":[["abf78001a603df16"]]},{"id":"ad240551.041798","type":"telegram receiver","z":"679aeaba.495934","name":"","bot":"","saveDataDir":"","filterCommands":false,"x":150,"y":480,"wires":[["9f1fe317.ec5a9"],[]]},{"id":"94525358.b338a","type":"gate","z":"679aeaba.495934","name":"","controlTopic":"control","defaultState":"closed","openCmd":"++Q&A","closeCmd":"**Q&A","toggleCmd":"","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":130,"y":60,"wires":[["80551574.b14a28"]]},{"id":"fe1beb85.909348","type":"gate","z":"679aeaba.495934","name":"","controlTopic":"control","defaultState":"closed","openCmd":"++chat","closeCmd":"**chat","toggleCmd":"","defaultCmd":"","statusCmd":"status","persist":false,"storeName":"memory","x":450,"y":280,"wires":[["9333cc42.beacf"]]},{"id":"fcec9ec0.d4032","type":"switch","z":"679aeaba.495934","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"++Q&A","vt":"str"},{"t":"eq","v":"**Q&A","vt":"str"},{"t":"eq","v":"++chat","vt":"str"},{"t":"eq","v":"**chat","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":130,"y":320,"wires":[["bb97445894cbd350"],["bb97445894cbd350"],["bb97445894cbd350"],["bb97445894cbd350"]]},{"id":"9f1fe317.ec5a9","type":"change","z":"679aeaba.495934","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.content","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":190,"y":400,"wires":[["fcec9ec0.d4032","fe1beb85.909348","94525358.b338a"]]},{"id":"5057fcd0.8bcb34","type":"change","z":"679aeaba.495934","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Q&A mode is open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":240,"wires":[["b9e2aec377884ea5"]]},{"id":"1c4aa46c.c8c57c","type":"change","z":"679aeaba.495934","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Q&A mode is closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":300,"wires":[["b9e2aec377884ea5"]]},{"id":"1ec27295.42bfcd","type":"switch","z":"679aeaba.495934","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"++Q&A","vt":"str"},{"t":"eq","v":"**Q&A","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":690,"y":260,"wires":[["5057fcd0.8bcb34"],["1c4aa46c.c8c57c"]]},{"id":"80551574.b14a28","type":"switch","z":"679aeaba.495934","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"++Q&A","vt":"str"},{"t":"eq","v":"**Q&A","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":290,"y":60,"wires":[[],[],["bed65cf37977c4aa"]]},{"id":"635eca65.b4eb64","type":"switch","z":"679aeaba.495934","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"++chat","vt":"str"},{"t":"eq","v":"**chat","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":690,"y":320,"wires":[["13bf80bf.a76e0f"],["aa20bef8.af91b"]]},{"id":"13bf80bf.a76e0f","type":"change","z":"679aeaba.495934","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"chat mode is open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":360,"wires":[["b9e2aec377884ea5"]]},{"id":"aa20bef8.af91b","type":"change","z":"679aeaba.495934","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"chat mode is closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":400,"wires":[["b9e2aec377884ea5"]]},{"id":"9333cc42.beacf","type":"switch","z":"679aeaba.495934","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"++chat","vt":"str"},{"t":"eq","v":"**chat","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":510,"y":180,"wi
res":[[],[],["88aabea7319d9816"]]}]
Screenshot from 2021-10-21 22-19-35

In the ‘set payload and headers’ node, replace APIKEY with your GPT3 api key. (To get an API, you have to apply here https://beta.openai.com/)

Screenshot from 2021-10-21 22-23-37

In the telegram node, add the API credentials of your new telegram bot. You can create a new bot from within botfather bot (https://t.me/botfather)

Here is the finished bot..

photo_2021-10-21_22-29-18

In this example, I’ve set the command ++Q&A to start Q&A mode, **Q&A to stop the Q&A mode, ++chat to start chat mode and **chat to stop chat mode. Of course, you can set any command you like by modifying the concerned nodes in the Node Red flow.

Most of the people who are currently using the API are developers who make use of the API in their apps, websites, or their corporate servers. But there would be some people who would like to use the API but do not know how to code.

The bot I have shared above can be used by any layperson. A student can do grammar correction of his essay, a child can get a summary of a tough concept, a schoolgirl can get a snippet of code by simply sending a message to the bot, a shopkeeper can sort the items in his shop from an invoice, a doctor can generalise and find connections from his clinical records and so on.

Messaging a telegram bot is much easier than opening the browser and using the playground. Also telegram messages do not get deleted, and are easy to read and share from any mobile phone.

Also, in this setup, the application can be changed by a simple key word/ key phrase. For example, changing from chatbot to grammar correction mode is as simple as sending a key phrase to the bot.

This bot is for the non-coding layperson, who wants to experience the magic of GPT-3 in his or her daily life.

Leave a Comment