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

Anthropic: allow for multiple system prompts #101

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

Conversation

ChrisB-TL
Copy link

@ChrisB-TL ChrisB-TL commented Dec 12, 2024

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 all SystemMessages into UserMessages.

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:

  • Filters out all SystemMessages from the main map, therefore omitting them from messages.
  • Moves the system property to the array of prompts format.
  • Always submits the prompt defined with ->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).
  • Move all SystemMessages 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?

  • If your system prompt has a cacheable part, and un uncacheable part, you need to split it into two prompts.
  • When it comes to implementing prompt caching, by treating SystemMessages 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):
->withMessages([
    (new SystemMessage('Cache me'))->withProviderMeta(Provider::Anthropic, ['cache_control' => ['type' => 'ephemeral']]),
    (new UserMessage('Cache me too'))->withProviderMeta(Provider::Anthropic, ['cache_control' => ['type' => 'ephemeral']]),
    new UserMessage('Please do not cache me')
])

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.

Copy link

netlify bot commented Dec 12, 2024

Deploy Preview for stunning-cannoli-238aa5 ready!

Name Link
🔨 Latest commit 4c151e7
🔍 Latest deploy log https://app.netlify.com/sites/stunning-cannoli-238aa5/deploys/675af0f8b302e80008d8e2c4
😎 Deploy Preview https://deploy-preview-101--stunning-cannoli-238aa5.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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.

1 participant