Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (docs): add baseten provider #3359

Closed
1 change: 1 addition & 0 deletions content/docs/02-foundations/02-providers-and-models.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You can also use the OpenAI provider with OpenAI-compatible APIs:

- [Groq](/providers/ai-sdk-providers/groq)
- [Perplexity](/providers/ai-sdk-providers/perplexity)
- [Baseten](/providers/ai-sdk-providers/baseten)
- [Fireworks](/providers/ai-sdk-providers/fireworks)

Our [language model specification](https://github.com/vercel/ai/tree/main/packages/provider/src/language-model/v1) is published as an open-source package, which you can use to create [custom providers](/providers/community-providers/custom-providers).
Expand Down
1 change: 1 addition & 0 deletions content/docs/02-guides/03-llama-3_1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const { text } = await generateText({
[Groq](/providers/ai-sdk-providers/groq), [Amazon
Bedrock](/providers/ai-sdk-providers/amazon-bedrock),
[Perplexity](/providers/ai-sdk-providers/perplexity),
[Baseten](/providers/ai-sdk-providers/baseten)
[Fireworks](/providers/ai-sdk-providers/fireworks), and more.
</Note>

Expand Down
146 changes: 146 additions & 0 deletions content/providers/02-openai-compatible-providers/01-baseten.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
---
title: Baseten
description: Use a Baseten OpenAI compatible API with the AI SDK.
---

# Baseten Provider

[Baseten](https://baseten.co/) is a platform for running and testing LLMs.
It allows you to deploy models that are OpenAI API compatible that you can use with the AI SDK.
For more information on
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incomplete?


## Setup

The Baseten provider is available via the `@ai-sdk/openai` module as it is compatible with the OpenAI API.
You can install it with

<Tabs items={['pnpm', 'npm', 'yarn']}>
<Tab>
<Snippet text="pnpm add @ai-sdk/openai" dark />
</Tab>
<Tab>
<Snippet text="npm install @ai-sdk/openai" dark />
</Tab>
<Tab>
<Snippet text="yarn add @ai-sdk/openai" dark />
</Tab>
</Tabs>

## Provider Instance

To use Baseten, you can create a custom provider instance with the `createOpenAI` function from `@ai-sdk/openai`:

```ts
import { createOpenAI } from '@ai-sdk/openai';

const BASETEN_MODEL_ID = '<deployment-id>';
const BASETEN_DEPLOYMENT_ID = null;

// see https://docs.baseten.co/api-reference/openai for more information
const basetenExtraPayload = {
model_id: BASETEN_MODEL_ID,
deployment_id: BASETEN_DEPLOYMENT_ID,
};

const baseten = createOpenAI({
name: 'baseten',
apiKey: process.env.BASETEN_API_KEY ?? '',
baseURL: 'https://bridge.baseten.co/v1/direct',
fetch: async (url, request) => {
const bodyWithBasetenPayload = JSON.stringify({
...JSON.parse(request.body),
baseten: basetenExtraPayload,
});
return await fetch(url, { ...request, body: bodyWithBasetenPayload });
},
});
```

Be sure to have your `BASETEN_API_KEY` set in your environment and the model `deployment id` ready. The `deployment_id` will be given after you have deployed the model on Baseten.

## Language Models

You can create [Baseten models](https://baseten.co/models) using a provider instance.
The first argument is the served model name, e.g. `ultravox`.

```ts
const model = baseten('ultravox');
```

### Example

You can use Baseten language models to generate text with the `generateText` function:

```ts
import { createOpenAI } from '@ai-sdk/openai';
import { generateText } from 'ai';

const BASETEN_MODEL_ID = '<deployment-id>';
const BASETEN_DEPLOYMENT_ID = null;

// see https://docs.baseten.co/api-reference/openai for more information
const basetenExtraPayload = {
model_id: BASETEN_MODEL_ID,
deployment_id: BASETEN_DEPLOYMENT_ID,
};

const baseten = createOpenAI({
name: 'baseten',
apiKey: process.env.BASETEN_API_KEY ?? '',
baseURL: 'https://bridge.baseten.co/v1/direct',
fetch: async (url, request) => {
const bodyWithBasetenPayload = JSON.stringify({
...JSON.parse(request.body),
baseten: basetenExtraPayload,
});
return await fetch(url, { ...request, body: bodyWithBasetenPayload });
},
});

const { text } = await generateText({
model: baseten('ultravox'),
prompt: 'Tell me about yourself in one sentence',
});

console.log(text);
```

Baseten language models are also able to generate text in a streaming fashion with the `streamText` function:

```ts
import { createOpenAI } from '@ai-sdk/openai';
import { streamText } from 'ai';

const BASETEN_MODEL_ID = '<deployment-id>';
const BASETEN_DEPLOYMENT_ID = null;

// see https://docs.baseten.co/api-reference/openai for more information
const basetenExtraPayload = {
model_id: BASETEN_MODEL_ID,
deployment_id: BASETEN_DEPLOYMENT_ID,
};

const baseten = createOpenAI({
name: 'baseten',
apiKey: process.env.BASETEN_API_KEY ?? '',
baseURL: 'https://bridge.baseten.co/v1/direct',
fetch: async (url, request) => {
const bodyWithBasetenPayload = JSON.stringify({
...JSON.parse(request.body),
baseten: basetenExtraPayload,
});
return await fetch(url, { ...request, body: bodyWithBasetenPayload });
},
});

const result = await streamText({
model: baseten('ultravox'),
prompt: 'Tell me about yourself in one sentence',
});

for await (const message of result.textStream) {
console.log(message);
}
```

Baseten language models can also be used in the `generateObject`, and `streamObject` functions.
1 change: 1 addition & 0 deletions content/providers/02-openai-compatible-providers/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ We provide detailed documentation for the following OpenAI compatible providers:

- [xAI](/providers/openai-compatible-providers/xai)
- [Perplexity](/providers/openai-compatible-providers/perplexity)
- [Baseten](/providers/openai-compatible-providers/baseten)
- [Fireworks](/providers/openai-compatible-providers/fireworks)
- [LM Studio](/providers/openai-compatible-providers/lmstudio)

Expand Down
44 changes: 44 additions & 0 deletions examples/ai-core/src/stream-text/baseten.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createOpenAI } from '@ai-sdk/openai';
import { streamText } from 'ai';
import 'dotenv/config';

const BASETEN_MODEL_ID = '<deployment-id>';
const BASETEN_DEPLOYMENT_ID = null;

// see https://docs.baseten.co/api-reference/openai for more information
const basetenExtraPayload = {
model_id: BASETEN_MODEL_ID,
deployment_id: BASETEN_DEPLOYMENT_ID,
};

const baseten = createOpenAI({
name: 'baseten',
apiKey: process.env.BASETEN_API_KEY ?? '',
baseURL: 'https://bridge.baseten.co/v1/direct',
fetch: async (url, request) => {
if (!request || !request.body) {
throw new Error('Request body is undefined');
}
const bodyWithBasetenPayload = JSON.stringify({
...JSON.parse(String(request.body)),
baseten: basetenExtraPayload,
});
return await fetch(url, { ...request, body: bodyWithBasetenPayload });
},
});

async function main() {
const result = await streamText({
model: baseten('<model-name>'), // The name of the model you are serving in the baseten deployment
prompt: 'Give me a poem about life',
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}
main().catch(console.error);
Loading