File size: 2,850 Bytes
0fcca52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import discord
import traceback

DISCORD_TOKEN = 'указать токен'

class MyClient(discord.Client):
    def __init__(self, *args, **kwargs):
        intents = discord.Intents.default()
        intents.message_content = True
        super().__init__(intents=intents, *args, **kwargs)

    async def on_message(self, message):
        print("Received message:", message.content)
        try:
            if message.author.id == self.user.id:
                return

            context = update_context(message.author.id, message, "user")
            print("Updated context:", context)

            messages = format_prompt(context)
            print("Formatted prompt:", messages)

            response = await call_huggingface_api(messages)
            print("Received response:", response)

            if response is not None and 'generated_text' in response[0]:
                response_text = response[0]['generated_text']
                print("Filtered response:", response_text)

                response_text = filter_response(response_text)
                print("Post-processed response:", response_text)

                # Check if the response contains the user's question
                if context[-1]["content"].lower() not in response_text.lower():
                    # If the response does not contain the user's question, clear the context and generate a new response
                    contexts[message.author.id] = []
                    context = update_context(message.author.id, message, "user")
                    messages = format_prompt(context)
                    response = await call_huggingface_api(messages)
                    response_text = response[0]['generated_text']
                    response_text = filter_response(response_text)
                    response_text = post_process(response_text)

                last_bot_response = response_text.split("user:")[-1].strip()
                print("Sending response:", last_bot_response)

                if len(last_bot_response) > 2000:
                    chunks = [last_bot_response[i:i+2000] for i in range(0, len(last_bot_response), 2000)]
                    for chunk in chunks:
                        await message.channel.send(chunk)
                else:
                    await message.channel.send(last_bot_response)
            else:
                print("Error generating text.")
                await message.channel.send("Error generating text.")
        except Exception as e:
            print(f"Error in on_message: {e}")
            print(traceback.format_exc())
            await message.channel.send("An error occurred while processing your message. Please try again later.")

async def main():
    client = MyClient()
    await client.start(DISCORD_TOKEN)