Skip to content

Commit

Permalink
feat: add register example
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Jun 29, 2024
1 parent cd73c6b commit 7f9580a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
14 changes: 13 additions & 1 deletion examples/ai-core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ define RUN_EXAMPLE_TARGET
pnpm tsx src/$(subst _,/,$(1)).ts
endef

all: complex embed embed-many generate-object generate-text stream-object stream-text
all: complex embed embed-many registry generate-object generate-text stream-object stream-text

# complex
.PHONY: complex complex-run complex-all semantic-router_main
Expand Down Expand Up @@ -40,6 +40,18 @@ embed-many_ollama-cosine-similarity:
$(call RUN_EXAMPLE_TARGET,$@)


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


# generate-object
.PHONY: generate-object generate-object-run generate-object-all generate-object_ollama generate-object_ollama-full-json generate-object_ollama-json generate-object_ollama-multimodal generate-object_ollama-tool
generate-object: generate-object-run generate-object-all
Expand Down
18 changes: 18 additions & 0 deletions examples/ai-core/src/registry/embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /usr/bin/env -S pnpm tsx

import { embed } from 'ai'
import { OllamaEmbeddingModelId } from 'ollama-ai-provider/src/ollama-embedding-settings'

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

async function main(model: OllamaEmbeddingModelId) {
const { embedding } = await embed({
model: registry.textEmbeddingModel(model),
value: 'sunny day at the beach',
})

console.log(embedding)
}

buildProgram('ollama:nomic-embed-text', main).catch(console.error)
7 changes: 7 additions & 0 deletions examples/ai-core/src/registry/setup-registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { experimental_createProviderRegistry as createProviderRegistry } from 'ai'
import { ollama } from 'ollama-ai-provider'

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

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({
model: registry.languageModel(model),
prompt: 'Invent a new holiday and describe its traditions.',
})

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

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

0 comments on commit 7f9580a

Please sign in to comment.