Skip to content
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

Do not add system prompt part when dynamic system prompt function returns empty value #864

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

giacbrd
Copy link

@giacbrd giacbrd commented Feb 6, 2025

The system_prompt decorator allows to dynamically add parts to the messages. However, a part generated by a decorated function is always added, even if the function output is useless, e.g. an empty string. Sometime the dynamic behaviour we need is to just increment the system prompt, according to the context.

This PR add this behaviour by:

  • avoiding adding a system prompt part in the messages if a system prompt function returns a value that evaluates to False
  • defining the return type of these functions as str | None, so that it is explicit that these function can optionally return a value

Example of a dynamic system prompt where we add some information only if the retrieve call is successful

@agent.system_prompt
async def get_more_information(ctx: RunContext[MyDeps]) -> str | None:
    response = await ctx.deps.http_client.get(
        'https://example.com',
        headers={'Authorization': f'Bearer {ctx.deps.api_key}'},
    )
    if response.status_code == 200 and response.text.strip():
        return f'You also have this information: {response.text}'
    return None

@github-actions github-actions bot temporarily deployed to deploy-preview February 6, 2025 17:07 Inactive
@github-actions github-actions bot temporarily deployed to deploy-preview February 6, 2025 19:52 Inactive
@github-actions github-actions bot temporarily deployed to deploy-preview February 6, 2025 20:36 Inactive
@github-actions github-actions bot temporarily deployed to deploy-preview February 6, 2025 20:52 Inactive
@github-actions github-actions bot temporarily deployed to deploy-preview February 6, 2025 21:10 Inactive
@giacbrd
Copy link
Author

giacbrd commented Feb 6, 2025

The use of Union[str, None] instead of str | None in most of the type hints is due to compatibility with python 3.9, which fails to run the new code otherwise

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.

1 participant