-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
149 lines (119 loc) · 4.84 KB
/
bot.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
from uuid import uuid1
import discord
from honcho import Honcho
from honcho.ext.langchain import messages_to_langchain
from graph import chat
from dspy import Example
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
intents.members = True
intents.reactions = True # Enable reactions intent
app_name = str(uuid1())
honcho = Honcho(app_name=app_name, base_url="http://localhost:8001") # uncomment to use local
#honcho = Honcho(app_name=app_name) # uses demo server at https://demo.honcho.dev
honcho.initialize()
bot = discord.Bot(intents=intents)
thumbs_up_messages = []
thumbs_down_messages = []
@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
@bot.event
async def on_member_join(member):
await member.send(
f"*Hello {member.name}, welcome to the Namastex Labs server! I'm an AI-powered bot designed to assist with data analysis, task management, and intelligent conversation.* "
"*Feel free to ask me about our AI solutions, data insights, or any other topics related to our services.* "
"*I'm here to help you navigate our offerings and provide relevant information.* "
"*You can use the /restart command to restart our conversation at any time.* "
"*If you have any questions or feedback, please let me know!* "
"*Looking forward to our interaction.*"
)
@bot.event
async def on_message(message):
if message.author == bot.user or message.guild is not None:
return
user_id = f"discord_{str(message.author.id)}"
user = honcho.get_or_create_user(user_id)
location_id = str(message.channel.id)
sessions = list(
user.get_sessions_generator(location_id, is_active=True, reverse=True)
)
if len(sessions) > 0:
session = sessions[0]
else:
session = user.create_session(location_id)
history = list(session.get_messages_generator())[:5]
chat_history = messages_to_langchain(history)
inp = message.content
user_message = session.create_message(is_user=True, content=inp)
async with message.channel.typing():
response = await chat(
chat_history=chat_history,
user_message=user_message,
session=session,
input=inp,
)
await message.channel.send(response)
session.create_message(is_user=False, content=response)
@bot.event
async def on_reaction_add(reaction, user):
# Ensure the bot does not react to its own reactions
if user == bot.user:
return
user_id = f"discord_{str(user.id)}"
honcho_user = honcho.get_or_create_user(user_id)
location_id = str(reaction.message.channel.id)
sessions = list(
honcho_user.get_sessions_generator(location_id, is_active=True, reverse=True)
)
if len(sessions) > 0:
session = sessions[0]
else:
session = honcho_user.create_session(location_id)
messages = list(session.get_messages_generator(reverse=True))
ai_responses = [message for message in messages if not message.is_user]
user_responses = [message for message in messages if message.is_user]
# most recent AI response
ai_response = ai_responses[0].content
user_response = user_responses[0]
user_state_storage = dict(honcho_user.metadata)
user_state = list(
session.get_metamessages_generator(
metamessage_type="user_state", message=user_response, reverse=True
)
)[0].content
examples = user_state_storage[user_state]["examples"]
# Check if the reaction is a thumbs up
if str(reaction.emoji) == "👍":
example = Example(
chat_input=user_response.content,
response=ai_response,
assessment_dimension=user_state,
label="yes",
).with_inputs("chat_input", "response", "assessment_dimension")
examples.append(example.toDict())
# Check if the reaction is a thumbs down
elif str(reaction.emoji) == "👎":
example = Example(
chat_input=user_response.content,
response=ai_response,
assessment_dimension=user_state,
label="no",
).with_inputs("chat_input", "response", "assessment_dimension")
examples.append(example.toDict())
user_state_storage[user_state]["examples"] = examples
honcho_user.update(metadata=user_state_storage)
@bot.slash_command(name="restart", description="Restart the Conversation")
async def restart(ctx):
user_id = f"discord_{str(ctx.author.id)}"
user = honcho.get_or_create_user(user_id)
location_id = str(ctx.channel_id)
sessions = list(user.get_sessions_generator(location_id, reverse=True))
sessions[0].close() if len(sessions) > 0 else None
msg = (
"Great! The conversation has been restarted. What would you like to talk about?"
)
await ctx.respond(msg)
bot.run(os.environ["BOT_TOKEN"])