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

Adds dynamic to system_prompt decorator, allowing reevaluation #560

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

Conversation

josead
Copy link

@josead josead commented Dec 28, 2024

Adds optional parameter "dynamic" to decorator "system_prompt".
Implements the reevaluation of system prompts.
Allows new prompts to be added only if they are dynamic.

Hello Pydantic Team,
In response to this issue I've created #531

Let me know if this is a good approach for the enhancement.
CC: @samuelcolvin @sydney-runkle

Regards!

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

LGTM in general, but you need to format code and fix linting issues.

@@ -526,16 +526,37 @@ def system_prompt(self, func: Callable[[], str], /) -> Callable[[], str]: ...
@overload
def system_prompt(self, func: Callable[[], Awaitable[str]], /) -> Callable[[], Awaitable[str]]: ...

@overload
def system_prompt(
self, func: _system_prompt.SystemPromptFunc[AgentDeps] | None = None, /, *, dynamic: bool = False
Copy link
Member

Choose a reason for hiding this comment

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

I think this overload can't have func, so the signature is just (self, *, dynamic: bool = False)

pydantic_ai_slim/pydantic_ai/agent.py Outdated Show resolved Hide resolved
@@ -830,8 +855,8 @@ async def add_tool(tool: Tool[AgentDeps]) -> None:
)

async def _prepare_messages(
self, user_prompt: str, message_history: list[_messages.ModelMessage] | None, run_context: RunContext[AgentDeps]
) -> list[_messages.ModelMessage]:
self, user_prompt: str, message_history: list[_messages.ModelMessage] | None, run_context: RunContext[AgentDeps]
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should have changed.

Copy link
Author

Choose a reason for hiding this comment

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

my bad, format on save is on.

]
)

def test_messages_history_reevaluate_system_prompt_dirty(set_event_loop: None):
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand what these tests are testing.

Please add a doctring, but also I think there are more tests here than really necessary.

@josead josead requested a review from samuelcolvin December 30, 2024 17:59
@josead
Copy link
Author

josead commented Dec 30, 2024

@samuelcolvin I'm reviewing this, since I found a bug, when using dynamic=True on one decorator it reevaluates (builds) all other sys_parts.

I will have to make a discrimination or annotate the message once is built.

@HamzaFarhan
Copy link

Hey @josead
Were you able to solve the issue?

@josead
Copy link
Author

josead commented Jan 1, 2025 via email

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.

3 participants