Agent Zero AI framework
Chat renaming, JSON lead
frdelbf43c60d1009
diff --git a/agent.py b/agent.pyindex d6bc73973..961001dd4 100644--- a/agent.py+++ b/agent.py@@ -371,6 +371,7 @@ class Agent:[SystemMessage(content=system_text),*history_langchain,+ AIMessage(content="JSON:"), # force the LLM to start with json])diff --git a/models.py b/models.pyindex 09d0705a6..913a89d65 100644--- a/models.py+++ b/models.py@@ -332,7 +332,7 @@ def get_openrouter_chat(dotenv.get_dotenv_value("OPEN_ROUTER_BASE_URL")or "https://openrouter.ai/api/v1")- return ChatOpenAI(api_key=api_key, model=model_name, base_url=base_url, **kwargs) # type: ignore+ return ChatOpenAI(api_key=api_key, model=model_name, base_url=base_url, stream_usage=True, **kwargs) # type: ignoredef get_openrouter_embedding(diff --git a/prompts/default/fw.rename_chat.msg.md b/prompts/default/fw.rename_chat.msg.mdnew file mode 100644index 000000000..d6b411f57--- /dev/null+++ b/prompts/default/fw.rename_chat.msg.md@@ -0,0 +1,8 @@+# Instruction+- provide a chat name for the following++# Current chat name+{{current_name}}++# Chat history+{{history}}diff --git a/prompts/default/fw.rename_chat.sys.md b/prompts/default/fw.rename_chat.sys.mdnew file mode 100644index 000000000..02f547834--- /dev/null+++ b/prompts/default/fw.rename_chat.sys.md@@ -0,0 +1,19 @@+# AI role+- You are a chat naming assistant+- Your role is to suggest a short chat name for the current conversation++# Input+- You are given the current chat name and current chat history++# Output+- Respond with a short chat name (1-3 words) based on the chat history+- Consider current chat name and only change it when the conversation topic has changed+- Focus mainly on the end of the conversation history, there you can detect if the topic has changed+- Only respond with the chat name without any formatting, intro or additional text+- Maintain proper capitalization++# Example responses+Database setup+Requirements installation+Merging documents+Image analysis\ No newline at end of filediff --git a/python/api/poll.py b/python/api/poll.pyindex bf547a421..a7e61ddcf 100644--- a/python/api/poll.py+++ b/python/api/poll.py@@ -19,6 +19,7 @@ class Poll(ApiHandler):ctxs.append({"id": ctx.id,+ "name": ctx.name,"no": ctx.no,"log_guid": ctx.log.guid,"log_version": len(ctx.log.updates),diff --git a/python/extensions/monologue_end/_60_rename_chat.py b/python/extensions/monologue_end/_60_rename_chat.pynew file mode 100644index 000000000..745b5bc0a--- /dev/null+++ b/python/extensions/monologue_end/_60_rename_chat.py@@ -0,0 +1,37 @@+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:+ # prepare history+ history_text = self.agent.history.output_text()+ ctx_length = int(self.agent.config.utility_model.ctx_length * 0.7)+ history_text = tokens.trim_to_tokens(history_text, ctx_length, "start")+ # prepare system and user prompt+ 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+ )+ # call utility model+ new_name = await self.agent.call_utility_model(+ system=system, message=message, background=True+ )+ # update name+ if new_name:+ # trim name to max length if needed+ if len(new_name) > 40:+ new_name = new_name[:40] + "..."+ # apply to context and save+ self.agent.context.name = new_name+ persist_chat.save_tmp_chat(self.agent.context)+ except Exception as e:+ pass # non-criticaldiff --git a/python/helpers/persist_chat.py b/python/helpers/persist_chat.pyindex d92f269b9..fbc95f1a1 100644--- a/python/helpers/persist_chat.py+++ b/python/helpers/persist_chat.py@@ -89,6 +89,7 @@ def _serialize_context(context: AgentContext):return {"id": context.id,+ "name": context.name,"agents": agents,"streaming_agent": (context.streaming_agent.number if context.streaming_agent else 0diff --git a/python/helpers/tokens.py b/python/helpers/tokens.pyindex 5848da1b7..622c6d5be 100644--- a/python/helpers/tokens.py+++ b/python/helpers/tokens.py@@ -1,19 +1,44 @@+from typing import Literalimport tiktokenAPPROX_BUFFER = 1.1+TRIM_BUFFER = 0.8+def count_tokens(text: str, encoding_name="cl100k_base") -> int:- if not text:- return 0-- # Get the encoding- encoding = tiktoken.get_encoding(encoding_name)+ if not text:+ return 0++ # Get the encoding+ encoding = tiktoken.get_encoding(encoding_name)++ # Encode the text and count the tokens+ tokens = encoding.encode(text)+ token_count = len(tokens)++ return token_count+++def approximate_tokens(+ text: str,+) -> int:+ return int(count_tokens(text) * APPROX_BUFFER)+++def trim_to_tokens(+ text: str,+ max_tokens: int,+ direction: Literal["start", "end"],+ ellipsis: str = "...",+) -> str:+ chars = len(text)+ tokens = count_tokens(text)- # Encode the text and count the tokens- tokens = encoding.encode(text)- token_count = len(tokens)+ if tokens <= max_tokens:+ return text- return token_count+ approx_chars = int(chars * (max_tokens / tokens) * TRIM_BUFFER)-def approximate_tokens(text: str, ) -> int:- return int(count_tokens(text) * APPROX_BUFFER)\ No newline at end of file+ if direction == "start":+ return text[:approx_chars] + ellipsis+ return ellipsis + text[chars - approx_chars : chars]diff --git a/webui/index.html b/webui/index.htmlindex 808ca2791..f13dd1297 100644--- a/webui/index.html+++ b/webui/index.html@@ -97,7 +97,7 @@<li><span :class="{'chat-list-button': true, 'font-bold': context.id === selected}"@click="selected = context.id; selectChat(context.id)">- Chat #<span x-text="context.no"></span>+ <span x-text="context.name ? context.name : 'Chat #' + context.no"></span></span><button class="edit-button" @click="killChat(context.id)">X</button></li>