-
-
Notifications
You must be signed in to change notification settings - Fork 21
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 #6 from sgomez/stream-tool
Experimental stream tool support
- Loading branch information
Showing
13 changed files
with
629 additions
and
38 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<typeof ollama>[0]) { | ||
const result = await streamObject({ | ||
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(), | ||
}), | ||
), | ||
}), | ||
}) | ||
|
||
for await (const partialObject of result.partialObjectStream) { | ||
console.clear() | ||
console.log(partialObject) | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#! /usr/bin/env -S pnpm tsx | ||
|
||
import { streamText } from 'ai' | ||
import { ollama } from 'ollama-ai-provider' | ||
import { z } from 'zod' | ||
|
||
import { buildProgram } from '../tools/command' | ||
import { weatherTool } from '../tools/weather-tool' | ||
|
||
async function main(model: Parameters<typeof ollama>[0]) { | ||
const result = await streamText({ | ||
model: ollama(model), | ||
prompt: 'What is the weather in San Francisco?', | ||
tools: { | ||
cityAttractions: { | ||
parameters: z.object({ city: z.string() }), | ||
}, | ||
weather: weatherTool, | ||
}, | ||
}) | ||
|
||
for await (const part of result.fullStream) { | ||
switch (part.type) { | ||
case 'text-delta': { | ||
console.log('Text delta:', part.textDelta) | ||
break | ||
} | ||
|
||
case 'tool-call': { | ||
switch (part.toolName) { | ||
case 'cityAttractions': { | ||
console.log('TOOL CALL cityAttractions') | ||
console.log(`city: ${part.args.city}`) // string | ||
break | ||
} | ||
|
||
case 'weather': { | ||
console.log('TOOL CALL weather') | ||
console.log(`location: ${part.args.location}`) // string | ||
break | ||
} | ||
} | ||
|
||
break | ||
} | ||
|
||
case 'tool-result': { | ||
switch (part.toolName) { | ||
// NOT AVAILABLE (NO EXECUTE METHOD) | ||
// case 'cityAttractions': { | ||
// console.log('TOOL RESULT cityAttractions'); | ||
// console.log(`city: ${part.args.city}`); // string | ||
// console.log(`result: ${part.result}`); | ||
// break; | ||
// } | ||
|
||
case 'weather': { | ||
console.log('TOOL RESULT weather') | ||
console.log(`location: ${part.args.location}`) // string | ||
console.log(`temperature: ${part.result.temperature}`) // number | ||
break | ||
} | ||
} | ||
|
||
break | ||
} | ||
|
||
case 'finish': { | ||
console.log('Finish reason:', part.finishReason) | ||
console.log('Usage:', part.usage) | ||
break | ||
} | ||
|
||
case 'error': { | ||
console.error('Error:', part.error) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
buildProgram('openhermes', 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
Oops, something went wrong.