Skip to content

Commit

Permalink
Add __cache__me__pls__ prefix for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspar09 committed Aug 15, 2024
1 parent 72eee78 commit cb50600
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/anthropic/src/anthropic-messages-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,23 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 {
options: Parameters<LanguageModelV1['doStream']>[0],
): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>> {
const { args, warnings } = await this.getArgs(options);
let { system, ...rest } = args;
if (system && system.startsWith('__cache__me__pls__')) {
// @ts-expect-error: This is allowed per the API
// https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
system = {
type: 'text',
text: system.replace('__cache__me__pls__', ''),
cache_control: { type: 'ephemeral' },
};
}

const { responseHeaders, value: response } = await postJsonToApi({
url: `${this.config.baseURL}/messages`,
headers: combineHeaders(this.config.headers(), options.headers),
body: {
...args,
...rest,
system,
stream: true,
},
failedResponseHandler: anthropicFailedResponseHandler,
Expand Down

0 comments on commit cb50600

Please # to comment.