Skip to content

Commit

Permalink
feat: add stream-object examples
Browse files Browse the repository at this point in the history
Except:

- ollama-tool.ts
  • Loading branch information
sgomez committed May 17, 2024
1 parent 9e52f67 commit e738ee9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/ai-core/src/stream-object/ollama-fullstream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /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,
mode: 'json',
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(),
}),
),
}),
})

for await (const part of result.fullStream) {
switch (part.type) {
case 'object': {
console.clear()
console.log(part.object)
break
}

case 'finish': {
console.log('Finish reason:', part.finishReason)
console.log('Usage:', part.usage)
break
}

case 'error': {
console.error('Error:', part.error)
break
}
}
}
}

buildProgram('llama3', main).catch(console.error)
35 changes: 35 additions & 0 deletions examples/ai-core/src/stream-object/ollama-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /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,
mode: 'json',
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(),
}),
),
}),
})

for await (const partialObject of result.partialObjectStream) {
console.clear()
console.log(partialObject)
}
}

buildProgram('llama3', main).catch(console.error)
34 changes: 34 additions & 0 deletions examples/ai-core/src/stream-object/ollama.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /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(),
}),
),
}),
})

for await (const partialObject of result.partialObjectStream) {
console.clear()
console.log(partialObject)
}
}

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

0 comments on commit e738ee9

Please sign in to comment.