mirror of
https://github.com/Laurent2916/nio-llm.git
synced 2024-11-21 05:38:48 +00:00
🐛 temporarily encapsulate message_callback's inside logic inside a try/catch
This commit is contained in:
parent
f92a20b2c5
commit
95c751ea30
|
@ -108,117 +108,120 @@ class LLMClient(AsyncClient):
|
||||||
event (`RoomMessageText`):
|
event (`RoomMessageText`):
|
||||||
The message event.
|
The message event.
|
||||||
"""
|
"""
|
||||||
logger.debug(f"New RoomMessageText: {event.source}")
|
try:
|
||||||
|
logger.debug(f"New RoomMessageText: {event.source}")
|
||||||
|
|
||||||
# ignore messages pre-dating our spawn time
|
# ignore messages pre-dating our spawn time
|
||||||
if event.server_timestamp < self.spawn_time:
|
if event.server_timestamp < self.spawn_time:
|
||||||
logger.debug("Ignoring message pre-spawn.")
|
logger.debug("Ignoring message pre-spawn.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# ignore messages not in our monitored room
|
# ignore messages not in our monitored room
|
||||||
if room.room_id != self.room:
|
if room.room_id != self.room:
|
||||||
logger.debug("Ignoring message in different room.")
|
logger.debug("Ignoring message in different room.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# ignore edited messages
|
# ignore edited messages
|
||||||
if "m.new_content" in event.source["content"]:
|
if "m.new_content" in event.source["content"]:
|
||||||
logger.debug("Ignoring edited message.")
|
logger.debug("Ignoring edited message.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# ignore thread messages
|
# ignore thread messages
|
||||||
if (
|
if (
|
||||||
"m.relates_to" in event.source["content"]
|
"m.relates_to" in event.source["content"]
|
||||||
and "rel_type" in event.source["content"]["m.relates_to"]
|
and "rel_type" in event.source["content"]["m.relates_to"]
|
||||||
and event.source["content"]["m.relates_to"]["rel_type"] == "m.thread"
|
and event.source["content"]["m.relates_to"]["rel_type"] == "m.thread"
|
||||||
):
|
):
|
||||||
logger.debug("Ignoring thread message.")
|
logger.debug("Ignoring thread message.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# update history
|
# update history
|
||||||
self.history.append(event)
|
self.history.append(event)
|
||||||
logger.debug(f"Updated history: {self.history}")
|
logger.debug(f"Updated history: {self.history}")
|
||||||
|
|
||||||
# update read receipt
|
# update read receipt
|
||||||
await self.room_read_markers(
|
await self.room_read_markers(
|
||||||
room_id=self.room,
|
room_id=self.room,
|
||||||
fully_read_event=event.event_id,
|
fully_read_event=event.event_id,
|
||||||
read_event=event.event_id,
|
read_event=event.event_id,
|
||||||
)
|
)
|
||||||
logger.debug(f"Updated read receipt to event: {event.event_id}")
|
logger.debug(f"Updated read receipt to event: {event.event_id}")
|
||||||
|
|
||||||
# ignore our own messages
|
# ignore our own messages
|
||||||
if event.sender == self.user:
|
if event.sender == self.user:
|
||||||
logger.debug("Ignoring our own message.")
|
logger.debug("Ignoring our own message.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# ignore messages not mentioning us
|
# ignore messages not mentioning us
|
||||||
if not (
|
if not (
|
||||||
"format" in event.source["content"]
|
"format" in event.source["content"]
|
||||||
and "formatted_body" in event.source["content"]
|
and "formatted_body" in event.source["content"]
|
||||||
and event.source["content"]["format"] == "org.matrix.custom.html"
|
and event.source["content"]["format"] == "org.matrix.custom.html"
|
||||||
and f'<a href="https://matrix.to/#/{self.uid}">{self.username}</a>'
|
and f'<a href="https://matrix.to/#/{self.uid}">{self.username}</a>'
|
||||||
in event.source["content"]["formatted_body"]
|
in event.source["content"]["formatted_body"]
|
||||||
):
|
):
|
||||||
logger.debug("Ignoring message not mentioning us.")
|
logger.debug("Ignoring message not mentioning us.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# start typing indicator loop
|
# start typing indicator loop
|
||||||
typing_task = asyncio.create_task(self.typing_loop())
|
typing_task = asyncio.create_task(self.typing_loop())
|
||||||
|
|
||||||
# generate response using llama.cpp
|
# generate response using llama.cpp
|
||||||
response = await openai.ChatCompletion.acreate(
|
response = await openai.ChatCompletion.acreate(
|
||||||
model="local-model",
|
model="local-model",
|
||||||
messages=[
|
messages=[
|
||||||
{
|
|
||||||
"content": self.preprompt,
|
|
||||||
"role": "system",
|
|
||||||
},
|
|
||||||
*[
|
|
||||||
{
|
{
|
||||||
"content": f"<{message.sender}>: {message.body}",
|
"content": self.preprompt,
|
||||||
"role": "assistant" if message.sender == self.uid else "user",
|
"role": "system",
|
||||||
}
|
},
|
||||||
for message in self.history
|
*[
|
||||||
|
{
|
||||||
|
"content": f"<{message.sender}>: {message.body}",
|
||||||
|
"role": "assistant" if message.sender == self.uid else "user",
|
||||||
|
}
|
||||||
|
for message in self.history
|
||||||
|
],
|
||||||
],
|
],
|
||||||
],
|
temperature=self.openai_temperature,
|
||||||
temperature=self.openai_temperature,
|
max_tokens=self.openai_max_tokens,
|
||||||
max_tokens=self.openai_max_tokens,
|
)
|
||||||
)
|
logger.debug(f"Generated response: {response}")
|
||||||
logger.debug(f"Generated response: {response}")
|
|
||||||
|
|
||||||
# retreive the response
|
# retreive the response
|
||||||
output = response["choices"][0]["message"]["content"].strip() # type: ignore
|
output = response["choices"][0]["message"]["content"].strip() # type: ignore
|
||||||
|
|
||||||
# strip the bot's uid from the response
|
# strip the bot's uid from the response
|
||||||
output = output.removeprefix(f"<{self.uid}>:").strip()
|
output = output.removeprefix(f"<{self.uid}>:").strip()
|
||||||
|
|
||||||
# detect mentions and replace them with html mentions
|
# detect mentions and replace them with html mentions
|
||||||
formatted_output = re.sub(
|
formatted_output = re.sub(
|
||||||
r"@[a-zA-Z-_]+:[^.]+\.[a-zA-Z]+",
|
r"@[a-zA-Z-_]+:[^.]+\.[a-zA-Z]+",
|
||||||
lambda match: f'<a href="https://matrix.to/#/{match.group(0)}"></a>',
|
lambda match: f'<a href="https://matrix.to/#/{match.group(0)}"></a>',
|
||||||
output,
|
output,
|
||||||
)
|
)
|
||||||
|
|
||||||
# replace newlines with <br>
|
# replace newlines with <br>
|
||||||
formatted_output = formatted_output.replace("\n", "<br>")
|
formatted_output = formatted_output.replace("\n", "<br>")
|
||||||
|
|
||||||
logger.debug(f"Formatted response: {formatted_output}")
|
logger.debug(f"Formatted response: {formatted_output}")
|
||||||
|
|
||||||
# send the response
|
# send the response
|
||||||
await self.room_send(
|
await self.room_send(
|
||||||
room_id=self.room,
|
room_id=self.room,
|
||||||
message_type="m.room.message",
|
message_type="m.room.message",
|
||||||
content={
|
content={
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": output,
|
"body": output,
|
||||||
"format": "org.matrix.custom.html",
|
"format": "org.matrix.custom.html",
|
||||||
"formatted_body": formatted_output,
|
"formatted_body": formatted_output,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
logger.debug(f"Sent response: {output}")
|
logger.debug(f"Sent response: {output}")
|
||||||
|
|
||||||
# stop typing indicator loop
|
# stop typing indicator loop
|
||||||
typing_task.cancel()
|
typing_task.cancel()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Exception in message_callback: {e}")
|
||||||
|
|
||||||
async def start(
|
async def start(
|
||||||
self,
|
self,
|
||||||
|
|
Loading…
Reference in a new issue