From d08a09717506fd6e29946d52a9176fdd2a84cbfe Mon Sep 17 00:00:00 2001 From: mudler Date: Wed, 23 Aug 2023 21:56:19 +0200 Subject: [PATCH] discord: fix interactions answers --- examples/discord/agent.py | 1 + examples/discord/main.py | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/discord/agent.py b/examples/discord/agent.py index 1fb32da..b09899b 100644 --- a/examples/discord/agent.py +++ b/examples/discord/agent.py @@ -19,6 +19,7 @@ from ascii_magic import AsciiArt from duckduckgo_search import DDGS from typing import Dict, List import os +from langchain.text_splitter import RecursiveCharacterTextSplitter import discord import openai import urllib.request diff --git a/examples/discord/main.py b/examples/discord/main.py index deff657..8af32d3 100644 --- a/examples/discord/main.py +++ b/examples/discord/main.py @@ -81,6 +81,7 @@ async def on_ready(): print(f"We have logged in as {client.user}") def run_localagi_thread_history(history, message, thread, loop): + agent.channel = message.channel def call(thing): return asyncio.run_coroutine_threadsafe(thing,loop).result() sent_message = call(thread.send(f"⚙️ LocalAGI starts")) @@ -115,6 +116,7 @@ def run_localagi_thread_history(history, message, thread, loop): def run_localagi_message(message, loop): + agent.channel = message.channel def call(thing): return asyncio.run_coroutine_threadsafe(thing,loop).result() sent_message = call(message.channel.send(f"⚙️ LocalAGI starts")) @@ -148,6 +150,7 @@ def run_localagi_message(message, loop): call(sent_message.edit(content=f"<@{user.id}> {conversation_history[-1]['content']}")) def run_localagi(interaction, prompt, loop): + agent.channel = interaction.channel def call(thing): return asyncio.run_coroutine_threadsafe(thing,loop).result() @@ -204,8 +207,6 @@ def run_localagi(interaction, prompt, loop): @app_commands.describe(prompt="Ask me anything!") async def localai(interaction: discord.Interaction, prompt: str): loop = asyncio.get_running_loop() - agent.loop = loop - agent.channel = interaction.channel threading.Thread(target=run_localagi, args=[interaction, prompt,loop]).start() # https://github.com/openai/gpt-discord-bot/blob/1161634a59c6fb642e58edb4f4fa1a46d2883d3b/src/utils.py#L15 @@ -224,16 +225,19 @@ def discord_message_to_message(message): return { "role": "user", "content": message.content } return None +@client.event +async def on_ready(): + loop = asyncio.get_running_loop() + agent.loop = loop + @client.event async def on_message(message): # ignore messages from the bot if message.author == client.user: return loop = asyncio.get_running_loop() - agent.loop = loop # ignore messages not in a thread channel = message.channel - agent.channel = channel if not isinstance(channel, discord.Thread) and client.user.mentioned_in(message): threading.Thread(target=run_localagi_message, args=[message,loop]).start() return