-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
derrick-yyang
wants to merge
8
commits into
vercel:main
from
derrick-yyang:derrick/baseten-vercel-integration
+193
−0
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5123e2b
Add baseten integration instructions
derrick-yyang 11173fe
Added some documentation on baseten deployment specific parameters
derrick-yyang e9d2d05
Added Baseten to more relevant documentation
derrick-yyang e93fdd1
pnpm lint
derrick-yyang 1cb3f0d
Added baseten example for stream text
derrick-yyang b936531
removed console log
derrick-yyang fdb25ff
Merge branch 'main' into derrick/baseten-vercel-integration
derrick-yyang 40d922a
Merge branch 'main' into derrick/baseten-vercel-integration
lgrammel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
146 changes: 146 additions & 0 deletions
146
content/providers/02-openai-compatible-providers/01-baseten.mdx
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,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 | ||
|
||
## 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. |
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,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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incomplete?