ParthSareen 7 miesięcy temu
rodzic
commit
f883e52ce7

+ 50 - 0
examples/structured_outputs/structured_outputs.ts

@@ -0,0 +1,50 @@
+import { Ollama } from '../../src/index.js';
+import { z } from 'zod';
+import { zodToJsonSchema } from 'zod-to-json-schema';
+
+const ollama = new Ollama();
+
+// Define the schema for friend info
+const FriendInfoSchema = z.object({
+    name: z.string(),
+    age: z.number().int(),
+    is_available: z.boolean()
+});
+
+// Define the schema for friend list
+const FriendListSchema = z.object({
+    friends: z.array(FriendInfoSchema)
+});
+
+async function run() {
+    // Convert the Zod schema to JSON Schema format
+    const jsonSchema = zodToJsonSchema(FriendListSchema);
+
+    // Can use manually defined schema directly
+    // const schema = { 'type': 'object', 'properties': { 'friends': { 'type': 'array', 'items': { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer' }, 'is_available': { 'type': 'boolean' } }, 'required': ['name', 'age', 'is_available'] } } }, 'required': ['friends'] }
+
+    const messages = [{
+        role: 'user',
+        content: 'I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format'
+    }];
+
+    const response = await ollama.chat({
+        model: 'llama3.1:8b',
+        messages: messages,
+        format: jsonSchema, // or format: schema
+        options: {
+            temperature: 0 // Make responses more deterministic
+        }
+    });
+
+    // Parse and validate the response
+    try {
+        console.log('\n', response.message.content, '\n');
+        const friendsResponse = FriendListSchema.parse(JSON.parse(response.message.content));
+        console.log('\n', friendsResponse, '\n');
+    } catch (error) {
+        console.error("Generated invalid response:", error);
+    }
+}
+
+run().catch(console.error);

+ 97 - 0
examples/tools/calculator.ts

@@ -0,0 +1,97 @@
+import { Ollama } from '../../src/index.js';
+
+const ollama = new Ollama();
+
+// Add two numbers function
+function addTwoNumbers(args: { a: number, b: number }): number {
+    return args.a + args.b;
+}
+
+// Subtract two numbers function 
+function subtractTwoNumbers(args: { a: number, b: number }): number {
+    return args.a - args.b;
+}
+
+// Tool definition for add function
+const addTwoNumbersTool = {
+    type: 'function',
+    function: {
+        name: 'addTwoNumbers',
+        description: 'Add two numbers together',
+        parameters: {
+            type: 'object',
+            required: ['a', 'b'],
+            properties: {
+                a: { type: 'number', description: 'The first number' },
+                b: { type: 'number', description: 'The second number' }
+            }
+        }
+    }
+};
+
+// Tool definition for subtract function
+const subtractTwoNumbersTool = {
+    type: 'function',
+    function: {
+        name: 'subtractTwoNumbers',
+        description: 'Subtract two numbers',
+        parameters: {
+            type: 'object',
+            required: ['a', 'b'],
+            properties: {
+                a: { type: 'number', description: 'The first number' },
+                b: { type: 'number', description: 'The second number' }
+            }
+        }
+    }
+};
+
+async function run(model: string) {
+    const messages = [{ role: 'user', content: 'What is three minus one?' }];
+    console.log('Prompt:', messages[0].content);
+
+    const availableFunctions = {
+        addTwoNumbers: addTwoNumbers,
+        subtractTwoNumbers: subtractTwoNumbers
+    };
+
+    const response = await ollama.chat({
+        model: model,
+        messages: messages,
+        tools: [addTwoNumbersTool, subtractTwoNumbersTool]
+    });
+
+    let output: number;
+    if (response.message.tool_calls) {
+        // Process tool calls from the response
+        for (const tool of response.message.tool_calls) {
+            const functionToCall = availableFunctions[tool.function.name];
+            if (functionToCall) {
+                console.log('Calling function:', tool.function.name);
+                console.log('Arguments:', tool.function.arguments);
+                output = functionToCall(tool.function.arguments);
+                console.log('Function output:', output);
+
+                // Add the function response to messages for the model to use
+                messages.push(response.message);
+                messages.push({
+                    role: 'tool',
+                    content: output.toString(),
+                });
+            } else {
+                console.log('Function', tool.function.name, 'not found');
+            }
+        }
+
+        // Get final response from model with function outputs
+        const finalResponse = await ollama.chat({
+            model: model,
+            messages: messages
+        });
+        console.log('Final response:', finalResponse.message.content);
+    } else {
+        console.log('No tool calls returned from model');
+    }
+}
+
+run('llama3.1:8b').catch(error => console.error("An error occurred:", error));

+ 5 - 1
examples/tools/tools.ts → examples/tools/flight-tracker.ts

@@ -1,4 +1,7 @@
-import ollama from 'ollama';
+// import ollama from 'ollama';
+import { Ollama } from '../../src/index.js';
+
+const ollama = new Ollama();
 
 // Simulates an API call to get flight times
 // In a real application, this would fetch data from a live database or API
@@ -70,6 +73,7 @@ async function run(model: string) {
         for (const tool of response.message.tool_calls) {
             const functionToCall = availableFunctions[tool.function.name];
             const functionResponse = functionToCall(tool.function.arguments);
+            console.log('functionResponse', functionResponse)
             // Add function response to the conversation
             messages.push({
                 role: 'tool',