Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Examples for structured outputs and tool use #172

Merged
merged 6 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions examples/structured_outputs/structured-outputs-image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Ollama } from '../../src/index.js';
BruceMacD marked this conversation as resolved.
Show resolved Hide resolved
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { createInterface } from 'readline';

const ollama = new Ollama();
BruceMacD marked this conversation as resolved.
Show resolved Hide resolved

// Define the schema for image objects
ParthSareen marked this conversation as resolved.
Show resolved Hide resolved
const ObjectSchema = z.object({
name: z.string(),
ParthSareen marked this conversation as resolved.
Show resolved Hide resolved
confidence: z.number(),
attributes: z.record(z.any()).optional()
});

// Define the schema for image description
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()
});

async function run() {
// Create readline interface for user input
const rl = createInterface({
input: process.stdin,
output: process.stdout
});

// Get path from user input
const path = await new Promise<string>(resolve => {
rl.question('Enter the path to your image: ', resolve);
});
rl.close();

// Verify the file exists and read it
try {
const imagePath = resolve(path);
const imageBuffer = readFileSync(imagePath);
const base64Image = imageBuffer.toString('base64');

// Convert the Zod schema to JSON Schema format
const jsonSchema = zodToJsonSchema(ImageDescriptionSchema);

const messages = [{
role: 'user',
content: 'Analyze this image and return a detailed JSON description including objects, scene, colors and any text detected. If you cannot determine certain details, leave those fields empty.',
images: [base64Image]
}];

const response = await ollama.chat({
model: 'llama3.2-vision',
ParthSareen marked this conversation as resolved.
Show resolved Hide resolved
messages: messages,
format: jsonSchema,
options: {
temperature: 0 // Make responses more deterministic
}
});

// Parse and validate the response
try {
const imageAnalysis = ImageDescriptionSchema.parse(JSON.parse(response.message.content));
console.log('\nImage Analysis:', imageAnalysis, '\n');
} catch (error) {
console.error("Generated invalid response:", error);
}

} catch (error) {
console.error("Error reading image file:", error);
}
}

run().catch(console.error);
50 changes: 50 additions & 0 deletions examples/structured_outputs/structured-outputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Ollama } from '../../src/index.js';
BruceMacD marked this conversation as resolved.
Show resolved Hide resolved
import { z } from 'zod';
ParthSareen marked this conversation as resolved.
Show resolved Hide resolved
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
BruceMacD marked this conversation as resolved.
Show resolved Hide resolved
// 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 changes: 97 additions & 0 deletions examples/tools/calculator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Ollama } from '../../src/index.js';

const ollama = new Ollama();
ParthSareen marked this conversation as resolved.
Show resolved Hide resolved

// 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));
6 changes: 5 additions & 1 deletion examples/tools/tools.ts → examples/tools/flight-tracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import ollama from 'ollama';
// import ollama from 'ollama';
import { Ollama } from '../../src/index.js';
BruceMacD marked this conversation as resolved.
Show resolved Hide resolved

const ollama = new Ollama();

// Simulates an API call to get flight times
ParthSareen marked this conversation as resolved.
Show resolved Hide resolved
// In a real application, this would fetch data from a live database or API
Expand Down Expand Up @@ -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',
Expand Down
Loading