112 lines
3.5 KiB
JavaScript
112 lines
3.5 KiB
JavaScript
const { Ollama } = require("ollama");
|
|
|
|
const ollama = new Ollama({ host: "http://127.0.0.1:11434" });
|
|
|
|
const tools = [{
|
|
type: 'function',
|
|
function: {
|
|
name: 'get_weather',
|
|
description: 'Get current weather for a location with simulated delay.',
|
|
parameters: {
|
|
type: 'object',
|
|
required: ['location'],
|
|
properties: {
|
|
location: {
|
|
type: 'string',
|
|
description: 'The city and country to get weather for'
|
|
},
|
|
unit: {
|
|
type: 'string',
|
|
enum: ['celsius', 'fahrenheit']
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}];
|
|
|
|
const availableFunctions = {
|
|
get_weather: async ({ location, unit = 'celsius' }) => {
|
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
const temp = unit === 'celsius' ? '22°C' : '72°F';
|
|
return `${location}: ${temp}, sunny (delayed response)`;
|
|
}
|
|
};
|
|
|
|
async function main() {
|
|
const messages = [
|
|
{ role: 'user', content: 'What is the current weather in Paris?' }
|
|
];
|
|
|
|
try {
|
|
const response = await ollama.chat({
|
|
model: 'qwen2.5:7b',
|
|
messages,
|
|
tools,
|
|
tool_choice: 'auto',
|
|
stream: false
|
|
});
|
|
|
|
const message = response.message;
|
|
|
|
if (message.tool_calls?.length > 0) {
|
|
for (const toolCall of message.tool_calls) {
|
|
try {
|
|
const toolName = toolCall.function.name;
|
|
const rawArgs = toolCall.function.arguments;
|
|
|
|
console.log(`⚙️ Received tool call for "${toolName}" with args:`, rawArgs);
|
|
|
|
// Handle different argument formats
|
|
let toolArgs;
|
|
try {
|
|
toolArgs = typeof rawArgs === 'string'
|
|
? JSON.parse(rawArgs)
|
|
: rawArgs;
|
|
} catch (e) {
|
|
throw new Error(`Invalid JSON arguments: ${rawArgs}`);
|
|
}
|
|
|
|
if (!availableFunctions[toolName]) {
|
|
throw new Error(`Tool ${toolName} not available`);
|
|
}
|
|
|
|
const result = await availableFunctions[toolName](toolArgs);
|
|
|
|
messages.push(
|
|
{
|
|
role: 'assistant',
|
|
content: null,
|
|
tool_calls: [toolCall]
|
|
},
|
|
{
|
|
role: 'tool',
|
|
content: result,
|
|
tool_call_id: toolCall.id
|
|
}
|
|
);
|
|
} catch (toolError) {
|
|
console.error('⚠️ Tool error:', toolError.message);
|
|
messages.push({
|
|
role: 'tool',
|
|
content: `Tool error: ${toolError.message}`,
|
|
tool_call_id: toolCall.id
|
|
});
|
|
}
|
|
}
|
|
|
|
const finalResponse = await ollama.chat({
|
|
model: 'qwen2.5:7b',
|
|
messages,
|
|
stream: false
|
|
});
|
|
|
|
console.log('🌍 Final answer:', finalResponse.message.content);
|
|
} else {
|
|
console.log('💬 Direct response:', message.content);
|
|
}
|
|
} catch (error) {
|
|
console.error('🚨 Chat error:', error.message);
|
|
}
|
|
}
|
|
|
|
main(); |