-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow caching system, user and assistant messages for Anthropic (#524)
* Allow caching system, user and assistant messages for Anthropic We want to allow with our syntaxt for users to tag messages with Anthropic cache control tag. Also as part of this PR we extract first system messages in an anthropic prompt as part of the configuration. This way we can remove the warning And last change in this PR is that we allow system messages to have more than one message. This is something that's not valid in OpenAI but is valid in Anthropic so we allow this at compiler level but we restric for OpenAI at Rules level * chore: updated docs * chore: update docs 2 * chore: updated docs 3 * chore: updated docs 4 --------- Co-authored-by: Gerard Clos <[email protected]>
- Loading branch information
1 parent
899329c
commit 3540dca
Showing
37 changed files
with
1,643 additions
and
521 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
title: Anthropic | ||
description: Common questions about Anthropic provider | ||
--- | ||
|
||
## How do I use the Anthropic cache? | ||
|
||
[Anthropic cache](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching) allows you to cache parts of a prompt. As explained in their documentation, you need to opt-in within the prompt to start caching parts of it. | ||
|
||
To do this, add `cacheControl: true` to the front matter of your prompt. | ||
|
||
```markdown | ||
--- | ||
provider: name-of-your-antropic-provider-in-latitude | ||
model: claude-3-5-sonnet-20241022 | ||
cacheControl: true | ||
--- | ||
``` | ||
|
||
Once this is set up, you can start caching specific parts of the prompt: | ||
|
||
``` | ||
<system>This part of the text is not cached</system> | ||
<system> | ||
Read this large book and answer users' questions. | ||
<text cache_control={{ { type: 'ephemeral' } }}> | ||
...BIG_BOOK_CONTENT... | ||
</text> | ||
</system> | ||
```` | ||
If you want an entire message to be cached, add the cache directive to the `user`, `assistant`, or `system` tags: | ||
``` | ||
<user cache_control={{ { type: 'ephemeral' } }}> | ||
This text will be cached. | ||
<text> | ||
This text will also be cached. | ||
</text> | ||
And this text as well. | ||
</user> | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
title: Anthropic provider rules | ||
description: Learn about the rules you need to follow when using the Anthropic provider in Latitude. | ||
--- | ||
|
||
1. [System messages must be followed by at least by another message](#rule-1-not-only-system-messages) | ||
2. [No system messages are allowed after an assistant or user message](#rule-2-no-system-messages-after-assistant-or-user-messages) | ||
|
||
### System messages must be followed by at least another message | ||
|
||
Anthropic does not consider system messages as part of the list of general messages and thus, if you don't add at least a user or assistant message, the list of messages sent to anthropic would be empty, which would result in an error. | ||
|
||
```json | ||
{ | ||
"system": [ | ||
{ | ||
"type": "text", | ||
"text": "You are an AI assistant tasked with analyzing literary works. Your goal is to provide insightful commentary on themes, characters, and writing style.\n" | ||
}, | ||
{ | ||
"type": "text", | ||
"text": "<the entire contents of Pride and Prejudice>", | ||
"cache_control": { "type": "ephemeral" } | ||
} | ||
], | ||
"messages": [] // this is invalid | ||
} | ||
``` | ||
|
||
So, the following prompt in Latitude is invalid for an Anthropic provider: | ||
|
||
``` | ||
--- | ||
provider: Anthropic | ||
model: claude-3-5-sonnet-latest | ||
--- | ||
This is a system message | ||
``` | ||
|
||
This would generate the following warning: | ||
|
||
![](/assets/provider_rules_1.png) | ||
|
||
Instead, add at least another message wrapped in a `<user>` or `<assistant>` tag: | ||
|
||
``` | ||
--- | ||
provider: Anthropic | ||
model: claude-3-5-sonnet-latest | ||
--- | ||
This is a system message | ||
<user>This is a user message</user> | ||
``` | ||
|
||
### No system messages are allowed after an assistant or user message | ||
|
||
Any system message added after an assistant or user message will be automatically converted into a user message: | ||
|
||
``` | ||
--- | ||
provider: Anthropic | ||
model: claude-3-5-sonnet-latest | ||
--- | ||
This is a system message | ||
<user>This is a user message</user> | ||
<system>This is another system message</system> /* This message will be converted to a user message */ | ||
``` | ||
|
||
This is because Anthropic does not consider system messages as part of the list of general messages and thus they can't be concatenated with other messages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: Google provider rules | ||
description: Learn about the rules you need to follow when using the Google provider in Latitude. | ||
--- | ||
|
||
1. [System messages must be at the beginning of the conversation](#rule-1-system-messages-must-be-at-the-beginning-of-the-conversation) | ||
|
||
### System messages must be at the beginning of the conversation | ||
|
||
Google only supports system messages at the beginning of the conversation. All other system messages are converted to user messages. | ||
|
||
``` | ||
--- | ||
provider: Google | ||
model: gemini-1.5-flash | ||
--- | ||
This is a system message | ||
<user>This is a user message</user> | ||
This is another system message /* This message will be converted to a user message */ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.