Skip to content

Agent Zero AI framework

Behaviour prompt

frdel1c026ee75f6f
diff --git a/prompts/default/agent.system.behaviour.md b/prompts/default/agent.system.behaviour.md
new file mode 100644
index 000000000..124815724
--- /dev/null
+++ b/prompts/default/agent.system.behaviour.md
@@ -0,0 +1,2 @@
+# Behavioral Rules
+{{rules}}
\ No newline at end of file
diff --git a/prompts/default/agent.system.behaviour_default.md b/prompts/default/agent.system.behaviour_default.md
new file mode 100644
index 000000000..ab2a2c6a0
--- /dev/null
+++ b/prompts/default/agent.system.behaviour_default.md
@@ -0,0 +1 @@
+- Use linux commands for simple tasks where possible instead of python
\ No newline at end of file
diff --git a/prompts/default/behaviour.merge.msg.md b/prompts/default/behaviour.merge.msg.md
new file mode 100644
index 000000000..5387ebcbf
--- /dev/null
+++ b/prompts/default/behaviour.merge.msg.md
@@ -0,0 +1,5 @@
+# Current ruleset
+{{current_rules}}
+
+# Adjustments
+{{adjustments}}
\ No newline at end of file
diff --git a/prompts/default/behaviour.merge.sys.md b/prompts/default/behaviour.merge.sys.md
new file mode 100644
index 000000000..31ff6cfe0
--- /dev/null
+++ b/prompts/default/behaviour.merge.sys.md
@@ -0,0 +1,17 @@
+# Assistant's job
+1. The assistant receives a markdown ruleset of AGENT's behaviour and JSON array of adjustments to be implemented
+2. Assistant merges the ruleset with the instructions JSON array into a new markdown ruleset
+3. Assistant keeps the ruleset short, removing any duplicates or redundant information
+
+# Format
+- The response format is a markdown format of instructions for AI AGENT explaining how the AGENT is supposed to behave
+- No level 1 headings (#), only level 2 headings (##) and bullet points (*)
+
+# Example when instructions found (do not output this example):
+```json
+# Language
+- The user want to communicate in Spanish, always write responses for the user in Spanish.
+
+# Format
+- User asked for shorted responses, be short and to the point
+```
\ No newline at end of file
diff --git a/prompts/default/behaviour.search.sys.md b/prompts/default/behaviour.search.sys.md
new file mode 100644
index 000000000..459e79bef
--- /dev/null
+++ b/prompts/default/behaviour.search.sys.md
@@ -0,0 +1,20 @@
+# Assistant's job
+1. The assistant receives a history of conversation between USER and AGENT
+2. Assistant searches for USER's commands to update AGENT's behaviour
+3. Assistant responds with JSON array of instructions to update AGENT's behaviour or empty array if none
+
+# Format
+- The response format is a JSON array of instructions on how the agent should behave in the future
+- If the history does not contain any instructions, the response will be an empty JSON array
+
+# Example when instructions found (do not output this example):
+```json
+[
+ "Never call the user by his name",
+]
+```
+
+# Example when no instructions:
+```json
+[]
+```
\ No newline at end of file
diff --git a/python/extensions/message_loop_prompts/_20_behaviour_prompt.py b/python/extensions/message_loop_prompts/_20_behaviour_prompt.py
new file mode 100644
index 000000000..7180e9008
--- /dev/null
+++ b/python/extensions/message_loop_prompts/_20_behaviour_prompt.py
@@ -0,0 +1,24 @@
+from datetime import datetime
+from python.helpers.extension import Extension
+from agent import Agent, LoopData
+from python.helpers import files, memory
+
+
+class BehaviourPrompt(Extension):
+
+ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
+ prompt = read_rules(self.agent)
+ loop_data.system.append(prompt)
+
+def get_custom_rules_file(agent: Agent):
+ return memory.get_memory_subdir_abs(agent) + f"/behaviour.md"
+
+def read_rules(agent: Agent):
+ rules_file = get_custom_rules_file(agent)
+ if files.exists(rules_file):
+ rules = files.read_file(rules_file)
+ return agent.read_prompt("agent.system.behaviour.md", rules=rules)
+ else:
+ rules = agent.read_prompt("agent.system.behaviour_default.md")
+ return agent.read_prompt("agent.system.behaviour.md", rules=rules)
+
\ No newline at end of file
diff --git a/python/extensions/monologue_start/_20_behaviour_update.py b/python/extensions/monologue_start/_20_behaviour_update.py
new file mode 100644
index 000000000..19583092b
--- /dev/null
+++ b/python/extensions/monologue_start/_20_behaviour_update.py
@@ -0,0 +1,73 @@
+import asyncio
+from datetime import datetime
+import json
+from python.helpers.extension import Extension
+from agent import Agent, LoopData
+from python.helpers import dirty_json, files, memory
+from python.helpers.log import LogItem
+from python.extensions.message_loop_prompts import _20_behaviour_prompt
+
+
+
+class BehaviourUpdate(Extension):
+
+ async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
+ log_item = self.agent.context.log.log(
+ type="util",
+ heading="Updating behaviour",
+ )
+ asyncio.create_task(self.update_rules(self.agent, loop_data, log_item))
+
+ async def update_rules(self, agent: Agent, loop_data: LoopData, log_item: LogItem, **kwargs):
+ adjustments = await self.get_adjustments(agent, loop_data, log_item)
+ if adjustments:
+ await self.merge_rules(agent, adjustments, loop_data, log_item)
+
+ async def get_adjustments(self, agent: Agent, loop_data: LoopData, log_item: LogItem, **kwargs) -> list[str] | None:
+
+ # get system message and chat history for util llm
+ system = self.agent.read_prompt("behaviour.search.sys.md")
+ msgs_text = self.agent.concat_messages(self.agent.history)
+
+ # log query streamed by LLM
+ def log_callback(content):
+ log_item.stream(content=content)
+
+ # call util llm to find solutions in history
+ adjustments_json = await self.agent.call_utility_llm(
+ system=system,
+ msg=msgs_text,
+ callback=log_callback,
+ )
+
+ adjustments = dirty_json.DirtyJson.parse_string(adjustments_json)
+
+ if adjustments:
+ log_item.update(adjustments=adjustments)
+ return adjustments # type: ignore # for now let's assume the model gets it right and outputs an array
+ else:
+ log_item.update(heading="No updates to behaviour")
+ return None
+
+ async def merge_rules(self, agent: Agent, adjustments: list[str], loop_data: LoopData, log_item: LogItem, **kwargs):
+ # get system message and current ruleset
+ system = self.agent.read_prompt("behaviour.merge.sys.md")
+ current_rules = _20_behaviour_prompt.read_rules(agent)
+
+ # log query streamed by LLM
+ def log_callback(content):
+ log_item.stream(ruleset=content)
+
+ msg = self.agent.read_prompt("behaviour.merge.msg.md", current_rules=current_rules, adjustments=json.dumps(adjustments))
+
+ # call util llm to find solutions in history
+ adjustments_merge = await self.agent.call_utility_llm(
+ system=system,
+ msg=msg,
+ callback=log_callback,
+ )
+
+ # update rules file
+ rules_file = _20_behaviour_prompt.get_custom_rules_file(agent)
+ files.write_file(rules_file, adjustments_merge)
+ log_item.update(heading="Behaviour updated")
\ No newline at end of file
diff --git a/python/helpers/memory.py b/python/helpers/memory.py
index 3b0c69610..1c91b4da5 100644
--- a/python/helpers/memory.py
+++ b/python/helpers/memory.py
@@ -348,3 +348,6 @@ class Memory:
@staticmethod
def get_timestamp():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+
+def get_memory_subdir_abs(agent: Agent) -> str:
+ return files.get_abs_path("memory", agent.config.memory_subdir or "default")
\ No newline at end of file
diff --git a/python/helpers/settings.py b/python/helpers/settings.py
index dc8dc80eb..045575573 100644
--- a/python/helpers/settings.py
+++ b/python/helpers/settings.py
@@ -20,7 +20,6 @@ class Settings(TypedDict):
embed_model_provider: str
embed_model_name: str
- embed_model_temperature: float
embed_model_kwargs: dict[str, str]
@@ -203,7 +202,6 @@ def get_embedding_model() -> Embeddings:
type=ModelType.EMBEDDING,
provider=ModelProvider[settings["embed_model_provider"]],
name=settings["embed_model_name"],
- temperature=settings["embed_model_temperature"],
**settings["embed_model_kwargs"],
)
@@ -232,7 +230,6 @@ def _get_default_settings() -> Settings:
util_model_kwargs={},
embed_model_provider=ModelProvider.OPENAI.name,
embed_model_name="text-embedding-3-small",
- embed_model_temperature=0,
embed_model_kwargs={},
)