From e738ee9dafc197cb9b3595b12e631986f7634bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20G=C3=B3mez?= Date: Fri, 17 May 2024 20:03:26 +0200 Subject: [PATCH] feat: add stream-object examples Except: - ollama-tool.ts --- .../src/stream-object/ollama-fullstream.ts | 51 +++++++++++++++++++ .../ai-core/src/stream-object/ollama-json.ts | 35 +++++++++++++ examples/ai-core/src/stream-object/ollama.ts | 34 +++++++++++++ 3 files changed, 120 insertions(+) create mode 100755 examples/ai-core/src/stream-object/ollama-fullstream.ts create mode 100755 examples/ai-core/src/stream-object/ollama-json.ts create mode 100755 examples/ai-core/src/stream-object/ollama.ts diff --git a/examples/ai-core/src/stream-object/ollama-fullstream.ts b/examples/ai-core/src/stream-object/ollama-fullstream.ts new file mode 100755 index 0000000..1fa762e --- /dev/null +++ b/examples/ai-core/src/stream-object/ollama-fullstream.ts @@ -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[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) diff --git a/examples/ai-core/src/stream-object/ollama-json.ts b/examples/ai-core/src/stream-object/ollama-json.ts new file mode 100755 index 0000000..7f25a69 --- /dev/null +++ b/examples/ai-core/src/stream-object/ollama-json.ts @@ -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[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) diff --git a/examples/ai-core/src/stream-object/ollama.ts b/examples/ai-core/src/stream-object/ollama.ts new file mode 100755 index 0000000..e6a3115 --- /dev/null +++ b/examples/ai-core/src/stream-object/ollama.ts @@ -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[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)