@ai-sdk/openai 0.0.0

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 ADDED
@@ -0,0 +1,80 @@
1
+ # Vercel AI SDK - OpenAI Provider
2
+
3
+ The OpenAI provider 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 `OpenAI` from `ai/openai` and initialize a provider instance with various settings:
17
+
18
+ ```ts
19
+ import { OpenAI } from '@ai-sdk/openai'
20
+
21
+ const openai = new OpenAI({
22
+ baseUrl: '', // optional base URL for proxies etc.
23
+ apiKey: '' // optional API key, default to env property OPENAI_API_KEY
24
+ organization: '' // optional organization
25
+ })
26
+ ```
27
+
28
+ The AI SDK also provides a shorthand `openai` import with an OpenAI provider instance that uses defaults:
29
+
30
+ ```ts
31
+ import { openai } from '@ai-sdk/openai';
32
+ ```
33
+
34
+ ## Chat Models
35
+
36
+ You can create models that call the [OpenAI chat API](https://platform.openai.com/docs/api-reference/chat) using the `.chat()` factory method.
37
+ The first argument is the model id, e.g. `gpt-4`.
38
+ The OpenAI chat models support tool calls and some have multi-modal capabilities.
39
+
40
+ ```ts
41
+ const model = openai.chat('gpt-3.5-turbo');
42
+ ```
43
+
44
+ OpenAI chat models support also some model specific settings that are not part of the [standard call settings](/docs/ai-core/settings).
45
+ You can pass them as an options argument:
46
+
47
+ ```ts
48
+ const model = openai.chat('gpt-3.5-turbo', {
49
+ logitBias: {
50
+ // optional likelihood for specific tokens
51
+ '50256': -100,
52
+ },
53
+ user: 'test-user', // optional unique user identifier
54
+ });
55
+ ```
56
+
57
+ ## Completion Models
58
+
59
+ You can create models that call the [OpenAI completions API](https://platform.openai.com/docs/api-reference/completions) using the `.completion()` factory method.
60
+ The first argument is the model id.
61
+ Currently only `gpt-3.5-turbo-instruct` is supported.
62
+
63
+ ```ts
64
+ const model = openai.completion('gpt-3.5-turbo-instruct');
65
+ ```
66
+
67
+ OpenAI completion models support also some model specific settings that are not part of the [standard call settings](/docs/ai-core/settings).
68
+ You can pass them as an options argument:
69
+
70
+ ```ts
71
+ const model = openai.chat('gpt-3.5-turbo', {
72
+ echo: true, // optional, echo the prompt in addition to the completion
73
+ logitBias: {
74
+ // optional likelihood for specific tokens
75
+ '50256': -100,
76
+ },
77
+ suffix: 'some text', // optional suffix that comes after a completion of inserted text
78
+ user: 'test-user', // optional unique user identifier
79
+ });
80
+ ```
@@ -0,0 +1,116 @@
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
+ * OpenAI provider.
97
+ */
98
+ declare class OpenAI {
99
+ readonly baseUrl?: string;
100
+ readonly apiKey?: string;
101
+ readonly organization?: string;
102
+ constructor(options?: {
103
+ baseUrl?: string;
104
+ apiKey?: string;
105
+ organization?: string;
106
+ });
107
+ private get baseConfig();
108
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
109
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
110
+ }
111
+ /**
112
+ * Default OpenAI provider instance.
113
+ */
114
+ declare const openai: OpenAI;
115
+
116
+ export { OpenAI, openai };
@@ -0,0 +1,116 @@
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
+ * OpenAI provider.
97
+ */
98
+ declare class OpenAI {
99
+ readonly baseUrl?: string;
100
+ readonly apiKey?: string;
101
+ readonly organization?: string;
102
+ constructor(options?: {
103
+ baseUrl?: string;
104
+ apiKey?: string;
105
+ organization?: string;
106
+ });
107
+ private get baseConfig();
108
+ chat(modelId: OpenAIChatModelId, settings?: OpenAIChatSettings): OpenAIChatLanguageModel;
109
+ completion(modelId: OpenAICompletionModelId, settings?: OpenAICompletionSettings): OpenAICompletionLanguageModel;
110
+ }
111
+ /**
112
+ * Default OpenAI provider instance.
113
+ */
114
+ declare const openai: OpenAI;
115
+
116
+ export { OpenAI, openai };