Fixed timestamp breaking api calls
This commit is contained in:
@@ -31,8 +31,9 @@ export class Ollama extends LLMProvider {
|
||||
|
||||
private fromStandard(history: LLMMessage[]): any[] {
|
||||
return history.map((h: any) => {
|
||||
if(h.role != 'tool') return h;
|
||||
return {role: 'tool', tool_name: h.name, content: h.error || h.content, timestamp: h.timestamp}
|
||||
const {timestamp, ...rest} = h;
|
||||
if(h.role != 'tool') return rest;
|
||||
return {role: 'tool', tool_name: h.name, content: h.error || h.content}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -90,22 +91,21 @@ export class Ollama extends LLMProvider {
|
||||
}
|
||||
|
||||
loopMessages.push({role: 'assistant', content: resp.message?.content, timestamp: Date.now()});
|
||||
|
||||
if(resp.message?.tool_calls?.length && !controller.signal.aborted) {
|
||||
history.push({...resp.message, timestamp: Date.now()});
|
||||
history.push(resp.message);
|
||||
const results = await Promise.all(resp.message.tool_calls.map(async (toolCall: any) => {
|
||||
const tool = (options.tools || this.ai.options.tools)?.find(findByProp('name', toolCall.function.name));
|
||||
if(!tool) return {role: 'tool', tool_name: toolCall.function.name, content: '{"error": "Tool not found"}', timestamp: Date.now()};
|
||||
if(!tool) return {role: 'tool', tool_name: toolCall.function.name, content: '{"error": "Tool not found"}'};
|
||||
const args = typeof toolCall.function.arguments === 'string' ? JSONAttemptParse(toolCall.function.arguments, {}) : toolCall.function.arguments;
|
||||
try {
|
||||
const result = await tool.fn(args, this.ai);
|
||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize(result), timestamp: Date.now()};
|
||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize(result)};
|
||||
} catch (err: any) {
|
||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'}), timestamp: Date.now()};
|
||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};
|
||||
}
|
||||
}));
|
||||
history.push(...results);
|
||||
loopMessages.push(...results);
|
||||
loopMessages.push(...results.map(r => ({...r, timestamp: Date.now()})));
|
||||
requestParams.messages = history;
|
||||
}
|
||||
} while (!controller.signal.aborted && resp.message?.tool_calls?.length);
|
||||
|
||||
Reference in New Issue
Block a user