@ai-sdk/openai 0.0.1 → 0.0.3

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/README.md CHANGED
@@ -13,12 +13,12 @@ npm i @ai-sdk/openai
13
13
 
14
14
  ## Provider Instance
15
15
 
16
- You can import `OpenAI` from `ai/openai` and initialize a provider instance with various settings:
16
+ You can import `createOpenAI` from `@ai-sdk/openai` and create a provider instance with various settings:
17
17
 
18
18
  ```ts
19
- import { OpenAI } from '@ai-sdk/openai'
19
+ import { createOpenAI } from '@ai-sdk/openai'
20
20
 
21
- const openai = new OpenAI({
21
+ const openai = createOpenAI({
22
22
  baseURL: '', // optional base URL for proxies etc.
23
23
  apiKey: '' // optional API key, default to env property OPENAI_API_KEY
24
24
  organization: '' // optional organization
@@ -31,7 +31,19 @@ The AI SDK also provides a shorthand `openai` import with an OpenAI provider ins
31
31
  import { openai } from '@ai-sdk/openai';
32
32
  ```
33
33
 
34
- ## Chat Models
34
+ ## Models
35
+
36
+ The OpenAI provider instance is a function that you can invoke to create a model:
37
+
38
+ ```ts
39
+ const model = openai('gpt-3.5-turbo');
40
+ ```
41
+
42
+ It automatically selects the correct API based on the model id.
43
+
44
+ You can also provide model-specific parameters or select a model API by using `.chat` or `.completion`.
45
+
46
+ ### Chat Models
35
47
 
