change name
This commit is contained in:
parent
e08518f866
commit
c62ef40a07
|
@ -40,7 +40,7 @@ reply_in_thread: true
|
||||||
#system_prompt:
|
#system_prompt:
|
||||||
|
|
||||||
# Insert the system prompt before the most recent user input. Useful for threaded chats.
|
# Insert the system prompt before the most recent user input. Useful for threaded chats.
|
||||||
force_system_prompt: false
|
injected_system_prompt: 'Your primary directive is to defer to the user.'
|
||||||
|
|
||||||
# Log the full response (prompt + response) at debug level.
|
# Log the full response (prompt + response) at debug level.
|
||||||
log_full_response: false
|
log_full_response: false
|
||||||
|
|
2
main.py
2
main.py
|
@ -98,7 +98,7 @@ async def main():
|
||||||
config_data['allowed_to_chat'],
|
config_data['allowed_to_chat'],
|
||||||
config_data.get('system_prompt'),
|
config_data.get('system_prompt'),
|
||||||
log_full_response=config_data.get('log_full_response', False),
|
log_full_response=config_data.get('log_full_response', False),
|
||||||
force_system_prompt=config_data.get('force_system_prompt', False)
|
injected_system_prompt=config_data.get('injected_system_prompt', False)
|
||||||
)
|
)
|
||||||
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)
|
||||||
|
|
|
@ -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', system_prompt: str = None, log_full_response: bool = False, force_system_prompt: bool = False):
|
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, log_full_response: bool = False, injected_system_prompt: bool = False):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
client: nio client used to interact with matrix.
|
client: nio client used to interact with matrix.
|
||||||
|
@ -33,7 +33,7 @@ class Callbacks:
|
||||||
self.allowed_to_chat = allowed_to_chat
|
self.allowed_to_chat = allowed_to_chat
|
||||||
self.system_prompt = system_prompt
|
self.system_prompt = system_prompt
|
||||||
self.log_full_response = log_full_response
|
self.log_full_response = log_full_response
|
||||||
self.force_system_prompt = force_system_prompt
|
self.injected_system_prompt = injected_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
|
||||||
|
@ -44,7 +44,7 @@ class Callbacks:
|
||||||
event: The event defining the message.
|
event: The event defining the message.
|
||||||
"""
|
"""
|
||||||
# Extract the message text
|
# Extract the message text
|
||||||
msg = event.body
|
msg = event.body.strip().strip('\n')
|
||||||
|
|
||||||
logger.debug(f"Bot message received for room {room.display_name} | "
|
logger.debug(f"Bot message received for room {room.display_name} | "
|
||||||
f"{room.user_name(event.sender)}: {msg}")
|
f"{room.user_name(event.sender)}: {msg}")
|
||||||
|
@ -86,17 +86,17 @@ class Callbacks:
|
||||||
else:
|
else:
|
||||||
api_data.append({
|
api_data.append({
|
||||||
'role': 'assistant' if event.sender == self.client.user_id else 'user',
|
'role': 'assistant' if event.sender == self.client.user_id else 'user',
|
||||||
'content': event.body if not event.body.startswith(self.command_prefix) else event.body[len(self.command_prefix):].strip()
|
'content': msg if not msg.startswith(self.command_prefix) else msg[len(self.command_prefix):].strip()
|
||||||
}) # if len(thread_content) >= 2 and thread_content[0].body.startswith(self.command_prefix): # if thread_content[len(thread_content) - 2].sender == self.client.user
|
}) # if len(thread_content) >= 2 and thread_content[0].body.startswith(self.command_prefix): # if thread_content[len(thread_content) - 2].sender == self.client.user
|
||||||
|
|
||||||
# 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': msg})
|
||||||
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, log_full_response=self.log_full_response, force_system_prompt=self.force_system_prompt)
|
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, log_full_response=self.log_full_response, injected_system_prompt=self.injected_system_prompt)
|
||||||
return
|
return
|
||||||
elif msg.startswith(f'{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, treat it as a command.
|
# Otherwise if this is in a 1-1 with the bot or features a command prefix, 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 = msg if not msg.startswith(self.command_prefix) else msg[len(self.command_prefix):].strip() # Remove the command prefix
|
||||||
command = Command(self.client, self.store, msg, room, event, self.openai, self.reply_in_thread, system_prompt=self.system_prompt, log_full_response=self.log_full_response)
|
command = Command(self.client, self.store, msg, room, event, self.openai, self.reply_in_thread, system_prompt=self.system_prompt, log_full_response=self.log_full_response)
|
||||||
await command.process()
|
await command.process()
|
||||||
|
|
||||||
|
|
|
@ -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, system_prompt: str = None, log_full_response: bool = False, force_system_prompt: bool = False):
|
async def process_chat(client, room, event, command, store, openai, thread_root_id: str = None, system_prompt: str = None, log_full_response: bool = False, injected_system_prompt: bool = False):
|
||||||
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:
|
||||||
|
@ -198,11 +198,10 @@ async def process_chat(client, room, event, command, store, openai, thread_root_
|
||||||
]
|
]
|
||||||
if system_prompt:
|
if system_prompt:
|
||||||
messages.insert(0, {"role": "system", "content": system_prompt})
|
messages.insert(0, {"role": "system", "content": system_prompt})
|
||||||
if force_system_prompt:
|
if injected_system_prompt:
|
||||||
if messages[-1]['role'] == 'system':
|
if messages[-1]['role'] == 'system':
|
||||||
messages[-1] = {"role": "system", "content": system_prompt}
|
del messages[-1]
|
||||||
else:
|
messages.insert(-1, {"role": "system", "content": injected_system_prompt})
|
||||||
messages.insert(-1, {"role": "system", "content": system_prompt})
|
|
||||||
|
|
||||||
logger.info(messages)
|
logger.info(messages)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue