-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
examples/ai-core/src/generate-object/ollama-request-body.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,31 @@ | ||
#! /usr/bin/env -S pnpm tsx | ||
|
||
import { generateObject } 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 { request } = await generateObject({ | ||
model: ollama(model), | ||
prompt: 'Generate a lasagna recipe.', | ||
schema: z.object({ | ||
recipe: z.object({ | ||
ingredients: z.array( | ||
z.object({ | ||
amount: z.string(), | ||
name: z.string(), | ||
}), | ||
), | ||
name: z.string(), | ||
steps: z.array(z.string()), | ||
}), | ||
}), | ||
}) | ||
|
||
console.log('REQUEST BODY') | ||
console.log(request.body) | ||
} | ||
|
||
buildProgram('llama3.1', 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#! /usr/bin/env -S pnpm tsx | ||
|
||
import { generateText, tool } 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 { text } = await generateText({ | ||
// disable all tools | ||
experimental_activeTools: [], | ||
maxSteps: 5, | ||
model: ollama(model), | ||
prompt: | ||
'What is the weather in San Francisco and what attractions should I visit?', | ||
tools: { | ||
cityAttractions: tool({ | ||
parameters: z.object({ city: z.string() }), | ||
}), | ||
weather: weatherTool, | ||
}, | ||
}) | ||
|
||
console.log(text) | ||
} | ||
|
||
buildProgram('llama3.1', main).catch(console.error) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,18 @@ | ||
#! /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 { request } = await generateText({ | ||
model: ollama(model), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
}) | ||
|
||
console.log('REQUEST BODY') | ||
console.log(request.body) | ||
} | ||
|
||
buildProgram('llama3.1', 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#! /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 { text, usage } = await generateText({ | ||
abortSignal: AbortSignal.timeout(1000), | ||
model: ollama(model), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
}) | ||
|
||
console.log(text) | ||
console.log() | ||
console.log('Usage:', usage) | ||
} | ||
|
||
buildProgram('llama3.1', main).catch(console.error) |
31 changes: 31 additions & 0 deletions
31
examples/ai-core/src/generate-text/ollama-tool-call-choice.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,31 @@ | ||
#! /usr/bin/env -S pnpm tsx | ||
|
||
import { generateText, tool } 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 generateText({ | ||
maxTokens: 512, | ||
model: ollama(model), | ||
prompt: | ||
'What is the weather in San Francisco and what attractions should I visit?', | ||
toolChoice: { | ||
toolName: 'weather', | ||
type: 'tool', | ||
}, | ||
tools: { | ||
cityAttractions: tool({ | ||
parameters: z.object({ city: z.string() }), | ||
}), | ||
weather: weatherTool, | ||
}, | ||
}) | ||
|
||
console.log(JSON.stringify(result, null, 2)) | ||
} | ||
|
||
buildProgram('llama3.1', 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#! /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, | ||
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(), | ||
}), | ||
), | ||
}), | ||
}) | ||
|
||
// consume stream | ||
for await (const part of result.partialObjectStream) { | ||
} | ||
|
||
console.log('REQUEST BODY') | ||
// eslint-disable-next-line unicorn/no-await-expression-member | ||
console.log((await result.request).body) | ||
} | ||
|
||
buildProgram('llama3.1', 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#! /usr/bin/env -S pnpm tsx | ||
|
||
import { streamText } from 'ai' | ||
import { ollama } from 'ollama-ai-provider' | ||
|
||
import { buildProgram } from '../tools/command' | ||
|
||
async function main(model: Parameters<typeof ollama>[0]) { | ||
const result = await streamText({ | ||
maxRetries: 5, | ||
maxTokens: 512, | ||
model: ollama(model), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
temperature: 0.3, | ||
}) | ||
|
||
// consume stream | ||
for await (const textPart of result.textStream) { | ||
} | ||
|
||
console.log('REQUEST BODY') | ||
// eslint-disable-next-line unicorn/no-await-expression-member | ||
console.log((await result.request).body) | ||
} | ||
|
||
buildProgram('llama3.1', main).catch(console.error) |