Merge pull request #7652 from chezsmithy/fix-bedrock-conversation-prompt-caching

fix: 🚑 Fix bedrock conversation prompt caching
This commit is contained in:
Dallin Romney 2025-09-10 12:12:45 -07:00 • committed by GitHub
commit de67442828
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 95 additions and 2 deletions

View file

@ -532,7 +532,7 @@ describe("Bedrock", () => {
);
});
it("should add cache points for prompt caching when needed", () => {
it("should add cache points when cacheBehavior.cacheConversation is enabled", () => {
// Create a test instance with caching behavior enabled
const bedrockWithCaching = new TestBedrock({
apiKey: "test-key",
@ -576,5 +576,95 @@ describe("Bedrock", () => {
const userMessageText = converted[0].content?.[0].text;
expect(userMessageText).toMatch(/First message.+/);
});
it("should add cache points when completionOptions.promptCaching is enabled", () => {
// Create a test instance with prompt caching enabled via completionOptions
const bedrockWithPromptCaching = new TestBedrock({
apiKey: "test-key",
model: "anthropic.claude-3-sonnet-20240229-v1:0",
region: "us-east-1",
});
// Set completionOptions.promptCaching (without cacheBehavior)
bedrockWithPromptCaching.completionOptions = {
...bedrockWithPromptCaching.completionOptions,
promptCaching: true,
};
const messages: ChatMessage[] = [
{ role: "user", content: "First message" } as UserChatMessage,
{ role: "assistant", content: "First response" },
{ role: "user", content: "Second message" } as UserChatMessage,
];
const availableTools = new Set<string>();
const converted = bedrockWithPromptCaching["_convertMessages"](
messages,
availableTools,
);
// The last two user messages should have cache points
expect(converted.length).toBe(3);
expect(converted[0].role).toBe("user");
expect(converted[0].content?.some((block) => block.cachePoint)).toBe(
true,
);
expect(converted[1].role).toBe("assistant");
expect(converted[2].role).toBe("user");
expect(converted[2].content?.some((block) => block.cachePoint)).toBe(
true,
);
// Verify that text content includes a cache ID suffix
const userMessageText = converted[0].content?.[0].text;
expect(userMessageText).toMatch(/First message.+/);
});
it("should not add cache points when both caching options are disabled", () => {
// Create a test instance with no caching enabled
const bedrockNoCaching = new TestBedrock({
apiKey: "test-key",
model: "anthropic.claude-3-sonnet-20240229-v1:0",
region: "us-east-1",
});
// Explicitly set no caching
bedrockNoCaching.cacheBehavior = {
cacheConversation: false,
cacheSystemMessage: false,
};
bedrockNoCaching.completionOptions = {
...bedrockNoCaching.completionOptions,
promptCaching: false,
};
const messages: ChatMessage[] = [
{ role: "user", content: "First message" } as UserChatMessage,
{ role: "assistant", content: "First response" },
{ role: "user", content: "Second message" } as UserChatMessage,
];
const availableTools = new Set<string>();
const converted = bedrockNoCaching["_convertMessages"](
messages,
availableTools,
);
// No cache points should be added
expect(converted.length).toBe(3);
expect(converted[0].role).toBe("user");
expect(converted[0].content?.some((block) => block.cachePoint)).toBe(
false,
);
expect(converted[1].role).toBe("assistant");
expect(converted[2].role).toBe("user");
expect(converted[2].content?.some((block) => block.cachePoint)).toBe(
false,
);
// Verify that text content doesn't include a cache ID suffix
const userMessageText = converted[0].content?.[0].text;
expect(userMessageText).toBe("First message");
});
});
});

View file

@ -513,7 +513,10 @@ class Bedrock extends BaseLLM {
// The second-to-last because it retrieves potentially already cached contents,
// The last one because we want it cached for later retrieval.
// See: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html
if (this.cacheBehavior?.cacheConversation) {
if (
this.cacheBehavior?.cacheConversation ||
this.completionOptions.promptCaching
) {
this._addCachingToLastTwoUserMessages(converted);
}