@ai-sdk/cerebras 2.0.16 → 2.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +3 -2
- package/src/cerebras-chat-options.ts +13 -0
- package/src/cerebras-provider.test.ts +133 -0
- package/src/cerebras-provider.ts +123 -0
- package/src/index.ts +7 -0
- package/src/version.ts +6 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @ai-sdk/cerebras
|
|
2
2
|
|
|
3
|
+
## 2.0.18
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8dc54db: chore: add src folders to package bundle
|
|
8
|
+
- Updated dependencies [8dc54db]
|
|
9
|
+
- @ai-sdk/openai-compatible@2.0.17
|
|
10
|
+
|
|
11
|
+
## 2.0.17
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [78555ad]
|
|
16
|
+
- @ai-sdk/openai-compatible@2.0.16
|
|
17
|
+
|
|
3
18
|
## 2.0.16
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -33,7 +33,7 @@ var import_provider_utils = require("@ai-sdk/provider-utils");
|
|
|
33
33
|
var import_v4 = require("zod/v4");
|
|
34
34
|
|
|
35
35
|
// src/version.ts
|
|
36
|
-
var VERSION = true ? "2.0.
|
|
36
|
+
var VERSION = true ? "2.0.18" : "0.0.0-test";
|
|
37
37
|
|
|
38
38
|
// src/cerebras-provider.ts
|
|
39
39
|
var cerebrasErrorSchema = import_v4.z.object({
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/cerebras",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.18",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
9
|
"files": [
|
|
10
10
|
"dist/**/*",
|
|
11
|
+
"src",
|
|
11
12
|
"CHANGELOG.md",
|
|
12
13
|
"README.md"
|
|
13
14
|
],
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
}
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@ai-sdk/openai-compatible": "2.0.
|
|
24
|
+
"@ai-sdk/openai-compatible": "2.0.17",
|
|
24
25
|
"@ai-sdk/provider": "3.0.4",
|
|
25
26
|
"@ai-sdk/provider-utils": "4.0.8"
|
|
26
27
|
},
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// https://inference-docs.cerebras.ai/models/overview
|
|
2
|
+
export type CerebrasChatModelId =
|
|
3
|
+
// production
|
|
4
|
+
| 'llama3.1-8b'
|
|
5
|
+
| 'llama-3.3-70b'
|
|
6
|
+
| 'gpt-oss-120b'
|
|
7
|
+
| 'qwen-3-32b'
|
|
8
|
+
// preview
|
|
9
|
+
| 'qwen-3-235b-a22b-instruct-2507'
|
|
10
|
+
| 'qwen-3-235b-a22b-thinking-2507'
|
|
11
|
+
| 'zai-glm-4.6'
|
|
12
|
+
| 'zai-glm-4.7'
|
|
13
|
+
| (string & {});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, Mock } from 'vitest';
|
|
2
|
+
import { createCerebras } from './cerebras-provider';
|
|
3
|
+
import { loadApiKey } from '@ai-sdk/provider-utils';
|
|
4
|
+
import { OpenAICompatibleChatLanguageModel } from '@ai-sdk/openai-compatible';
|
|
5
|
+
|
|
6
|
+
// Add type assertion for the mocked class
|
|
7
|
+
const OpenAICompatibleChatLanguageModelMock =
|
|
8
|
+
OpenAICompatibleChatLanguageModel as unknown as Mock;
|
|
9
|
+
|
|
10
|
+
vi.mock('@ai-sdk/openai-compatible', () => ({
|
|
11
|
+
OpenAICompatibleChatLanguageModel: vi.fn(),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
vi.mock('./version', () => ({
|
|
15
|
+
VERSION: '0.0.0-test',
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
vi.mock('@ai-sdk/provider-utils', async () => {
|
|
19
|
+
const actual = await vi.importActual('@ai-sdk/provider-utils');
|
|
20
|
+
return {
|
|
21
|
+
...actual,
|
|
22
|
+
loadApiKey: vi.fn().mockReturnValue('mock-api-key'),
|
|
23
|
+
withoutTrailingSlash: vi.fn(url => url),
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('CerebrasProvider', () => {
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
vi.clearAllMocks();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('createCerebras', () => {
|
|
33
|
+
it('should create a CerebrasProvider instance with default options', () => {
|
|
34
|
+
const provider = createCerebras();
|
|
35
|
+
const model = provider('model-id');
|
|
36
|
+
|
|
37
|
+
const constructorCall =
|
|
38
|
+
OpenAICompatibleChatLanguageModelMock.mock.calls[0];
|
|
39
|
+
const config = constructorCall[1];
|
|
40
|
+
config.headers();
|
|
41
|
+
|
|
42
|
+
expect(loadApiKey).toHaveBeenCalledWith({
|
|
43
|
+
apiKey: undefined,
|
|
44
|
+
environmentVariableName: 'CEREBRAS_API_KEY',
|
|
45
|
+
description: 'Cerebras API key',
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should create a CerebrasProvider instance with custom options', () => {
|
|
50
|
+
const options = {
|
|
51
|
+
apiKey: 'custom-key',
|
|
52
|
+
baseURL: 'https://custom.url',
|
|
53
|
+
headers: { 'Custom-Header': 'value' },
|
|
54
|
+
};
|
|
55
|
+
const provider = createCerebras(options);
|
|
56
|
+
provider('model-id');
|
|
57
|
+
|
|
58
|
+
const constructorCall =
|
|
59
|
+
OpenAICompatibleChatLanguageModelMock.mock.calls[0];
|
|
60
|
+
const config = constructorCall[1];
|
|
61
|
+
config.headers();
|
|
62
|
+
|
|
63
|
+
expect(loadApiKey).toHaveBeenCalledWith({
|
|
64
|
+
apiKey: 'custom-key',
|
|
65
|
+
environmentVariableName: 'CEREBRAS_API_KEY',
|
|
66
|
+
description: 'Cerebras API key',
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should pass header', async () => {
|
|
71
|
+
const fetchMock = vi
|
|
72
|
+
.fn()
|
|
73
|
+
.mockResolvedValue(new Response('{}', { status: 200 }));
|
|
74
|
+
|
|
75
|
+
const provider = createCerebras({ fetch: fetchMock });
|
|
76
|
+
provider('model-id');
|
|
77
|
+
|
|
78
|
+
const constructorCall = vi.mocked(OpenAICompatibleChatLanguageModel).mock
|
|
79
|
+
.calls[0];
|
|
80
|
+
const config = constructorCall[1];
|
|
81
|
+
const headers = config.headers();
|
|
82
|
+
|
|
83
|
+
await fetchMock('https://api.cerebras.ai/v1/test', {
|
|
84
|
+
method: 'POST',
|
|
85
|
+
headers,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(fetchMock.mock.calls[0][1].headers['user-agent']).toContain(
|
|
89
|
+
'ai-sdk/cerebras/0.0.0-test',
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should return a chat model when called as a function', () => {
|
|
94
|
+
const provider = createCerebras();
|
|
95
|
+
const modelId = 'foo-model-id';
|
|
96
|
+
|
|
97
|
+
const model = provider(modelId);
|
|
98
|
+
expect(model).toBeInstanceOf(OpenAICompatibleChatLanguageModel);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('languageModel', () => {
|
|
103
|
+
it('should construct a language model with correct configuration', () => {
|
|
104
|
+
const provider = createCerebras();
|
|
105
|
+
const modelId = 'foo-model-id';
|
|
106
|
+
|
|
107
|
+
const model = provider.languageModel(modelId);
|
|
108
|
+
|
|
109
|
+
expect(model).toBeInstanceOf(OpenAICompatibleChatLanguageModel);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('embeddingModel', () => {
|
|
114
|
+
it('should throw NoSuchModelError when attempting to create embedding model', () => {
|
|
115
|
+
const provider = createCerebras();
|
|
116
|
+
|
|
117
|
+
expect(() => provider.embeddingModel('any-model')).toThrow(
|
|
118
|
+
'No such embeddingModel: any-model',
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('chat', () => {
|
|
124
|
+
it('should construct a chat model with correct configuration', () => {
|
|
125
|
+
const provider = createCerebras();
|
|
126
|
+
const modelId = 'foo-model-id';
|
|
127
|
+
|
|
128
|
+
const model = provider.chat(modelId);
|
|
129
|
+
|
|
130
|
+
expect(model).toBeInstanceOf(OpenAICompatibleChatLanguageModel);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { OpenAICompatibleChatLanguageModel } from '@ai-sdk/openai-compatible';
|
|
2
|
+
import {
|
|
3
|
+
LanguageModelV3,
|
|
4
|
+
NoSuchModelError,
|
|
5
|
+
ProviderV3,
|
|
6
|
+
} from '@ai-sdk/provider';
|
|
7
|
+
import {
|
|
8
|
+
FetchFunction,
|
|
9
|
+
loadApiKey,
|
|
10
|
+
withoutTrailingSlash,
|
|
11
|
+
withUserAgentSuffix,
|
|
12
|
+
} from '@ai-sdk/provider-utils';
|
|
13
|
+
import { CerebrasChatModelId } from './cerebras-chat-options';
|
|
14
|
+
import { z } from 'zod/v4';
|
|
15
|
+
import { ProviderErrorStructure } from '@ai-sdk/openai-compatible';
|
|
16
|
+
import { VERSION } from './version';
|
|
17
|
+
|
|
18
|
+
// Add error schema and structure
|
|
19
|
+
const cerebrasErrorSchema = z.object({
|
|
20
|
+
message: z.string(),
|
|
21
|
+
type: z.string(),
|
|
22
|
+
param: z.string(),
|
|
23
|
+
code: z.string(),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export type CerebrasErrorData = z.infer<typeof cerebrasErrorSchema>;
|
|
27
|
+
|
|
28
|
+
const cerebrasErrorStructure: ProviderErrorStructure<CerebrasErrorData> = {
|
|
29
|
+
errorSchema: cerebrasErrorSchema,
|
|
30
|
+
errorToMessage: data => data.message,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export interface CerebrasProviderSettings {
|
|
34
|
+
/**
|
|
35
|
+
Cerebras API key.
|
|
36
|
+
*/
|
|
37
|
+
apiKey?: string;
|
|
38
|
+
/**
|
|
39
|
+
Base URL for the API calls.
|
|
40
|
+
*/
|
|
41
|
+
baseURL?: string;
|
|
42
|
+
/**
|
|
43
|
+
Custom headers to include in the requests.
|
|
44
|
+
*/
|
|
45
|
+
headers?: Record<string, string>;
|
|
46
|
+
/**
|
|
47
|
+
Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
48
|
+
or to provide a custom fetch implementation for e.g. testing.
|
|
49
|
+
*/
|
|
50
|
+
fetch?: FetchFunction;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface CerebrasProvider extends ProviderV3 {
|
|
54
|
+
/**
|
|
55
|
+
Creates a Cerebras model for text generation.
|
|
56
|
+
*/
|
|
57
|
+
(modelId: CerebrasChatModelId): LanguageModelV3;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
Creates a Cerebras model for text generation.
|
|
61
|
+
*/
|
|
62
|
+
languageModel(modelId: CerebrasChatModelId): LanguageModelV3;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
Creates a Cerebras chat model for text generation.
|
|
66
|
+
*/
|
|
67
|
+
chat(modelId: CerebrasChatModelId): LanguageModelV3;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @deprecated Use `embeddingModel` instead.
|
|
71
|
+
*/
|
|
72
|
+
textEmbeddingModel(modelId: string): never;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function createCerebras(
|
|
76
|
+
options: CerebrasProviderSettings = {},
|
|
77
|
+
): CerebrasProvider {
|
|
78
|
+
const baseURL = withoutTrailingSlash(
|
|
79
|
+
options.baseURL ?? 'https://api.cerebras.ai/v1',
|
|
80
|
+
);
|
|
81
|
+
const getHeaders = () =>
|
|
82
|
+
withUserAgentSuffix(
|
|
83
|
+
{
|
|
84
|
+
Authorization: `Bearer ${loadApiKey({
|
|
85
|
+
apiKey: options.apiKey,
|
|
86
|
+
environmentVariableName: 'CEREBRAS_API_KEY',
|
|
87
|
+
description: 'Cerebras API key',
|
|
88
|
+
})}`,
|
|
89
|
+
...options.headers,
|
|
90
|
+
},
|
|
91
|
+
`ai-sdk/cerebras/${VERSION}`,
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const createLanguageModel = (modelId: CerebrasChatModelId) => {
|
|
95
|
+
return new OpenAICompatibleChatLanguageModel(modelId, {
|
|
96
|
+
provider: `cerebras.chat`,
|
|
97
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
98
|
+
headers: getHeaders,
|
|
99
|
+
fetch: options.fetch,
|
|
100
|
+
errorStructure: cerebrasErrorStructure,
|
|
101
|
+
supportsStructuredOutputs: true,
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const provider = (modelId: CerebrasChatModelId) =>
|
|
106
|
+
createLanguageModel(modelId);
|
|
107
|
+
|
|
108
|
+
provider.specificationVersion = 'v3' as const;
|
|
109
|
+
provider.languageModel = createLanguageModel;
|
|
110
|
+
provider.chat = createLanguageModel;
|
|
111
|
+
|
|
112
|
+
provider.embeddingModel = (modelId: string) => {
|
|
113
|
+
throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });
|
|
114
|
+
};
|
|
115
|
+
provider.textEmbeddingModel = provider.embeddingModel;
|
|
116
|
+
provider.imageModel = (modelId: string) => {
|
|
117
|
+
throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return provider;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const cerebras = createCerebras();
|
package/src/index.ts
ADDED