Fixing message combination on anthropic
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "0.1.19",
|
||||
"version": "0.1.20",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -73,11 +73,11 @@ export class Anthropic extends LLMProvider {
|
||||
};
|
||||
|
||||
let resp: any;
|
||||
const loopMessages: any[] = [];
|
||||
const assistantMessages: string[] = [];
|
||||
do {
|
||||
resp = await this.client.messages.create(requestParams);
|
||||
if(options.stream) {
|
||||
if(loopMessages.length) options.stream({text: '\n\n'});
|
||||
if(assistantMessages.length) options.stream({text: '\n\n'});
|
||||
resp.content = [];
|
||||
for await (const chunk of resp) {
|
||||
if(controller.signal.aborted) break;
|
||||
@@ -104,7 +104,8 @@ export class Anthropic extends LLMProvider {
|
||||
}
|
||||
}
|
||||
|
||||
loopMessages.push({role: 'assistant', content: resp.content, timestamp: Date.now()});
|
||||
const textContent = resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
|
||||
if(textContent) assistantMessages.push(textContent);
|
||||
const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');
|
||||
if(toolCalls.length && !controller.signal.aborted) {
|
||||
history.push({role: 'assistant', content: resp.content});
|
||||
@@ -118,18 +119,12 @@ export class Anthropic extends LLMProvider {
|
||||
return {type: 'tool_result', tool_use_id: toolCall.id, is_error: true, content: err?.message || err?.toString() || 'Unknown'};
|
||||
}
|
||||
}));
|
||||
const userMsg = {role: 'user', content: results};
|
||||
history.push(userMsg);
|
||||
loopMessages.push({...userMsg, timestamp: Date.now()});
|
||||
history.push({role: 'user', content: results});
|
||||
requestParams.messages = history;
|
||||
}
|
||||
} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));
|
||||
|
||||
const combinedContent = loopMessages.filter(m => m.role === 'assistant')
|
||||
.map(m => m.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n'))
|
||||
.filter(c => c).join('\n\n');
|
||||
if(options.stream) options.stream({done: true});
|
||||
res(this.toStandard([...history, {role: 'assistant', content: combinedContent, timestamp: Date.now()}]));
|
||||
res(this.toStandard([...history, {role: 'assistant', content: assistantMessages.join('\n\n'), timestamp: Date.now()}]));
|
||||
});
|
||||
|
||||
return Object.assign(response, {abort: () => controller.abort()});
|
||||
|
||||
Reference in New Issue
Block a user