Skip to content

Commit

Permalink
feat: add customProvider examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Aug 27, 2024
1 parent 99ab6aa commit b298722
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
7 changes: 5 additions & 2 deletions examples/ai-core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ embed-many_ollama-cosine-similarity:


# registry
.PHONY: registry registry-run registry-all registry_embed registry_stream-text
.PHONY: registry registry-run registry-all registry_embed registry_stream-text registry_stream-multimodal
registry: registry-run registry-all
registry-run:
echo - examples/registry:
registry-all: registry_embed registry_stream-text
registry-all: registry_embed registry_stream-text registry_stream-multimodal
registry_embed:
$(call RUN_EXAMPLE_TARGET,$@)
registry_stream-text:
$(call RUN_EXAMPLE_TARGET,$@)
registry_stream-multimodal:
$(call RUN_EXAMPLE_TARGET,$@)



# generate-object
Expand Down
16 changes: 13 additions & 3 deletions examples/ai-core/src/registry/setup-registry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { experimental_createProviderRegistry as createProviderRegistry } from 'ai'
import {
experimental_createProviderRegistry as createProviderRegistry,
experimental_customProvider as customProvider,
} from 'ai'
import { ollama } from 'ollama-ai-provider'

const myOllama = customProvider({
fallbackProvider: ollama,
languageModels: {
multimodal: ollama('llava'),
text: ollama('llama3.1'),
},
})

export const registry = createProviderRegistry({
// register provider with prefix and custom setup:
ollama,
ollama: myOllama,
})
31 changes: 31 additions & 0 deletions examples/ai-core/src/registry/stream-multimodal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#! /usr/bin/env -S pnpm tsx

import fs from 'node:fs'

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

import { buildProgram } from '../tools/command'
import { registry } from './setup-registry'

async function main(model: Parameters<typeof ollama>[0]) {
const result = await streamText({
maxTokens: 512,
messages: [
{
content: [
{ text: 'Describe the image in detail.', type: 'text' },
{ image: fs.readFileSync('./data/comic-cat.png'), type: 'image' },
],
role: 'user',
},
],
model: registry.languageModel(model),
})

for await (const textPart of result.textStream) {
process.stdout.write(textPart)
}
}

buildProgram('ollama:multimodal', main).catch(console.error)
2 changes: 1 addition & 1 deletion examples/ai-core/src/registry/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ async function main(model: Parameters<typeof ollama>[0]) {
}
}

buildProgram('ollama:llama3', main).catch(console.error)
buildProgram('ollama:text', main).catch(console.error)

0 comments on commit b298722

Please sign in to comment.