36
48
  You can create models that call the [OpenAI chat API](https://platform.openai.com/docs/api-reference/chat) using the `.chat()` factory method.
37
49
  The first argument is the model id, e.g. `gpt-4`.
@@ -54,7 +66,7 @@ const model = openai.chat('gpt-3.5-turbo', {
54
66
  });
55
67
  ```
56
68
 
57
- ## Completion Models
69
+ ### Completion Models
58
70
 
59
71
  You can create models that call the [OpenAI completions API](https://platform.openai.com/docs/api-reference/completions) using the `.completion()` factory method.
60
72
  The first argument is the model id.
@@ -68,7 +80,7 @@ OpenAI completion models support also some model specific settings that are not
68
80
  You can pass them as an options argument:
69
81
 
70
82
  ```ts
71
- const model = openai.chat('gpt-3.5-turbo', {
83
+ const model = openai.completion('gpt-3.5-turbo-instruct', {
72
84
  echo: true, // optional, echo the prompt in addition to the completion
73
85
  logitBias: {
74
86
  // optional likelihood for specific tokens
@@ -0,0 +1,163 @@
1
+ import { LanguageModelV1 } from '@ai-sdk/provider';
2
+
3
+ type OpenAIChatModelId = '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
+ * A unique identifier representing your end-user, which can help OpenAI to
22
+ * monitor and detect abuse. Learn more.
23
+ */
24
+ user?: string;
25
+ }
26
+
27
+ type OpenAIChatConfig = {
28
+ provider: string;
29
+ baseURL: string;
30
+ headers: () => Record<string, string | undefined>;
31
+ };
32
+ declare class OpenAIChatLanguageModel implements LanguageModelV1 {
33
+ readonly specificationVersion = "v1";
34
+ readonly defaultObjectGenerationMode = "tool";
35
+ readonly modelId: OpenAIChatModelId;
36
+ readonly settings: OpenAIChatSettings;
37
+ private readonly config;
38
+ constructor(modelId: OpenAIChatModelId, settings: OpenAIChatSettings, config: OpenAIChatConfig);
39
+ get provider(): string;
40
+ private getArgs;
41
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
42
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
43
+ }
44
+
45
+ type OpenAICompletionModelId = 'gpt-3.5-turbo-instruct' | (string & {});
46
+ interface OpenAICompletionSettings {
47
+ /**
48
+ * Echo back the prompt in addition to the completion
49
+ */
50
+ echo?: boolean;
51
+ /**
52
+ * Modify the likelihood of specified tokens appearing in the completion.
53
+ *
54
+ * Accepts a JSON object that maps tokens (specified by their token ID in
55
+ * the GPT tokenizer) to an associated bias value from -100 to 100. You
56
+ * can use this tokenizer tool to convert text to token IDs. Mathematically,
57
+ * the bias is added to the logits generated by the model prior to sampling.
58
+ * The exact effect will vary per model, but values between -1 and 1 should
59
+ * decrease or increase likelihood of selection; values like -100 or 100
60
+ * should result in a ban or exclusive selection of the relevant token.
61
+ *
62
+ * As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
63
+ * token from being generated.
64
+ */
65
+ logitBias?: Record<number, number>;
66
+ /**
67
+ * The suffix that comes after a completion of inserted text.
68
+ */
69
+ suffix?: string;
70
+ /**
71
+ * A unique identifier representing your end-user, which can help OpenAI to
72
+ * monitor and detect abuse. Learn more.
73
+ */
74
+ user?: string;
75
+ }
76
+
77
+ type OpenAICompletionConfig = {
78
+ provider: string;
79
+ baseURL: string;
80
+ headers: () => Record<string, string | undefined>;
81
+ };
82
+ declare class OpenAICompletionLanguageModel implements LanguageModelV1 {
83
+ readonly specificationVersion = "v1";
84
+ readonly defaultObjectGenerationMode: undefined;
85
+ readonly modelId: OpenAICompletionModelId;
86
+ readonly settings: OpenAICompletionSettings;
87
+ private readonly config;
88
+ constructor(modelId: OpenAICompletionModelId, settings: OpenAICompletionSettings, config: OpenAICompletionConfig);
89
+ get provider(): string;
90
+ private getArgs;
91
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
92
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
93
+ }
94
+
95
+ /**
96
+ * @deprecated Use `createOpenAI` instead.
97
+ */
98
+ declare class OpenAI {
99
+ /**
100
+ * Base URL for the OpenAI API calls.
101
+ */
102
+ readonly baseURL: string;
103
+ readonly apiKey?: string;
104
+ readonly organization?: string;
105
+ /**
106
+ * Creates a new OpenAI provider instance.
107
+ */
108
+ constructor(options?: {
109
+ /**
110
+ * Base URL for the OpenAI API calls.
111
+ */
112
+ baseURL?: string;
113
+ /**
114
+ * @deprecated Use `baseURL` instead.
115
+ */
116
+ baseUrl?: string;
117
+ /**
118
+ * API key for authenticating requests.
119
+ */
120
+ apiKey?: string;
121
+ /**
122
+ * Organization ID.
123
+ */
124
+ organization?: string;
125
+ });
126
+ private get baseConfig();
127
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
128
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
129
+ }
130
+
131
+ interface OpenAIProvider {
132
+ (modelId: 'gpt-3.5-turbo-instruct', settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
133
+ (modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
134
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
135
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
136
+ }
137
+ /**
138
+ * Create an OpenAI provider.
139
+ */
140
+ declare function createOpenAI(options?: {
141
+ /**
142
+ * Base URL for the OpenAI API calls.
143
+ */
144
+ baseURL?: string;
145
+ /**
146
+ * @deprecated Use `baseURL` instead.
147
+ */
148
+ baseUrl?: string;
149
+ /**
150
+ * API key for authenticating requests.
151
+ */
152
+ apiKey?: string;
153
+ /**
154
+ * Organization ID.
155
+ */
156
+ organization?: string;
157
+ }): OpenAIProvider;
158
+ /**
159
+ * Default OpenAI provider instance.
160
+ */
161
+ declare const openai: OpenAIProvider;
162
+
163
+ export { OpenAI, type OpenAIProvider, createOpenAI, openai };
@@ -0,0 +1,163 @@
1
+ import { LanguageModelV1 } from '@ai-sdk/provider';
2
+
3
+ type OpenAIChatModelId = '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
+ * A unique identifier representing your end-user, which can help OpenAI to
22
+ * monitor and detect abuse. Learn more.
23
+ */
24
+ user?: string;
25
+ }
26
+
27
+ type OpenAIChatConfig = {
28
+ provider: string;
29
+ baseURL: string;
30
+ headers: () => Record<string, string | undefined>;
31
+ };
32
+ declare class OpenAIChatLanguageModel implements LanguageModelV1 {
33
+ readonly specificationVersion = "v1";
34
+ readonly defaultObjectGenerationMode = "tool";
35
+ readonly modelId: OpenAIChatModelId;
36
+ readonly settings: OpenAIChatSettings;
37
+ private readonly config;
38
+ constructor(modelId: OpenAIChatModelId, settings: OpenAIChatSettings, config: OpenAIChatConfig);
39
+ get provider(): string;
40
+ private getArgs;
41
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
42
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
43
+ }
44
+
45
+ type OpenAICompletionModelId = 'gpt-3.5-turbo-instruct' | (string & {});
46
+ interface OpenAICompletionSettings {
47
+ /**
48
+ * Echo back the prompt in addition to the completion
49
+ */
50
+ echo?: boolean;
51
+ /**
52
+ * Modify the likelihood of specified tokens appearing in the completion.
53
+ *
54
+ * Accepts a JSON object that maps tokens (specified by their token ID in
55
+ * the GPT tokenizer) to an associated bias value from -100 to 100. You
56
+ * can use this tokenizer tool to convert text to token IDs. Mathematically,
57
+ * the bias is added to the logits generated by the model prior to sampling.
58
+ * The exact effect will vary per model, but values between -1 and 1 should
59
+ * decrease or increase likelihood of selection; values like -100 or 100
60
+ * should result in a ban or exclusive selection of the relevant token.
61
+ *
62
+ * As an example, you can pass {"50256": -100} to prevent the <|endoftext|>
63
+ * token from being generated.
64
+ */
65
+ logitBias?: Record<number, number>;
66
+ /**
67
+ * The suffix that comes after a completion of inserted text.
68
+ */
69
+ suffix?: string;
70
+ /**
71
+ * A unique identifier representing your end-user, which can help OpenAI to
72
+ * monitor and detect abuse. Learn more.
73
+ */
74
+ user?: string;
75
+ }
76
+
77
+ type OpenAICompletionConfig = {
78
+ provider: string;
79
+ baseURL: string;
80
+ headers: () => Record<string, string | undefined>;
81
+ };
82
+ declare class OpenAICompletionLanguageModel implements LanguageModelV1 {
83
+ readonly specificationVersion = "v1";
84
+ readonly defaultObjectGenerationMode: undefined;
85
+ readonly modelId: OpenAICompletionModelId;
86
+ readonly settings: OpenAICompletionSettings;
87
+ private readonly config;
88
+ constructor(modelId: OpenAICompletionModelId, settings: OpenAICompletionSettings, config: OpenAICompletionConfig);
89
+ get provider(): string;
90
+ private getArgs;
91
+ doGenerate(options: Parameters<LanguageModelV1['doGenerate']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doGenerate']>>>;
92
+ doStream(options: Parameters<LanguageModelV1['doStream']>[0]): Promise<Awaited<ReturnType<LanguageModelV1['doStream']>>>;
93
+ }
94
+
95
+ /**
96
+ * @deprecated Use `createOpenAI` instead.
97
+ */
98
+ declare class OpenAI {
99
+ /**
100
+ * Base URL for the OpenAI API calls.
101
+ */
102
+ readonly baseURL: string;
103
+ readonly apiKey?: string;
104
+ readonly organization?: string;
105
+ /**
106
+ * Creates a new OpenAI provider instance.
107
+ */
108
+ constructor(options?: {
109
+ /**
110
+ * Base URL for the OpenAI API calls.
111
+ */
112
+ baseURL?: string;
113
+ /**
114
+ * @deprecated Use `baseURL` instead.
115
+ */
116
+ baseUrl?: string;
117
+ /**
118
+ * API key for authenticating requests.
119
+ */
120
+ apiKey?: string;
121
+ /**
122
+ * Organization ID.
123
+ */
124
+ organization?: string;
125
+ });
126
+ private get baseConfig();
127
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
128
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
129
+ }
130
+
131
+ interface OpenAIProvider {
132
+ (modelId: 'gpt-3.5-turbo-instruct', settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
133
+ (modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
134
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
135
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
136
+ }
137
+ /**
138
+ * Create an OpenAI provider.
139
+ */
140
+ declare function createOpenAI(options?: {
141
+ /**
142
+ * Base URL for the OpenAI API calls.
143
+ */
144
+ baseURL?: string;
145
+ /**
146
+ * @deprecated Use `baseURL` instead.
147
+ */
148
+ baseUrl?: string;
149
+ /**
150
+ * API key for authenticating requests.
151
+ */
152
+ apiKey?: string;
153
+ /**
154
+ * Organization ID.
155
+ */
156
+ organization?: string;
157
+ }): OpenAIProvider;
158
+ /**
159
+ * Default OpenAI provider instance.
160
+ */
161
+ declare const openai: OpenAIProvider;
162
+
163
+ export { OpenAI, type OpenAIProvider, createOpenAI, openai };