|
from python.helpers import persist_chat, tokens |
|
from python.helpers.extension import Extension |
|
from agent import LoopData |
|
import asyncio |
|
|
|
|
|
class RenameChat(Extension): |
|
|
|
async def execute(self, loop_data: LoopData = LoopData(), **kwargs): |
|
asyncio.create_task(self.change_name()) |
|
|
|
async def change_name(self): |
|
try: |
|
|
|
history_text = self.agent.history.output_text() |
|
ctx_length = int(self.agent.config.utility_model.ctx_length * 0.3) |
|
history_text = tokens.trim_to_tokens(history_text, ctx_length, "start") |
|
|
|
system = self.agent.read_prompt("fw.rename_chat.sys.md") |
|
current_name = self.agent.context.name |
|
message = self.agent.read_prompt( |
|
"fw.rename_chat.msg.md", current_name=current_name, history=history_text |
|
) |
|
|
|
new_name = await self.agent.call_utility_model( |
|
system=system, message=message, background=True |
|
) |
|
|
|
if new_name: |
|
|
|
if len(new_name) > 40: |
|
new_name = new_name[:40] + "..." |
|
|
|
self.agent.context.name = new_name |
|
persist_chat.save_tmp_chat(self.agent.context) |
|
except Exception as e: |
|
pass |
|
|