@ai-sdk/openai 0.0.0-85f9a635-20240518005312 → 0.0.0-98261322-20260122142521

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.
Files changed (123) hide show
  1. package/CHANGELOG.md +2910 -0
  2. package/README.md +11 -173
  3. package/dist/index.d.mts +870 -187
  4. package/dist/index.d.ts +870 -187
  5. package/dist/index.js +5467 -617
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +5555 -619
  8. package/dist/index.mjs.map +1 -1
  9. package/dist/internal/index.d.mts +1067 -0
  10. package/dist/internal/index.d.ts +1067 -0
  11. package/dist/internal/index.js +5742 -0
  12. package/dist/internal/index.js.map +1 -0
  13. package/dist/internal/index.mjs +5788 -0
  14. package/dist/internal/index.mjs.map +1 -0
  15. package/docs/03-openai.mdx +2018 -0
  16. package/internal.d.ts +1 -0
  17. package/package.json +30 -18
  18. package/src/chat/__fixtures__/azure-model-router.1.chunks.txt +8 -0
  19. package/src/chat/__snapshots__/openai-chat-language-model.test.ts.snap +88 -0
  20. package/src/chat/convert-openai-chat-usage.ts +57 -0
  21. package/src/chat/convert-to-openai-chat-messages.test.ts +516 -0
  22. package/src/chat/convert-to-openai-chat-messages.ts +225 -0
  23. package/src/chat/get-response-metadata.ts +15 -0
  24. package/src/chat/map-openai-finish-reason.ts +19 -0
  25. package/src/chat/openai-chat-api.ts +198 -0
  26. package/src/chat/openai-chat-language-model.test.ts +3496 -0
  27. package/src/chat/openai-chat-language-model.ts +700 -0
  28. package/src/chat/openai-chat-options.ts +186 -0
  29. package/src/chat/openai-chat-prepare-tools.test.ts +322 -0
  30. package/src/chat/openai-chat-prepare-tools.ts +84 -0
  31. package/src/chat/openai-chat-prompt.ts +70 -0
  32. package/src/completion/convert-openai-completion-usage.ts +46 -0
  33. package/src/completion/convert-to-openai-completion-prompt.ts +93 -0
  34. package/src/completion/get-response-metadata.ts +15 -0
  35. package/src/completion/map-openai-finish-reason.ts +19 -0
  36. package/src/completion/openai-completion-api.ts +81 -0
  37. package/src/completion/openai-completion-language-model.test.ts +752 -0
  38. package/src/completion/openai-completion-language-model.ts +336 -0
  39. package/src/completion/openai-completion-options.ts +58 -0
  40. package/src/embedding/__snapshots__/openai-embedding-model.test.ts.snap +43 -0
  41. package/src/embedding/openai-embedding-api.ts +13 -0
  42. package/src/embedding/openai-embedding-model.test.ts +146 -0
  43. package/src/embedding/openai-embedding-model.ts +95 -0
  44. package/src/embedding/openai-embedding-options.ts +30 -0
  45. package/src/image/openai-image-api.ts +35 -0
  46. package/src/image/openai-image-model.test.ts +722 -0
  47. package/src/image/openai-image-model.ts +305 -0
  48. package/src/image/openai-image-options.ts +28 -0
  49. package/src/index.ts +9 -0
  50. package/src/internal/index.ts +19 -0
  51. package/src/openai-config.ts +18 -0
  52. package/src/openai-error.test.ts +34 -0
  53. package/src/openai-error.ts +22 -0
  54. package/src/openai-language-model-capabilities.test.ts +93 -0
  55. package/src/openai-language-model-capabilities.ts +54 -0
  56. package/src/openai-provider.test.ts +98 -0
  57. package/src/openai-provider.ts +270 -0
  58. package/src/openai-tools.ts +114 -0
  59. package/src/responses/__fixtures__/openai-apply-patch-tool-delete.1.chunks.txt +5 -0
  60. package/src/responses/__fixtures__/openai-apply-patch-tool.1.chunks.txt +38 -0
  61. package/src/responses/__fixtures__/openai-apply-patch-tool.1.json +69 -0
  62. package/src/responses/__fixtures__/openai-code-interpreter-tool.1.chunks.txt +393 -0
  63. package/src/responses/__fixtures__/openai-code-interpreter-tool.1.json +137 -0
  64. package/src/responses/__fixtures__/openai-error.1.chunks.txt +4 -0
  65. package/src/responses/__fixtures__/openai-error.1.json +8 -0
  66. package/src/responses/__fixtures__/openai-file-search-tool.1.chunks.txt +94 -0
  67. package/src/responses/__fixtures__/openai-file-search-tool.1.json +89 -0
  68. package/src/responses/__fixtures__/openai-file-search-tool.2.chunks.txt +93 -0
  69. package/src/responses/__fixtures__/openai-file-search-tool.2.json +112 -0
  70. package/src/responses/__fixtures__/openai-image-generation-tool.1.chunks.txt +16 -0
  71. package/src/responses/__fixtures__/openai-image-generation-tool.1.json +96 -0
  72. package/src/responses/__fixtures__/openai-local-shell-tool.1.chunks.txt +7 -0
  73. package/src/responses/__fixtures__/openai-local-shell-tool.1.json +70 -0
  74. package/src/responses/__fixtures__/openai-mcp-tool-approval.1.chunks.txt +11 -0
  75. package/src/responses/__fixtures__/openai-mcp-tool-approval.1.json +169 -0
  76. package/src/responses/__fixtures__/openai-mcp-tool-approval.2.chunks.txt +123 -0
  77. package/src/responses/__fixtures__/openai-mcp-tool-approval.2.json +176 -0
  78. package/src/responses/__fixtures__/openai-mcp-tool-approval.3.chunks.txt +11 -0
  79. package/src/responses/__fixtures__/openai-mcp-tool-approval.3.json +169 -0
  80. package/src/responses/__fixtures__/openai-mcp-tool-approval.4.chunks.txt +84 -0
  81. package/src/responses/__fixtures__/openai-mcp-tool-approval.4.json +182 -0
  82. package/src/responses/__fixtures__/openai-mcp-tool.1.chunks.txt +373 -0
  83. package/src/responses/__fixtures__/openai-mcp-tool.1.json +159 -0
  84. package/src/responses/__fixtures__/openai-reasoning-encrypted-content.1.chunks.txt +110 -0
  85. package/src/responses/__fixtures__/openai-reasoning-encrypted-content.1.json +117 -0
  86. package/src/responses/__fixtures__/openai-shell-tool.1.chunks.txt +182 -0
  87. package/src/responses/__fixtures__/openai-shell-tool.1.json +73 -0
  88. package/src/responses/__fixtures__/openai-web-search-tool.1.chunks.txt +185 -0
  89. package/src/responses/__fixtures__/openai-web-search-tool.1.json +266 -0
  90. package/src/responses/__snapshots__/openai-responses-language-model.test.ts.snap +10955 -0
  91. package/src/responses/convert-openai-responses-usage.ts +53 -0
  92. package/src/responses/convert-to-openai-responses-input.test.ts +2976 -0
  93. package/src/responses/convert-to-openai-responses-input.ts +578 -0
  94. package/src/responses/map-openai-responses-finish-reason.ts +22 -0
  95. package/src/responses/openai-responses-api.test.ts +89 -0
  96. package/src/responses/openai-responses-api.ts +1086 -0
  97. package/src/responses/openai-responses-language-model.test.ts +6927 -0
  98. package/src/responses/openai-responses-language-model.ts +1932 -0
  99. package/src/responses/openai-responses-options.ts +312 -0
  100. package/src/responses/openai-responses-prepare-tools.test.ts +924 -0
  101. package/src/responses/openai-responses-prepare-tools.ts +264 -0
  102. package/src/responses/openai-responses-provider-metadata.ts +39 -0
  103. package/src/speech/openai-speech-api.ts +38 -0
  104. package/src/speech/openai-speech-model.test.ts +202 -0
  105. package/src/speech/openai-speech-model.ts +137 -0
  106. package/src/speech/openai-speech-options.ts +22 -0
  107. package/src/tool/apply-patch.ts +141 -0
  108. package/src/tool/code-interpreter.ts +104 -0
  109. package/src/tool/file-search.ts +145 -0
  110. package/src/tool/image-generation.ts +126 -0
  111. package/src/tool/local-shell.test-d.ts +20 -0
  112. package/src/tool/local-shell.ts +72 -0
  113. package/src/tool/mcp.ts +125 -0
  114. package/src/tool/shell.ts +85 -0
  115. package/src/tool/web-search-preview.ts +139 -0
  116. package/src/tool/web-search.test-d.ts +13 -0
  117. package/src/tool/web-search.ts +179 -0
  118. package/src/transcription/openai-transcription-api.ts +37 -0
  119. package/src/transcription/openai-transcription-model.test.ts +507 -0
  120. package/src/transcription/openai-transcription-model.ts +232 -0
  121. package/src/transcription/openai-transcription-options.ts +50 -0
  122. package/src/transcription/transcription-test.mp3 +0 -0
  123. package/src/version.ts +6 -0
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # Vercel AI SDK - OpenAI Provider
1
+ # AI SDK - OpenAI Provider
2
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.
3
+ The **[OpenAI provider](https://ai-sdk.dev/providers/ai-sdk-providers/openai)** for the [AI SDK](https://ai-sdk.dev/docs)
4
+ contains language model support for the OpenAI chat and completion APIs and embedding model support for the OpenAI embeddings API.
5
5
 
6
6
  ## Setup
7
7
 
@@ -19,180 +19,18 @@ You can import the default provider instance `openai` from `@ai-sdk/openai`:
19
19
  import { openai } from '@ai-sdk/openai';
20
20
  ```
21
21
 
22
- If you need a customized setup, you can import `createOpenAI` from `@ai-sdk/openai` and create a provider instance with your settings:
22
+ ## Example
23
23
 
24
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:
25
+ import { openai } from '@ai-sdk/openai';
26
+ import { generateText } from 'ai';
145
27
 
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
28
+ const { text } = await generateText({
29
+ model: openai('gpt-4-turbo'),
30
+ prompt: 'Write a vegetarian lasagna recipe for 4 people.',
155
31
  });
156
32
  ```
157
33
 
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_
34
+ ## Documentation
196
35
 
197
- A unique identifier representing your end-user, which can help OpenAI to
198
- monitor and detect abuse. Learn more.
36
+ Please check out the **[OpenAI provider documentation](https://ai-sdk.dev/providers/ai-sdk-providers/openai)** for more information.