@ -59,12 +59,14 @@ class Callbacks(object):
else :
english_original_message = None
chat_message = self . bot . chat_history . room ( room . display_name ) . add ( event . event_id , event . server_timestamp , room . user_name ( event . sender ) , event . sender == self . client . user , is_command , relates_to , event . body , language , english_original_message )
chat_message = self . bot . chat_history . room ( room . room_id ) . add ( event . event_id , event . server_timestamp , room . user_name ( event . sender ) , event . sender == self . client . user , is_command , relates_to , event . body , language , english_original_message )
# parse keywords
self . bot . extra_info = { " persona " : [ ] , " scenario " : [ ] , " example_dialogue " : [ ] }
for i , keyword in enumerate ( self . bot . keywords ) :
if re . search ( keyword [ " regex " ] , event . body , flags = re . IGNORECASE ) :
if not ' active ' in self . bot . keywords [ i ] or self . bot . keywords [ i ] [ ' active ' ] < 1 :
self . bot . chat_history . room ( room . room_id ) . setFastForward ( False )
self . bot . keywords [ i ] [ ' active ' ] = int ( keyword [ " duration " ] )
logger . info ( f " keyword \" { keyword [ ' regex ' ] } \" detected " )
if ' active ' in self . bot . keywords [ i ] :
@ -185,7 +187,7 @@ class Callbacks(object):
elif event . body . startswith ( ' !temperature ' ) :
self . bot . temperature = float ( event . body . removeprefix ( ' !temperature ' ) . strip ( ) )
elif event . body . startswith ( ' !begin ' ) :
self . bot . chat_history . room ( room . display_name ) . clear ( )
self . bot . chat_history . room ( room . room_id ) . clear ( )
self . bot . room_config [ room . room_id ] [ " tick " ] = 0
await self . bot . write_conf2 ( self . bot . name )
await self . bot . send_message ( self . client , room . room_id , self . bot . greeting )
@ -197,30 +199,30 @@ class Callbacks(object):
self . bot . room_config [ room . room_id ] [ " disabled " ] = True
return
elif event . body . startswith ( ' !!! ' ) :
if self . bot . chat_history . room ( room . display_name ) . getLen ( ) < 3 :
if self . bot . chat_history . room ( room . room_id ) . getLen ( ) < 3 :
return
chat_history_item = self . bot . chat_history . room ( room . display_name ) . remove ( 1 ) # current
chat_history_item = self . bot . chat_history . room ( room . room_id ) . remove ( 1 ) # current
await self . client . room_redact ( room . room_id , chat_history_item . event_id , reason = " user-request " )
chat_history_item = self . bot . chat_history . room ( room . display_name ) . remove ( 1 )
chat_history_item = self . bot . chat_history . room ( room . room_id ) . remove ( 1 )
await self . client . room_redact ( room . room_id , chat_history_item . event_id , reason = " user-request " )
chat_history_item = self . bot . chat_history . room ( room . display_name ) . remove ( 1 )
chat_history_item = self . bot . chat_history . room ( room . room_id ) . remove ( 1 )
await self . client . room_redact ( room . room_id , chat_history_item . event_id , reason = " user-request " )
return
elif event . body . startswith ( ' !! ' ) :
if self . bot . chat_history . room ( room . display_name ) . getLen ( ) < 3 :
if self . bot . chat_history . room ( room . room_id ) . getLen ( ) < 3 :
return
chat_history_item = self . bot . chat_history . room ( room . display_name ) . remove ( 1 ) # current
chat_history_item = self . bot . chat_history . room ( room . room_id ) . remove ( 1 ) # current
await self . client . room_redact ( room . room_id , chat_history_item . event_id , reason = " user-request " )
chat_history_item = self . bot . chat_history . room ( room . display_name ) . remove ( 1 )
chat_history_item = self . bot . chat_history . room ( room . room_id ) . remove ( 1 )
await self . client . room_redact ( room . room_id , chat_history_item . event_id , reason = " user-request " )
chat_message = self . bot . chat_history . room ( room . display_name ) . getLastItem ( ) # new current
chat_message = self . bot . chat_history . room ( room . room_id ) . getLastItem ( ) # new current
# don't return, we generate a new answer
elif event . body . startswith ( ' !replace ' ) :
if self . bot . chat_history . room ( room . display_name ) . getLen ( ) < 3 :
if self . bot . chat_history . room ( room . room_id ) . getLen ( ) < 3 :
return
chat_history_item = self . bot . chat_history . room ( room . display_name ) . remove ( 1 ) # current
chat_history_item = self . bot . chat_history . room ( room . room_id ) . remove ( 1 ) # current
await self . client . room_redact ( room . room_id , chat_history_item . event_id , reason = " user-request " )
chat_history_item = self . bot . chat_history . room ( room . display_name ) . remove ( 1 )
chat_history_item = self . bot . chat_history . room ( room . room_id ) . remove ( 1 )
await self . client . room_redact ( room . room_id , chat_history_item . event_id , reason = " user-request " )
new_answer = event . body . removeprefix ( ' !replace ' ) . strip ( )
await self . bot . send_message ( self . client , room . room_id , new_answer , reply_to = chat_history_item . relates_to_event )
@ -237,7 +239,7 @@ class Callbacks(object):
# send, mail, drop, snap picture, photo, image, portrait
pass
full_prompt = await text_ai . get_full_prompt ( chat_message . getTranslation ( " en " ) , self . bot , self . bot . chat_history . room ( room . display_name ) )
full_prompt = await text_ai . get_full_prompt ( chat_message . getTranslation ( " en " ) , self . bot , self . bot . chat_history . room ( room . room_id ) )
num_tokens = await text_ai . num_tokens ( full_prompt )
logger . debug ( full_prompt )
logger . info ( f " Prompt has " + str ( num_tokens ) + " tokens " )
@ -288,11 +290,12 @@ class Callbacks(object):
logger . info ( f " event redacted in room { room . room_id } . event_id: { event . redacts } " )
for bot in bots :
# for room in bot.chat_history.chat_rooms.keys():
if room . display_name in bot . chat_history . chat_rooms :
if room . room_id in bot . chat_history . chat_rooms :
logger . info ( " room found " )
if bot . chat_history . chat_rooms [ room . display_name ] . exists_id ( event . redacts ) :
if bot . chat_history . chat_rooms [ room . room_id ] . exists_id ( event . redacts ) :
logger . info ( " found it " )
bot . chat_history . chat_rooms [ room . display_name ] . remove_id ( event . redacts )
bot . chat_history . chat_rooms [ room . room_id ] . remove_id ( event . redacts )
self . bot . chat_history . room ( room . room_id ) . setFastForward ( False )
class ChatBot ( object ) :
""" Main chatbot """
@ -338,6 +341,7 @@ class ChatBot(object):
self . scenario = scenario
self . greeting = greeting
self . example_dialogue = example_dialogue
self . chat_history = BotChatHistory ( self . name )
def get_persona ( self ) :
return ' ' . join ( [ self . persona , ' ' . join ( self . extra_info [ ' persona ' ] ) ] )
@ -374,7 +378,6 @@ class ChatBot(object):
async def login ( self ) :
self . config = AsyncClientConfig ( store_sync_tokens = True )
self . client = AsyncClient ( self . homeserver , self . user_id , store_path = STORE_PATH , config = self . config )
self . chat_history = BotChatHistory ( self . name )
self . callbacks = Callbacks ( self . client , self )
self . client . add_event_callback ( self . callbacks . message_cb , RoomMessageText )
self . client . add_event_callback ( self . callbacks . invite_cb , InviteEvent )