@ai-sdk/cerebras 3.0.81 → 3.0.82
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.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/docs/40-cerebras.mdx +4 -1
- package/package.json +4 -4
- package/src/cerebras-provider.ts +16 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @ai-sdk/cerebras
|
|
2
2
|
|
|
3
|
+
## 3.0.82
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 7716e18: Support promises and synchronous or asynchronous callbacks in provider API key and token settings. Resolve credentials per authenticated request while preserving existing authentication behavior and synchronous credential serialization.
|
|
8
|
+
|
|
9
|
+
The experimental OpenAI Live `getServerWebSocketConfig()` method can now resolve asynchronously. Await its result before using the connection settings.
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [7716e18]
|
|
12
|
+
- Updated dependencies [5c247e9]
|
|
13
|
+
- Updated dependencies [dcd2f8d]
|
|
14
|
+
- @ai-sdk/openai-compatible@3.0.82
|
|
15
|
+
- @ai-sdk/provider-utils@5.0.72
|
|
16
|
+
- @ai-sdk/provider@4.0.35
|
|
17
|
+
|
|
3
18
|
## 3.0.81
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { LanguageModelV4, ProviderV4 } from "@ai-sdk/provider";
|
|
2
|
-
import { FetchFunction } from "@ai-sdk/provider-utils";
|
|
2
|
+
import { FetchFunction, Resolvable } from "@ai-sdk/provider-utils";
|
|
3
3
|
import { z } from "zod/v4";
|
|
4
4
|
//#region src/cerebras-chat-options.d.ts
|
|
5
5
|
type CerebrasChatModelId = 'gpt-oss-120b' | 'gemma-4-31b' | (string & {});
|
|
@@ -15,8 +15,9 @@ type CerebrasErrorData = z.infer<typeof cerebrasErrorSchema>;
|
|
|
15
15
|
interface CerebrasProviderSettings {
|
|
16
16
|
/**
|
|
17
17
|
* Cerebras API key.
|
|
18
|
+
* A function is called before each request and may return a promise.
|
|
18
19
|
*/
|
|
19
|
-
apiKey?: string
|
|
20
|
+
apiKey?: Resolvable<string>;
|
|
20
21
|
/**
|
|
21
22
|
* Base URL for the API calls.
|
|
22
23
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/cerebras-chat-options.ts","../src/cerebras-provider.ts","../src/cerebras-chat-language-model-options.ts","../src/version.ts"],"mappings":";;;;KACY;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/cerebras-chat-options.ts","../src/cerebras-provider.ts","../src/cerebras-chat-language-model-options.ts","../src/version.ts"],"mappings":";;;;KACY;;;cCmBN,qBAAmB,EAAA;;;;;GAKvB,EAAA,KAAA;KAEU,oBAAoB,EAAE,aAAa;UAkE9B;;;;;EAKf,SAAS;;;;EAIT;;;;EAIA,UAAU;;;;;EAKV,QAAQ;;UAGO,yBAAyB;;;;GAIvC,SAAS,sBAAsB;;;;EAKhC,cAAc,SAAS,sBAAsB;;;;EAK7C,KAAK,SAAS,sBAAsB;;;;EAKpC,mBAAmB;;wBAGL,eACd,UAAS,2BACR;qBAiDU,UAAQ;;;cCvLR,kCAAgC,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsB3C,EAAA,KAAA;KAEU,mCAAmC,EAAE,aACxC;;;qBC3BI"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NoSuchModelError } from "@ai-sdk/provider";
|
|
2
|
-
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, loadApiKey, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
2
|
+
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, loadApiKey, resolveWith, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
3
3
|
import { OpenAICompatibleChatLanguageModel } from "@ai-sdk/openai-compatible";
|
|
4
4
|
import { z } from "zod/v4";
|
|
5
5
|
//#region src/cerebras-chat-language-model.ts
|
|
@@ -61,7 +61,7 @@ var CerebrasChatLanguageModel = class CerebrasChatLanguageModel extends OpenAICo
|
|
|
61
61
|
};
|
|
62
62
|
//#endregion
|
|
63
63
|
//#region src/version.ts
|
|
64
|
-
const VERSION = "3.0.
|
|
64
|
+
const VERSION = "3.0.82";
|
|
65
65
|
//#endregion
|
|
66
66
|
//#region src/cerebras-provider.ts
|
|
67
67
|
const cerebrasErrorStructure = {
|
|
@@ -100,14 +100,14 @@ function transformCerebrasRequestBody(args) {
|
|
|
100
100
|
}
|
|
101
101
|
function createCerebras(options = {}) {
|
|
102
102
|
const baseURL = withoutTrailingSlash(options.baseURL ?? "https://api.cerebras.ai/v1");
|
|
103
|
-
const getHeaders = () => withUserAgentSuffix({
|
|
103
|
+
const getHeaders = () => resolveWith(options.apiKey, (apiKey) => withUserAgentSuffix({
|
|
104
104
|
Authorization: `Bearer ${loadApiKey({
|
|
105
|
-
apiKey
|
|
105
|
+
apiKey,
|
|
106
106
|
environmentVariableName: "CEREBRAS_API_KEY",
|
|
107
107
|
description: "Cerebras API key"
|
|
108
108
|
})}`,
|
|
109
109
|
...options.headers
|
|
110
|
-
}, `ai-sdk-cerebras/${VERSION}`);
|
|
110
|
+
}, `ai-sdk-cerebras/${VERSION}`));
|
|
111
111
|
const createLanguageModel = (modelId) => {
|
|
112
112
|
return new CerebrasChatLanguageModel(modelId, {
|
|
113
113
|
provider: `cerebras.chat`,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/cerebras-chat-language-model.ts","../src/version.ts","../src/cerebras-provider.ts"],"sourcesContent":["import { OpenAICompatibleChatLanguageModel } from '@ai-sdk/openai-compatible';\nimport type {\n LanguageModelV4,\n LanguageModelV4CallOptions,\n LanguageModelV4GenerateResult,\n LanguageModelV4StreamPart,\n LanguageModelV4StreamResult,\n} from '@ai-sdk/provider';\nimport {\n serializeModelOptions,\n WORKFLOW_DESERIALIZE,\n WORKFLOW_SERIALIZE,\n} from '@ai-sdk/provider-utils';\nimport type { CerebrasChatModelId } from './cerebras-chat-language-model-options';\n\ntype CerebrasChatConfig = ConstructorParameters<\n typeof OpenAICompatibleChatLanguageModel\n>[1];\n\nfunction isStructuredOutputWithToolCallsFinishReason({\n rawFinishReason,\n hasText,\n responseFormatType,\n}: {\n rawFinishReason: string | undefined;\n hasText: boolean;\n responseFormatType:\n | NonNullable<LanguageModelV4CallOptions['responseFormat']>['type']\n | undefined;\n}) {\n return (\n responseFormatType === 'json' && rawFinishReason === 'tool_calls' && hasText\n );\n}\n\nexport class CerebrasChatLanguageModel\n extends OpenAICompatibleChatLanguageModel\n implements LanguageModelV4\n{\n static [WORKFLOW_SERIALIZE](model: CerebrasChatLanguageModel) {\n return serializeModelOptions({\n modelId: model.modelId,\n config: model.config,\n });\n }\n\n static [WORKFLOW_DESERIALIZE](options: {\n modelId: CerebrasChatModelId;\n config: CerebrasChatConfig;\n }) {\n return new CerebrasChatLanguageModel(options.modelId, options.config);\n }\n\n async doGenerate(\n options: LanguageModelV4CallOptions,\n ): Promise<LanguageModelV4GenerateResult> {\n const result = await super.doGenerate(options);\n\n if (\n !isStructuredOutputWithToolCallsFinishReason({\n rawFinishReason: result.finishReason.raw,\n hasText: result.content.some(\n part => part.type === 'text' && part.text.length > 0,\n ),\n responseFormatType: options.responseFormat?.type,\n })\n ) {\n return result;\n }\n\n return {\n ...result,\n // Cerebras GLM can return valid structured output text while also\n // repeating a tool call. Treat that mixed response as the final answer.\n content: result.content.filter(part => part.type !== 'tool-call'),\n finishReason: {\n ...result.finishReason,\n unified: 'stop',\n },\n };\n }\n\n async doStream(\n options: LanguageModelV4CallOptions,\n ): Promise<LanguageModelV4StreamResult> {\n const result = await super.doStream(options);\n let hasText = false;\n\n return {\n ...result,\n stream: result.stream.pipeThrough(\n new TransformStream<\n LanguageModelV4StreamPart,\n LanguageModelV4StreamPart\n >({\n transform(part, controller) {\n if (part.type === 'text-delta' && part.delta.length > 0) {\n hasText = true;\n }\n\n if (\n part.type === 'finish' &&\n isStructuredOutputWithToolCallsFinishReason({\n rawFinishReason: part.finishReason.raw,\n hasText,\n responseFormatType: options.responseFormat?.type,\n })\n ) {\n controller.enqueue({\n ...part,\n finishReason: {\n ...part.finishReason,\n unified: 'stop',\n },\n });\n return;\n }\n\n if (\n options.responseFormat?.type === 'json' &&\n hasText &&\n (part.type === 'tool-input-start' ||\n part.type === 'tool-input-delta' ||\n part.type === 'tool-input-end' ||\n part.type === 'tool-call')\n ) {\n return;\n }\n\n controller.enqueue(part);\n },\n }),\n ),\n };\n }\n}\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n","import type { ProviderErrorStructure } from '@ai-sdk/openai-compatible';\nimport {\n NoSuchModelError,\n type LanguageModelV4,\n type ProviderV4,\n} from '@ai-sdk/provider';\nimport {\n loadApiKey,\n withoutTrailingSlash,\n withUserAgentSuffix,\n type FetchFunction,\n} from '@ai-sdk/provider-utils';\nimport { CerebrasChatLanguageModel } from './cerebras-chat-language-model';\nimport type { CerebrasChatModelId } from './cerebras-chat-options';\nimport { z } from 'zod/v4';\nimport { VERSION } from './version';\n\n// Add error schema and structure\nconst cerebrasErrorSchema = z.object({\n message: z.string(),\n type: z.string(),\n param: z.string(),\n code: z.string(),\n});\n\nexport type CerebrasErrorData = z.infer<typeof cerebrasErrorSchema>;\n\nconst cerebrasErrorStructure: ProviderErrorStructure<CerebrasErrorData> = {\n errorSchema: cerebrasErrorSchema,\n errorToMessage: data => data.message,\n};\n\n/**\n * Cerebras expects assistant reasoning history in the `reasoning` field, while\n * the shared OpenAI-compatible converter serializes it as `reasoning_content`.\n */\nfunction transformCerebrasRequestBody(\n args: Record<string, any>,\n): Record<string, any> {\n const {\n max_tokens: maxTokens,\n parallelToolCalls,\n topLogprobs,\n logitBias,\n serviceTier,\n reasoningFormat,\n promptCacheKey,\n ...restArgs\n } = args;\n\n return {\n ...restArgs,\n ...(maxTokens !== undefined && {\n max_completion_tokens: maxTokens,\n }),\n ...(parallelToolCalls !== undefined && {\n parallel_tool_calls: parallelToolCalls,\n }),\n ...(topLogprobs !== undefined && { top_logprobs: topLogprobs }),\n ...(logitBias !== undefined && { logit_bias: logitBias }),\n ...(serviceTier !== undefined && { service_tier: serviceTier }),\n ...(reasoningFormat !== undefined && {\n reasoning_format: reasoningFormat,\n }),\n ...(promptCacheKey !== undefined && {\n prompt_cache_key: promptCacheKey,\n }),\n messages: Array.isArray(args.messages)\n ? args.messages.map(message => {\n if (\n message == null ||\n typeof message !== 'object' ||\n message.role !== 'assistant' ||\n !('reasoning_content' in message)\n ) {\n return message;\n }\n\n const { reasoning_content, ...rest } = message;\n\n return {\n ...rest,\n ...(!('reasoning' in rest) && reasoning_content !== undefined\n ? { reasoning: reasoning_content }\n : {}),\n };\n })\n : args.messages,\n };\n}\n\nexport interface CerebrasProviderSettings {\n /**\n * Cerebras API key.\n */\n apiKey?: string;\n /**\n * Base URL for the API calls.\n */\n baseURL?: string;\n /**\n * Custom headers to include in the requests.\n */\n headers?: Record<string, string>;\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\nexport interface CerebrasProvider extends ProviderV4 {\n /**\n * Creates a Cerebras model for text generation.\n */\n (modelId: CerebrasChatModelId): LanguageModelV4;\n\n /**\n * Creates a Cerebras model for text generation.\n */\n languageModel(modelId: CerebrasChatModelId): LanguageModelV4;\n\n /**\n * Creates a Cerebras chat model for text generation.\n */\n chat(modelId: CerebrasChatModelId): LanguageModelV4;\n\n /**\n * @deprecated Use `embeddingModel` instead.\n */\n textEmbeddingModel(modelId: string): never;\n}\n\nexport function createCerebras(\n options: CerebrasProviderSettings = {},\n): CerebrasProvider {\n const baseURL = withoutTrailingSlash(\n options.baseURL ?? 'https://api.cerebras.ai/v1',\n );\n const getHeaders = () =>\n withUserAgentSuffix(\n {\n Authorization: `Bearer ${loadApiKey({\n apiKey: options.apiKey,\n environmentVariableName: 'CEREBRAS_API_KEY',\n description: 'Cerebras API key',\n })}`,\n ...options.headers,\n },\n `ai-sdk-cerebras/${VERSION}`,\n );\n\n const createLanguageModel = (modelId: CerebrasChatModelId) => {\n return new CerebrasChatLanguageModel(modelId, {\n provider: `cerebras.chat`,\n url: ({ path }) => `${baseURL}${path}`,\n headers: getHeaders,\n fetch: options.fetch,\n errorStructure: cerebrasErrorStructure,\n supportsStructuredOutputs: true,\n transformRequestBody: transformCerebrasRequestBody,\n });\n };\n\n const provider = (modelId: CerebrasChatModelId) =>\n createLanguageModel(modelId);\n\n provider.specificationVersion = 'v4' as const;\n provider.languageModel = createLanguageModel;\n provider.chat = createLanguageModel;\n\n provider.embeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n };\n provider.textEmbeddingModel = provider.embeddingModel;\n provider.imageModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'imageModel' });\n };\n\n return provider;\n}\n\nexport const cerebras = createCerebras();\n"],"mappings":";;;;;AAmBA,SAAS,4CAA4C,EACnD,iBACA,SACA,sBAOC;CACD,OACE,uBAAuB,UAAU,oBAAoB,gBAAgB;AAEzE;AAEA,IAAa,4BAAb,MAAa,kCACH,kCAEV;CACE,QAAQ,oBAAoB,OAAkC;EAC5D,OAAO,sBAAsB;GAC3B,SAAS,MAAM;GACf,QAAQ,MAAM;EAChB,CAAC;CACH;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WAAW,GAAG,UAAU;GAChC,SAAS;GACT,OAAO,QAAQ;GACf,gBAAgB;GAChB,2BAA2B;GAC3B,sBAAsB;EACxB,CAAC;CACH;CAEA,MAAM,YAAY,YAChB,oBAAoB,OAAO;CAE7B,SAAS,uBAAuB;CAChC,SAAS,gBAAgB;CACzB,SAAS,OAAO;CAEhB,SAAS,kBAAkB,YAAoB;EAC7C,MAAM,IAAI,iBAAiB;GAAE;GAAS,WAAW;EAAiB,CAAC;CACrE;CACA,SAAS,qBAAqB,SAAS;CACvC,SAAS,cAAc,YAAoB;EACzC,MAAM,IAAI,iBAAiB;GAAE;GAAS,WAAW;EAAa,CAAC;CACjE;CAEA,OAAO;AACT;AAEA,MAAa,WAAW,eAAe"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/cerebras-chat-language-model.ts","../src/version.ts","../src/cerebras-provider.ts"],"sourcesContent":["import { OpenAICompatibleChatLanguageModel } from '@ai-sdk/openai-compatible';\nimport type {\n LanguageModelV4,\n LanguageModelV4CallOptions,\n LanguageModelV4GenerateResult,\n LanguageModelV4StreamPart,\n LanguageModelV4StreamResult,\n} from '@ai-sdk/provider';\nimport {\n serializeModelOptions,\n WORKFLOW_DESERIALIZE,\n WORKFLOW_SERIALIZE,\n} from '@ai-sdk/provider-utils';\nimport type { CerebrasChatModelId } from './cerebras-chat-language-model-options';\n\ntype CerebrasChatConfig = ConstructorParameters<\n typeof OpenAICompatibleChatLanguageModel\n>[1];\n\nfunction isStructuredOutputWithToolCallsFinishReason({\n rawFinishReason,\n hasText,\n responseFormatType,\n}: {\n rawFinishReason: string | undefined;\n hasText: boolean;\n responseFormatType:\n | NonNullable<LanguageModelV4CallOptions['responseFormat']>['type']\n | undefined;\n}) {\n return (\n responseFormatType === 'json' && rawFinishReason === 'tool_calls' && hasText\n );\n}\n\nexport class CerebrasChatLanguageModel\n extends OpenAICompatibleChatLanguageModel\n implements LanguageModelV4\n{\n static [WORKFLOW_SERIALIZE](model: CerebrasChatLanguageModel) {\n return serializeModelOptions({\n modelId: model.modelId,\n config: model.config,\n });\n }\n\n static [WORKFLOW_DESERIALIZE](options: {\n modelId: CerebrasChatModelId;\n config: CerebrasChatConfig;\n }) {\n return new CerebrasChatLanguageModel(options.modelId, options.config);\n }\n\n async doGenerate(\n options: LanguageModelV4CallOptions,\n ): Promise<LanguageModelV4GenerateResult> {\n const result = await super.doGenerate(options);\n\n if (\n !isStructuredOutputWithToolCallsFinishReason({\n rawFinishReason: result.finishReason.raw,\n hasText: result.content.some(\n part => part.type === 'text' && part.text.length > 0,\n ),\n responseFormatType: options.responseFormat?.type,\n })\n ) {\n return result;\n }\n\n return {\n ...result,\n // Cerebras GLM can return valid structured output text while also\n // repeating a tool call. Treat that mixed response as the final answer.\n content: result.content.filter(part => part.type !== 'tool-call'),\n finishReason: {\n ...result.finishReason,\n unified: 'stop',\n },\n };\n }\n\n async doStream(\n options: LanguageModelV4CallOptions,\n ): Promise<LanguageModelV4StreamResult> {\n const result = await super.doStream(options);\n let hasText = false;\n\n return {\n ...result,\n stream: result.stream.pipeThrough(\n new TransformStream<\n LanguageModelV4StreamPart,\n LanguageModelV4StreamPart\n >({\n transform(part, controller) {\n if (part.type === 'text-delta' && part.delta.length > 0) {\n hasText = true;\n }\n\n if (\n part.type === 'finish' &&\n isStructuredOutputWithToolCallsFinishReason({\n rawFinishReason: part.finishReason.raw,\n hasText,\n responseFormatType: options.responseFormat?.type,\n })\n ) {\n controller.enqueue({\n ...part,\n finishReason: {\n ...part.finishReason,\n unified: 'stop',\n },\n });\n return;\n }\n\n if (\n options.responseFormat?.type === 'json' &&\n hasText &&\n (part.type === 'tool-input-start' ||\n part.type === 'tool-input-delta' ||\n part.type === 'tool-input-end' ||\n part.type === 'tool-call')\n ) {\n return;\n }\n\n controller.enqueue(part);\n },\n }),\n ),\n };\n }\n}\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n","import type { ProviderErrorStructure } from '@ai-sdk/openai-compatible';\nimport {\n NoSuchModelError,\n type LanguageModelV4,\n type ProviderV4,\n} from '@ai-sdk/provider';\nimport {\n type Resolvable,\n resolveWith,\n loadApiKey,\n withoutTrailingSlash,\n withUserAgentSuffix,\n type FetchFunction,\n} from '@ai-sdk/provider-utils';\nimport { CerebrasChatLanguageModel } from './cerebras-chat-language-model';\nimport type { CerebrasChatModelId } from './cerebras-chat-options';\nimport { z } from 'zod/v4';\nimport { VERSION } from './version';\n\n// Add error schema and structure\nconst cerebrasErrorSchema = z.object({\n message: z.string(),\n type: z.string(),\n param: z.string(),\n code: z.string(),\n});\n\nexport type CerebrasErrorData = z.infer<typeof cerebrasErrorSchema>;\n\nconst cerebrasErrorStructure: ProviderErrorStructure<CerebrasErrorData> = {\n errorSchema: cerebrasErrorSchema,\n errorToMessage: data => data.message,\n};\n\n/**\n * Cerebras expects assistant reasoning history in the `reasoning` field, while\n * the shared OpenAI-compatible converter serializes it as `reasoning_content`.\n */\nfunction transformCerebrasRequestBody(\n args: Record<string, any>,\n): Record<string, any> {\n const {\n max_tokens: maxTokens,\n parallelToolCalls,\n topLogprobs,\n logitBias,\n serviceTier,\n reasoningFormat,\n promptCacheKey,\n ...restArgs\n } = args;\n\n return {\n ...restArgs,\n ...(maxTokens !== undefined && {\n max_completion_tokens: maxTokens,\n }),\n ...(parallelToolCalls !== undefined && {\n parallel_tool_calls: parallelToolCalls,\n }),\n ...(topLogprobs !== undefined && { top_logprobs: topLogprobs }),\n ...(logitBias !== undefined && { logit_bias: logitBias }),\n ...(serviceTier !== undefined && { service_tier: serviceTier }),\n ...(reasoningFormat !== undefined && {\n reasoning_format: reasoningFormat,\n }),\n ...(promptCacheKey !== undefined && {\n prompt_cache_key: promptCacheKey,\n }),\n messages: Array.isArray(args.messages)\n ? args.messages.map(message => {\n if (\n message == null ||\n typeof message !== 'object' ||\n message.role !== 'assistant' ||\n !('reasoning_content' in message)\n ) {\n return message;\n }\n\n const { reasoning_content, ...rest } = message;\n\n return {\n ...rest,\n ...(!('reasoning' in rest) && reasoning_content !== undefined\n ? { reasoning: reasoning_content }\n : {}),\n };\n })\n : args.messages,\n };\n}\n\nexport interface CerebrasProviderSettings {\n /**\n * Cerebras API key.\n * A function is called before each request and may return a promise.\n */\n apiKey?: Resolvable<string>;\n /**\n * Base URL for the API calls.\n */\n baseURL?: string;\n /**\n * Custom headers to include in the requests.\n */\n headers?: Record<string, string>;\n /**\n * Custom fetch implementation. You can use it as a middleware to intercept requests,\n * or to provide a custom fetch implementation for e.g. testing.\n */\n fetch?: FetchFunction;\n}\n\nexport interface CerebrasProvider extends ProviderV4 {\n /**\n * Creates a Cerebras model for text generation.\n */\n (modelId: CerebrasChatModelId): LanguageModelV4;\n\n /**\n * Creates a Cerebras model for text generation.\n */\n languageModel(modelId: CerebrasChatModelId): LanguageModelV4;\n\n /**\n * Creates a Cerebras chat model for text generation.\n */\n chat(modelId: CerebrasChatModelId): LanguageModelV4;\n\n /**\n * @deprecated Use `embeddingModel` instead.\n */\n textEmbeddingModel(modelId: string): never;\n}\n\nexport function createCerebras(\n options: CerebrasProviderSettings = {},\n): CerebrasProvider {\n const baseURL = withoutTrailingSlash(\n options.baseURL ?? 'https://api.cerebras.ai/v1',\n );\n const getHeaders = () =>\n resolveWith(options.apiKey, apiKey =>\n withUserAgentSuffix(\n {\n Authorization: `Bearer ${loadApiKey({\n apiKey,\n environmentVariableName: 'CEREBRAS_API_KEY',\n description: 'Cerebras API key',\n })}`,\n ...options.headers,\n },\n `ai-sdk-cerebras/${VERSION}`,\n ),\n );\n\n const createLanguageModel = (modelId: CerebrasChatModelId) => {\n return new CerebrasChatLanguageModel(modelId, {\n provider: `cerebras.chat`,\n url: ({ path }) => `${baseURL}${path}`,\n headers: getHeaders,\n fetch: options.fetch,\n errorStructure: cerebrasErrorStructure,\n supportsStructuredOutputs: true,\n transformRequestBody: transformCerebrasRequestBody,\n });\n };\n\n const provider = (modelId: CerebrasChatModelId) =>\n createLanguageModel(modelId);\n\n provider.specificationVersion = 'v4' as const;\n provider.languageModel = createLanguageModel;\n provider.chat = createLanguageModel;\n\n provider.embeddingModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n };\n provider.textEmbeddingModel = provider.embeddingModel;\n provider.imageModel = (modelId: string) => {\n throw new NoSuchModelError({ modelId, modelType: 'imageModel' });\n };\n\n return provider;\n}\n\nexport const cerebras = createCerebras();\n"],"mappings":";;;;;AAmBA,SAAS,4CAA4C,EACnD,iBACA,SACA,sBAOC;CACD,OACE,uBAAuB,UAAU,oBAAoB,gBAAgB;AAEzE;AAEA,IAAa,4BAAb,MAAa,kCACH,kCAEV;CACE,QAAQ,oBAAoB,OAAkC;EAC5D,OAAO,sBAAsB;GAC3B,SAAS,MAAM;GACf,QAAQ,MAAM;EAChB,CAAC;CACH;CAEA,QAAQ,sBAAsB,SAG3B;EACD,OAAO,IAAI,0BAA0B,QAAQ,SAAS,QAAQ,MAAM;CACtE;CAEA,MAAM,WACJ,SACwC;EACxC,MAAM,SAAS,MAAM,MAAM,WAAW,OAAO;EAE7C,IACE,CAAC,4CAA4C;GAC3C,iBAAiB,OAAO,aAAa;GACrC,SAAS,OAAO,QAAQ,MACtB,SAAQ,KAAK,SAAS,UAAU,KAAK,KAAK,SAAS,CACrD;GACA,oBAAoB,QAAQ,gBAAgB;EAC9C,CAAC,GAED,OAAO;EAGT,OAAO;GACL,GAAG;GAGH,SAAS,OAAO,QAAQ,QAAO,SAAQ,KAAK,SAAS,WAAW;GAChE,cAAc;IACZ,GAAG,OAAO;IACV,SAAS;GACX;EACF;CACF;CAEA,MAAM,SACJ,SACsC;EACtC,MAAM,SAAS,MAAM,MAAM,SAAS,OAAO;EAC3C,IAAI,UAAU;EAEd,OAAO;GACL,GAAG;GACH,QAAQ,OAAO,OAAO,YACpB,IAAI,gBAGF,EACA,UAAU,MAAM,YAAY;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IACE,WAAW,QACX,OAAO,YAAY,YACnB,QAAQ,SAAS,eACjB,EAAE,uBAAuB,UAEzB,OAAO;GAGT,MAAM,EAAE,mBAAmB,GAAG,SAAS;GAEvC,OAAO;IACL,GAAG;IACH,GAAI,EAAE,eAAe,SAAS,sBAAsB,KAAA,IAChD,EAAE,WAAW,kBAAkB,IAC/B,CAAC;GACP;EACF,CAAC,IACD,KAAK;CACX;AACF;AA6CA,SAAgB,eACd,UAAoC,CAAC,GACnB;CAClB,MAAM,UAAU,qBACd,QAAQ,WAAW,4BACrB;CACA,MAAM,mBACJ,YAAY,QAAQ,SAAQ,WAC1B,oBACE;EACE,eAAe,UAAU,WAAW;GAClC;GACA,yBAAyB;GACzB,aAAa;EACf,CAAC;EACD,GAAG,QAAQ;CACb,GACA,mBAAmB,SACrB,CACF;CAEF,MAAM,uBAAuB,YAAiC;EAC5D,OAAO,IAAI,0BAA0B,SAAS;GAC5C,UAAU;GACV,MAAM,EAAE,WAAW,GAAG,UAAU;GAChC,SAAS;GACT,OAAO,QAAQ;GACf,gBAAgB;GAChB,2BAA2B;GAC3B,sBAAsB;EACxB,CAAC;CACH;CAEA,MAAM,YAAY,YAChB,oBAAoB,OAAO;CAE7B,SAAS,uBAAuB;CAChC,SAAS,gBAAgB;CACzB,SAAS,OAAO;CAEhB,SAAS,kBAAkB,YAAoB;EAC7C,MAAM,IAAI,iBAAiB;GAAE;GAAS,WAAW;EAAiB,CAAC;CACrE;CACA,SAAS,qBAAqB,SAAS;CACvC,SAAS,cAAc,YAAoB;EACzC,MAAM,IAAI,iBAAiB;GAAE;GAAS,WAAW;EAAa,CAAC;CACjE;CAEA,OAAO;AACT;AAEA,MAAa,WAAW,eAAe"}
|
package/docs/40-cerebras.mdx
CHANGED
|
@@ -87,11 +87,14 @@ You can use the following optional settings to customize the Cerebras provider i
|
|
|
87
87
|
Use a different URL prefix for API calls.
|
|
88
88
|
The default prefix is `https://api.cerebras.ai/v1`.
|
|
89
89
|
|
|
90
|
-
- **apiKey**
|
|
90
|
+
- **apiKey** _string | PromiseLike<string> | (() => string | PromiseLike<string>)_
|
|
91
91
|
|
|
92
92
|
API key that is being sent using the `Authorization` header. It defaults to
|
|
93
93
|
the `CEREBRAS_API_KEY` environment variable.
|
|
94
94
|
|
|
95
|
+
A function is called before each request and may return a promise. Use it to
|
|
96
|
+
supply refreshed credentials; a promise value resolves to the same key each time.
|
|
97
|
+
|
|
95
98
|
- **headers** _Record<string,string>_
|
|
96
99
|
|
|
97
100
|
Custom headers to include in the requests.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/cerebras",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.82",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -29,9 +29,9 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ai-sdk/openai-compatible": "3.0.
|
|
33
|
-
"@ai-sdk/provider": "4.0.
|
|
34
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
32
|
+
"@ai-sdk/openai-compatible": "3.0.82",
|
|
33
|
+
"@ai-sdk/provider": "4.0.35",
|
|
34
|
+
"@ai-sdk/provider-utils": "5.0.72"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "22.19.19",
|
package/src/cerebras-provider.ts
CHANGED
|
@@ -5,6 +5,8 @@ import {
|
|
|
5
5
|
type ProviderV4,
|
|
6
6
|
} from '@ai-sdk/provider';
|
|
7
7
|
import {
|
|
8
|
+
type Resolvable,
|
|
9
|
+
resolveWith,
|
|
8
10
|
loadApiKey,
|
|
9
11
|
withoutTrailingSlash,
|
|
10
12
|
withUserAgentSuffix,
|
|
@@ -92,8 +94,9 @@ function transformCerebrasRequestBody(
|
|
|
92
94
|
export interface CerebrasProviderSettings {
|
|
93
95
|
/**
|
|
94
96
|
* Cerebras API key.
|
|
97
|
+
* A function is called before each request and may return a promise.
|
|
95
98
|
*/
|
|
96
|
-
apiKey?: string
|
|
99
|
+
apiKey?: Resolvable<string>;
|
|
97
100
|
/**
|
|
98
101
|
* Base URL for the API calls.
|
|
99
102
|
*/
|
|
@@ -138,16 +141,18 @@ export function createCerebras(
|
|
|
138
141
|
options.baseURL ?? 'https://api.cerebras.ai/v1',
|
|
139
142
|
);
|
|
140
143
|
const getHeaders = () =>
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
144
|
+
resolveWith(options.apiKey, apiKey =>
|
|
145
|
+
withUserAgentSuffix(
|
|
146
|
+
{
|
|
147
|
+
Authorization: `Bearer ${loadApiKey({
|
|
148
|
+
apiKey,
|
|
149
|
+
environmentVariableName: 'CEREBRAS_API_KEY',
|
|
150
|
+
description: 'Cerebras API key',
|
|
151
|
+
})}`,
|
|
152
|
+
...options.headers,
|
|
153
|
+
},
|
|
154
|
+
`ai-sdk-cerebras/${VERSION}`,
|
|
155
|
+
),
|
|
151
156
|
);
|
|
152
157
|
|
|
153
158
|
const createLanguageModel = (modelId: CerebrasChatModelId) => {
|