-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from sgomez/tool-choice
Tool choice support
- Loading branch information
Showing
9 changed files
with
294 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
.SILENT: | ||
.DEFAULT_GOAL := all | ||
|
||
define RUN_EXAMPLE_TARGET | ||
echo -- examples/$(subst _,/,$(1)) | ||
pnpm tsx src/$(subst _,/,$(1)).ts > /dev/null | ||
endef | ||
|
||
define RUN_EXAMPLE_CHAT | ||
echo -- examples/$(subst _,/,$(1)) | ||
pnpm tsx src/$(subst _,/,$(1)).ts | ||
endef | ||
|
||
all: complex embed embed-many generate-object generate-text stream-object stream-text | ||
|
||
# complex | ||
.PHONY: complex complex-run complex-all semantic-router_main | ||
complex: complex-run complex-all | ||
complex-run: | ||
echo - examples/complex: | ||
complex-all: semantic-router_main | ||
semantic-router_main: | ||
$(call RUN_EXAMPLE_TARGET,complex/$@) | ||
|
||
|
||
# embed | ||
.PHONY: embed embed-run embed-all embed_ollama | ||
embed: embed-run embed-all | ||
embed-run: | ||
echo - examples/embed: | ||
embed-all: embed_ollama | ||
embed_ollama: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
|
||
|
||
# embed-many | ||
.PHONY: embed-many embed-many-run embed-many-all embed-many_ollama | ||
embed-many: embed-many-run embed-many-all | ||
embed-many-run: | ||
echo - examples/embed-many: | ||
embed-many-all: embed-many_ollama | ||
embed-many_ollama: | ||
$(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 | ||
generate-object-run: | ||
echo - examples/generate-object: | ||
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_ollama: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-object_ollama-full-json: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-object_ollama-json: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-object_ollama-multimodal: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-object_ollama-tool: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
|
||
|
||
# generate-text | ||
.PHONY: generate-text generate-text-run generate-text-all generate-text_ollama generate-text_ollama-completion generate-text_ollama-completion-chat generate-text_ollama-multimodal generate-text_ollama-multimodal-base64 generate-text_ollama-multimodal-url generate-text_ollama-system-message-a generate-text_ollama-system-message-b generate-text_ollama-tool-call | ||
generate-text: generate-text-run generate-text-all | ||
generate-text-run: | ||
echo - examples/generate-text: | ||
generate-text-all: generate-text_ollama generate-text_ollama-completion generate-text_ollama-completion-chat generate-text_ollama-multimodal generate-text_ollama-multimodal-base64 generate-text_ollama-system-message-a generate-text_ollama-system-message-b generate-text_ollama-tool-call | ||
generate-text_ollama: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-completion: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-completion-chat: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-multimodal: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-multimodal-base64: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-multimodal-url: # manual, not supported | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-system-message-a: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-system-message-b: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
generate-text_ollama-tool-call: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
|
||
|
||
# stream-object | ||
.PHONY: stream-object stream-object-run stream-object-all stream-object_ollama stream-object_ollama-fullstream stream-object_ollama-json | ||
stream-object: stream-object-run stream-object-all | ||
stream-object-run: | ||
echo - examples/stream-object: | ||
stream-object-all: stream-object_ollama stream-object_ollama-fullstream stream-object_ollama-json | ||
stream-object_ollama: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
stream-object_ollama-fullstream: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
stream-object_ollama-json: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
|
||
|
||
# stream-text | ||
.PHONY: stream-text stream-text-run stream-text-all stream-text_ollama stream-text_ollama-abort stream-text_ollama-completion stream-text_ollama-completion-chat stream-text_ollama-reader | ||
stream-text: stream-text-run stream-text-all | ||
stream-text-run: | ||
echo - examples/stream-text: | ||
stream-text-all: stream-text_ollama stream-text_ollama-abort stream-text_ollama-completion stream-text_ollama-completion-chat stream-text_ollama-reader | ||
stream-text_ollama: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
stream-text_ollama-abort: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
stream-text_ollama-chatbot: # manual | ||
$(call RUN_EXAMPLE_CHAT,$@) | ||
stream-text_ollama-completion: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
stream-text_ollama-completion-chat: | ||
$(call RUN_EXAMPLE_CHAT,$@) | ||
stream-text_ollama-reader: | ||
$(call RUN_EXAMPLE_TARGET,$@) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#! /usr/bin/env -S pnpm tsx | ||
|
||
import { generateObject } from 'ai' | ||
import { ollama } from 'ollama-ai-provider' | ||
import { OllamaChatModelId } from 'ollama-ai-provider/src/ollama-chat-settings' | ||
import { z } from 'zod' | ||
|
||
import { buildProgram } from '../tools/command' | ||
|
||
async function main(model: OllamaChatModelId) { | ||
const result = await generateObject({ | ||
maxTokens: 2000, | ||
mode: 'tool', | ||
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(), | ||
}), | ||
), | ||
}), | ||
}) | ||
|
||
console.log(JSON.stringify(result.object, null, 2)) | ||
} | ||
|
||
buildProgram('mistral', main).catch(console.error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/ollama/src/generate-tool/inject-tools-schema-into-system.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { LanguageModelV1FunctionTool } from '@ai-sdk/provider' | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { injectToolsSchemaIntoSystem } from '@/generate-tool/inject-tools-schema-into-system' | ||
|
||
describe('injectToolsSchemaIntoSystem', () => { | ||
it('should return system message if no tools are present', () => { | ||
// Arrange | ||
const system = 'You are a helpful and honest assistant.' | ||
|
||
// Act | ||
const systemWithTools = injectToolsSchemaIntoSystem({ system }) | ||
|
||
// Assert | ||
expect(systemWithTools).toEqual(system) | ||
}) | ||
|
||
it('should return system message with tools', () => { | ||
// Arrange | ||
const system = 'You are a helpful and honest assistant.' | ||
const tools: LanguageModelV1FunctionTool[] = [ | ||
{ | ||
description: 'Sum numbers', | ||
name: 'sum', | ||
parameters: { type: 'object' }, | ||
type: 'function', | ||
}, | ||
{ | ||
description: 'Multiply numbers', | ||
name: 'multiply', | ||
parameters: { type: 'object' }, | ||
type: 'function', | ||
}, | ||
] | ||
|
||
// Act | ||
const systemWithTools = injectToolsSchemaIntoSystem({ system, tools }) | ||
|
||
// Assert | ||
expect(systemWithTools).toMatch(/You are a helpful and honest assistant./) | ||
expect(systemWithTools).toMatch(/You have access to the following tools:/) | ||
expect(systemWithTools).toMatch(/"name":"sum"/) | ||
expect(systemWithTools).toMatch(/"name":"multiply"/) | ||
}) | ||
|
||
it('should return system message with choiced tool', () => { | ||
// Arrange | ||
const system = 'You are a helpful and honest assistant.' | ||
const tools: LanguageModelV1FunctionTool[] = [ | ||
{ | ||
description: 'Sum numbers', | ||
name: 'sum', | ||
parameters: { type: 'object' }, | ||
type: 'function', | ||
}, | ||
{ | ||
description: 'Multiply numbers', | ||
name: 'multiply', | ||
parameters: { type: 'object' }, | ||
type: 'function', | ||
}, | ||
] | ||
const toolChoice = 'sum' | ||
|
||
// Act | ||
const systemWithTools = injectToolsSchemaIntoSystem({ | ||
system, | ||
toolChoice, | ||
tools, | ||
}) | ||
|
||
// Assert | ||
expect(systemWithTools).toMatch(/You are a helpful and honest assistant./) | ||
expect(systemWithTools).toMatch(/You have access to the following tools:/) | ||
expect(systemWithTools).toMatch(/"name":"sum"/) | ||
expect(systemWithTools).not.toMatch(/"name":"multiply"/) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters