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

Add system prompts independently of history of messages #531

Open
josead opened this issue Dec 23, 2024 · 3 comments
Open

Add system prompts independently of history of messages #531

josead opened this issue Dec 23, 2024 · 3 comments

Comments

@josead
Copy link

josead commented Dec 23, 2024

Hello! I wanted to know how to make the @agent.system_prompt decorator being called on every run, when there is history of messages added. I'm basically trying to evaluate the dependencies on each run (the dependencies will change independently of the run and I don't want to use a tool for this because is not needed)
Here is how to reproduce:

 # where I declare the agent
 chat_agent = Agent(
        'openai:gpt-4o',
        system_prompt="Some prompt",
        deps_type=ChatAgentDeps,
    )
    
   ...
    # where I run the agent on each message from the user received
    @chat_agent.system_prompt
    def get_current_status_of_tasks(ctx: RunContext[ChatAgentDeps]):
          print("System Prompt:", ctx.deps.aa_agent.pending_tasks)
          print("System Prompt:", ctx.deps.aa_agent.done_tasks)
          return f"""Pending Action Items: {ctx.deps.aa_agent.get_pending_tasks()}
      Done Action Items: {ctx.deps.aa_agent.get_done_tasks()}"""

    result = await chat_agent.run(message, deps=ChatAgentDeps(
        aa_agent=session["agent"]
    ), message_history=session.get("history", []))
    
    session['history'] = result.all_messages()

The function get_current_status_of_tasks only gets called once.

Am I doing something wrong here? Is this expected? Thanks in advance!

@HamzaFarhan
Copy link

Yes as of now, self._sys_parts is not called if message_history is passed:

https://github.com/pydantic/pydantic-ai/blob/629495cd1083b74680858f0ce44cc8ba9e19e48a/pydantic_ai_slim/pydantic_ai/agent.py#L835C1-L847C24

async def _prepare_messages(
    self, user_prompt: str, message_history: list[_messages.ModelMessage] | None, run_context: RunContext[AgentDeps]
) -> list[_messages.ModelMessage]:
    if message_history:
        # shallow copy messages
        messages = message_history.copy()
        messages.append(_messages.ModelRequest([_messages.UserPromptPart(user_prompt)]))
    else:
        parts = await self._sys_parts(run_context)
        parts.append(_messages.UserPromptPart(user_prompt))
        messages: list[_messages.ModelMessage] = [_messages.ModelRequest(parts)]


    return messages

@josead
Copy link
Author

josead commented Dec 23, 2024

Hey @HamzaFarhan, thanks for your quick answer. Is this to prevent the parts to repeat? Is there a simpler way to inject a system prompt into messages? I could do workarounds here, to accept system events to be injected as part of the conversation, do you see the Agents supporting this in the future?

Regards

@HamzaFarhan
Copy link

@josead I'm not sure actually, I did try manually calling self._sys_parts and replacing the system prompt in the message_history when passing it in and it worked.
Looks like @samuelcolvin is open to altering the logic: #496

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

No branches or pull requests

2 participants