-
Notifications
You must be signed in to change notification settings - Fork 809
Add instructions
parameter
#1360
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
Conversation
Docs Preview
|
PR Change SummaryAdded the
Modified Files
How can I customize these reviews?Check out the Hyperlint AI Reviewer docs for more information on how to customize the review. If you just want to ignore it on this PR, you can add the Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add |
if self.instructions or self.instructions_functions: | ||
instructions = await self._instructions(run_context) | ||
if message_history: | ||
messages.extend(message_history) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some models take instructions/system prompt as a message, some as a completely separate field:
separate:
- Anthropic
- Bedrock
- Gemini
As message:
- Cohere
- Grow
- Mistral
- OpenAI
Roughly 50%. Since extracting something from a list is harder than keeping it separate, then adding it, I wonder if we would be better of sending a separate field with instructions (and maybe system prompts) to the Model
than adding it to messages?
Things left here:
|
docs/agents.md
Outdated
- `instructions` when that information should be per-agent based. | ||
- `system_prompts` when you want the model to know about previous system prompts. | ||
|
||
They are mutually exclusive, you cannot use both in the same agent. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are mutually exclusive, you cannot use both in the same agent. |
I guess if we do concatenation we can remove this note, but I think we should add an admonition saying basically — "For most use cases, you should use instructions. If you know what you are doing though and want to preserve system prompt messages in the message history, you can achieve that using the system_prompt
argument/decorator." or similar.
# Shallow copy messages | ||
messages.extend(message_history) | ||
# Reevaluate any dynamic system prompt parts | ||
await self._reevaluate_dynamic_prompts(messages, run_context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should do this whether or not there are instructions; I think it will allow us to remove the logic fork above contingent on the presence of instructions/instruction functions.
Summary
We are trying to make it easier to replace system prompts when passing the message history between agents as part of the workflow. With system prompts if you pass a history, that agent system prompt will do nothing - usually what people want is to use the system prompt from that agent on the call.
This fixes only the case where you need the system prompt to be replaced between agents.
Checklist
Generated by Copilot
This pull request introduces the concept of "instructions" to the
Agent
class and updates various parts of the codebase to support this new feature. The most important changes include the addition of new fields and methods to handle instructions, updates to theAgent
class constructor, and modifications to existing functions to incorporate instructions.New Feature: Instructions for Agents
Addition of Instructions Fields and Methods:
instructions
andinstructions_functions
fields to theUserPromptNode
class inpydantic_ai_slim/pydantic_ai/_agent_graph.py
._instructions
method to prepare instructions inpydantic_ai_slim/pydantic_ai/_agent_graph.py
.instructions
andinstructions_functions
fields to theAgent
class inpydantic_ai_slim/pydantic_ai/agent.py
.instructions
decorator method in theAgent
class to register instructions functions.Constructor Updates:
Agent
class constructor to acceptinstructions
as a parameter and handle conflicts withsystem_prompt
. [1] [2]Codebase Modifications
pydantic_ai_slim/pydantic_ai/agent.py
to includeinstructions
in the request preparation and execution processes. [1] [2]docs/agents.md
,docs/api/models/function.md
,docs/message-history.md
,docs/models.md
, anddocs/tools.md
to includeinstructions
in the examples. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17]Example Script
main.py
to demonstrate the use ofinstructions
with theAgent
class.These changes collectively enhance the functionality of the
Agent
class by allowing the inclusion of instructions, providing more flexibility and control over agent behavior.