@ai-sdk/openai 0.0.0-85f9a635-20240518005312

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/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2023 Vercel, Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,198 @@
1
+ # Vercel AI SDK - OpenAI Provider
2
+
3
+ The [OpenAI](https://platform.openai.com/) provider for the [Vercel AI SDK](https://sdk.vercel.ai/docs) contains language model support for the OpenAI chat and completion APIs.
4
+ It creates language model objects that can be used with the `generateText`, `streamText`, `generateObject`, and `streamObject` AI functions.
5
+
6
+ ## Setup
7
+
8
+ The OpenAI provider is available in the `@ai-sdk/openai` module. You can install it with
9
+
10
+ ```bash
11
+ npm i @ai-sdk/openai
12
+ ```
13
+
14
+ ## Provider Instance
15
+
16
+ You can import the default provider instance `openai` from `@ai-sdk/openai`:
17
+
18
+ ```ts
19
+ import { openai } from '@ai-sdk/openai';
20
+ ```
21
+
22
+ If you need a customized setup, you can import `createOpenAI` from `@ai-sdk/openai` and create a provider instance with your settings:
23
+
24
+ ```ts
25
+ import { createOpenAI } from '@ai-sdk/openai';
26
+
27
+ const openai = createOpenAI({
28
+ // custom settings
29
+ });
30
+ ```
31
+
32
+ You can use the following optional settings to customize the OpenAI provider instance:
33
+
34
+ - **baseURL** _string_
35
+
36
+ Use a different URL prefix for API calls, e.g. to use proxy servers.
37
+ The default prefix is `https://api.openai.com/v1`.
38
+
39
+ - **apiKey** _string_
40
+
41
+ API key that is being send using the `Authorization` header.
42
+ It defaults to the `OPENAI_API_KEY` environment variable.
43
+
44
+ - **organization** _string_
45
+
46
+ OpenAI Organization.
47
+
48
+ - **project** _string_
49
+
50
+ OpenAI project.
51
+
52
+ - **headers** _Record<string,string>_
53
+
54
+ Custom headers to include in the requests.
55
+
56
+ ## Models
57
+
58
+ The OpenAI provider instance is a function that you can invoke to create a model:
59
+
60
+ ```ts
61
+ const model = openai('gpt-3.5-turbo');
62
+ ```
63
+
64
+ It automatically selects the correct API based on the model id.
65
+ You can also pass additional settings in the second argument:
66
+
67
+ ```ts
68
+ const model = openai('gpt-3.5-turbo', {
69
+ // additional settings
70
+ });
71
+ ```
72
+
73
+ The available options depend on the API that's automatically chosen for the model (see below).
74
+ If you want to explicitly select a specific model API, you can use `.chat` or `.completion`.
75
+
76
+ ### Chat Models
77
+
78
+ You can create models that call the [OpenAI chat API](https://platform.openai.com/docs/api-reference/chat) using the `.chat()` factory method.
79
+ The first argument is the model id, e.g. `gpt-4`.
80
+ The OpenAI chat models support tool calls and some have multi-modal capabilities.
81
+
82
+ ```ts
83
+ const model = openai.chat('gpt-3.5-turbo');
84
+ ```
85
+
86
+ OpenAI chat models support also some model specific settings that are not part of the [standard call settings](/docs/ai-core/settings).
87
+ You can pass them as an options argument:
88
+
89
+ ```ts
90
+ const model = openai.chat('gpt-3.5-turbo', {
91
+ logitBias: {
92
+ // optional likelihood for specific tokens
93
+ '50256': -100,
94
+ },
95
+ user: 'test-user', // optional unique user identifier
96
+ });
97
+ ```
98
+
99
+ The following optional settings are available for OpenAI chat models:
100
+
101
+ - **logitBias** _Record<number, number>_
102
+
103
+ Modifies the likelihood of specified tokens appearing in the completion.
104
+
105
+ Accepts a JSON object that maps tokens (specified by their token ID in
106
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
107
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
108
+ the bias is added to the logits generated by the model prior to sampling.
109
+ The exact effect will vary per model, but values between -1 and 1 should
110
+ decrease or increase likelihood of selection; values like -100 or 100
111
+ should result in a ban or exclusive selection of the relevant token.
112
+
113
+ As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
114
+ token from being generated.
115
+
116
+ - **logProbs** _boolean | number_
117
+
118
+ Return the log probabilities of the tokens. Including logprobs will increase
119
+ the response size and can slow down response times. However, it can
120
+ be useful to better understand how the model is behaving.
121
+
122
+ Setting to true will return the log probabilities of the tokens that
123
+ were generated.
124
+
125
+ Setting to a number will return the log probabilities of the top n
126
+ tokens that were generated.
127
+
128
+ - **user** _string_
129
+
130
+ A unique identifier representing your end-user, which can help OpenAI to
131
+ monitor and detect abuse. Learn more.
132
+
133
+ ### Completion Models
134
+
135
+ You can create models that call the [OpenAI completions API](https://platform.openai.com/docs/api-reference/completions) using the `.completion()` factory method.
136
+ The first argument is the model id.
137
+ Currently only `gpt-3.5-turbo-instruct` is supported.
138
+
139
+ ```ts
140
+ const model = openai.completion('gpt-3.5-turbo-instruct');
141
+ ```
142
+
143
+ OpenAI completion models support also some model specific settings that are not part of the [standard call settings](/docs/ai-core/settings).
144
+ You can pass them as an options argument:
145
+
146
+ ```ts
147
+ const model = openai.completion('gpt-3.5-turbo-instruct', {
148
+ echo: true, // optional, echo the prompt in addition to the completion
149
+ logitBias: {
150
+ // optional likelihood for specific tokens
151
+ '50256': -100,
152
+ },
153
+ suffix: 'some text', // optional suffix that comes after a completion of inserted text
154
+ user: 'test-user', // optional unique user identifier
155
+ });
156
+ ```
157
+
158
+ The following optional settings are available for OpenAI completion models:
159
+
160
+ - **echo**: _boolean_
161
+
162
+ Echo back the prompt in addition to the completion.
163
+
164
+ - **logitBias** _Record<number, number>_
165
+
166
+ Modifies the likelihood of specified tokens appearing in the completion.
167
+
168
+ Accepts a JSON object that maps tokens (specified by their token ID in
169
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
170
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
171
+ the bias is added to the logits generated by the model prior to sampling.
172
+ The exact effect will vary per model, but values between -1 and 1 should
173
+ decrease or increase likelihood of selection; values like -100 or 100
174
+ should result in a ban or exclusive selection of the relevant token.
175
+
176
+ As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
177
+ token from being generated.
178
+
179
+ - **logProbs** _boolean | number_
180
+
181
+ Return the log probabilities of the tokens. Including logprobs will increase
182
+ the response size and can slow down response times. However, it can
183
+ be useful to better understand how the model is behaving.
184
+
185
+ Setting to true will return the log probabilities of the tokens that
186
+ were generated.
187
+
188
+ Setting to a number will return the log probabilities of the top n
189
+ tokens that were generated.
190
+
191
+ - **suffix** _string_
192
+
193
+ The suffix that comes after a completion of inserted text.
194
+
195
+ - **user** _string_
196
+
197
+ A unique identifier representing your end-user, which can help OpenAI to
198
+ monitor and detect abuse. Learn more.
@@ -0,0 +1,255 @@
1
+ import { LanguageModelV1, EmbeddingModelV1 } from '@ai-sdk/provider';
2
+
3
+ type OpenAIChatModelId = 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4-turbo' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-turbo-preview' | 'gpt-4-0125-preview' | 'gpt-4-1106-preview' | 'gpt-4-vision-preview' | 'gpt-4' | 'gpt-4-0613' | 'gpt-4-32k' | 'gpt-4-32k-0613' | 'gpt-3.5-turbo-0125' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-1106' | 'gpt-3.5-turbo-16k' | 'gpt-3.5-turbo-0613' | 'gpt-3.5-turbo-16k-0613' | (string & {});
4
+ interface OpenAIChatSettings {
5
+ /**
6
+ Modify the likelihood of specified tokens appearing in the completion.
7
+
8
+ Accepts a JSON object that maps tokens (specified by their token ID in
9
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
10
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
11
+ the bias is added to the logits generated by the model prior to sampling.
12
+ The exact effect will vary per model, but values between -1 and 1 should
13
+ decrease or increase likelihood of selection; values like -100 or 100
14
+ should result in a ban or exclusive selection of the relevant token.
15
+
16
+ As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
17
+ token from being generated.
18
+ */
19
+ logitBias?: Record<number, number>;
20
+ /**
21
+ Return the log probabilities of the tokens. Including logprobs will increase
22
+ the response size and can slow down response times. However, it can
23
+ be useful to better understand how the model is behaving.
24
+
25
+ Setting to true will return the log probabilities of the tokens that
26
+ were generated.
27
+
28
+ Setting to a number will return the log probabilities of the top n
29
+ tokens that were generated.
30
+ */
31
+ logprobs?: boolean | number;
32
+ /**
33
+ A unique identifier representing your end-user, which can help OpenAI to
34
+ monitor and detect abuse. Learn more.
35
+ */
36
+ user?: string;
37
+ }
38
+
39
+ type OpenAIChatConfig = {
40
+ provider: string;
41
+ baseURL: string;
42
+ compatibility: 'strict' | 'compatible';
43
+ headers: () => Record<string, string | undefined>;
44
+ };
45
+ declare class OpenAIChatLanguageModel implements LanguageModelV1 {
46
+ readonly specificationVersion = "v1";
47
+ readonly defaultObjectGenerationMode = "tool";
48
+ readonly modelId: OpenAIChatModelId;
49
+ readonly settings: OpenAIChatSettings;
50
+ private readonly config;
51
+ constructor(modelId: OpenAIChatModelId, settings: OpenAIChatSettings, config: OpenAIChatConfig);
52
+ get provider(): string;
53
+ private getArgs;
54
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
55
+ doRawStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Omit<Awaited<ReturnType<LanguageModelV1['doStream']>>, 'stream'> & {
56
+ stream: ReadableStream<Uint8Array>;
57
+ }>;
58
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
59
+ }
60
+
61
+ type OpenAICompletionModelId = 'gpt-3.5-turbo-instruct' | (string & {});
62
+ interface OpenAICompletionSettings {
63
+ /**
64
+ Echo back the prompt in addition to the completion.
65
+ */
66
+ echo?: boolean;
67
+ /**
68
+ Modify the likelihood of specified tokens appearing in the completion.
69
+
70
+ Accepts a JSON object that maps tokens (specified by their token ID in
71
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
72
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
73
+ the bias is added to the logits generated by the model prior to sampling.
74
+ The exact effect will vary per model, but values between -1 and 1 should
75
+ decrease or increase likelihood of selection; values like -100 or 100
76
+ should result in a ban or exclusive selection of the relevant token.
77
+
78
+ As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
79
+ token from being generated.
80
+ */
81
+ logitBias?: Record<number, number>;
82
+ /**
83
+ Return the log probabilities of the tokens. Including logprobs will increase
84
+ the response size and can slow down response times. However, it can
85
+ be useful to better understand how the model is behaving.
86
+
87
+ Setting to true will return the log probabilities of the tokens that
88
+ were generated.
89
+
90
+ Setting to a number will return the log probabilities of the top n
91
+ tokens that were generated.
92
+ */
93
+ logprobs?: boolean | number;
94
+ /**
95
+ The suffix that comes after a completion of inserted text.
96
+ */
97
+ suffix?: string;
98
+ /**
99
+ A unique identifier representing your end-user, which can help OpenAI to
100
+ monitor and detect abuse. Learn more.
101
+ */
102
+ user?: string;
103
+ }
104
+
105
+ type OpenAICompletionConfig = {
106
+ provider: string;
107
+ baseURL: string;
108
+ compatibility: 'strict' | 'compatible';
109
+ headers: () => Record<string, string | undefined>;
110
+ };
111
+ declare class OpenAICompletionLanguageModel implements LanguageModelV1 {
112
+ readonly specificationVersion = "v1";
113
+ readonly defaultObjectGenerationMode: undefined;
114
+ readonly modelId: OpenAICompletionModelId;
115
+ readonly settings: OpenAICompletionSettings;
116
+ private readonly config;
117
+ constructor(modelId: OpenAICompletionModelId, settings: OpenAICompletionSettings, config: OpenAICompletionConfig);
118
+ get provider(): string;
119
+ private getArgs;
120
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
121
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
122
+ }
123
+
124
+ type OpenAIEmbeddingModelId = 'text-embedding-3-small' | 'text-embedding-3-large' | 'text-embedding-ada-002' | (string & {});
125
+ interface OpenAIEmbeddingSettings {
126
+ /**
127
+ Override the maximum number of embeddings per call.
128
+ */
129
+ maxEmbeddingsPerCall?: number;
130
+ /**
131
+ Override the parallelism of embedding calls.
132
+ */
133
+ supportsParallelCalls?: boolean;
134
+ /**
135
+ The number of dimensions the resulting output embeddings should have.
136
+ Only supported in text-embedding-3 and later models.
137
+ */
138
+ dimensions?: number;
139
+ /**
140
+ A unique identifier representing your end-user, which can help OpenAI to
141
+ monitor and detect abuse. Learn more.
142
+ */
143
+ user?: string;
144
+ }
145
+
146
+ type OpenAIEmbeddingConfig = {
147
+ provider: string;
148
+ baseURL: string;
149
+ headers: () => Record<string, string | undefined>;
150
+ };
151
+ declare class OpenAIEmbeddingModel implements EmbeddingModelV1<string> {
152
+ readonly specificationVersion = "v1";
153
+ readonly modelId: OpenAIEmbeddingModelId;
154
+ private readonly config;
155
+ private readonly settings;
156
+ get provider(): string;
157
+ get maxEmbeddingsPerCall(): number;
158
+ get supportsParallelCalls(): boolean;
159
+ constructor(modelId: OpenAIEmbeddingModelId, settings: OpenAIEmbeddingSettings, config: OpenAIEmbeddingConfig);
160
+ doEmbed({ values, abortSignal, }: Parameters<EmbeddingModelV1<string>['doEmbed']>[0]): Promise<Awaited<ReturnType<EmbeddingModelV1<string>['doEmbed']>>>;
161
+ }
162
+
163
+ interface OpenAIProvider {
164
+ (modelId: 'gpt-3.5-turbo-instruct', settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
165
+ (modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
166
+ /**
167
+ Creates an OpenAI chat model for text generation.
168
+ */
169
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
170
+ /**
171
+ Creates an OpenAI completion model for text generation.
172
+ */
173
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
174
+ /**
175
+ Creates a model for text embeddings.
176
+ */
177
+ embedding(modelId: OpenAIEmbeddingModelId, settings?: OpenAIEmbeddingSettings): OpenAIEmbeddingModel;
178
+ }
179
+ interface OpenAIProviderSettings {
180
+ /**
181
+ Base URL for the OpenAI API calls.
182
+ */
183
+ baseURL?: string;
184
+ /**
185
+ @deprecated Use `baseURL` instead.
186
+ */
187
+ baseUrl?: string;
188
+ /**
189
+ API key for authenticating requests.
190
+ */
191
+ apiKey?: string;
192
+ /**
193
+ OpenAI Organization.
194
+ */
195
+ organization?: string;
196
+ /**
197
+ OpenAI project.
198
+ */
199
+ project?: string;
200
+ /**
201
+ Custom headers to include in the requests.
202
+ */
203
+ headers?: Record<string, string>;
204
+ /**
205
+ OpenAI compatibility mode. Should be set to `strict` when using the OpenAI API,
206
+ and `compatible` when using 3rd party providers. In `compatible` mode, newer
207
+ information such as streamOptions are not being sent. Defaults to 'compatible'.
208
+ */
209
+ compatibility?: 'strict' | 'compatible';
210
+ }
211
+ /**
212
+ Create an OpenAI provider instance.
213
+ */
214
+ declare function createOpenAI(options?: OpenAIProviderSettings): OpenAIProvider;
215
+ /**
216
+ Default OpenAI provider instance. It uses 'strict' compatibility mode.
217
+ */
218
+ declare const openai: OpenAIProvider;
219
+
220
+ /**
221
+ @deprecated Use `createOpenAI` instead.
222
+ */
223
+ declare class OpenAI {
224
+ /**
225
+ Use a different URL prefix for API calls, e.g. to use proxy servers.
226
+ The default prefix is `https://api.openai.com/v1`.
227
+ */
228
+ readonly baseURL: string;
229
+ /**
230
+ API key that is being send using the `Authorization` header.
231
+ It defaults to the `OPENAI_API_KEY` environment variable.
232
+ */
233
+ readonly apiKey?: string;
234
+ /**
235
+ OpenAI Organization.
236
+ */
237
+ readonly organization?: string;
238
+ /**
239
+ OpenAI project.
240
+ */
241
+ readonly project?: string;
242
+ /**
243
+ Custom headers to include in the requests.
244
+ */
245
+ readonly headers?: Record<string, string>;
246
+ /**
247
+ * Creates a new OpenAI provider instance.
248
+ */
249
+ constructor(options?: OpenAIProviderSettings);
250
+ private get baseConfig();
251
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
252
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
253
+ }
254
+
255
+ export { OpenAI, type OpenAIProvider, type OpenAIProviderSettings, createOpenAI, openai };