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 (packages/openai-compatible): Base for OpenAI-compatible providers. #3812

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

shaper
Copy link
Contributor

@shaper shaper commented Nov 21, 2024

Breakout from #3781. Follow-up items:

@shaper shaper force-pushed the shaper/pr/provider-openai-compatible branch from 90b9c52 to e32f8db Compare November 21, 2024 23:55
@shaper shaper force-pushed the shaper/pr/provider-openai-compatible branch from e32f8db to 2978847 Compare November 21, 2024 23:57
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
```

## Example
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There should be a section on creating a custom provider leveraging this package. I'm planning to add that once we have the TogetherAI one done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't able to find a model on together.ai that supported image inputs after 30m of trying a few that seemed like they might. So I switched to OpenAI and validated it's operational. I will try with other companies/models later as we generalize the error schema and so forth to make it easier to test others.


async function main() {
const togetherai = createOpenAICompatible({
apiKeyEnvVarName: 'TOGETHER_AI_API_KEY',
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's completely remove the concept of env variables for openai compatible. it's an unnecessary complexity in this special case, users can just pass the env property and we have less API surface

Copy link
Collaborator

Choose a reason for hiding this comment

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

(it it only there for convenience to begin with, and here convenience becomes complexity)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering if we should go even further and remove the api key completely. some providers don't need api keys, and other have different header settings. instead it should be possible to send in default headers.


const { text } = await generateText({
model: createOpenAICompatible({
apiKeyEnvVarName: 'EXAMPLE_API_KEY',
Copy link
Collaborator

Choose a reason for hiding this comment

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

directly pass env property instead

Description of the API key environment variable (for use in error messages).
*/
apiKeyEnvVarDescription?: string;

Copy link
Collaborator

Choose a reason for hiding this comment

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

lets remove api key related code completely. instead users can provide the key via headers. reason being that some providers have no api keys and there are different ways to send it via headers

Comment on lines +111 to +118
const getHeaders = () => ({
Authorization: `Bearer ${loadApiKey({
apiKey: options.apiKey,
environmentVariableName: options.apiKeyEnvVarName ?? '',
description: options.apiKeyEnvVarDescription ?? '',
})}`,
...options.headers,
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can get rid of this and just use options.headers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants