Skip to content

Refactor code agent system prompt #1153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 9, 2025
Merged

Conversation

aymeric-roucher
Copy link
Collaborator

No description provided.

@aymeric-roucher aymeric-roucher force-pushed the refactor-system-prompt branch from 2f30c4e to 3113c0f Compare April 7, 2025 13:35
@@ -210,6 +201,16 @@ def __init__(
self.agent_name = self.__class__.__name__
self.model = model
self.prompt_templates = prompt_templates or EMPTY_PROMPT_TEMPLATES
if prompt_templates is not None:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These asserts should guide users trying to customize their agent's prompts.

@@ -113,18 +113,15 @@ system_prompt: |-
If no tool call is needed, use final_answer tool to return your answer.
4. Never re-do a tool call that you previously did with the exact same parameters.
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the monetary reward because putting an objective like this can help exploits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny hack 👍

@@ -158,7 +158,7 @@ def parse_json_blob(json_blob: str) -> Tuple[Dict[str, str], str]:
json_data = json.loads(json_data, strict=False)
return json_data, json_blob[:first_accolade_index]
except IndexError:
raise ValueError("The JSON blob you used is invalid")
raise ValueError("The model output does not contain any JSON blob.")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify the error.

Takes inputs: {{tool.inputs}}
Returns an output of type: {{tool.output_type}}
{%- endfor %}
def {{ tool.name }}({% for arg_name, arg_info in tool.inputs.items() %}{{ arg_name }}: {{ arg_info.type }}{% if not loop.last %}, {% endif %}{% endfor %}) -> {{tool.output_type}}:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make tool definitions look like python functions: should be easier to read for the model.

@aymeric-roucher aymeric-roucher requested a review from A-Mahla April 8, 2025 16:28
Copy link
Member

@A-Mahla A-Mahla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @aymeric-roucher. Just leaving a few comments.

initial_plan="",
update_facts_pre_messages="",
update_facts_post_messages="",
update_plan_pre_messages="",
update_plan_post_messages="",
Copy link
Member

@A-Mahla A-Mahla Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: In a future PR, it would be great to migrate PlanningPromptTemplate, ManagedAgentPromptTemplate, FinalAnswerPromptTemplate, and PromptTemplates to pydantic.BaseModel to enable default initialization. Ultimately, this will allow for a simpler declaration:

EMPTY_PROMPT_TEMPLATES = PromptTemplates()

Comment on lines +205 to +213
missing_keys = set(EMPTY_PROMPT_TEMPLATES.keys()) - set(prompt_templates.keys())
assert not missing_keys, (
f"Some prompt templates are missing from your custom `prompt_templates`: {missing_keys}"
)
for key in EMPTY_PROMPT_TEMPLATES.keys():
for subkey in EMPTY_PROMPT_TEMPLATES[key]:
assert subkey in prompt_templates[key], (
f"Some prompt templates are missing from your custom `prompt_templates`: {subkey} under {key}"
)
Copy link
Member

@A-Mahla A-Mahla Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi: A Pydantic PromptTemplates model enables automatic validation. The code could simply be:

PromptTemplates(**prompt_templates)  # It raises an error if any field or type doesn't match `PromptTemplates`

Alternatively, the function could accept only a PromptTemplates BaseModel instance as an argument.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will also make the EMPTY_PROMPT_TEMPLATES variable unnecessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@albertvillanova pointer for discussion when you come back.

@@ -113,18 +113,15 @@ system_prompt: |-
If no tool call is needed, use final_answer tool to return your answer.
4. Never re-do a tool call that you previously did with the exact same parameters.
Now Begin! If you solve the task correctly, you will receive a reward of $1,000,000.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny hack 👍

Co-authored-by: A-Mahla <89754740+A-Mahla@users.noreply.github.com>
@aymeric-roucher aymeric-roucher merged commit 67ebe70 into main Apr 9, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants