@ai-sdk/openai 1.0.7 → 1.0.9

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 CHANGED
@@ -1,5 +1,22 @@
1
1
  # @ai-sdk/openai
2
2
 
3
+ ## 1.0.9
4
+
5
+ ### Patch Changes
6
+
7
+ - 3fab0fb: feat (provider/openai): support reasoning_effort setting
8
+ - e956eed: feat (provider/openai): update model list and add o1
9
+ - 6faab13: feat (provider/openai): simulated streaming setting
10
+
11
+ ## 1.0.8
12
+
13
+ ### Patch Changes
14
+
15
+ - 09a9cab: feat (ai/core): add experimental generateImage function
16
+ - Updated dependencies [09a9cab]
17
+ - @ai-sdk/provider@1.0.2
18
+ - @ai-sdk/provider-utils@2.0.4
19
+
3
20
  ## 1.0.7
4
21
 
5
22
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
- import { LanguageModelV1, ProviderV1, EmbeddingModelV1 } from '@ai-sdk/provider';
1
+ import { LanguageModelV1, ProviderV1, EmbeddingModelV1, ImageModelV1 } from '@ai-sdk/provider';
2
2
  import { FetchFunction } from '@ai-sdk/provider-utils';
3
3
 
4
- type OpenAIChatModelId = 'o1-preview' | 'o1-mini' | 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4o-2024-08-06' | 'gpt-4o-2024-11-20' | 'gpt-4o-audio-preview' | 'gpt-4o-audio-preview-2024-10-01' | 'gpt-4o-mini' | 'gpt-4o-mini-2024-07-18' | 'gpt-4-turbo' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-turbo-preview' | 'gpt-4-0125-preview' | 'gpt-4-1106-preview' | 'gpt-4' | 'gpt-4-0613' | 'gpt-3.5-turbo-0125' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-1106' | (string & {});
4
+ type OpenAIChatModelId = 'o1' | 'o1-2024-12-17' | 'o1-mini' | 'o1-mini-2024-09-12' | 'o1-preview' | 'o1-preview-2024-09-12' | 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4o-2024-08-06' | 'gpt-4o-2024-11-20' | 'gpt-4o-audio-preview' | 'gpt-4o-audio-preview-2024-10-01' | 'gpt-4o-audio-preview-2024-12-17' | 'gpt-4o-mini' | 'gpt-4o-mini-2024-07-18' | 'gpt-4-turbo' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-turbo-preview' | 'gpt-4-0125-preview' | 'gpt-4-1106-preview' | 'gpt-4' | 'gpt-4-0613' | 'gpt-3.5-turbo-0125' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-1106' | (string & {});
5
5
  interface OpenAIChatSettings {
6
6
  /**
7
7
  Modify the likelihood of specified tokens appearing in the completion.
@@ -65,6 +65,17 @@ interface OpenAIChatSettings {
65
65
  Defaults to `false`.
66
66
  */
67
67
  downloadImages?: boolean;
68
+ /**
69
+ Simulates streaming by using a normal generate call and returning it as a stream.
70
+ Enable this if the model that you are using does not support streaming.
71
+
72
+ Defaults to `false`.
73
+ */
74
+ simulateStreaming?: boolean;
75
+ /**
76
+ Reasoning effort for reasoning models. Defaults to `medium`.
77
+ */
78
+ reasoningEffort?: 'low' | 'medium' | 'high';
68
79
  }
69
80
 
70
81
  type OpenAICompletionModelId = 'gpt-3.5-turbo-instruct' | (string & {});
@@ -156,6 +167,8 @@ interface OpenAIEmbeddingSettings {
156
167
  user?: string;
157
168
  }
158
169
 
