I am pretty new to setting up my bot - I used chatgpt to assist me with the setup.
I have a bot which responds when the user tags it with a query. The user can then reply to the bot's message, and the bot should continue the conversation. This happens, but the bot is including the previous response text in any follow-up messages.
this is my code:
context = {}
def chatgpt_response(prompt):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=100,
temperature=0.5
)
response_dict = response.get("choices")
if response_dict and len(response_dict) > 0:
prompt_response = response_dict[0]["text"]
return prompt_response[:2000]
async def on_message(self, message):
if message.author == client.user:
return
if client.user in message.mentions:
user_message = message.content.replace(f"<@!{client.user.id}> ", "")
if message.author.id in context:
context[message.author.id]["prompt"] += user_message
bot_response = chatgpt_response(prompt=context[message.author.id]["prompt"])
else:
bot_response = chatgpt_response(prompt=user_message)
context[message.author.id] = {"prompt": user_message, "response": bot_response}
await message.channel.send(bot_response)
elif message.author.id in context:
context[message.author.id]["prompt"] += message.content
bot_response = chatgpt_response(prompt=context[message.author.id]["prompt"])
await message.channel.send(bot_response)