Skip to content

Commit

Permalink
Add experimental ai/core features (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Mar 22, 2024
1 parent 1c5a39e commit e83bfe3
Show file tree
Hide file tree
Showing 177 changed files with 8,852 additions and 134 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-books-melt.md
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.
2 changes: 1 addition & 1 deletion docs/pages/docs/guides/providers/openai-functions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const functionCallHandler: FunctionCallHandler = async (
messages: [
...chatMessages,
{
id: nanoid(),
id: generateId(),
name: 'get_current_weather',
role: 'function' as const,
content: JSON.stringify({
Expand Down
4 changes: 4 additions & 0 deletions examples/ai-core/.env.example
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=""
25 changes: 25 additions & 0 deletions examples/ai-core/README.md
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
```
Binary file added examples/ai-core/data/comic-cat.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions examples/ai-core/package.json
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"
}
}
32 changes: 32 additions & 0 deletions examples/ai-core/src/generate-object/mistral-json.ts
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();
32 changes: 32 additions & 0 deletions examples/ai-core/src/generate-object/mistral-tool.ts
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();
34 changes: 34 additions & 0 deletions examples/ai-core/src/generate-object/mistral.ts
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();
32 changes: 32 additions & 0 deletions examples/ai-core/src/generate-object/openai-json.ts
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();
33 changes: 33 additions & 0 deletions examples/ai-core/src/generate-object/openai-tool.ts
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();
34 changes: 34 additions & 0 deletions examples/ai-core/src/generate-object/openai.ts
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();
62 changes: 62 additions & 0 deletions examples/ai-core/src/generate-text/mistral-tool-call.ts
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();
21 changes: 21 additions & 0 deletions examples/ai-core/src/generate-text/mistral.ts
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 examples/ai-core/src/generate-text/openai-completion-chat.ts
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();

0 comments on commit e83bfe3

Please sign in to comment.