mirror of
https://github.com/Laurent2916/nio-llm.git
synced 2024-10-23 01:56:21 +00:00
Compare commits
No commits in common. "95c751ea303364f523204eed06d8fb650710582c" and "8dac964d2d8dea5ff16ec4190ebb1fbea648a235" have entirely different histories.
95c751ea30
...
8dac964d2d
|
@ -77,12 +77,12 @@ class LLMClient(AsyncClient):
|
||||||
|
|
||||||
async def typing_loop(
|
async def typing_loop(
|
||||||
self,
|
self,
|
||||||
sleep_time: int = 15,
|
sleep_time: int = 10,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Send typing indicators every `sleep_time` seconds.
|
"""Send typing indicators every `sleep_time` seconds.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
sleep_time (`int`, default `15`):
|
sleep_time (`int`, default `10`):
|
||||||
The time to sleep between sending typing indicators.
|
The time to sleep between sending typing indicators.
|
||||||
"""
|
"""
|
||||||
logging.debug("Started typing indicator.")
|
logging.debug("Started typing indicator.")
|
||||||
|
@ -108,120 +108,115 @@ class LLMClient(AsyncClient):
|
||||||
event (`RoomMessageText`):
|
event (`RoomMessageText`):
|
||||||
The message event.
|
The message event.
|
||||||
"""
|
"""
|
||||||
try:
|
logger.debug(f"New RoomMessageText: {event.source}")
|
||||||
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,
|
"content": self.preprompt,
|
||||||
"role": "system",
|
"role": "system",
|
||||||
},
|
|
||||||
*[
|
|
||||||
{
|
|
||||||
"content": f"<{message.sender}>: {message.body}",
|
|
||||||
"role": "assistant" if message.sender == self.uid else "user",
|
|
||||||
}
|
|
||||||
for message in self.history
|
|
||||||
],
|
|
||||||
],
|
|
||||||
temperature=self.openai_temperature,
|
|
||||||
max_tokens=self.openai_max_tokens,
|
|
||||||
)
|
|
||||||
logger.debug(f"Generated response: {response}")
|
|
||||||
|
|
||||||
# retreive the response
|
|
||||||
output = response["choices"][0]["message"]["content"].strip() # type: ignore
|
|
||||||
|
|
||||||
# strip the bot's uid from the response
|
|
||||||
output = output.removeprefix(f"<{self.uid}>:").strip()
|
|
||||||
|
|
||||||
# detect mentions and replace them with html mentions
|
|
||||||
formatted_output = re.sub(
|
|
||||||
r"@[a-zA-Z-_]+:[^.]+\.[a-zA-Z]+",
|
|
||||||
lambda match: f'<a href="https://matrix.to/#/{match.group(0)}"></a>',
|
|
||||||
output,
|
|
||||||
)
|
|
||||||
|
|
||||||
# replace newlines with <br>
|
|
||||||
formatted_output = formatted_output.replace("\n", "<br>")
|
|
||||||
|
|
||||||
logger.debug(f"Formatted response: {formatted_output}")
|
|
||||||
|
|
||||||
# send the response
|
|
||||||
await self.room_send(
|
|
||||||
room_id=self.room,
|
|
||||||
message_type="m.room.message",
|
|
||||||
content={
|
|
||||||
"msgtype": "m.text",
|
|
||||||
"body": output,
|
|
||||||
"format": "org.matrix.custom.html",
|
|
||||||
"formatted_body": formatted_output,
|
|
||||||
},
|
},
|
||||||
)
|
*[
|
||||||
logger.debug(f"Sent response: {output}")
|
{
|
||||||
|
"content": f"{message.sender}: {message.body}",
|
||||||
|
"role": "assistant" if message.sender == self.uid else "user",
|
||||||
|
}
|
||||||
|
for message in self.history
|
||||||
|
],
|
||||||
|
],
|
||||||
|
temperature=self.openai_temperature,
|
||||||
|
max_tokens=self.openai_max_tokens,
|
||||||
|
)
|
||||||
|
logger.debug(f"Generated response: {response}")
|
||||||
|
|
||||||
# stop typing indicator loop
|
# retreive the response
|
||||||
typing_task.cancel()
|
output = response["choices"][0]["message"]["content"] # type: ignore
|
||||||
except Exception as e:
|
output = output.strip().removeprefix(f"{self.uid}:").strip()
|
||||||
logger.error(f"Exception in message_callback: {e}")
|
|
||||||
|
# replace newlines with <br>
|
||||||
|
formatted_output = output.replace("\n", "<br>")
|
||||||
|
|
||||||
|
# detect mentions and replace them with html mentions
|
||||||
|
formatted_output = re.sub(
|
||||||
|
r"@[^:]+:[^ :]+",
|
||||||
|
lambda match: f'<a href="https://matrix.to/#/{match.group(0)}"></a>',
|
||||||
|
formatted_output,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.debug(f"Formatted response: {formatted_output}")
|
||||||
|
|
||||||
|
# send the response
|
||||||
|
await self.room_send(
|
||||||
|
room_id=self.room,
|
||||||
|
message_type="m.room.message",
|
||||||
|
content={
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"body": output,
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": formatted_output,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
logger.debug(f"Sent response: {output}")
|
||||||
|
|
||||||
|
# stop typing indicator loop
|
||||||
|
typing_task.cancel()
|
||||||
|
|
||||||
async def start(
|
async def start(
|
||||||
self,
|
self,
|
||||||
|
|
Loading…
Reference in a new issue