mirror of
https://github.com/Laurent2916/nio-llm.git
synced 2024-11-23 22:58:48 +00:00
✨ ignore edited message, and more robust detection of mentions
This commit is contained in:
parent
65bcb80f85
commit
6b3c9fc12d
|
@ -22,21 +22,23 @@ class LLMClient(AsyncClient):
|
||||||
homeserver: str,
|
homeserver: str,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
preprompt: str,
|
preprompt: str,
|
||||||
room: str,
|
|
||||||
ggml_path: str,
|
ggml_path: str,
|
||||||
|
room: str,
|
||||||
):
|
):
|
||||||
"""Create a new LLMClient instance."""
|
"""Create a new LLMClient instance."""
|
||||||
super().__init__(
|
self.uid = f"@{username}:{homeserver.removeprefix('https://')}"
|
||||||
user=f"@{username}:{homeserver.removeprefix('https://')}",
|
|
||||||
homeserver=homeserver,
|
|
||||||
device_id=device_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.spawn_time = time.time() * 1000
|
self.spawn_time = time.time() * 1000
|
||||||
self.username = username
|
self.username = username
|
||||||
self.preprompt = preprompt
|
self.preprompt = preprompt
|
||||||
self.room = room
|
self.room = room
|
||||||
|
|
||||||
|
# create the AsyncClient instance
|
||||||
|
super().__init__(
|
||||||
|
user=self.uid,
|
||||||
|
homeserver=homeserver,
|
||||||
|
device_id=device_id,
|
||||||
|
)
|
||||||
|
|
||||||
# create the Llama instance
|
# create the Llama instance
|
||||||
self.llm = Llama(
|
self.llm = Llama(
|
||||||
model_path=ggml_path,
|
model_path=ggml_path,
|
||||||
|
@ -48,36 +50,47 @@ class LLMClient(AsyncClient):
|
||||||
|
|
||||||
async def message_callback(self, room: MatrixRoom, event: RoomMessageText):
|
async def message_callback(self, room: MatrixRoom, event: RoomMessageText):
|
||||||
"""Process new messages as they come in."""
|
"""Process new messages as they come in."""
|
||||||
logger.debug(f"Received new message in room {room.room_id}.")
|
logger.debug(f"New RoomMessageText: {event.source}")
|
||||||
logger.debug(f"Message body: {event.body}")
|
|
||||||
|
|
||||||
# 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 pre-spawn
|
# 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 sent in other rooms
|
# 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
|
||||||
|
|
||||||
if self.username not in event.body:
|
# ignore edited messages
|
||||||
|
if "m.new_content" in event.source["content"]:
|
||||||
|
logger.debug("Ignoring edited message.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# ignore messages not mentioning us
|
||||||
|
if not (
|
||||||
|
"format" in event.source["content"]
|
||||||
|
and "formatted_body" in event.source["content"]
|
||||||
|
and event.source["content"]["format"] == "org.matrix.custom.html"
|
||||||
|
and f'<a href="https://matrix.to/#/{self.uid}">{self.username}</a>'
|
||||||
|
in event.source["content"]["formatted_body"]
|
||||||
|
):
|
||||||
logger.debug("Ignoring message not directed at us.")
|
logger.debug("Ignoring message not directed at us.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# generate prompt from message
|
||||||
prompt = dedent(
|
prompt = dedent(
|
||||||
f"""
|
f"""
|
||||||
{self.preprompt}
|
{self.preprompt}
|
||||||
<{event.sender}>: {event.body}
|
<{event.sender}>: {event.body}
|
||||||
<pipobot>:
|
<{self.username}>:
|
||||||
""",
|
""",
|
||||||
).strip()
|
).strip()
|
||||||
|
|
||||||
logger.debug(f"Prompt: {prompt}")
|
logger.debug(f"Prompt: {prompt}")
|
||||||
|
|
||||||
# enable typing indicator
|
# enable typing indicator
|
||||||
|
@ -87,6 +100,7 @@ class LLMClient(AsyncClient):
|
||||||
timeout=100000000,
|
timeout=100000000,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# generate response using llama.cpp
|
||||||
output = self.llm(
|
output = self.llm(
|
||||||
prompt,
|
prompt,
|
||||||
max_tokens=100,
|
max_tokens=100,
|
||||||
|
|
Loading…
Reference in a new issue