Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/uipath/src/uipath/agent/react/conversational_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PromptUserSettings(BaseModel):
_AGENT_SYSTEM_PROMPT_PREFIX_TEMPLATE = """You are {{CONVERSATIONAL_AGENT_SERVICE_PREFIX_agentName}}.
The current date is: {{CONVERSATIONAL_AGENT_SERVICE_PREFIX_currentDate}}.
Understand user goals through conversation and use appropriate tools to fulfill requests.
{{CONVERSATIONAL_AGENT_SERVICE_PREFIX_conversationIdPrompt}}

=====================================================================
PRECEDENCE HIERARCHY
Expand Down Expand Up @@ -136,18 +137,25 @@ class PromptUserSettings(BaseModel):
{user_settings_json}
```"""

_CONVERSATION_ID_TEMPLATE = """
The current conversation ID is {conversation_id}.
You should generally not discuss this conversation ID with the user, but it may be useful to include as a tool-call argument when relevant."
"""


def get_chat_system_prompt(
model: str,
system_message: str,
agent_name: str | None,
user_settings: PromptUserSettings | None = None,
conversation_id: str | None = None,
) -> str:
"""Generate a system prompt for a conversational agent.

Args:
agent_definition: Conversational agent definition
user_settings: Optional user data that is injected into the system prompt.
conversation_id: Optional conversation identifier that is injected into the system prompt.

Returns:
The complete system prompt string
Expand Down Expand Up @@ -177,6 +185,10 @@ def get_chat_system_prompt(
"{{CONVERSATIONAL_AGENT_SERVICE_PREFIX_userSettingsPrompt}}",
get_user_settings_template(user_settings),
)
prompt = prompt.replace(
"{{CONVERSATIONAL_AGENT_SERVICE_PREFIX_conversationIdPrompt}}",
get_conversation_id_template(conversation_id),
)

return prompt

Expand All @@ -190,7 +202,7 @@ def get_user_settings_template(
user_settings: User profile information

Returns:
The user context template with JSON or empty string
The filled-in user settings template if user_settings is provided, otherwise an empty string
"""
if user_settings is None:
return ""
Expand All @@ -205,3 +217,17 @@ def get_user_settings_template(

user_settings_json = json.dumps(settings_dict, ensure_ascii=False)
return _USER_CONTEXT_TEMPLATE.format(user_settings_json=user_settings_json)


def get_conversation_id_template(conversation_id: str | None) -> str:
"""Get the conversation-ID prompt section.

Args:
conversation_id: The ID of the current conversation, if any

Returns:
The filled-in conversation ID template if conversation_id is provided, otherwise an empty string
"""
if not conversation_id:
return ""
return _CONVERSATION_ID_TEMPLATE.format(conversation_id=conversation_id)
62 changes: 62 additions & 0 deletions packages/uipath/tests/agent/react/test_conversational_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,68 @@ def test_generate_system_prompt_unnamed_agent_uses_default(self):
assert "You are Unnamed Agent." in prompt


class TestConversationIdInPrompt:
"""Tests for conversation_id in generated prompts."""

def test_prompt_includes_conversation_id_when_provided(self):
prompt = get_chat_system_prompt(
model="claude-3-sonnet",
system_message=SYSTEM_MESSAGE,
agent_name="Test Agent",
user_settings=None,
conversation_id="conv-abc-123",
)

assert "The current conversation ID is conv-abc-123" in prompt
assert (
"You should generally not discuss this conversation ID with the user, but it may be useful to include as a tool-call argument when relevant."
in prompt
)
assert (
"{{CONVERSATIONAL_AGENT_SERVICE_PREFIX_conversationIdPrompt}}" not in prompt
)

def test_prompt_omits_conversation_id_section_when_none(self):
prompt = get_chat_system_prompt(
model="claude-3-sonnet",
system_message=SYSTEM_MESSAGE,
agent_name="Test Agent",
user_settings=None,
conversation_id=None,
)

assert "conversation ID" not in prompt
assert (
"{{CONVERSATIONAL_AGENT_SERVICE_PREFIX_conversationIdPrompt}}" not in prompt
)

def test_prompt_omits_conversation_id_section_when_empty_string(self):
prompt = get_chat_system_prompt(
model="claude-3-sonnet",
system_message=SYSTEM_MESSAGE,
agent_name="Test Agent",
user_settings=None,
conversation_id="",
)

assert "conversation ID" not in prompt

def test_prompt_defaults_to_no_conversation_id(self):
"""conversation_id parameter defaults to None — call sites that don't
pass it must not get a dangling placeholder."""
prompt = get_chat_system_prompt(
model="claude-3-sonnet",
system_message=SYSTEM_MESSAGE,
agent_name="Test Agent",
user_settings=None,
)

assert (
"{{CONVERSATIONAL_AGENT_SERVICE_PREFIX_conversationIdPrompt}}" not in prompt
)
assert "conversation ID" not in prompt


class TestCitationFormat:
"""Tests for citation format in generated prompts."""

Expand Down
Loading