Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: combine successive user messages in bedrock #749

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/providers/bedrock/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,31 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
required: true,
transform: (params: BedrockChatCompletionsParams) => {
if (!params.messages) return [];
return params.messages
const transformedMessages = params.messages
.filter((msg) => msg.role !== 'system')
.map((msg) => {
return {
role: msg.role === 'assistant' ? 'assistant' : 'user',
content: getMessageContent(msg),
};
});
let prevRole = '';
// combine user messages in succession
const combinedMessages = transformedMessages.reduce(
(acc: typeof transformedMessages, msg) => {
if (msg.role === 'user' && prevRole === 'user') {
const lastMessage = acc[acc.length - 1];
const newContent = [...lastMessage.content, ...msg.content];
lastMessage.content = newContent as typeof lastMessage.content;
} else {
acc.push(msg);
}
prevRole = msg.role;
return acc;
},
[]
);
return combinedMessages;
},
},
{
Expand Down
Loading