In this very short article I want to show how anyone can create their own Telegram bot, which will be an intermediary for communication between humans and the GPT3.5 model (at the moment). Yes, I, like everyone else, was patient for a long time, but finally succumbed to this madness too, though I didn’t want to at first. At present, there are a lot of different tools and ready-made programs which use OpenAI’s GPT model one way or another. What is the fundamental difference between what I want to demonstrate to the community today? And the difference is that firstly, I want to show how to make your own bot, which will be personally yours. Secondly, all you need to do is to create two simple functions and do the initialization. And third, by adding this bot to a group chat you will be able to communicate with it all over the place. And this is not the same as one-to-one communication. When other users see a question from one of the participants, they voluntarily and involuntarily come up with new thoughts and ideas about how to complement the question of the previous user. And the one who asked the first question, seeing what imagination his friends in the chat have shown, just the same can be even more interesting to develop his original idea. In such a collaboration with GPT, a few people can find much more interesting and useful applications for the neural network than if everyone was doing it alone. Well, let’s do it!
Installation
Installation
PacletInstall["ChristopherWolfram/OpenAILink"];PacletInstall["KirillBelov/Objects"];PacletInstall["KirillBelov/TelegramBot"];
Needs
Needs
After installation, you must load into the current session:
<<KirillBelov`TelegramBot`<<ChristopherWolfram`OpenAILink`
GPT3.5 Simple Interface
GPT3.5 Simple Interface
Next step. You need to create a wrapper over the functions from the OpenAILink package, such that it can save previous requests and responses in the history. This function should take a chat ID and text as input and return only text:
chatGPT[chatId_,text_String]:=Module[{userMessage,assistantMessage,chat,errorText=""},If[Not[KeyExistsQ[$chats,chatId]],$chats[chatId]={}];chat=$chats[chatId];userMessage=OpenAIChatMessageObject["user",text];AppendTo[chat,userMessage];assistantMessage=Check[OpenAIChatComplete[chat],chat={userMessage};errorText="[HISTORY REMOVED]\n";OpenAIChatComplete[chat],OpenAIChatComplete::openAIResponseFailureMessage];AppendTo[chat,assistantMessage];(*Return*)$chats[chatId]=chat;errorText<>assistantMessage[[-1,"Text"]]]
An important point. The fact is that when the total number of tokens in the GPT3.5 model request exceeds 4096, an error occurs. If this error occurs - it means that it is necessary to clear the chat history. Here in the line with the Check function does exactly that. The moment when the chat is full - then all is deleted and is only the last message to which the model should respond.
TelegramBot Update Handler
TelegramBot Update Handler
And this part is related to interaction with Telegram Bots API. We need to create a function that takes as input the bot itself (more precisely, its internal representation in the Wolfram language) and an association that contains data about the message that was sent to the chat and that the bot “reads”:
handler[bot_TelegramBot,update_Association]:=Module[{user,chatId,text,result},chatId=update["message","chat","id"];text=update["message","text"];Print[update];result=chatGPT[chatId,text];Print[sendMessage[bot,chatId,result]];]
Run
Run
And the last step. Here you need to set your own token values, which are used to access the OpenAI API and for the token that is used to interact with Telegram. In addition, a temporary chat storage is created. The very last line launches the bot every 5 seconds for the next 7 days:
$chats=<||>;$OpenAIKey="<your OpenAI token>";wlgptbot=TelegramBot["<yourTelegramBot token>"];task=CreateBotSession[wlgptbot,handler,{5,(60/5)*60*60*24*7}];
Done! If you want to add your bot in the general chat, then do not forget to give him permission in the settings to read public chats, as well as better make him the administrator of this chat.
Conclusion
Conclusion
Thank you all for your attention! I hope the reader will find something useful. I want to add that when I wrote this code, I was pleased once again with how cool, clear and compact it is to write programs in Wolfram Language. Basically there are three lines with settings and two with actual functions that implement the simple business logic of the application. On the other hand I was saddened as always by the fact that unfortunately Wolfram language is not as popular as it really deserves...
By the way, everyone can try how it works either in the group chat at the link https://t.me/WolframMathematicaRU or you can write to this bot from this chat in private (but then unfortunately, I will not find out how interested in this at all and will lose all the charm of the collaboration of users). Do not be confused by the fact that previous users communicated in Russian. I’m the administrator of this chat room and you can write there in any language and on almost any topic (except insults and such, of course).
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Create your own Telegram GPT3 .5 bot
by Kirill Belov
Wolfram Community, STAFF PICKS, April 13 2023
https://community.wolfram.com/groups/-/m/t/2897457
by Kirill Belov
Wolfram Community, STAFF PICKS, April 13 2023
https://community.wolfram.com/groups/-/m/t/2897457