Skip to content

Commit

Permalink
feat: update examples to SDK 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Nov 22, 2024
1 parent be754cc commit dc9711d
Show file tree
Hide file tree
Showing 11 changed files with 449 additions and 174 deletions.
8 changes: 4 additions & 4 deletions examples/ai-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"private": true,
"scripts": {},
"dependencies": {
"@opentelemetry/sdk-node": "0.52.0",
"@opentelemetry/sdk-node": "0.54.2",
"@opentelemetry/auto-instrumentations-node": "0.47.0",
"@opentelemetry/sdk-trace-node": "1.25.0",
"ai": "latest",
"@opentelemetry/sdk-trace-node": "1.27.0",
"ai": "^4.0.0",
"commander": "^12.1.0",
"mathjs": "12.4.2",
"ollama-ai-provider": "workspace:latest",
Expand All @@ -17,6 +17,6 @@
"devDependencies": {
"@types/node": "20.11.20",
"tsx": "4.7.1",
"typescript": "5.5.4"
"typescript": "5.6.3"
}
}
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-object/ollama-multimodal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ async function main(model: Parameters<typeof ollama>[0]) {
console.log(JSON.stringify(object.artwork, null, 2))
}

buildProgram('llava:13b', main).catch(console.error)
buildProgram('llama3.2-vision', main).catch(console.error)
26 changes: 26 additions & 0 deletions examples/ai-core/src/generate-text/ollama-multi-step-continue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#! /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 { steps, text, usage } = await generateText({
experimental_continueSteps: true, // 4096 output tokens
maxSteps: 5,
model: ollama(model),
prompt:
'Write a book about Roman history, ' +
'from the founding of the city of Rome ' +
'to the fall of the Western Roman Empire. ' +
'Each chapter MUST HAVE at least 1000 words.',
})

console.log(text)
console.log()
console.log('Usage:', usage)
console.log('# of steps:', steps.length)
}

buildProgram('qwen2.5', main).catch(console.error)
2 changes: 1 addition & 1 deletion examples/ai-core/src/generate-text/ollama-multi-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { z } from 'zod'
import { buildProgram } from '../tools/command'

async function main(model: Parameters<typeof ollama>[0]) {
const { text, usage } = await generateText({
await generateText({
maxSteps: 5,
model: ollama(model),
onStepFinish: (step) => {
Expand Down
4 changes: 2 additions & 2 deletions examples/ai-core/src/registry/setup-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { ollama } from 'ollama-ai-provider'
const myOllama = customProvider({
fallbackProvider: ollama,
languageModels: {
multimodal: ollama('llava'),
text: ollama('llama3.1'),
multimodal: ollama('llama3.2-vision'),
text: ollama('llama3.2'),
},
})

Expand Down
86 changes: 0 additions & 86 deletions examples/ai-core/src/stream-text/ollama-chatbot-with-tools.ts

This file was deleted.

24 changes: 19 additions & 5 deletions examples/ai-core/src/stream-text/ollama-chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import * as readline from 'node:readline/promises'

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

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

Expand All @@ -21,20 +22,33 @@ async function main(model: Parameters<typeof ollama>[0]) {
messages.push({ content: userInput, role: 'user' })

const result = await streamText({
maxSteps: 5,
messages,
model: ollama(model),
system: `You are a helpful, respectful and honest assistant.`,
tools: {
weather: tool({
description: 'Get the weather in a location',
execute: async ({ location }) => ({
location,
temperature: 72 + Math.floor(Math.random() * 21) - 10,
}),
parameters: z.object({
location: z
.string()
.describe('The location to get the weather for'),
}),
}),
},
})

let fullResponse = ''
process.stdout.write('\nAssistant: ')
for await (const delta of result.textStream) {
fullResponse += delta
process.stdout.write(delta)
}
process.stdout.write('\n\n')

messages.push({ content: fullResponse, role: 'assistant' })
// eslint-disable-next-line unicorn/no-await-expression-member
messages.push(...(await result.response).messages)
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/ai-core/src/tools/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ollama } from 'ollama-ai-provider'

export async function buildProgram(
defaultModel:
| Parameters<typeof ollama>[0]
| Parameters<typeof ollama.embedding>[0],
| Parameters<typeof ollama.languageModel>[0]
| Parameters<typeof ollama.textEmbeddingModel>[0],
action: (model: string) => Promise<void>,
) {
const program = new Command()
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^2.1.3"
},
"engines": {
"node": "^18.0.0 || ^20.0.0"
},
"homepage": "https://github.com/sgomez/ollama-ai-provider",
"repository": {
"type": "git",
Expand All @@ -48,5 +51,5 @@
"keywords": [
"ai"
],
"packageManager": "pnpm@9.6.0"
"packageManager": "pnpm@9.12.3"
}
Loading

0 comments on commit dc9711d

Please sign in to comment.