add system prompt
This commit is contained in:
parent
7affc308a7
commit
db011cc992
|
@ -1,6 +1,7 @@
|
||||||
.idea
|
.idea
|
||||||
bot-store/
|
*-store/
|
||||||
config.yaml
|
*.yaml
|
||||||
|
!config.sample.yaml
|
||||||
|
|
||||||
# ---> Python
|
# ---> Python
|
||||||
# Byte-compiled / optimized / DLL files
|
# Byte-compiled / optimized / DLL files
|
||||||
|
|
|
@ -34,3 +34,7 @@ autojoin_rooms:
|
||||||
command_prefix: '!c'
|
command_prefix: '!c'
|
||||||
|
|
||||||
reply_in_thread: true
|
reply_in_thread: true
|
||||||
|
|
||||||
|
# The system message helps set the behavior of the assistant.
|
||||||
|
# For example, you can instruct the assistant with "You are a helpful assistant."
|
||||||
|
system_prompt:
|
8
main.py
8
main.py
|
@ -89,7 +89,7 @@ async def main():
|
||||||
storage = Storage(Path(config_data['data_storage'], 'matrixgpt.db'))
|
storage = Storage(Path(config_data['data_storage'], 'matrixgpt.db'))
|
||||||
|
|
||||||
# Set up event callbacks
|
# Set up event callbacks
|
||||||
callbacks = Callbacks(client, storage, config_data['command_prefix'], openai_config, config_data.get('reply_in_thread', False), config_data['allowed_to_invite'], config_data['allowed_to_chat'])
|
callbacks = Callbacks(client, storage, config_data['command_prefix'], openai_config, config_data.get('reply_in_thread', False), config_data['allowed_to_invite'], config_data['allowed_to_chat'], config_data.get('system_prompt'))
|
||||||
client.add_event_callback(callbacks.message, RoomMessageText)
|
client.add_event_callback(callbacks.message, RoomMessageText)
|
||||||
client.add_event_callback(callbacks.invite_event_filtered_callback, InviteMemberEvent)
|
client.add_event_callback(callbacks.invite_event_filtered_callback, InviteMemberEvent)
|
||||||
client.add_event_callback(callbacks.decryption_failure, MegolmEvent)
|
client.add_event_callback(callbacks.decryption_failure, MegolmEvent)
|
||||||
|
@ -137,13 +137,13 @@ async def main():
|
||||||
except (ClientConnectionError, ServerDisconnectedError):
|
except (ClientConnectionError, ServerDisconnectedError):
|
||||||
logger.warning("Unable to connect to homeserver, retrying in 15s...")
|
logger.warning("Unable to connect to homeserver, retrying in 15s...")
|
||||||
time.sleep(15)
|
time.sleep(15)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
await client.close()
|
||||||
|
sys.exit()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.critical(traceback.format_exc())
|
logger.critical(traceback.format_exc())
|
||||||
logger.critical('Sleeping 5s...')
|
logger.critical('Sleeping 5s...')
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
except KeyboardInterrupt:
|
|
||||||
await client.close()
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -2,7 +2,7 @@ import logging
|
||||||
|
|
||||||
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
||||||
|
|
||||||
from .chat_functions import get_thread_content, process_chat, send_text_to_room
|
from .chat_functions import process_chat, send_text_to_room
|
||||||
# from .config import Config
|
# from .config import Config
|
||||||
from .storage import Storage
|
from .storage import Storage
|
||||||
|
|
||||||
|
@ -19,7 +19,8 @@ class Command:
|
||||||
room: MatrixRoom,
|
room: MatrixRoom,
|
||||||
event: RoomMessageText,
|
event: RoomMessageText,
|
||||||
openai,
|
openai,
|
||||||
reply_in_thread
|
reply_in_thread,
|
||||||
|
system_prompt: str = None,
|
||||||
):
|
):
|
||||||
"""A command made by a user.
|
"""A command made by a user.
|
||||||
|
|
||||||
|
@ -45,6 +46,7 @@ class Command:
|
||||||
self.args = self.command.split()[1:]
|
self.args = self.command.split()[1:]
|
||||||
self.openai = openai
|
self.openai = openai
|
||||||
self.reply_in_thread = reply_in_thread
|
self.reply_in_thread = reply_in_thread
|
||||||
|
self.system_prompt = system_prompt
|
||||||
|
|
||||||
async def process(self):
|
async def process(self):
|
||||||
"""Process the command"""
|
"""Process the command"""
|
||||||
|
@ -60,7 +62,7 @@ class Command:
|
||||||
await self._process_chat()
|
await self._process_chat()
|
||||||
|
|
||||||
async def _process_chat(self):
|
async def _process_chat(self):
|
||||||
await process_chat(self.client, self.room, self.event, self.command, self.store, self.openai)
|
await process_chat(self.client, self.room, self.event, self.command, self.store, self.openai, system_prompt=self.system_prompt)
|
||||||
|
|
||||||
async def _show_help(self):
|
async def _show_help(self):
|
||||||
"""Show the help text"""
|
"""Show the help text"""
|
||||||
|
|
|
@ -13,7 +13,7 @@ logger = logging.getLogger('MatrixGPT')
|
||||||
|
|
||||||
|
|
||||||
class Callbacks:
|
class Callbacks:
|
||||||
def __init__(self, client: AsyncClient, store: Storage, command_prefix: str, openai, reply_in_thread, allowed_to_invite, allowed_to_chat='all'):
|
def __init__(self, client: AsyncClient, store: Storage, command_prefix: str, openai, reply_in_thread, allowed_to_invite, allowed_to_chat='all', system_prompt: str = None, ):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
client: nio client used to interact with matrix.
|
client: nio client used to interact with matrix.
|
||||||
|
@ -31,6 +31,7 @@ class Callbacks:
|
||||||
self.reply_in_thread = reply_in_thread
|
self.reply_in_thread = reply_in_thread
|
||||||
self.allowed_to_invite = allowed_to_invite if allowed_to_invite else []
|
self.allowed_to_invite = allowed_to_invite if allowed_to_invite else []
|
||||||
self.allowed_to_chat = allowed_to_chat
|
self.allowed_to_chat = allowed_to_chat
|
||||||
|
self.system_prompt = system_prompt
|
||||||
|
|
||||||
async def message(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
async def message(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
||||||
"""Callback for when a message event is received
|
"""Callback for when a message event is received
|
||||||
|
@ -69,7 +70,7 @@ class Callbacks:
|
||||||
# room.member_count > 2 ... we assume a public room
|
# room.member_count > 2 ... we assume a public room
|
||||||
# room.member_count <= 2 ... we assume a DM
|
# room.member_count <= 2 ... we assume a DM
|
||||||
# General message listener
|
# General message listener
|
||||||
if not msg.startswith(self.command_prefix) and is_thread(event) and not self.store.check_seen_event(event.event_id):
|
if not msg.startswith(f'{self.command_prefix} ') and is_thread(event) and not self.store.check_seen_event(event.event_id):
|
||||||
await self.client.room_typing(room.room_id, typing_state=True, timeout=3000)
|
await self.client.room_typing(room.room_id, typing_state=True, timeout=3000)
|
||||||
thread_content = await get_thread_content(self.client, room, event)
|
thread_content = await get_thread_content(self.client, room, event)
|
||||||
api_data = []
|
api_data = []
|
||||||
|
@ -80,9 +81,9 @@ class Callbacks:
|
||||||
# message = Message(self.client, self.store, msg, room, event, self.reply_in_thread)
|
# message = Message(self.client, self.store, msg, room, event, self.reply_in_thread)
|
||||||
# await message.process()
|
# await message.process()
|
||||||
api_data.append({'role': 'user', 'content': event.body})
|
api_data.append({'role': 'user', 'content': event.body})
|
||||||
await process_chat(self.client, room, event, api_data, self.store, self.openai, thread_root_id=thread_content[0].event_id)
|
await process_chat(self.client, room, event, api_data, self.store, self.openai, thread_root_id=thread_content[0].event_id, system_prompt=self.system_prompt)
|
||||||
return
|
return
|
||||||
elif msg.startswith(self.command_prefix) or room.member_count == 2:
|
elif msg.startswith(f'{self.command_prefix} ') or room.member_count == 2:
|
||||||
# Otherwise if this is in a 1-1 with the bot or features a command prefix,
|
# Otherwise if this is in a 1-1 with the bot or features a command prefix,
|
||||||
# treat it as a command
|
# treat it as a command
|
||||||
msg = event.body if not event.body.startswith(self.command_prefix) else event.body[len(self.command_prefix):].strip() # Remove the command prefix
|
msg = event.body if not event.body.startswith(self.command_prefix) else event.body[len(self.command_prefix):].strip() # Remove the command prefix
|
||||||
|
@ -181,13 +182,13 @@ class Callbacks:
|
||||||
|
|
||||||
event: The encrypted event that we were unable to decrypt.
|
event: The encrypted event that we were unable to decrypt.
|
||||||
"""
|
"""
|
||||||
logger.error(f"Failed to decrypt event '{event.event_id}' in room '{room.room_id}'!"
|
# logger.error(f"Failed to decrypt event '{event.event_id}' in room '{room.room_id}'!"
|
||||||
f"\n\n"
|
# f"\n\n"
|
||||||
f"Tip: try using a different device ID in your config file and restart."
|
# f"Tip: try using a different device ID in your config file and restart."
|
||||||
f"\n\n"
|
# f"\n\n"
|
||||||
f"If all else fails, delete your store directory and let the bot recreate "
|
# f"If all else fails, delete your store directory and let the bot recreate "
|
||||||
f"it (your reminders will NOT be deleted, but the bot may respond to existing "
|
# f"it (your reminders will NOT be deleted, but the bot may respond to existing "
|
||||||
f"commands a second time).")
|
# f"commands a second time).")
|
||||||
|
|
||||||
red_x_and_lock_emoji = "❌ 🔐"
|
red_x_and_lock_emoji = "❌ 🔐"
|
||||||
|
|
||||||
|
|
|
@ -184,7 +184,7 @@ async def get_thread_content(client: AsyncClient, room: MatrixRoom, base_event:
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
|
||||||
async def process_chat(client, room, event, command, store, openai, thread_root_id: str = None):
|
async def process_chat(client, room, event, command, store, openai, thread_root_id: str = None, system_prompt: str = None):
|
||||||
if not store.check_seen_event(event.event_id):
|
if not store.check_seen_event(event.event_id):
|
||||||
await client.room_typing(room.room_id, typing_state=True, timeout=3000)
|
await client.room_typing(room.room_id, typing_state=True, timeout=3000)
|
||||||
# if self.reply_in_thread:
|
# if self.reply_in_thread:
|
||||||
|
@ -196,6 +196,9 @@ async def process_chat(client, room, event, command, store, openai, thread_root_
|
||||||
messages = [
|
messages = [
|
||||||
{'role': 'user', 'content': command},
|
{'role': 'user', 'content': command},
|
||||||
]
|
]
|
||||||
|
if system_prompt:
|
||||||
|
messages.insert(0, {"role": "system", "content": system_prompt}, )
|
||||||
|
print(messages)
|
||||||
|
|
||||||
response = openai['openai'].ChatCompletion.create(
|
response = openai['openai'].ChatCompletion.create(
|
||||||
model=openai['model'],
|
model=openai['model'],
|
||||||
|
|
Loading…
Reference in New Issue