Browse Source

Update examples

ParthSareen 7 months ago
parent
commit
02ecd355b6

+ 21 - 16
examples/structured_outputs/structured-outputs-image.ts

@@ -1,31 +1,36 @@
-import { Ollama } from '../../src/index.js';
+import ollama from 'ollama';
+
 import { z } from 'zod';
 import { z } from 'zod';
 import { zodToJsonSchema } from 'zod-to-json-schema';
 import { zodToJsonSchema } from 'zod-to-json-schema';
 import { readFileSync } from 'fs';
 import { readFileSync } from 'fs';
 import { resolve } from 'path';
 import { resolve } from 'path';
 import { createInterface } from 'readline';
 import { createInterface } from 'readline';
 
 
-const ollama = new Ollama();
+/*
+    Ollama vision capabilities with structured outputs
+    It takes an image file as input and returns a structured JSON description of the image contents
+    including detected objects, scene analysis, colors, and any text found in the image
+*/
 
 
 // Define the schema for image objects
 // Define the schema for image objects
 const ObjectSchema = z.object({
 const ObjectSchema = z.object({
-    name: z.string(),
-    confidence: z.number(),
-    attributes: z.record(z.any()).optional()
+    name: z.string().describe('The name of the object'),
+    confidence: z.number().min(0).max(1).describe('The confidence score of the object detection'),
+    attributes: z.record(z.any()).optional().describe('Additional attributes of the object')
 });
 });
 
 
-// Define the schema for image description
+// Schema for individual objects detected in the image
 const ImageDescriptionSchema = z.object({
 const ImageDescriptionSchema = z.object({
-    summary: z.string(),
-    objects: z.array(ObjectSchema),
-    scene: z.string(),
-    colors: z.array(z.string()),
-    time_of_day: z.enum(['Morning', 'Afternoon', 'Evening', 'Night']),
-    setting: z.enum(['Indoor', 'Outdoor', 'Unknown']),
-    text_content: z.string().optional()
+    summary: z.string().describe('A concise summary of the image'),
+    objects: z.array(ObjectSchema).describe('An array of objects detected in the image'),
+    scene: z.string().describe('The scene of the image'),
+    colors: z.array(z.string()).describe('An array of colors detected in the image'),
+    time_of_day: z.enum(['Morning', 'Afternoon', 'Evening', 'Night']).describe('The time of day the image was taken'),
+    setting: z.enum(['Indoor', 'Outdoor', 'Unknown']).describe('The setting of the image'),
+    text_content: z.string().describe('Any text detected in the image')
 });
 });
 
 
-async function run() {
+async function run(model: string) {
     // Create readline interface for user input
     // Create readline interface for user input
     const rl = createInterface({
     const rl = createInterface({
         input: process.stdin,
         input: process.stdin,
@@ -54,7 +59,7 @@ async function run() {
         }];
         }];
 
 
         const response = await ollama.chat({
         const response = await ollama.chat({
-            model: 'llama3.2-vision',
+            model: model,
             messages: messages,
             messages: messages,
             format: jsonSchema,
             format: jsonSchema,
             options: {
             options: {
@@ -75,4 +80,4 @@ async function run() {
     }
     }
 }
 }
 
 
-run().catch(console.error);
+run('llama3.2-vision').catch(console.error);

+ 32 - 11
examples/structured_outputs/structured-outputs.ts

@@ -1,27 +1,48 @@
-import { Ollama } from '../../src/index.js';
+import ollama from 'ollama';
 import { z } from 'zod';
 import { z } from 'zod';
 import { zodToJsonSchema } from 'zod-to-json-schema';
 import { zodToJsonSchema } from 'zod-to-json-schema';
 
 
-const ollama = new Ollama();
+/*
+    Ollama structured outputs capabilities
+    It parses the response from the model into a structured JSON object using Zod
+*/
 
 
 // Define the schema for friend info
 // Define the schema for friend info
 const FriendInfoSchema = z.object({
 const FriendInfoSchema = z.object({
-    name: z.string(),
-    age: z.number().int(),
-    is_available: z.boolean()
+    name: z.string().describe('The name of the friend'),
+    age: z.number().int().describe('The age of the friend'),
+    is_available: z.boolean().describe('Whether the friend is available')
 });
 });
 
 
 // Define the schema for friend list
 // Define the schema for friend list
 const FriendListSchema = z.object({
 const FriendListSchema = z.object({
-    friends: z.array(FriendInfoSchema)
+    friends: z.array(FriendInfoSchema).describe('An array of friends')
 });
 });
 
 
-async function run() {
+async function run(model: string) {
     // Convert the Zod schema to JSON Schema format
     // Convert the Zod schema to JSON Schema format
     const jsonSchema = zodToJsonSchema(FriendListSchema);
     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'] }
+    /* 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 = [{
     const messages = [{
         role: 'user',
         role: 'user',
@@ -29,7 +50,7 @@ async function run() {
     }];
     }];
 
 
     const response = await ollama.chat({
     const response = await ollama.chat({
-        model: 'llama3.1:8b',
+        model: model,
         messages: messages,
         messages: messages,
         format: jsonSchema, // or format: schema
         format: jsonSchema, // or format: schema
         options: {
         options: {
@@ -47,4 +68,4 @@ async function run() {
     }
     }
 }
 }
 
 
-run().catch(console.error);
+run('llama3.1:8b').catch(console.error);

+ 1 - 3
examples/tools/flight-tracker.ts

@@ -1,7 +1,5 @@
-// import ollama from 'ollama';
-import { Ollama } from '../../src/index.js';
+import ollama from 'ollama';
 
 
-const ollama = new Ollama();
 
 
 // Simulates an API call to get flight times
 // Simulates an API call to get flight times
 // In a real application, this would fetch data from a live database or API
 // In a real application, this would fetch data from a live database or API