Skip to content

Commit 0b0ea14

Browse files
committed
fix: manage system prompt erase while added intents injected
1 parent c3caa50 commit 0b0ea14

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

lib/abi/services/agent/Agent.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,13 @@ def read_makefile() -> str:
519519
list_tools_available,
520520
list_subagents_available,
521521
list_intents_available,
522-
get_current_active_agent,
523-
get_supervisor_agent,
524522
]
525523
if self.state.supervisor_agent and self.state.supervisor_agent != self.name:
526524
tools.append(request_help)
525+
526+
if self.state.supervisor_agent is not None or len(self._agents) > 0:
527+
tools.append(get_current_active_agent)
528+
tools.append(get_supervisor_agent)
527529

528530
if self.state.supervisor_agent == self.name and os.getenv("ENV") == "dev":
529531
tools.append(read_makefile)
@@ -740,10 +742,12 @@ def current_active_agent(self, state: MessagesState) -> Command:
740742

741743
if "CURRENT_DATE" not in self._system_prompt:
742744
from datetime import datetime
743-
self._system_prompt += f"\n\n\nCURRENT_DATE: {datetime.now().strftime('%Y-%m-%d')}\n"
745+
current_date_str = f"CURRENT_DATE: The current date is {datetime.now().strftime('%Y-%m-%d')}\n"
746+
self._system_prompt = self._system_prompt + "\n" + current_date_str
744747
self.set_system_prompt(self._system_prompt)
748+
return Command(goto="current_active_agent")
745749

746-
# logger.debug(f"System prompt: {self._system_prompt}")
750+
logger.debug(f"💬 System prompt: {self._system_prompt}")
747751
return Command(goto="continue_conversation")
748752

749753
def continue_conversation(self, state: MessagesState) -> Command:
@@ -886,17 +890,13 @@ def call_tools(self, state: MessagesState) -> list[Command]:
886890
args = {"state": state, "tool_call": {**tool_call, "role": "tool_call"}}
887891

888892
try:
889-
890-
logger.debug(f"tool_call: {tool_call}")
891-
logger.debug(f"tool_input_fields: {tool_input_fields}")
892-
logger.debug(f"args: {args}")
893+
logger.debug(f"TOOL CALL ARGS: {args}")
893894
tool_response = tool_.invoke(args)
894895
called_tools.append(tool_)
895-
896+
logger.debug(f"TOOL CALL RESPONSE: {tool_response}, TYPE: {type(tool_response)}")
896897

897898
if isinstance(tool_response, ToolMessage):
898899
results.append(Command(update={"messages": [tool_response]}))
899-
900900
# handle tools that return Command directly
901901
elif isinstance(tool_response, Command):
902902
results.append(tool_response)
@@ -923,13 +923,17 @@ def call_tools(self, state: MessagesState) -> list[Command]:
923923
break
924924

925925
# If the last response is a ToolMessage, we want the model to interpret it.
926-
if isinstance(pd.get(results[-1], 'update.messages[-1]', None), ToolMessage) and not pd.get(results[-1], 'update.messages[-1]', None).name.startswith("transfer_to_"):
926+
if (
927+
isinstance(pd.get(results[-1], 'update.messages[-1]', None), ToolMessage) and
928+
not pd.get(results[-1], 'update.messages[-1]', None).name.startswith("transfer_to_")
929+
):
927930
if return_direct is False:
928931
logger.debug(f"ToolMessage found in results SENDING TO CALL_MODEL: {results[-1]}")
929932
results.append(Command(goto="call_model"))
930933
else:
931934
logger.debug("Injecting ToolMessage into AIMessage for the user to see.")
932935
last_message = pd.get(results[-1], 'update.messages[-1]', None)
936+
logger.debug(f"last_message: {last_message}")
933937
results.append(Command(update={"messages": [AIMessage(content=last_message.content)]}))
934938

935939
if (

lib/abi/services/agent/IntentAgent.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -733,32 +733,39 @@ def inject_intents_in_system_prompt(self, state: IntentState):
733733
logger.debug("💉 Inject Intents in System Prompt")
734734

735735
# We reset the system prompt to the original one.
736-
self._system_prompt = self._configuration.system_prompt
736+
# self._system_prompt = self._configuration.system_prompt
737737

738738
if "intent_mapping" in state and len(state["intent_mapping"]["intents"]) > 0:
739739
intents = state["intent_mapping"]["intents"]
740-
updated_system_prompt = f"""{self._configuration.system_prompt}
740+
intents_str = ""
741+
for intent in intents:
742+
intents_str += f"-Mapped intent: `{intent['intent'].intent_value}`, tool to call: `{intent['intent'].intent_target}`\n"
743+
744+
if "<intents_rules>" not in self._system_prompt:
745+
updated_system_prompt = f"""{self._system_prompt}
741746
742-
---
743-
INTENT RULES:
747+
<intents_rules>
744748
Everytime a user is sending a message, a system is trying to map the prompt/message to an intent or a list of intents using a vector search.
745749
The following is the list of mapped intents. This list will change over time as new messages comes in.
746750
You must analyze if the user message and the mapped intents are related to each other.
747751
If it's the case, you must take them into account, otherwise you must ignore the ones that are not related.
748752
If you endup with a single intent which is of type RAW, you must output the intent_target and nothing else as there will be tests asserting the correctness of the output.
749753
If you endup with a single intent which is of type TOOL, you must call this tool.
750754
755+
<intents>\n{intents_str}\n</intents>
751756
752-
757+
</intents_rules>
753758
"""
754-
755-
for intent in intents:
756-
updated_system_prompt += f"""
757-
- {intent['intent']}
758-
"""
759+
else:
760+
import re
761+
pattern = r'(<intents>)(.*?)(</intents>)'
762+
def replace(match):
763+
return f"{match.group(1)}\n{intents_str}\n{match.group(3)}"
764+
updated_system_prompt = re.sub(pattern, replace, self._system_prompt, flags=re.DOTALL)
759765

760-
updated_system_prompt += "\n\nEND INTENT RULES"
761766
self._system_prompt = updated_system_prompt
767+
self.set_system_prompt(self._system_prompt)
768+
logger.debug(f"Injected in system prompt: {self._system_prompt}")
762769

763770

764771
def duplicate(self, queue: Queue | None = None, agent_shared_state: AgentSharedState | None = None) -> "IntentAgent":

0 commit comments

Comments
 (0)