Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into env-gen-type-update
Browse files Browse the repository at this point in the history
ykethan authored Oct 25, 2024

Verified

This commit was signed with the committer’s verified signature.
targos Michaël Zasso
2 parents 9ff00cb + e4cdb59 commit 56595a5
Showing 14 changed files with 263 additions and 181 deletions.
2 changes: 2 additions & 0 deletions .changeset/polite-bats-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
5 changes: 5 additions & 0 deletions .changeset/sour-seahorses-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/ai-constructs': minor
---

Include accumulated turn content in chunk mutation
44 changes: 22 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -105,46 +105,93 @@ void describe('Bedrock converse adapter', () => {
// See mockConverseStreamCommandOutput below of how split chunks are mocked.
assert.deepStrictEqual(chunks, [
{
accumulatedTurnContent: [
{
text: 'b',
},
],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockText: 'b',
contentBlockIndex: 0,
contentBlockDeltaIndex: 0,
},
{
accumulatedTurnContent: [
{
text: 'block1',
},
],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockText: 'lock1',
contentBlockIndex: 0,
contentBlockDeltaIndex: 1,
},
{
accumulatedTurnContent: [
{
text: 'block1',
},
],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockIndex: 0,
contentBlockDoneAtIndex: 1,
},
{
accumulatedTurnContent: [
{
text: 'block1',
},
{
text: 'b',
},
],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockText: 'b',
contentBlockIndex: 1,
contentBlockDeltaIndex: 0,
},
{
accumulatedTurnContent: [
{
text: 'block1',
},
{
text: 'block2',
},
],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockText: 'lock2',
contentBlockIndex: 1,
contentBlockDeltaIndex: 1,
},
{
accumulatedTurnContent: [
{
text: 'block1',
},
{
text: 'block2',
},
],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockIndex: 1,
contentBlockDoneAtIndex: 1,
},
{
accumulatedTurnContent: [
{
text: 'block1',
},
{
text: 'block2',
},
],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockIndex: 1,
@@ -648,12 +695,14 @@ void describe('Bedrock converse adapter', () => {
await askBedrockWithStreaming(adapter);
assert.deepStrictEqual(chunks, [
{
accumulatedTurnContent: [{ toolUse: clientToolUse }],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockIndex: 0,
contentBlockToolUse: JSON.stringify({ toolUse: clientToolUse }),
},
{
accumulatedTurnContent: [{ toolUse: clientToolUse }],
conversationId: event.conversationId,
associatedUserMessageId: event.currentMessageId,
contentBlockIndex: 0,
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import {
} from './types.js';
import { ConversationTurnEventToolsProvider } from './event-tools-provider';
import { ConversationMessageHistoryRetriever } from './conversation_message_history_retriever';
import * as bedrock from '@aws-sdk/client-bedrock-runtime';

/**
* This class is responsible for interacting with Bedrock Converse API
@@ -173,6 +174,9 @@ export class BedrockConverseAdapter {
let blockIndex = 0;
let lastBlockIndex = 0;
let stopReason = '';
// Accumulates client facing content per turn.
// So that upstream can persist full message at the end of the streaming.
const accumulatedTurnContent: Array<bedrock.ContentBlock> = [];
do {
const toolConfig = this.createToolConfiguration();
const converseCommandInput: ConverseStreamCommandInput = {
@@ -202,10 +206,12 @@ export class BedrockConverseAdapter {
let toolUseInput: string = '';
let blockDeltaIndex = 0;
let lastBlockDeltaIndex = 0;
// Accumulate current message for the tool use loop purpose.
const accumulatedAssistantMessage: Message = {
role: undefined,
content: [],
};

for await (const chunk of bedrockResponse.stream) {
this.logger.debug('Bedrock Converse Stream response chunk:', chunk);
if (chunk.messageStart) {
@@ -230,6 +236,7 @@ export class BedrockConverseAdapter {
} else if (chunk.contentBlockDelta.delta?.text) {
text += chunk.contentBlockDelta.delta.text;
yield {
accumulatedTurnContent: [...accumulatedTurnContent, { text }],
conversationId: this.event.conversationId,
associatedUserMessageId: this.event.currentMessageId,
contentBlockText: chunk.contentBlockDelta.delta.text,
@@ -248,7 +255,9 @@ export class BedrockConverseAdapter {
this.clientToolByName.has(toolUseBlock.toolUse.name)
) {
clientToolsRequested = true;
accumulatedTurnContent.push(toolUseBlock);
yield {
accumulatedTurnContent: [...accumulatedTurnContent],
conversationId: this.event.conversationId,
associatedUserMessageId: this.event.currentMessageId,
contentBlockIndex: blockIndex,
@@ -263,7 +272,9 @@ export class BedrockConverseAdapter {
accumulatedAssistantMessage.content?.push({
text,
});
accumulatedTurnContent.push({ text });
yield {
accumulatedTurnContent: [...accumulatedTurnContent],
conversationId: this.event.conversationId,
associatedUserMessageId: this.event.currentMessageId,
contentBlockIndex: blockIndex,
@@ -285,6 +296,7 @@ export class BedrockConverseAdapter {
// For now if any of client tools is used we ignore executable tools
// and propagate result back to client.
yield {
accumulatedTurnContent: [...accumulatedTurnContent],
conversationId: this.event.conversationId,
associatedUserMessageId: this.event.currentMessageId,
contentBlockIndex: lastBlockIndex,
@@ -313,6 +325,7 @@ export class BedrockConverseAdapter {
} while (stopReason === 'tool_use');

yield {
accumulatedTurnContent: [...accumulatedTurnContent],
conversationId: this.event.conversationId,
associatedUserMessageId: this.event.currentMessageId,
contentBlockIndex: lastBlockIndex,
Loading

0 comments on commit 56595a5

Please sign in to comment.