1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import ollama from 'ollama';
- import { z } from 'zod';
- import { zodToJsonSchema } from 'zod-to-json-schema';
- const FriendInfoSchema = z.object({
- 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')
- });
- const FriendListSchema = z.object({
- friends: z.array(FriendInfoSchema).describe('An array of friends')
- });
- async function run(model: string) {
-
- const jsonSchema = zodToJsonSchema(FriendListSchema);
-
- 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: model,
- messages: messages,
- format: jsonSchema,
- options: {
- temperature: 0
- }
- });
-
- try {
- const friendsResponse = FriendListSchema.parse(JSON.parse(response.message.content));
- console.log(friendsResponse);
- } catch (error) {
- console.error("Generated invalid response:", error);
- }
- }
- run('llama3.1:8b').catch(console.error);
|