-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathllamabot.py
356 lines (284 loc) · 9.76 KB
/
llamabot.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from datetime import datetime
from pathlib import Path
import pickle
import os
import traceback
import discord
from discord.ext import commands
from llama_index import VectorStoreIndex, StorageContext, ServiceContext
from llama_index.postprocessor import FixedRecencyPostprocessor
from llama_index.embeddings import GeminiEmbedding
from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.schema import TextNode, QueryBundle
from llama_index.vector_stores.types import (
MetadataFilter,
MetadataFilters,
FilterOperator,
)
from llama_index import set_global_handler
import qdrant_client
import settings
from models import Message
from prompts import prompt
set_global_handler("simple")
logger = settings.logging.getLogger("bot")
persist_dir = "./.persist"
messages_path = Path(persist_dir + "/messages.pkl")
listening_path = Path(persist_dir + "/listening.pkl")
messages_path.parent.mkdir(parents=True, exist_ok=True)
def persist_listening():
global listening
# print(listening)
with open(listening_path, 'wb') as file:
pickle.dump(listening, file)
def persist_messages():
global messages
# print(messages)
with open(messages_path, 'wb') as file:
pickle.dump(messages, file)
def process_incoming_message(message):
"""Replace user id with handle for mentions."""
content = message.content
for user in message.mentions:
mention_str = f'<@{user.id}>'
content = content.replace(mention_str, f'@{user.name}')
message.content = content
return message
if messages_path.is_file():
with open(messages_path, 'rb') as file:
messages = pickle.load(file)
else:
messages: dict[int, list[Message]] = {}
persist_messages()
if listening_path.is_file():
with open(listening_path, 'rb') as file:
listening = pickle.load(file)
else:
listening: dict[int, bool] = {}
persist_listening()
# initialize qdrant client
qd_client = qdrant_client.QdrantClient(
url=settings.QDRANT_URL,
api_key=settings.QDRANT_API_KEY
)
qd_collection = 'discord_llamabot'
embed_model = GeminiEmbedding()
use_openai = bool(os.environ.get("USE_OPENAI", False))
use_cohere = bool(os.environ.get("USE_COHERE", False))
# print(use_openai)
# if os.environ.get("GOOGLE_API_KEY", None):
if use_openai:
from llama_index.llms import OpenAI
# from llama_index.embeddings import OpenAIEmbedding
print("Using GPT-4")
llm=OpenAI(
model="gpt-4-0125-preview",
)
# embed_model = OpenAIEmbedding(model="text-embedding-3-small")
elif use_cohere:
from llama_index.llms import Cohere
print("Using Cohere")
llm=Cohere(api_key=os.environ.get('COHERE_KEY'))
else:
from llama_index.llms import Gemini
print("Using Gemini Pro")
llm=Gemini()
vector_store = QdrantVectorStore(client=qd_client,
collection_name=qd_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
service_context = ServiceContext.from_defaults(
embed_model=embed_model,
llm=llm)
index = VectorStoreIndex([],
storage_context=storage_context,
service_context=service_context)
persist_messages()
persist_listening()
def run():
"Run LlamaBot."
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(
command_prefix='/',
intents=intents
)
@bot.event
async def on_ready():
logger.info(f"User: {bot.user} (ID: {bot.user.id})")
@bot.command(
aliases=['li']
)
async def listen(ctx):
"Llama will start listening to messages in this channel from now on."
global listening
listening[ctx.guild.id] = True
persist_listening()
logger.info(f"Listening to messages on channel {ctx.channel.name} of server: {ctx.guild.id} "
f"from {datetime.now().strftime('%m-%d-%Y %H:%M:%S')}")
await ctx.send('Listening to your messages now.')
@bot.command(
aliases=['s']
)
async def stop(ctx):
"Llama will stop listening to messages in this channel from now on."
global listening
listening[ctx.guild.id] = False
persist_listening()
logger.info(f"Stopped Listening to messages on channel "
f"{ctx.channel.name} from {datetime.now().strftime('%m-%d-%Y')}")
await ctx.send('Stopped listening to messages.')
@bot.command(
aliases=['f']
)
async def forget(ctx):
"Llama will forget everything it remembered. It will forget all messages, todo, reminders etc."
forget_all(ctx)
await ctx.send('All messages forgotten & stopped listening to yall')
@bot.command(
aliases=['st']
)
async def status(ctx):
"Status of LlamaBot, whether it's listening or not"
await ctx.send(
"Listening to yall👂" if listening.get(ctx.guild.id, False) \
else "Not Listening 🙉"
)
@bot.command(
aliases=['l']
)
async def llama(ctx, *query):
"Llama will answer your query"
global listening
if not listening.get(ctx.guild.id, False):
await ctx.message.reply(
"I'm not listening to what y'all saying 🙈🙉🙊. "
"\nRun \"/listen\" if you want me to start listening."
)
return
if len(query) == 0:
await ctx.message.reply("What?")
return
user_messages = [msg for msg in messages.get(ctx.guild.id, []) if msg.author!=str(bot.user) and not msg.just_msg.startswith("/")]
# print(user_messages)
if len(user_messages) == 0:
await ctx.message.reply("Hey, Bot's knowledge base is empty now. Please say something before asking it questions.")
return
try:
async with ctx.typing():
await ctx.message.reply(await answer_query(" ".join(query), ctx, bot))
except:
tb = traceback.format_exc()
print(tb)
await ctx.message.reply("The bot encountered an error, will try to fix it soon. Feel free to send a dm to @rsrohan99 about it or open an issue on GitHub https://github.com/rsrohan99/llamabot, any kind of feedback is really appreciated, thanks.")
@bot.event
async def on_message(message):
global listening
# if message.author == bot.user:
# return
message = process_incoming_message(message)
if listening.get(message.guild.id, False):
if message.content.startswith('/'):
if message.content.startswith('/l') or message.content.startswith('/llama'):
remember_message(message, True)
else:
remember_message(message, message.author==bot.user)
await bot.process_commands(message)
def remember_message(message, save_only_message):
when = message.created_at
who=message.author
msg_content = message.content
# print(message)
logger.info(
f"Remembering new message \"{msg_content}\" from {who} on channel "
f"{message.channel.name} at {datetime.now().strftime('%m-%d-%Y %H:%M:%S')}"
)
msg_str = f"[{when.strftime('%m-%d-%Y %H:%M:%S')}] - @{who} on #[{str(message.channel)[:15]}]: `{msg_content}`"
if not save_only_message:
node = TextNode(
text=msg_str,
metadata={
'author': str(who),
'posted_at': str(when),
'channel_id': message.channel.id,
'guild_id': message.guild.id
},
excluded_llm_metadata_keys=['author', 'posted_at', 'channel_id', 'guild_id'],
excluded_embed_metadata_keys=['author', 'posted_at', 'channel_id', 'guild_id'],
)
index.insert_nodes([node])
if not messages.get(message.guild.id, None):
messages[message.guild.id] = []
messages[message.guild.id].append(Message(
is_in_thread=str(message.channel.type) == 'public_thread',
posted_at=when,
author=str(who),
message_str=msg_str,
channel_id=message.channel.id,
just_msg=message.content
))
persist_messages()
async def answer_query(query, ctx, bot):
channel_id = ctx.channel.id
thread_messages = [
msg.message_str for msg in messages.get(ctx.guild.id, []) if msg.channel_id==channel_id
][-1*settings.LAST_N_MESSAGES:-1]
partially_formatted_prompt = prompt.partial_format(
replies="\n".join(thread_messages),
user_asking=str(ctx.author),
bot_name=str(bot.user)
)
filters = MetadataFilters(
filters=[
MetadataFilter(
key="guild_id", operator=FilterOperator.EQ, value=ctx.guild.id
),
MetadataFilter(
key="author", operator=FilterOperator.NE, value=str(bot.user)
),
]
)
postprocessor = FixedRecencyPostprocessor(
top_k=8,
date_key="posted_at", # the key in the metadata to find the date
service_context=service_context
)
query_engine = index.as_query_engine(
service_context=service_context,
filters=filters,
similarity_top_k=8,
node_postprocessors=[postprocessor])
query_engine.update_prompts(
{"response_synthesizer:text_qa_template": partially_formatted_prompt}
)
replies_query = [
msg.just_msg for msg in messages.get(ctx.guild.id, []) if msg.channel_id==channel_id
][-1*settings.LAST_N_MESSAGES:-1]
replies_query.append(query)
# print(replies_query)
return query_engine.query(QueryBundle(
query_str=query,
custom_embedding_strs=replies_query
))
def forget_all(ctx):
from qdrant_client.http import models as rest
global qd_client
try:
messages.pop(ctx.guild.id)
listening.pop(ctx.guild.id)
except KeyError:
pass
persist_messages()
persist_listening()
qd_client.delete(
collection_name=qd_collection,
points_selector=rest.Filter(
must=[
rest.FieldCondition(
key="guild_id", match=rest.MatchValue(value=ctx.guild.id)
)
]
),
)
bot.run(settings.DISCORD_API_SECRET, root_logger=True)
if __name__ == "__main__":
run()