Fixed tool calls
All checks were successful
Publish Library / Build NPM Project (push) Successful in 39s
Publish Library / Tag Version (push) Successful in 8s

This commit is contained in:
2025-12-27 17:27:53 -05:00
parent 98dd0bb323
commit 6dce0e8954
6 changed files with 29 additions and 15 deletions

View File

@@ -49,6 +49,7 @@ export class Ollama extends LLMProvider {
if(options.compress) history = await this.ai.language.compressHistory(<any>history, options.compress.max, options.compress.min);
if(options.system) history.unshift({role: 'system', content: system})
const tools = options.tools || this.ai.options.tools || [];
const requestParams: any = {
model: options.model || this.model,
messages: history,
@@ -58,7 +59,7 @@ export class Ollama extends LLMProvider {
temperature: options.temperature || this.ai.options.temperature || 0.7,
num_predict: options.max_tokens || this.ai.options.max_tokens || 4096,
},
tools: (options.tools || this.ai.options.tools || []).map(t => ({
tools: tools.map(t => ({
type: 'function',
function: {
name: t.name,
@@ -74,7 +75,11 @@ export class Ollama extends LLMProvider {
let resp: any, isFirstMessage = true;
do {
resp = await this.client.chat(requestParams);
resp = await this.client.chat(requestParams).catch(err => {
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
throw err;
});
if(options.stream) {
if(!isFirstMessage) options.stream({text: '\n\n'});
else isFirstMessage = false;
@@ -93,7 +98,7 @@ export class Ollama extends LLMProvider {
if(resp.message?.tool_calls?.length && !controller.signal.aborted) {
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));
const tool = tools.find(findByProp('name', toolCall.function.name));
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 {