|
@ -89,8 +89,8 @@ class AI(object): |
|
|
self.rooms = {} |
|
|
self.rooms = {} |
|
|
|
|
|
|
|
|
from ..wrappers.langchain_koboldcpp import KoboldCpp |
|
|
from ..wrappers.langchain_koboldcpp import KoboldCpp |
|
|
self.llm_chat = KoboldCpp(temperature=self.bot.temperature, endpoint_url="http://172.16.85.10:5001/api/latest/generate", stop=['<|endoftext|>']) |
|
|
self.llm_chat = KoboldCpp(temperature=self.bot.temperature, endpoint_url="http://172.16.85.10:5001/api/latest/generate", stop=['<|endoftext|>'], verbose=True) |
|
|
self.llm_summary = KoboldCpp(temperature=0.2, endpoint_url="http://172.16.85.10:5002/api/latest/generate", stop=['<|endoftext|>'], max_tokens=512) |
|
|
self.llm_summary = KoboldCpp(temperature=0.2, endpoint_url="http://172.16.85.10:5002/api/latest/generate", stop=['<|endoftext|>'], max_tokens=512, verbose=True) |
|
|
self.llm_chat_model = "pygmalion-7b" |
|
|
self.llm_chat_model = "pygmalion-7b" |
|
|
self.llm_summary_model = "vicuna-13b" |
|
|
self.llm_summary_model = "vicuna-13b" |
|
|
self.text_wrapper = text_wrapper |
|
|
self.text_wrapper = text_wrapper |
|
@ -108,9 +108,13 @@ class AI(object): |
|
|
moving_summary = self.bot.rooms[room_id]['moving_summary'] |
|
|
moving_summary = self.bot.rooms[room_id]['moving_summary'] |
|
|
else: |
|
|
else: |
|
|
moving_summary = "No previous events." |
|
|
moving_summary = "No previous events." |
|
|
|
|
|
if "last_message_ids_summarized" in self.bot.rooms[room_id]: |
|
|
|
|
|
last_message_ids_summarized = self.bot.rooms[room_id]['last_message_ids_summarized'] |
|
|
|
|
|
else: |
|
|
|
|
|
last_message_ids_summarized = [] |
|
|
if not human_prefix: |
|
|
if not human_prefix: |
|
|
human_prefix = "Human" |
|
|
human_prefix = "Human" |
|
|
memory = CustomMemory(memory_key="chat_history", input_key="input", human_prefix=human_prefix, ai_prefix=self.bot.name, llm=self.llm_summary, summary_prompt=prompt_progressive_summary, moving_summary_buffer=moving_summary, max_len=1200, min_len=200) |
|
|
memory = CustomMemory(memory_key="chat_history", input_key="input", human_prefix=human_prefix, ai_prefix=self.bot.name, llm=self.llm_summary, summary_prompt=prompt_progressive_summary, moving_summary_buffer=moving_summary, max_len=1200, min_len=200, last_message_ids_summarized=last_message_ids_summarized) |
|
|
self.rooms[room_id]["memory"] = memory |
|
|
self.rooms[room_id]["memory"] = memory |
|
|
#memory.chat_memory.add_ai_message(self.bot.greeting) |
|
|
#memory.chat_memory.add_ai_message(self.bot.greeting) |
|
|
else: |
|
|
else: |
|
@ -122,6 +126,9 @@ class AI(object): |
|
|
async def add_chat_message(self, message): |
|
|
async def add_chat_message(self, message): |
|
|
room_id = message.additional_kwargs['room_id'] |
|
|
room_id = message.additional_kwargs['room_id'] |
|
|
conversation_memory = self.get_memory(room_id) |
|
|
conversation_memory = self.get_memory(room_id) |
|
|
|
|
|
if 'event_id' in message.additional_kwargs and message.additional_kwargs['event_id'] in conversation_memory.last_message_ids_summarized: |
|
|
|
|
|
#don't add already summarized messages |
|
|
|
|
|
return |
|
|
conversation_memory.chat_memory.messages.append(message) |
|
|
conversation_memory.chat_memory.messages.append(message) |
|
|
conversation_memory.chat_memory_day.messages.append(message) |
|
|
conversation_memory.chat_memory_day.messages.append(message) |
|
|
|
|
|
|
|
@ -218,6 +225,7 @@ class AI(object): |
|
|
# the resulting template text to feed it into the instruct prompt's instruction |
|
|
# the resulting template text to feed it into the instruct prompt's instruction |
|
|
# or do this with the prompt.partial() |
|
|
# or do this with the prompt.partial() |
|
|
|
|
|
|
|
|
|
|
|
for i in range(1): |
|
|
prompt = prompt_chat.partial( |
|
|
prompt = prompt_chat.partial( |
|
|
ai_name=self.bot.name, |
|
|
ai_name=self.bot.name, |
|
|
persona=self.bot.persona, |
|
|
persona=self.bot.persona, |
|
@ -225,9 +233,9 @@ class AI(object): |
|
|
human_name=chat_human_name, |
|
|
human_name=chat_human_name, |
|
|
ai_name_chat=chat_ai_name, |
|
|
ai_name_chat=chat_ai_name, |
|
|
) |
|
|
) |
|
|
if "summary" in prompt_chat.input_variables: |
|
|
if "summary" in prompt.input_variables: |
|
|
prompt = prompt.partial(summary=conversation_memory.moving_summary_buffer) |
|
|
prompt = prompt.partial(summary=conversation_memory.moving_summary_buffer) |
|
|
if "example_dialogue" in prompt_chat.input_variables: |
|
|
if "example_dialogue" in prompt.input_variables: |
|
|
prompt = prompt.partial( |
|
|
prompt = prompt.partial( |
|
|
example_dialogue=self.bot.example_dialogue.replace("{{user}}", chat_human_name) |
|
|
example_dialogue=self.bot.example_dialogue.replace("{{user}}", chat_human_name) |
|
|
) |
|
|
) |
|
@ -238,8 +246,12 @@ class AI(object): |
|
|
if prompt_len+256 > 2000: |
|
|
if prompt_len+256 > 2000: |
|
|
logger.warning(f"Prompt too large. Estimated {prompt_len} tokens") |
|
|
logger.warning(f"Prompt too large. Estimated {prompt_len} tokens") |
|
|
await reply_fn(f"<WARNING> Prompt too large. Estimated {prompt_len} tokens") |
|
|
await reply_fn(f"<WARNING> Prompt too large. Estimated {prompt_len} tokens") |
|
|
|
|
|
if i == 0: |
|
|
await conversation_memory.prune_memory(conversation_memory.min_len) |
|
|
await conversation_memory.prune_memory(conversation_memory.min_len) |
|
|
|
|
|
elif i == 1: |
|
|
|
|
|
conversation_memory.moving_summary_buffer = await self.summarize(conversation_memory.moving_summary_buffer) |
|
|
|
|
|
else: |
|
|
|
|
|
break |
|
|
|
|
|
|
|
|
#roleplay_chain = RoleplayChain(llm_chain=chain, character_name=self.bot.name, persona=self.bot.persona, scenario=self.bot.scenario, ai_name_chat=chat_ai_name, human_name_chat=chat_human_name) |
|
|
#roleplay_chain = RoleplayChain(llm_chain=chain, character_name=self.bot.name, persona=self.bot.persona, scenario=self.bot.scenario, ai_name_chat=chat_ai_name, human_name_chat=chat_human_name) |
|
|
|
|
|
|
|
@ -288,6 +300,7 @@ class AI(object): |
|
|
new_summary_len = self.llm_chat.get_num_tokens(conversation_memory.moving_summary_buffer) |
|
|
new_summary_len = self.llm_chat.get_num_tokens(conversation_memory.moving_summary_buffer) |
|
|
logger.info(f"Refined summary from {summary_len} tokens to {new_summary_len} tokens ({new_summary_len-summary_len} tokens)") |
|
|
logger.info(f"Refined summary from {summary_len} tokens to {new_summary_len} tokens ({new_summary_len-summary_len} tokens)") |
|
|
self.bot.rooms[room_id]['moving_summary'] = conversation_memory.moving_summary_buffer |
|
|
self.bot.rooms[room_id]['moving_summary'] = conversation_memory.moving_summary_buffer |
|
|
|
|
|
self.bot.rooms[room_id]['last_message_ids_summarized'] = conversation_memory.last_message_ids_summarized |
|
|
|
|
|
|
|
|
return output |
|
|
return output |
|
|
|
|
|
|
|
|