Open
Conversation
Collaborator
|
Here's a minimal example derived https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching from anthropic import AsyncAnthropic
from app_utils import load_dotenv
from shiny.express import ui
load_dotenv()
llm = AsyncAnthropic()
chat = ui.Chat(id="chat")
chat.ui()
chat.update_user_input(value="Analyze the major themes in 'Pride and Prejudice'")
@chat.on_user_submit
async def _():
response = await llm.beta.prompt_caching.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
stream=True,
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=[{"role": "user", "content": chat.user_input()}],
)
await chat.append_message_stream(response) |
cpsievert
reviewed
Nov 4, 2024
| if isinstance(chunk, RawPromptCachingBetaMessageStartEvent): | ||
| return True | ||
|
|
||
| return False |
Collaborator
There was a problem hiding this comment.
This change to can_normalize_chunk() looks good.
If we want to also support this feature for the non-streaming case, we'll want to also update can_normalize() to know about PromptCachingBetaMessage (i.e., the return that you get with llm.beta.prompt_caching.messages.create(..., stream=False))
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This change adds support for Anthopic's beta prompt caching feature.