new file: newnewnewver.js

modified:   newver.js
This commit is contained in:
2025-01-24 04:01:13 -03:00
parent 9ea63561a5
commit a90eb30591
2 changed files with 115 additions and 7 deletions

112
newnewnewver.js Normal file
View File

@@ -0,0 +1,112 @@
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();

View File

@@ -37,11 +37,11 @@ function googleSearch(args) {
return `Starting google search for ${args.query}, please wait a few minutes`; return `Starting google search for ${args.query}, please wait a few minutes`;
} }
async function googleResult(query) { function googleResult(query) {
const result = "hatsune miku"; const result = "hatsune miku";
messageHistory.push({ role: "tool", content: `googleSearch: ${result}` }); messageHistory.push({ role: "tool", content: `googleSearch: ${result}` });
await getResponse(); getResponse();
} }
const availableFunctions = { const availableFunctions = {
@@ -147,14 +147,12 @@ async function handleToolCall(toolCalls) {
} }
try { try {
// Execute the tool function
console.log(`Calling function: ${name}`); console.log(`Calling function: ${name}`);
console.log(`Arguments:`, args); console.log(`Arguments:`, args);
const output = await functionToCall(args); const output = await functionToCall(args);
console.log(`Function output: ${output}`); console.log(`Function output: ${output}`);
// Update message history with tool output
messageHistory.push({ role: "tool", content: `${name}: ${output}` }); messageHistory.push({ role: "tool", content: `${name}: ${output}` });
} catch (error) { } catch (error) {
@@ -163,7 +161,6 @@ async function handleToolCall(toolCalls) {
} }
} }
// Process follow-up responses from the model after all tool calls are executed
await getResponse(); await getResponse();
} }
@@ -175,8 +172,6 @@ async function getResponse() {
}); });
messageHistory.push(response.message); messageHistory.push(response.message);
// Handle additional tool calls, if any
if (response.message.tool_calls) { if (response.message.tool_calls) {
await handleToolCall(response.message.tool_calls); await handleToolCall(response.message.tool_calls);
} else { } else {
@@ -187,6 +182,7 @@ async function getResponse() {
async function runModel() { async function runModel() {
messageHistory.push({ role: "system", content: "You are Nyamma, a cheerful and cute anime character with a playful, cat-like personality. You often use adorable expressions like \"nya~\" and love making others smile. Youre kind-hearted, energetic, and always eager to help, but can also be a bit mischievous in a charming way. You speak in a light, bubbly tone and sometimes mix in cat-like sounds, especially when you're excited or curious." }); messageHistory.push({ role: "system", content: "You are Nyamma, a cheerful and cute anime character with a playful, cat-like personality. You often use adorable expressions like \"nya~\" and love making others smile. Youre kind-hearted, energetic, and always eager to help, but can also be a bit mischievous in a charming way. You speak in a light, bubbly tone and sometimes mix in cat-like sounds, especially when you're excited or curious." });
//while (true) { //while (true) {
try { try {
//const userMessage = await getUserInput(); //const userMessage = await getUserInput();