I am writing a telegram bot based on OpenAI. There is a problem with multithreading. When one user asks the bot, another person can get the same information. For example: First user: Do you know Konstantin Polukhin? Bot: Yes, and begins to describe it..
The second user: Do you respect him? Bot: Yes, I know Konstantin Polukhin.
It is necessary to make sure that the data does not overlap and the bot can say something related to the request of another user.
I tried many methods that were suggested, but none helped.
Code:
from aiogram import Bot,types
import openai
import requests
from googletrans import Translator
from aiogram.utils import executor
from aiogram.dispatcher import Dispatcher
TOKEN = " "
bot = Bot(token=TOKEN)
openai.api_key = " "
dp = Dispatcher(bot)
my_list = [" ", " ", ""]
@dp.message_handler(content_types = ["text"])
async def start(message: types.Message):
#print("\n" + my_list[0] + "\n" + my_list[1])
translator = Translator()
dest_language = "en"
translated_text = translator.translate(message.text, dest=dest_language).text
my_list[2] = "\n\nHuman: " + translated_text + "\n\nAI: "
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"{my_list[0] + my_list[1] + my_list[2]}",
temperature=0.5,
max_tokens=1024,
top_p=1.0,
frequency_penalty=0.5,
presence_penalty=0.0)
dest_language_2 = "ru"
translated_text1= translator.translate(text=response['choices'][0]['text'], dest=dest_language_2).text
await message.answer(translated_text1)
my_list[1] = "\n\nAI: " + response.choices[0].text
my_list[0] = "\n\nHuman: " + translated_text
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=False)```