Changesets · Branches · Tags · Compare
| Changeset | ef599343e4ea1970a51b7da4b5905795c3aa9269e4e7fe85b84d1341d3c34b42 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-09-10 12:12 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-09-10 12:12 -07:00 |
| Parents | 14decbdb4a2e, bb0f4212e0e0 |
2 files changed
modified core/llm/llms/Bedrock.test.ts +91 −1
| @@ -532,7 +532,7 @@ | ||
| 532 | 532 | ); |
| 533 | 533 | }); |
| 534 | 534 | |
| 535 | - it("should add cache points for prompt caching when needed", () => { | |
| 535 | + it("should add cache points when cacheBehavior.cacheConversation is enabled", () => { | |
| 536 | 536 | // Create a test instance with caching behavior enabled |
| 537 | 537 | const bedrockWithCaching = new TestBedrock({ |
| 538 | 538 | apiKey: "test-key", |
| @@ -576,5 +576,95 @@ | ||
| 576 | 576 | const userMessageText = converted[0].content?.[0].text; |
| 577 | 577 | expect(userMessageText).toMatch(/First message.+/); |
| 578 | 578 | }); |
| 579 | + | |
| 580 | + it("should add cache points when completionOptions.promptCaching is enabled", () => { | |
| 581 | + // Create a test instance with prompt caching enabled via completionOptions | |
| 582 | + const bedrockWithPromptCaching = new TestBedrock({ | |
| 583 | + apiKey: "test-key", | |
| 584 | + model: "anthropic.claude-3-sonnet-20240229-v1:0", | |
| 585 | + region: "us-east-1", | |
| 586 | + }); | |
| 587 | + | |
| 588 | + // Set completionOptions.promptCaching (without cacheBehavior) | |
| 589 | + bedrockWithPromptCaching.completionOptions = { | |
| 590 | + ...bedrockWithPromptCaching.completionOptions, | |
| 591 | + promptCaching: true, | |
| 592 | + }; | |
| 593 | + | |
| 594 | + const messages: ChatMessage[] = [ | |
| 595 | + { role: "user", content: "First message" } as UserChatMessage, | |
| 596 | + { role: "assistant", content: "First response" }, | |
| 597 | + { role: "user", content: "Second message" } as UserChatMessage, | |
| 598 | + ]; | |
| 599 | + | |
| 600 | + const availableTools = new Set<string>(); | |
| 601 | + const converted = bedrockWithPromptCaching["_convertMessages"]( | |
| 602 | + messages, | |
| 603 | + availableTools, | |
| 604 | + ); | |
| 605 | + | |
| 606 | + // The last two user messages should have cache points | |
| 607 | + expect(converted.length).toBe(3); | |
| 608 | + expect(converted[0].role).toBe("user"); | |
| 609 | + expect(converted[0].content?.some((block) => block.cachePoint)).toBe( | |
| 610 | + true, | |
| 611 | + ); | |
| 612 | + expect(converted[1].role).toBe("assistant"); | |
| 613 | + expect(converted[2].role).toBe("user"); | |
| 614 | + expect(converted[2].content?.some((block) => block.cachePoint)).toBe( | |
| 615 | + true, | |
| 616 | + ); | |
| 617 | + | |
| 618 | + // Verify that text content includes a cache ID suffix | |
| 619 | + const userMessageText = converted[0].content?.[0].text; | |
| 620 | + expect(userMessageText).toMatch(/First message.+/); | |
| 621 | + }); | |
| 622 | + | |
| 623 | + it("should not add cache points when both caching options are disabled", () => { | |
| 624 | + // Create a test instance with no caching enabled | |
| 625 | + const bedrockNoCaching = new TestBedrock({ | |
| 626 | + apiKey: "test-key", | |
| 627 | + model: "anthropic.claude-3-sonnet-20240229-v1:0", | |
| 628 | + region: "us-east-1", | |
| 629 | + }); | |
| 630 | + | |
| 631 | + // Explicitly set no caching | |
| 632 | + bedrockNoCaching.cacheBehavior = { | |
| 633 | + cacheConversation: false, | |
| 634 | + cacheSystemMessage: false, | |
| 635 | + }; | |
| 636 | + bedrockNoCaching.completionOptions = { | |
| 637 | + ...bedrockNoCaching.completionOptions, | |
| 638 | + promptCaching: false, | |
| 639 | + }; | |
| 640 | + | |
| 641 | + const messages: ChatMessage[] = [ | |
| 642 | + { role: "user", content: "First message" } as UserChatMessage, | |
| 643 | + { role: "assistant", content: "First response" }, | |
| 644 | + { role: "user", content: "Second message" } as UserChatMessage, | |
| 645 | + ]; | |
| 646 | + | |
| 647 | + const availableTools = new Set<string>(); | |
| 648 | + const converted = bedrockNoCaching["_convertMessages"]( | |
| 649 | + messages, | |
| 650 | + availableTools, | |
| 651 | + ); | |
| 652 | + | |
| 653 | + // No cache points should be added | |
| 654 | + expect(converted.length).toBe(3); | |
| 655 | + expect(converted[0].role).toBe("user"); | |
| 656 | + expect(converted[0].content?.some((block) => block.cachePoint)).toBe( | |
| 657 | + false, | |
| 658 | + ); | |
| 659 | + expect(converted[1].role).toBe("assistant"); | |
| 660 | + expect(converted[2].role).toBe("user"); | |
| 661 | + expect(converted[2].content?.some((block) => block.cachePoint)).toBe( | |
| 662 | + false, | |
| 663 | + ); | |
| 664 | + | |
| 665 | + // Verify that text content doesn't include a cache ID suffix | |
| 666 | + const userMessageText = converted[0].content?.[0].text; | |
| 667 | + expect(userMessageText).toBe("First message"); | |
| 668 | + }); | |
| 579 | 669 | }); |
| 580 | 670 | }); |
modified core/llm/llms/Bedrock.ts +4 −1
| @@ -513,7 +513,10 @@ | ||
| 513 | 513 | // The second-to-last because it retrieves potentially already cached contents, |
| 514 | 514 | // The last one because we want it cached for later retrieval. |
| 515 | 515 | // See: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html |
| 516 | - if (this.cacheBehavior?.cacheConversation) { | |
| 516 | + if ( | |
| 517 | + this.cacheBehavior?.cacheConversation || | |
| 518 | + this.completionOptions.promptCaching | |
| 519 | + ) { | |
| 517 | 520 | this._addCachingToLastTwoUserMessages(converted); |
| 518 | 521 | } |
| 519 | 522 |