♻️ working poc

This commit is contained in:
Laureηt 2023-05-22 21:19:11 +02:00
parent d1b3b55ee3
commit c7a6c050d4
Signed by: Laurent
SSH key fingerprint: SHA256:kZEpW8cMJ54PDeCvOhzreNr4FSh6R13CMGH/POoO8DI
2 changed files with 138 additions and 42 deletions

View file

@ -2,8 +2,10 @@
import asyncio import asyncio
import logging import logging
import time
from textwrap import dedent from textwrap import dedent
import click
from llama_cpp import Llama from llama_cpp import Llama
from nio import AsyncClient, MatrixRoom, RoomMessageText from nio import AsyncClient, MatrixRoom, RoomMessageText
@ -15,17 +17,24 @@ class LLMClient(AsyncClient):
def __init__( def __init__(
self, self,
user: str, username: str,
homeserver: str, homeserver: str,
device_id: str, device_id: str,
preprompt: str,
room: str,
): ):
"""Create a new LLMClient instance.""" """Create a new LLMClient instance."""
super().__init__( super().__init__(
user=user, user=f"@{username}:{homeserver.removeprefix('https://')}",
homeserver=homeserver, homeserver=homeserver,
device_id=device_id, device_id=device_id,
) )
self.spawn_time = time.time() * 1000
self.username = username
self.preprompt = preprompt
self.room = room
# create the Llama instance # create the Llama instance
self.llm = Llama( self.llm = Llama(
model_path="../../../llama.cpp/models/sv13B/stable-vicuna-13B.ggml.q5_1.bin", model_path="../../../llama.cpp/models/sv13B/stable-vicuna-13B.ggml.q5_1.bin",
@ -37,28 +46,49 @@ 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."""
# ignore messages sent in other rooms logger.debug(f"Received new message in room {room.room_id}.")
if room.room_id != ROOM: logger.debug(f"Message body: {event.body}")
# ignore our own messages
if event.sender == self.user:
logger.debug("Ignoring our own message.")
return return
if f"<{USERNAME}>" in event.body: # ignore messages pre-spawn
logging.debug("Received message including our identifier") if event.server_timestamp < self.spawn_time:
logger.debug("Ignoring message pre-spawn.")
return
# ignore messages sent in other rooms
if room.room_id != self.room:
logger.debug("Ignoring message in different room.")
return
if self.username not in event.body:
logger.debug("Ignoring message not directed at us.")
return
prompt = dedent( prompt = dedent(
f""" f"""
{PREPROMPT} {self.preprompt}
<{event.sender}>: {event.body} <{event.sender}>: {event.body}
<{USERNAME}>: <pipobot>:
""", """,
).strip() ).strip()
logger.debug(f"Prompt: {prompt}")
# enable typing indicator # enable typing indicator
await self.room_typing(ROOM, typing_state=True) await self.room_typing(
self.room,
typing_state=True,
timeout=100000000,
)
output = self.llm( output = self.llm(
prompt, prompt,
max_tokens=100, max_tokens=100,
stop=["<{event.sender}>:", "\n"], stop=["<{event.sender}>"],
echo=True, echo=True,
) )
@ -67,11 +97,11 @@ class LLMClient(AsyncClient):
output = output.removeprefix(prompt).strip() output = output.removeprefix(prompt).strip()
# disable typing indicator # disable typing indicator
await self.room_typing(ROOM, typing_state=False) await self.room_typing(self.room, typing_state=False)
# send the response # send the response
await self.room_send( await self.room_send(
room_id=ROOM, room_id=self.room,
message_type="m.room.message", message_type="m.room.message",
content={ content={
"msgtype": "m.text", "msgtype": "m.text",
@ -80,24 +110,62 @@ class LLMClient(AsyncClient):
) )
async def main() -> None: @click.command()
@click.option("--homeserver", "-h", help="The homeserver to connect to.", required=True)
@click.option("--device-id", "-d", help="The device ID to use.", required=True)
@click.option("--username", "-u", help="The username to log in as.", required=True)
@click.option("--password", "-p", help="The password to log in with.", required=True)
@click.option("--room", "-r", help="The room to join.", required=True)
@click.option("--preprompt", "-t", help="The preprompt to use.", required=True)
def main(
homeserver: str,
device_id: str,
username: str,
password: str,
room: str,
preprompt,
) -> None:
asyncio.get_event_loop().run_until_complete(
_main(
homeserver=homeserver,
device_id=device_id,
username=username,
password=password,
preprompt=preprompt,
room=room,
)
)
async def _main(
homeserver: str,
device_id: str,
username: str,
password: str,
room: str,
preprompt,
) -> None:
"""Run the main program.""" """Run the main program."""
# create the client # create the client
client = LLMClient( client = LLMClient(
homeserver=HOMESERVER, homeserver=homeserver,
device_id=DEVICE_ID, device_id=device_id,
user=USERNAME, username=username,
room=room,
preprompt=preprompt,
) )
# Login to the homeserver # Login to the homeserver
print(await client.login(PASSWORD)) print(await client.login(password))
# Join the room, if not already joined # Join the room, if not already joined
print(await client.join(ROOM)) print(await client.join(room))
# Sync with the server forever # Sync with the server forever
await client.sync_forever(timeout=30000) await client.sync_forever(timeout=30000)
if __name__ == "__main__": if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main()) logging.basicConfig(level=logging.DEBUG)
main(auto_envvar_prefix="NIOLLM")
main(auto_envvar_prefix="NIOLLM")

28
nio-llm/test.py Normal file
View file

@ -0,0 +1,28 @@
from textwrap import dedent
from llama_cpp import Llama
llm = Llama(model_path="../../../llama.cpp/models/sv13B/stable-vicuna-13B.ggml.q5_1.bin", n_threads=12)
msg = dedent(
"""
You are pipobot, an arrogant assistant. Answer as concisely as possible.
<@fainsil:inpt.fr>: Qu'est ce qu'une intégrale de Lebesgue ?
<@pipobot:inpt.fr>:
""",
).strip()
print(msg)
print(repr(msg))
output = llm(
msg,
max_tokens=100,
stop=["<@fainsil:inpt.fr>:", "\n"],
echo=True,
)
print(output)
res = output["choices"][0]["text"]
print(res)
print(res.removeprefix(msg).strip())