- Notifications
You must be signed in to change notification settings - Fork 1.4k
Insert Instructions at the end of System Prompt #3614
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
base: main
Are you sure you want to change the base?
Insert Instructions at the end of System Prompt #3614
Conversation
DouweM left a comment
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.
@siddhantbhagat8 Thanks Siddhant! Please look at my notes and add a test in test_agent.py, by using both system_prompt and instructions decorators on one agent
| """Just maps a `pydantic_ai.Message` to a `cohere.ChatMessageV2`.""" | ||
| cohere_messages: list[ChatMessageV2] = [] | ||
| system_prompt_count = sum( | ||
| 1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart) |
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.
This will result in a wrong index when the system prompt parts are not all of the beginning, and it seems brittle to use an index derived from Pydantic AI messages in a list named cohere_messages, which may or may not map 1:1. So I think we should count system parts at the start of the actual cohere_messages list instead
| """Just maps a `pydantic_ai.Message` to a `groq.types.ChatCompletionMessageParam`.""" | ||
| groq_messages: list[chat.ChatCompletionMessageParam] = [] | ||
| system_prompt_count = sum( | ||
| 1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart) |
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.
Same issue as up
| """Just maps a `pydantic_ai.Message` to a `huggingface_hub.ChatCompletionInputMessage`.""" | ||
| hf_messages: list[ChatCompletionInputMessage | ChatCompletionOutputMessage] = [] | ||
| system_prompt_count = sum( | ||
| 1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart) |
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.
Same :)
| ) -> list[MistralMessages]: | ||
| """Just maps a `pydantic_ai.Message` to a `MistralMessage`.""" | ||
| mistral_messages: list[MistralMessages] = [] | ||
| system_prompt_count = sum( |
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.
Same!
| for message in messages: | ||
| for part in message.parts: | ||
| if isinstance(part, SystemPromptPart): | ||
| system_prompt_count += 1 |
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.
And same :)
| assert isinstance(instructions, str) | ||
| openai_messages.insert(0, responses.EasyInputMessageParam(role='system', content=instructions)) | ||
| system_prompt_count = sum( | ||
| 1 for m in messages if isinstance(m, ModelRequest) for p in m.parts if isinstance(p, SystemPromptPart) |
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.
And here as well. We should find the right index in the actual openai_messages list
Description
Changed the insertion logic to insert instructions after all system prompt parts
Simple append:
anthropic.pygoogle.pygemini.pybedrock.pyInsert at a particular position:
For models that build a messages list, count
SystemPromptPartinstances and insert at that indexopenai.pycohere.pygroq.pymistral.pyhuggingface.pyTesting
I ran the tests locally, everything passed. If I should add a test for this change, please let me know the correct location for the test :)