Skip to content

Commit

Permalink
Include historical messages
Browse files Browse the repository at this point in the history
  • Loading branch information
buckyroberts committed Aug 6, 2024
1 parent 12d8932 commit 75b9491
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion thenewboston/discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ async def ia_command(ctx, *, text):
await ctx.reply('Please, register at https://thenewboston.com')
return

historical_messages = await get_historical_messages(ctx)

# TODO(dmu) HIGH: Interact with OpenAI in async way
response = OpenAIClient.get_instance().get_chat_completion(
settings.DISCORD_CREATE_RESPONSE_PROMPT_NAME,
input_variables={'text': text},
input_variables={
'messages': historical_messages,
'text': text
},
tracked_user=user,
tags=['discord_bot_response']
)
Expand All @@ -48,5 +53,22 @@ async def ia_command(ctx, *, text):
await ctx.reply('Oops.. Looks like something went wrong. Our team has been notified.')


async def get_historical_messages(ctx):
results = []

async for message in ctx.channel.history(limit=10):
if '_ia' in str(message.author):
results.append({'role': 'assistant', 'content': [{'type': 'text', 'text': message.content}]})
else:
content = message.content

if content.startswith('/ia'):
content = content[3:].strip()

results.append({'role': 'user', 'content': [{'type': 'text', 'text': content}]})

return results[::-1]


if __name__ == '__main__':
bot.run(settings.DISCORD_BOT_TOKEN, log_handler=None)

0 comments on commit 75b9491

Please sign in to comment.