Anthropic: allow for multiple system prompts #101
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Most providers' API include system messages in the messages array with a "system" role, and allow you to make multiple. Anthropic does not support the system role, and instead has a "system" property, separate from messages.
Prism currently allows for one system prompt via
->withSystemPrompt()
, and converts allSystemMessage
s intoUserMessage
s.It is not well documented, but the system property allows for an array of system prompts. See for instance the example in the prompt caching docs. I have validated that this works.
This PR:
SystemMessage
s from the main map, therefore omitting them from messages.->withSystemPrompt()
at the top of the system prompts array (same behaviour as other providers implementations, which puts it at the top of the messages array).SystemMessage
s to the system prompts array in the order they were declared.Why is this needed?
Primarily because Anthropic prompt caching supports system prompts but requires you to use the array format to specify the cache_control property. This is therefore a precursor to implementing prompt caching.
Why not just move to an array format, with a single system prompt?
SystemMessage
s correctly as system prompts, you would be able to use the same API for both system prompts and user prompts. E.g. (making some assumptions about the desired API):Is this a breaking change?
Kind of, but not really!
If you were using SystemMessages before, they would have been submitted as UserMessages in the order that they were defined (i.e. potentially between UserMessages). SystemMessages would now still be sent in the correct order compared to each other, but are taken out of the context of the UserMessages.
However, given the docs currently state that all SystemMessages are converted to UserMessages, I cannot imagine many (if any) people are doing this.
This is also really a limitation of Anthropic's API implementation, which doesn't allow you to place system prompts between other prompts.