Handle multiple AI responses in one question better.
This commit is contained in:
@@ -22,8 +22,9 @@ export class Ollama extends LLMProvider {
|
||||
}
|
||||
} else if(history[i].role == 'tool') {
|
||||
const error = history[i].content.startsWith('{"error":');
|
||||
history[i] = {role: 'tool', name: history[i].tool_name, args: history[i].args, [error ? 'error' : 'content']: history[i].content};
|
||||
history[i] = {role: 'tool', name: history[i].tool_name, args: history[i].args, [error ? 'error' : 'content']: history[i].content, timestamp: history[i].timestamp};
|
||||
}
|
||||
if(!history[i]?.timestamp) history[i].timestamp = Date.now();
|
||||
}
|
||||
return history;
|
||||
}
|
||||
@@ -31,7 +32,7 @@ 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}
|
||||
return {role: 'tool', tool_name: h.name, content: h.error || h.content, timestamp: h.timestamp}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -39,7 +40,7 @@ export class Ollama extends LLMProvider {
|
||||
const controller = new AbortController();
|
||||
const response = new Promise<any>(async (res, rej) => {
|
||||
let system = options.system || this.ai.options.system;
|
||||
let history = this.fromStandard([...options.history || [], {role: 'user', content: message}]);
|
||||
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
||||
if(history[0].roll == 'system') {
|
||||
if(!system) system = history.shift();
|
||||
else history.shift();
|
||||
@@ -70,11 +71,12 @@ export class Ollama extends LLMProvider {
|
||||
}))
|
||||
}
|
||||
|
||||
// Run tool chains
|
||||
let resp: any;
|
||||
const loopMessages: any[] = [];
|
||||
do {
|
||||
resp = await this.client.chat(requestParams);
|
||||
if(options.stream) {
|
||||
if(loopMessages.length) options.stream({text: '\n\n'});
|
||||
resp.message = {role: 'assistant', content: '', tool_calls: []};
|
||||
for await (const chunk of resp) {
|
||||
if(controller.signal.aborted) break;
|
||||
@@ -87,27 +89,33 @@ export class Ollama extends LLMProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// Run tools
|
||||
loopMessages.push({role: 'assistant', content: resp.message?.content, timestamp: Date.now()});
|
||||
|
||||
if(resp.message?.tool_calls?.length && !controller.signal.aborted) {
|
||||
history.push(resp.message);
|
||||
history.push({...resp.message, timestamp: Date.now()});
|
||||
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"}'};
|
||||
if(!tool) return {role: 'tool', tool_name: toolCall.function.name, content: '{"error": "Tool not found"}', timestamp: Date.now()};
|
||||
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)};
|
||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize(result), timestamp: Date.now()};
|
||||
} catch (err: any) {
|
||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'})};
|
||||
return {role: 'tool', tool_name: toolCall.function.name, args, content: JSONSanitize({error: err?.message || err?.toString() || 'Unknown'}), timestamp: Date.now()};
|
||||
}
|
||||
}));
|
||||
history.push(...results);
|
||||
loopMessages.push(...results);
|
||||
requestParams.messages = history;
|
||||
}
|
||||
} while (!controller.signal.aborted && resp.message?.tool_calls?.length);
|
||||
|
||||
const combinedContent = loopMessages.filter(m => m.role === 'assistant')
|
||||
.map(m => m.content).filter(c => c).join('\n\n');
|
||||
if(options.stream) options.stream({done: true});
|
||||
res(this.toStandard([...history, {role: 'assistant', content: resp.message?.content}]));
|
||||
res(this.toStandard([...history, {role: 'assistant', content: combinedContent, timestamp: Date.now()}]));
|
||||
});
|
||||
|
||||
return Object.assign(response, {abort: () => controller.abort()});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user