170
+ type OpenAIImageModelId = 'dall-e-3' | 'dall-e-2' | (string & {});
171
+
159
172
  interface OpenAIProvider extends ProviderV1 {
160
173
  (modelId: 'gpt-3.5-turbo-instruct', settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
161
174
  (modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): LanguageModelV1;
@@ -186,6 +199,10 @@ interface OpenAIProvider extends ProviderV1 {
186
199
  Creates a model for text embeddings.
187
200
  */
188
201
  textEmbeddingModel(modelId: OpenAIEmbeddingModelId, settings?: OpenAIEmbeddingSettings): EmbeddingModelV1<string>;
202
+ /**
203
+ Creates a model for image generation.
204
+ */
205
+ image(modelId: OpenAIImageModelId): ImageModelV1;
189
206
  }
190
207
  interface OpenAIProviderSettings {
191
208
  /**
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { LanguageModelV1, ProviderV1, EmbeddingModelV1 } from '@ai-sdk/provider';
1
+ import { LanguageModelV1, ProviderV1, EmbeddingModelV1, ImageModelV1 } from '@ai-sdk/provider';
2
2
  import { FetchFunction } from '@ai-sdk/provider-utils';
3
3
 
4
- type OpenAIChatModelId = 'o1-preview' | 'o1-mini' | 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4o-2024-08-06' | 'gpt-4o-2024-11-20' | 'gpt-4o-audio-preview' | 'gpt-4o-audio-preview-2024-10-01' | 'gpt-4o-mini' | 'gpt-4o-mini-2024-07-18' | 'gpt-4-turbo' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-turbo-preview' | 'gpt-4-0125-preview' | 'gpt-4-1106-preview' | 'gpt-4' | 'gpt-4-0613' | 'gpt-3.5-turbo-0125' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-1106' | (string & {});
4
+ type OpenAIChatModelId = 'o1' | 'o1-2024-12-17' | 'o1-mini' | 'o1-mini-2024-09-12' | 'o1-preview' | 'o1-preview-2024-09-12' | 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4o-2024-08-06' | 'gpt-4o-2024-11-20' | 'gpt-4o-audio-preview' | 'gpt-4o-audio-preview-2024-10-01' | 'gpt-4o-audio-preview-2024-12-17' | 'gpt-4o-mini' | 'gpt-4o-mini-2024-07-18' | 'gpt-4-turbo' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-turbo-preview' | 'gpt-4-0125-preview' | 'gpt-4-1106-preview' | 'gpt-4' | 'gpt-4-0613' | 'gpt-3.5-turbo-0125' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-1106' | (string & {});
5
5
  interface OpenAIChatSettings {
6
6
  /**
7
7
  Modify the likelihood of specified tokens appearing in the completion.
@@ -65,6 +65,17 @@ interface OpenAIChatSettings {
65
65
  Defaults to `false`.
66
66
  */
67
67
  downloadImages?: boolean;
68
+ /**
69
+ Simulates streaming by using a normal generate call and returning it as a stream.
70
+ Enable this if the model that you are using does not support streaming.
71
+
72
+ Defaults to `false`.
73
+ */
74
+ simulateStreaming?: boolean;
75
+ /**
76
+ Reasoning effort for reasoning models. Defaults to `medium`.
77
+ */
78
+ reasoningEffort?: 'low' | 'medium' | 'high';
68
79
  }
69
80
 
70
81
  type OpenAICompletionModelId = 'gpt-3.5-turbo-instruct' | (string & {});
@@ -156,6 +167,8 @@ interface OpenAIEmbeddingSettings {
156
167
  user?: string;
157
168
  }
158
169
 
170
+ type OpenAIImageModelId = 'dall-e-3' | 'dall-e-2' | (string & {});
171
+
159
172
  interface OpenAIProvider extends ProviderV1 {
160
173
  (modelId: 'gpt-3.5-turbo-instruct', settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
161
174
  (modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): LanguageModelV1;
@@ -186,6 +199,10 @@ interface OpenAIProvider extends ProviderV1 {
186
199
  Creates a model for text embeddings.
187
200
  */
188
201
  textEmbeddingModel(modelId: OpenAIEmbeddingModelId, settings?: OpenAIEmbeddingSettings): EmbeddingModelV1<string>;
202
+ /**
203
+ Creates a model for image generation.
204
+ */
205
+ image(modelId: OpenAIImageModelId): ImageModelV1;
189
206
  }
190
207
  interface OpenAIProviderSettings {
191
208
  /**
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ __export(src_exports, {
26
26
  module.exports = __toCommonJS(src_exports);
27
27
 
28
28
  // src/openai-provider.ts
29
- var import_provider_utils6 = require("@ai-sdk/provider-utils");
29
+ var import_provider_utils7 = require("@ai-sdk/provider-utils");
30
30
 
31
31
  // src/openai-chat-language-model.ts
32
32
  var import_provider3 = require("@ai-sdk/provider");
@@ -376,7 +376,7 @@ var OpenAIChatLanguageModel = class {
376
376
  seed,
377
377
  providerMetadata
378
378
  }) {
379
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
379
+ var _a, _b, _c, _d, _e, _f, _g, _h;
380
380
  const type = mode.type;
381
381
  const warnings = [];
382
382
  if (topK != null) {
@@ -430,10 +430,11 @@ var OpenAIChatLanguageModel = class {
430
430
  stop: stopSequences,
431
431
  seed,
432
432
  // openai specific settings:
433
- max_completion_tokens: (_c = (_b = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _b.maxCompletionTokens) != null ? _c : void 0,
434
- store: (_e = (_d = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _d.store) != null ? _e : void 0,
435
- metadata: (_g = (_f = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _f.metadata) != null ? _g : void 0,
436
- prediction: (_i = (_h = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _h.prediction) != null ? _i : void 0,
433
+ max_completion_tokens: (_b = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _b.maxCompletionTokens,
434
+ store: (_c = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _c.store,
435
+ metadata: (_d = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _d.metadata,
436
+ prediction: (_e = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _e.prediction,
437
+ reasoning_effort: (_g = (_f = providerMetadata == null ? void 0 : providerMetadata.openai) == null ? void 0 : _f.reasoningEffort) != null ? _g : this.settings.reasoningEffort,
437
438
  // messages:
438
439
  messages: convertToOpenAIChatMessages({
439
440
  prompt,
@@ -473,7 +474,7 @@ var OpenAIChatLanguageModel = class {
473
474
  json_schema: {
474
475
  schema: mode.schema,
475
476
  strict: true,
476
- name: (_j = mode.name) != null ? _j : "response",
477
+ name: (_h = mode.name) != null ? _h : "response",
477
478
  description: mode.description
478
479
  }
479
480
  } : { type: "json_object" }
@@ -584,6 +585,42 @@ var OpenAIChatLanguageModel = class {
584
585
  };
585
586
  }
586
587
  async doStream(options) {
588
+ if (this.settings.simulateStreaming) {
589
+ const result = await this.doGenerate(options);
590
+ const simulatedStream = new ReadableStream({
591
+ start(controller) {
592
+ controller.enqueue({ type: "response-metadata", ...result.response });
593
+ if (result.text) {
594
+ controller.enqueue({
595
+ type: "text-delta",
596
+ textDelta: result.text
597
+ });
598
+ }
599
+ if (result.toolCalls) {
600
+ for (const toolCall of result.toolCalls) {
601
+ controller.enqueue({
602
+ type: "tool-call",
603
+ ...toolCall
604
+ });
605
+ }
606
+ }
607
+ controller.enqueue({
608
+ type: "finish",
609
+ finishReason: result.finishReason,
610
+ usage: result.usage,
611
+ logprobs: result.logprobs,
612
+ providerMetadata: result.providerMetadata
613
+ });
614
+ controller.close();
615
+ }
616
+ });
617
+ return {
618
+ stream: simulatedStream,
619
+ rawCall: result.rawCall,
620
+ rawResponse: result.rawResponse,
621
+ warnings: result.warnings
622
+ };
623
+ }
587
624
  const { args, warnings } = this.getArgs(options);
588
625
  const body = {
589
626
  ...args,
@@ -1336,14 +1373,65 @@ var openaiTextEmbeddingResponseSchema = import_zod4.z.object({
1336
1373
  usage: import_zod4.z.object({ prompt_tokens: import_zod4.z.number() }).nullish()
1337
1374
  });
1338
1375
 
1376
+ // src/openai-image-model.ts
1377
+ var import_provider_utils6 = require("@ai-sdk/provider-utils");
1378
+ var import_zod5 = require("zod");
1379
+ var OpenAIImageModel = class {
1380
+ constructor(modelId, config) {
1381
+ this.specificationVersion = "v1";
1382
+ this.modelId = modelId;
1383
+ this.config = config;
1384
+ }
1385
+ get provider() {
1386
+ return this.config.provider;
1387
+ }
1388
+ async doGenerate({
1389
+ prompt,
1390
+ n,
1391
+ size,
1392
+ providerOptions,
1393
+ headers,
1394
+ abortSignal
1395
+ }) {
1396
+ var _a;
1397
+ const { value: response } = await (0, import_provider_utils6.postJsonToApi)({
1398
+ url: this.config.url({
1399
+ path: "/images/generations",
1400
+ modelId: this.modelId
1401
+ }),
1402
+ headers: (0, import_provider_utils6.combineHeaders)(this.config.headers(), headers),
1403
+ body: {
1404
+ model: this.modelId,
1405
+ prompt,
1406
+ n,
1407
+ size,
1408
+ ...(_a = providerOptions.openai) != null ? _a : {},
1409
+ response_format: "b64_json"
1410
+ },
1411
+ failedResponseHandler: openaiFailedResponseHandler,
1412
+ successfulResponseHandler: (0, import_provider_utils6.createJsonResponseHandler)(
1413
+ openaiImageResponseSchema
1414
+ ),
1415
+ abortSignal,
1416
+ fetch: this.config.fetch
1417
+ });
1418
+ return {
1419
+ images: response.data.map((item) => item.b64_json)
1420
+ };
1421
+ }
1422
+ };
1423
+ var openaiImageResponseSchema = import_zod5.z.object({
1424
+ data: import_zod5.z.array(import_zod5.z.object({ b64_json: import_zod5.z.string() }))
1425
+ });
1426
+
1339
1427
  // src/openai-provider.ts
1340
1428
  function createOpenAI(options = {}) {
1341
1429
  var _a, _b, _c;
1342
- const baseURL = (_a = (0, import_provider_utils6.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.openai.com/v1";
1430
+ const baseURL = (_a = (0, import_provider_utils7.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://api.openai.com/v1";
1343
1431
  const compatibility = (_b = options.compatibility) != null ? _b : "compatible";
1344
1432
  const providerName = (_c = options.name) != null ? _c : "openai";
1345
1433
  const getHeaders = () => ({
1346
- Authorization: `Bearer ${(0, import_provider_utils6.loadApiKey)({
1434
+ Authorization: `Bearer ${(0, import_provider_utils7.loadApiKey)({
1347
1435
  apiKey: options.apiKey,
1348
1436
  environmentVariableName: "OPENAI_API_KEY",
1349
1437
  description: "OpenAI"
@@ -1372,6 +1460,12 @@ function createOpenAI(options = {}) {
1372
1460
  headers: getHeaders,
1373
1461
  fetch: options.fetch
1374
1462
  });
1463
+ const createImageModel = (modelId) => new OpenAIImageModel(modelId, {
1464
+ provider: `${providerName}.image`,
1465
+ url: ({ path }) => `${baseURL}${path}`,
1466
+ headers: getHeaders,
1467
+ fetch: options.fetch
1468
+ });
1375
1469
  const createLanguageModel = (modelId, settings) => {
1376
1470
  if (new.target) {
1377
1471
  throw new Error(
@@ -1395,6 +1489,7 @@ function createOpenAI(options = {}) {
1395
1489
  provider.embedding = createEmbeddingModel;
1396
1490
  provider.textEmbedding = createEmbeddingModel;
1397
1491
  provider.textEmbeddingModel = createEmbeddingModel;
1492
+ provider.image = createImageModel;
1398
1493
  return provider;
1399
1494
  }
1400
1495
  var openai = createOpenAI({