Skip to content

Commit

Permalink
🐛 fix: fix claude first message can not be assistant (lobehub#5001)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Dec 12, 2024
1 parent bd083fa commit 063cd61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
27 changes: 18 additions & 9 deletions src/libs/agent-runtime/utils/anthropicHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ describe('anthropicHelpers', () => {
]);
});

it('messages should start with user', async () => {
const messages: OpenAIChatMessage[] = [
{ content: 'Hi', role: 'assistant' },
{ content: 'Hello', role: 'user' },
];

const contents = await buildAnthropicMessages(messages);

expect(contents).toHaveLength(2);
expect(contents).toEqual([
{ content: 'Hi', role: 'user' },
{ content: 'Hello', role: 'user' },
]);
});

it('messages should end with user', async () => {
const messages: OpenAIChatMessage[] = [
{ content: 'Hello', role: 'user' },
Expand All @@ -237,10 +252,9 @@ describe('anthropicHelpers', () => {

const contents = await buildAnthropicMessages(messages);

expect(contents).toHaveLength(4);
expect(contents).toHaveLength(3);
expect(contents).toEqual([
{ content: 'Hello', role: 'user' },
{ content: '_', role: 'assistant' },
{ content: 'Hello', role: 'user' },
{ content: 'Hi', role: 'assistant' },
]);
Expand All @@ -257,15 +271,11 @@ describe('anthropicHelpers', () => {

const contents = await buildAnthropicMessages(messages);

expect(contents).toHaveLength(9);
expect(contents).toHaveLength(5);
expect(contents).toEqual([
{ content: '_', role: 'user' },
{ content: 'a', role: 'assistant' },
{ content: '_', role: 'user' },
{ content: 'a', role: 'user' },
{ content: 'b', role: 'assistant' },
{ content: '_', role: 'user' },
{ content: 'c', role: 'assistant' },
{ content: '_', role: 'user' },
{ content: 'd', role: 'assistant' },
{ content: '你好', role: 'user' },
]);
Expand Down Expand Up @@ -370,7 +380,6 @@ describe('anthropicHelpers', () => {
],
role: 'user',
},
{ content: '_', role: 'assistant' },
{ content: '继续', role: 'user' },
]);
});
Expand Down
13 changes: 5 additions & 8 deletions src/libs/agent-runtime/utils/anthropicHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ export const buildAnthropicMessages = async (
oaiMessages: OpenAIChatMessage[],
): Promise<Anthropic.Messages.MessageParam[]> => {
const messages: Anthropic.Messages.MessageParam[] = [];
let lastRole = 'assistant';
let pendingToolResults: Anthropic.ToolResultBlockParam[] = [];

for (const message of oaiMessages) {
const index = oaiMessages.indexOf(message);

// refs: https://docs.anthropic.com/claude/docs/tool-use#tool-use-and-tool-result-content-blocks
if (message.role === 'tool') {
pendingToolResults.push({
Expand All @@ -135,17 +135,14 @@ export const buildAnthropicMessages = async (
role: 'user',
});
pendingToolResults = [];
lastRole = 'user';
}
} else {
const anthropicMessage = await buildAnthropicMessage(message);

if (lastRole === anthropicMessage.role) {
messages.push({ content: '_', role: lastRole === 'user' ? 'assistant' : 'user' });
}

lastRole = anthropicMessage.role;
messages.push(anthropicMessage);
messages.push({
...anthropicMessage,
role: index === 0 && anthropicMessage.role === 'assistant' ? 'user' : anthropicMessage.role,
});
}
}

Expand Down

0 comments on commit 063cd61

Please sign in to comment.