-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental ai/core features (#1205)
- Loading branch information
Showing
177 changed files
with
8,852 additions
and
134 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,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
Added ai/core functions (streamText, generateText, streamObject, generateObject). Add OpenAI and Mistral providers. |
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,4 @@ | ||
OPENAI_API_KEY="" | ||
MISTRAL_API_KEY="" | ||
PERPLEXITY_API_KEY="" | ||
FIREWORKS_API_KEY="" |
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,25 @@ | ||
# Basic Examples | ||
|
||
Basic examples for the `ai/core` functions (script usage). | ||
|
||
## Usage | ||
|
||
1. Create .env file with the following content (and more settings, depending on the providers you want to use): | ||
|
||
```sh | ||
OPENAI_API_KEY="YOUR_OPENAI_API_KEY" | ||
... | ||
``` | ||
|
||
2. Run the following commands from the root directory of the AI SDK repo: | ||
|
||
```sh | ||
pnpm install | ||
pnpm build | ||
``` | ||
|
||
3. Run any example (from the `examples/basic` directory) with the following command: | ||
|
||
```sh | ||
pnpm tsx src/path/to/example.ts | ||
``` |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,15 @@ | ||
{ | ||
"name": "ai-core-examples", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"ai": "latest", | ||
"dotenv": "16.4.5", | ||
"zod": "3.22.4", | ||
"zod-to-json-schema": "3.22.4" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.11.20", | ||
"tsx": "4.7.1" | ||
} | ||
} |
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,32 @@ | ||
import { experimental_generateObject } from 'ai'; | ||
import { Mistral } from 'ai/mistral'; | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
|
||
dotenv.config(); | ||
|
||
const mistral = new Mistral(); | ||
|
||
async function main() { | ||
const result = await experimental_generateObject({ | ||
model: mistral.chat('open-mistral-7b'), | ||
schema: z.object({ | ||
characters: z.array( | ||
z.object({ | ||
name: z.string(), | ||
class: z | ||
.string() | ||
.describe('Character class, e.g. warrior, mage, or thief.'), | ||
description: z.string(), | ||
}), | ||
), | ||
}), | ||
mode: 'json', | ||
prompt: | ||
'Generate 3 character descriptions for a fantasy role playing game.', | ||
}); | ||
|
||
console.log(JSON.stringify(result.object, null, 2)); | ||
} | ||
|
||
main(); |
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,32 @@ | ||
import { experimental_generateObject } from 'ai'; | ||
import { Mistral } from 'ai/mistral'; | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
|
||
dotenv.config(); | ||
|
||
const mistral = new Mistral(); | ||
|
||
async function main() { | ||
const result = await experimental_generateObject({ | ||
model: mistral.chat('mistral-large-latest'), | ||
schema: z.object({ | ||
characters: z.array( | ||
z.object({ | ||
name: z.string(), | ||
class: z | ||
.string() | ||
.describe('Character class, e.g. warrior, mage, or thief.'), | ||
description: z.string(), | ||
}), | ||
), | ||
}), | ||
mode: 'tool', | ||
prompt: | ||
'Generate 3 character descriptions for a fantasy role playing game.', | ||
}); | ||
|
||
console.log(JSON.stringify(result.object, null, 2)); | ||
} | ||
|
||
main(); |
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,34 @@ | ||
import { experimental_generateObject } from 'ai'; | ||
import { Mistral } from 'ai/mistral'; | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
|
||
dotenv.config(); | ||
|
||
const mistral = new Mistral(); | ||
|
||
async function main() { | ||
const result = await experimental_generateObject({ | ||
model: mistral.chat('open-mistral-7b'), | ||
schema: z.object({ | ||
recipe: z.object({ | ||
name: z.string(), | ||
ingredients: z.array( | ||
z.object({ | ||
name: z.string(), | ||
amount: z.string(), | ||
}), | ||
), | ||
steps: z.array(z.string()), | ||
}), | ||
}), | ||
prompt: 'Generate a lasagna recipe.', | ||
}); | ||
|
||
console.log(JSON.stringify(result.object.recipe, null, 2)); | ||
console.log(); | ||
console.log('Token usage:', result.usage); | ||
console.log('Finish reason:', result.finishReason); | ||
} | ||
|
||
main(); |
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,32 @@ | ||
import { experimental_generateObject } from 'ai'; | ||
import { OpenAI } from 'ai/openai'; | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
|
||
dotenv.config(); | ||
|
||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
const result = await experimental_generateObject({ | ||
model: openai.chat('gpt-4-turbo-preview'), | ||
schema: z.object({ | ||
characters: z.array( | ||
z.object({ | ||
name: z.string(), | ||
class: z | ||
.string() | ||
.describe('Character class, e.g. warrior, mage, or thief.'), | ||
description: z.string(), | ||
}), | ||
), | ||
}), | ||
mode: 'json', | ||
prompt: | ||
'Generate 3 character descriptions for a fantasy role playing game.', | ||
}); | ||
|
||
console.log(JSON.stringify(result.object, null, 2)); | ||
} | ||
|
||
main(); |
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 @@ | ||
import { experimental_generateObject } from 'ai'; | ||
import { OpenAI } from 'ai/openai'; | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
|
||
dotenv.config(); | ||
|
||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
const result = await experimental_generateObject({ | ||
model: openai.chat('gpt-4-turbo-preview'), | ||
maxTokens: 2000, | ||
schema: z.object({ | ||
characters: z.array( | ||
z.object({ | ||
name: z.string(), | ||
class: z | ||
.string() | ||
.describe('Character class, e.g. warrior, mage, or thief.'), | ||
description: z.string(), | ||
}), | ||
), | ||
}), | ||
mode: 'tool', | ||
prompt: | ||
'Generate 3 character descriptions for a fantasy role playing game.', | ||
}); | ||
|
||
console.log(JSON.stringify(result.object, null, 2)); | ||
} | ||
|
||
main(); |
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,34 @@ | ||
import { experimental_generateObject } from 'ai'; | ||
import { OpenAI } from 'ai/openai'; | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
|
||
dotenv.config(); | ||
|
||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
const result = await experimental_generateObject({ | ||
model: openai.chat('gpt-4-turbo-preview'), | ||
schema: z.object({ | ||
recipe: z.object({ | ||
name: z.string(), | ||
ingredients: z.array( | ||
z.object({ | ||
name: z.string(), | ||
amount: z.string(), | ||
}), | ||
), | ||
steps: z.array(z.string()), | ||
}), | ||
}), | ||
prompt: 'Generate a lasagna recipe.', | ||
}); | ||
|
||
console.log(JSON.stringify(result.object.recipe, null, 2)); | ||
console.log(); | ||
console.log('Token usage:', result.usage); | ||
console.log('Finish reason:', result.finishReason); | ||
} | ||
|
||
main(); |
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,62 @@ | ||
import { experimental_generateText, tool } from 'ai'; | ||
import { Mistral } from 'ai/mistral'; | ||
import dotenv from 'dotenv'; | ||
import { z } from 'zod'; | ||
import { weatherTool } from '../tools/weather-tool'; | ||
|
||
dotenv.config(); | ||
|
||
const mistral = new Mistral(); | ||
|
||
async function main() { | ||
const result = await experimental_generateText({ | ||
model: mistral.chat('mistral-large-latest'), | ||
maxTokens: 512, | ||
tools: { | ||
weather: weatherTool, | ||
cityAttractions: tool({ | ||
parameters: z.object({ city: z.string() }), | ||
}), | ||
}, | ||
prompt: | ||
'What is the weather in San Francisco and what attractions should I visit?', | ||
}); | ||
|
||
// typed tool calls: | ||
for (const toolCall of result.toolCalls) { | ||
switch (toolCall.toolName) { | ||
case 'cityAttractions': { | ||
toolCall.args.city; // string | ||
break; | ||
} | ||
|
||
case 'weather': { | ||
toolCall.args.location; // string | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// typed tool results for tools with execute method: | ||
for (const toolResult of result.toolResults) { | ||
switch (toolResult.toolName) { | ||
// NOT AVAILABLE (NO EXECUTE METHOD) | ||
// case 'cityAttractions': { | ||
// toolResult.args.city; // string | ||
// toolResult.result; | ||
// break; | ||
// } | ||
|
||
case 'weather': { | ||
toolResult.args.location; // string | ||
toolResult.result.location; // string | ||
toolResult.result.temperature; // number | ||
break; | ||
} | ||
} | ||
} | ||
|
||
console.log(JSON.stringify(result, null, 2)); | ||
} | ||
|
||
main(); |
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,21 @@ | ||
import { experimental_generateText } from 'ai'; | ||
import { Mistral } from 'ai/mistral'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const mistral = new Mistral(); | ||
|
||
async function main() { | ||
const result = await experimental_generateText({ | ||
model: mistral.chat('open-mistral-7b'), | ||
prompt: 'Invent a new holiday and describe its traditions.', | ||
}); | ||
|
||
console.log(result.text); | ||
console.log(); | ||
console.log('Token usage:', result.usage); | ||
console.log('Finish reason:', result.finishReason); | ||
} | ||
|
||
main(); |
33 changes: 33 additions & 0 deletions
33
examples/ai-core/src/generate-text/openai-completion-chat.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,33 @@ | ||
import { experimental_generateText } from 'ai'; | ||
import { OpenAI } from 'ai/openai'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const openai = new OpenAI(); | ||
|
||
async function main() { | ||
const result = await experimental_generateText({ | ||
model: openai.completion('gpt-3.5-turbo-instruct'), | ||
maxTokens: 1024, | ||
system: 'You are a helpful chatbot.', | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: 'Hello!', | ||
}, | ||
{ | ||
role: 'assistant', | ||
content: 'Hello! How can I help you today?', | ||
}, | ||
{ | ||
role: 'user', | ||
content: 'I need help with my computer.', | ||
}, | ||
], | ||
}); | ||
|
||
console.log(result.text); | ||
} | ||
|
||
main(); |
Oops, something went wrong.