Skip to content

Commit

Permalink
chore: update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Oct 27, 2024
1 parent 01115da commit 6ce7dce
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/ai-core/src/generate-object/ollama-request-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /usr/bin/env -S pnpm tsx

import { generateObject } from 'ai'
import { ollama } from 'ollama-ai-provider'
import { z } from 'zod'

import { buildProgram } from '../tools/command'

async function main(model: Parameters<typeof ollama>[0]) {
const { request } = await generateObject({
model: ollama(model),
prompt: 'Generate a lasagna recipe.',
schema: z.object({
recipe: z.object({
ingredients: z.array(
z.object({
amount: z.string(),
name: z.string(),
}),
),
name: z.string(),
steps: z.array(z.string()),
}),
}),
})

console.log('REQUEST BODY')
console.log(request.body)
}

buildProgram('llama3.1', main).catch(console.error)
29 changes: 29 additions & 0 deletions examples/ai-core/src/generate-text/ollama-active-tools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#! /usr/bin/env -S pnpm tsx

import { generateText, tool } from 'ai'
import { ollama } from 'ollama-ai-provider'
import { z } from 'zod'

import { buildProgram } from '../tools/command'
import { weatherTool } from '../tools/weather-tool'

async function main(model: Parameters<typeof ollama>[0]) {
const { text } = await generateText({
// disable all tools
experimental_activeTools: [],
maxSteps: 5,
model: ollama(model),
prompt:
'What is the weather in San Francisco and what attractions should I visit?',
tools: {
cityAttractions: tool({
parameters: z.object({ city: z.string() }),
}),
weather: weatherTool,
},
})

console.log(text)
}

buildProgram('llama3.1', main).catch(console.error)
18 changes: 18 additions & 0 deletions examples/ai-core/src/generate-text/ollama-request-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /usr/bin/env -S pnpm tsx

import { generateText } from 'ai'
import { ollama } from 'ollama-ai-provider'

import { buildProgram } from '../tools/command'

async function main(model: Parameters<typeof ollama>[0]) {
const { request } = await generateText({
model: ollama(model),
prompt: 'Invent a new holiday and describe its traditions.',
})

console.log('REQUEST BODY')
console.log(request.body)
}

buildProgram('llama3.1', main).catch(console.error)
20 changes: 20 additions & 0 deletions examples/ai-core/src/generate-text/ollama-timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#! /usr/bin/env -S pnpm tsx

import { generateText } from 'ai'
import { ollama } from 'ollama-ai-provider'

import { buildProgram } from '../tools/command'

async function main(model: Parameters<typeof ollama>[0]) {
const { text, usage } = await generateText({
abortSignal: AbortSignal.timeout(1000),
model: ollama(model),
prompt: 'Invent a new holiday and describe its traditions.',
})

console.log(text)
console.log()
console.log('Usage:', usage)
}

buildProgram('llama3.1', main).catch(console.error)
31 changes: 31 additions & 0 deletions examples/ai-core/src/generate-text/ollama-tool-call-choice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /usr/bin/env -S pnpm tsx

import { generateText, tool } from 'ai'
import { ollama } from 'ollama-ai-provider'
import { z } from 'zod'

import { buildProgram } from '../tools/command'
import { weatherTool } from '../tools/weather-tool'

async function main(model: Parameters<typeof ollama>[0]) {
const result = await generateText({
maxTokens: 512,
model: ollama(model),
prompt:
'What is the weather in San Francisco and what attractions should I visit?',
toolChoice: {
toolName: 'weather',
type: 'tool',
},
tools: {
cityAttractions: tool({
parameters: z.object({ city: z.string() }),
}),
weather: weatherTool,
},
})

console.log(JSON.stringify(result, null, 2))
}

buildProgram('llama3.1', main).catch(console.error)
37 changes: 37 additions & 0 deletions examples/ai-core/src/stream-object/ollama-request-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#! /usr/bin/env -S pnpm tsx

import { streamObject } from 'ai'
import { ollama } from 'ollama-ai-provider'
import { z } from 'zod'

import { buildProgram } from '../tools/command'

async function main(model: Parameters<typeof ollama>[0]) {
const result = await streamObject({
maxTokens: 2000,
model: ollama(model),
prompt:
'Generate 3 character descriptions for a fantasy role playing game.',
schema: z.object({
characters: z.array(
z.object({
class: z
.string()
.describe('Character class, e.g. warrior, mage, or thief.'),
description: z.string(),
name: z.string(),
}),
),
}),
})

// consume stream
for await (const part of result.partialObjectStream) {
}

console.log('REQUEST BODY')
// eslint-disable-next-line unicorn/no-await-expression-member
console.log((await result.request).body)
}

buildProgram('llama3.1', main).catch(console.error)
26 changes: 26 additions & 0 deletions examples/ai-core/src/stream-text/ollama-request-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#! /usr/bin/env -S pnpm tsx

import { streamText } from 'ai'
import { ollama } from 'ollama-ai-provider'

import { buildProgram } from '../tools/command'

async function main(model: Parameters<typeof ollama>[0]) {
const result = await streamText({
maxRetries: 5,
maxTokens: 512,
model: ollama(model),
prompt: 'Invent a new holiday and describe its traditions.',
temperature: 0.3,
})

// consume stream
for await (const textPart of result.textStream) {
}

console.log('REQUEST BODY')
// eslint-disable-next-line unicorn/no-await-expression-member
console.log((await result.request).body)
}

buildProgram('llama3.1', main).catch(console.error)

0 comments on commit 6ce7dce

Please sign in to comment.