-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.py
110 lines (85 loc) · 2.93 KB
/
serve.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
import discord
import os
import logging
import aiohttp
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
INFERENCE_SERVER = os.getenv("INFERENCE_SERVER")
BOT_SYSTEM_PROMPT = os.getenv(
"BOT_SYSTEM_PROMPT", "You are Wumlla, a helpful assistant."
)
logger = logging.getLogger(__name__)
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s")
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
async def call_llm(messages: list):
payload = {
"model": "local-llm",
"messages": messages,
"temperature": 0.7,
}
async with aiohttp.ClientSession() as session:
async with session.post(
f"{INFERENCE_SERVER}/v1/chat/completions", json=payload
) as response:
if response.status == 200:
res_data = await response.json()
res_msg = res_data["choices"][0]["message"]["content"]
return res_msg
else:
logger.error(f"Request failed with status code: {response.status}")
logger.error(f"Response Text: {await response.text()}")
return None
def arrange_msgs_history(messages: list):
msgs_container = [
{
"role": "system",
"content": BOT_SYSTEM_PROMPT,
}
]
for msg in messages:
msgs_container.append(
{
"role": "user" if msg.author != bot.user else "assistant",
"content": msg.content,
}
)
return msgs_container
def chunk_message(message, max_length=2000):
chunks = []
while message:
if len(message) <= max_length:
chunks.append(message)
break
chunks.append(message[:max_length])
message = message[max_length:]
return chunks
@bot.event
async def on_ready():
logger.info(f"Logged in as {bot.user}")
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if isinstance(message.channel, discord.Thread):
if message.channel.parent and message.channel.parent.name == "local-llm-wmll":
try:
async with message.channel.typing():
messages = []
async for msg in message.channel.history(limit=20):
messages.append(msg)
msgs = arrange_msgs_history(reversed(messages))
res = await call_llm(msgs)
chunks = chunk_message(res)
for chunk in chunks:
await message.channel.send(chunk)
except Exception as e:
logger.exception(e)
await message.channel.send(f"An error occurred: {e}")
await bot.process_commands(message)
if __name__ == "__main__":
bot.run(BOT_TOKEN)