langchain 0.0.196 → 0.0.197-rc.1

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 (225) hide show
  1. package/LICENSE +21 -0
  2. package/dist/agents/executor.cjs +351 -1
  3. package/dist/agents/executor.d.ts +61 -3
  4. package/dist/agents/executor.js +350 -1
  5. package/dist/agents/openai/index.cjs +6 -2
  6. package/dist/agents/openai/index.js +6 -2
  7. package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
  8. package/dist/base_language/count_tokens.cjs +4 -4
  9. package/dist/base_language/count_tokens.d.ts +1 -1
  10. package/dist/base_language/count_tokens.js +1 -1
  11. package/dist/base_language/index.cjs +3 -3
  12. package/dist/base_language/index.d.ts +1 -1
  13. package/dist/base_language/index.js +1 -1
  14. package/dist/cache/base.cjs +1 -1
  15. package/dist/cache/base.d.ts +1 -1
  16. package/dist/cache/base.js +1 -1
  17. package/dist/cache/index.cjs +2 -2
  18. package/dist/cache/index.d.ts +1 -1
  19. package/dist/cache/index.js +1 -1
  20. package/dist/callbacks/base.cjs +1 -1
  21. package/dist/callbacks/base.d.ts +1 -1
  22. package/dist/callbacks/base.js +1 -1
  23. package/dist/callbacks/handlers/console.cjs +1 -1
  24. package/dist/callbacks/handlers/console.d.ts +1 -1
  25. package/dist/callbacks/handlers/console.js +1 -1
  26. package/dist/callbacks/handlers/initialize.cjs +1 -1
  27. package/dist/callbacks/handlers/initialize.d.ts +1 -1
  28. package/dist/callbacks/handlers/initialize.js +1 -1
  29. package/dist/callbacks/handlers/log_stream.cjs +1 -1
  30. package/dist/callbacks/handlers/log_stream.d.ts +1 -1
  31. package/dist/callbacks/handlers/log_stream.js +1 -1
  32. package/dist/callbacks/handlers/run_collector.cjs +1 -1
  33. package/dist/callbacks/handlers/run_collector.d.ts +1 -1
  34. package/dist/callbacks/handlers/run_collector.js +1 -1
  35. package/dist/callbacks/handlers/tracer.cjs +1 -1
  36. package/dist/callbacks/handlers/tracer.d.ts +1 -1
  37. package/dist/callbacks/handlers/tracer.js +1 -1
  38. package/dist/callbacks/handlers/tracer_langchain.cjs +1 -1
  39. package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -1
  40. package/dist/callbacks/handlers/tracer_langchain.js +1 -1
  41. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +1 -1
  42. package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -1
  43. package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -1
  44. package/dist/callbacks/manager.cjs +1 -1
  45. package/dist/callbacks/manager.d.ts +1 -1
  46. package/dist/callbacks/manager.js +1 -1
  47. package/dist/callbacks/promises.cjs +1 -1
  48. package/dist/callbacks/promises.d.ts +1 -1
  49. package/dist/callbacks/promises.js +1 -1
  50. package/dist/chains/base.cjs +16 -0
  51. package/dist/chains/base.d.ts +2 -0
  52. package/dist/chains/base.js +16 -0
  53. package/dist/chains/openai_functions/structured_output.cjs +1 -1
  54. package/dist/chains/openai_functions/structured_output.d.ts +1 -1
  55. package/dist/chains/openai_functions/structured_output.js +1 -1
  56. package/dist/chains/openai_moderation.cjs +2 -2
  57. package/dist/chains/openai_moderation.d.ts +1 -1
  58. package/dist/chains/openai_moderation.js +1 -1
  59. package/dist/chat_models/anthropic.cjs +15 -348
  60. package/dist/chat_models/anthropic.d.ts +1 -156
  61. package/dist/chat_models/anthropic.js +1 -346
  62. package/dist/chat_models/base.cjs +1 -1
  63. package/dist/chat_models/base.d.ts +1 -1
  64. package/dist/chat_models/base.js +1 -1
  65. package/dist/chat_models/bedrock/web.cjs +21 -1
  66. package/dist/chat_models/bedrock/web.d.ts +1 -1
  67. package/dist/chat_models/bedrock/web.js +21 -1
  68. package/dist/chat_models/fireworks.d.ts +2 -2
  69. package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
  70. package/dist/chat_models/minimax.d.ts +2 -2
  71. package/dist/chat_models/openai.cjs +4 -698
  72. package/dist/chat_models/openai.d.ts +4 -137
  73. package/dist/chat_models/openai.js +2 -695
  74. package/dist/document.cjs +2 -2
  75. package/dist/document.d.ts +1 -1
  76. package/dist/document.js +1 -1
  77. package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
  78. package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
  79. package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
  80. package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
  81. package/dist/document_loaders/web/github.cjs +105 -0
  82. package/dist/document_loaders/web/github.d.ts +26 -0
  83. package/dist/document_loaders/web/github.js +105 -0
  84. package/dist/document_loaders/web/s3.d.ts +1 -1
  85. package/dist/embeddings/base.cjs +1 -1
  86. package/dist/embeddings/base.d.ts +1 -1
  87. package/dist/embeddings/base.js +1 -1
  88. package/dist/embeddings/cache_backed.cjs +1 -1
  89. package/dist/embeddings/cache_backed.js +1 -1
  90. package/dist/embeddings/openai.cjs +2 -240
  91. package/dist/embeddings/openai.d.ts +1 -82
  92. package/dist/embeddings/openai.js +1 -239
  93. package/dist/experimental/openai_assistant/index.cjs +3 -3
  94. package/dist/experimental/openai_assistant/index.d.ts +1 -1
  95. package/dist/experimental/openai_assistant/index.js +1 -1
  96. package/dist/experimental/openai_assistant/schema.d.ts +1 -1
  97. package/dist/experimental/openai_files/index.cjs +2 -2
  98. package/dist/experimental/openai_files/index.d.ts +1 -1
  99. package/dist/experimental/openai_files/index.js +1 -1
  100. package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
  101. package/dist/llms/base.cjs +1 -1
  102. package/dist/llms/base.d.ts +1 -1
  103. package/dist/llms/base.js +1 -1
  104. package/dist/llms/bedrock/web.cjs +21 -1
  105. package/dist/llms/bedrock/web.d.ts +1 -1
  106. package/dist/llms/bedrock/web.js +21 -1
  107. package/dist/llms/fireworks.d.ts +2 -2
  108. package/dist/llms/openai-chat.cjs +3 -445
  109. package/dist/llms/openai-chat.d.ts +4 -123
  110. package/dist/llms/openai-chat.js +2 -443
  111. package/dist/llms/openai.cjs +6 -530
  112. package/dist/llms/openai.d.ts +4 -123
  113. package/dist/llms/openai.js +2 -525
  114. package/dist/load/index.cjs +4 -2
  115. package/dist/load/index.d.ts +2 -2
  116. package/dist/load/index.js +4 -2
  117. package/dist/load/serializable.cjs +1 -1
  118. package/dist/load/serializable.d.ts +1 -1
  119. package/dist/load/serializable.js +1 -1
  120. package/dist/memory/base.cjs +2 -2
  121. package/dist/memory/base.d.ts +2 -2
  122. package/dist/memory/base.js +2 -2
  123. package/dist/output_parsers/list.cjs +4 -122
  124. package/dist/output_parsers/list.d.ts +1 -57
  125. package/dist/output_parsers/list.js +1 -119
  126. package/dist/output_parsers/openai_functions.cjs +1 -1
  127. package/dist/output_parsers/openai_functions.d.ts +1 -1
  128. package/dist/output_parsers/openai_functions.js +1 -1
  129. package/dist/output_parsers/structured.d.ts +1 -1
  130. package/dist/prompts/base.cjs +8 -8
  131. package/dist/prompts/base.d.ts +3 -3
  132. package/dist/prompts/base.js +3 -3
  133. package/dist/prompts/chat.cjs +13 -15
  134. package/dist/prompts/chat.d.ts +2 -1
  135. package/dist/prompts/chat.js +2 -1
  136. package/dist/prompts/few_shot.cjs +4 -15
  137. package/dist/prompts/few_shot.d.ts +1 -1
  138. package/dist/prompts/few_shot.js +1 -1
  139. package/dist/prompts/index.cjs +2 -2
  140. package/dist/prompts/index.d.ts +1 -1
  141. package/dist/prompts/index.js +1 -1
  142. package/dist/prompts/pipeline.cjs +3 -15
  143. package/dist/prompts/pipeline.d.ts +1 -1
  144. package/dist/prompts/pipeline.js +1 -1
  145. package/dist/prompts/prompt.cjs +3 -15
  146. package/dist/prompts/prompt.d.ts +1 -1
  147. package/dist/prompts/prompt.js +1 -1
  148. package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +3 -15
  149. package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -1
  150. package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -1
  151. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +1 -1
  152. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -1
  153. package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -1
  154. package/dist/prompts/selectors/conditional.cjs +6 -15
  155. package/dist/prompts/selectors/conditional.d.ts +1 -1
  156. package/dist/prompts/selectors/conditional.js +1 -1
  157. package/dist/prompts/serde.cjs +0 -15
  158. package/dist/prompts/serde.d.ts +1 -1
  159. package/dist/prompts/serde.js +1 -1
  160. package/dist/prompts/template.cjs +9 -15
  161. package/dist/prompts/template.d.ts +1 -1
  162. package/dist/prompts/template.js +1 -1
  163. package/dist/runnables/index.cjs +1 -1
  164. package/dist/runnables/index.d.ts +1 -1
  165. package/dist/runnables/index.js +1 -1
  166. package/dist/schema/document.cjs +3 -3
  167. package/dist/schema/document.d.ts +1 -1
  168. package/dist/schema/document.js +1 -1
  169. package/dist/schema/index.cjs +12 -12
  170. package/dist/schema/index.d.ts +11 -11
  171. package/dist/schema/index.js +7 -7
  172. package/dist/schema/output_parser.cjs +1 -1
  173. package/dist/schema/output_parser.d.ts +1 -1
  174. package/dist/schema/output_parser.js +1 -1
  175. package/dist/schema/retriever.cjs +1 -1
  176. package/dist/schema/retriever.d.ts +1 -1
  177. package/dist/schema/retriever.js +1 -1
  178. package/dist/schema/runnable/base.cjs +1 -1
  179. package/dist/schema/runnable/base.d.ts +1 -1
  180. package/dist/schema/runnable/base.js +1 -1
  181. package/dist/schema/runnable/branch.cjs +1 -1
  182. package/dist/schema/runnable/branch.d.ts +1 -1
  183. package/dist/schema/runnable/branch.js +1 -1
  184. package/dist/schema/runnable/config.d.ts +1 -1
  185. package/dist/schema/runnable/index.cjs +1 -1
  186. package/dist/schema/runnable/index.d.ts +1 -1
  187. package/dist/schema/runnable/index.js +1 -1
  188. package/dist/schema/runnable/passthrough.cjs +1 -1
  189. package/dist/schema/runnable/passthrough.d.ts +1 -1
  190. package/dist/schema/runnable/passthrough.js +1 -1
  191. package/dist/schema/runnable/router.cjs +1 -1
  192. package/dist/schema/runnable/router.d.ts +1 -1
  193. package/dist/schema/runnable/router.js +1 -1
  194. package/dist/schema/storage.cjs +1 -1
  195. package/dist/schema/storage.d.ts +1 -1
  196. package/dist/schema/storage.js +1 -1
  197. package/dist/tools/base.cjs +4 -108
  198. package/dist/tools/base.d.ts +1 -72
  199. package/dist/tools/base.js +1 -105
  200. package/dist/tools/bingserpapi.d.ts +1 -1
  201. package/dist/tools/convert_to_openai.cjs +4 -38
  202. package/dist/tools/convert_to_openai.d.ts +1 -11
  203. package/dist/tools/convert_to_openai.js +1 -35
  204. package/dist/tools/searchapi.d.ts +1 -1
  205. package/dist/tools/serpapi.d.ts +1 -1
  206. package/dist/tools/serper.d.ts +1 -1
  207. package/dist/types/openai-types.d.ts +1 -133
  208. package/dist/util/async_caller.cjs +1 -1
  209. package/dist/util/async_caller.d.ts +1 -1
  210. package/dist/util/async_caller.js +1 -1
  211. package/dist/util/openai.cjs +2 -18
  212. package/dist/util/openai.d.ts +1 -1
  213. package/dist/util/openai.js +1 -17
  214. package/dist/util/openapi.d.ts +2 -2
  215. package/dist/util/prompt-layer.d.ts +1 -1
  216. package/dist/vectorstores/momento_vector_index.cjs +39 -0
  217. package/dist/vectorstores/momento_vector_index.d.ts +17 -1
  218. package/dist/vectorstores/momento_vector_index.js +40 -1
  219. package/dist/vectorstores/mongodb_atlas.cjs +22 -2
  220. package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
  221. package/dist/vectorstores/mongodb_atlas.js +22 -2
  222. package/package.json +12 -10
  223. package/dist/util/openai-format-fndef.cjs +0 -81
  224. package/dist/util/openai-format-fndef.d.ts +0 -44
  225. package/dist/util/openai-format-fndef.js +0 -77
@@ -1,448 +1,7 @@
1
- import { OpenAI as OpenAIClient } from "openai";
2
- import { GenerationChunk } from "../schema/index.js";
3
- import { getEndpoint } from "../util/azure.js";
1
+ import { OpenAIChat } from "@langchain/openai";
4
2
  import { getEnvironmentVariable } from "../util/env.js";
5
3
  import { promptLayerTrackRequest } from "../util/prompt-layer.js";
6
- import { LLM } from "./base.js";
7
- import { wrapOpenAIClientError } from "../util/openai.js";
8
- /**
9
- * Wrapper around OpenAI large language models that use the Chat endpoint.
10
- *
11
- * To use you should have the `openai` package installed, with the
12
- * `OPENAI_API_KEY` environment variable set.
13
- *
14
- * To use with Azure you should have the `openai` package installed, with the
15
- * `AZURE_OPENAI_API_KEY`,
16
- * `AZURE_OPENAI_API_INSTANCE_NAME`,
17
- * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
18
- * and `AZURE_OPENAI_API_VERSION` environment variable set.
19
- *
20
- * @remarks
21
- * Any parameters that are valid to be passed to {@link
22
- * https://platform.openai.com/docs/api-reference/chat/create |
23
- * `openai.createCompletion`} can be passed through {@link modelKwargs}, even
24
- * if not explicitly available on this class.
25
- *
26
- * @augments BaseLLM
27
- * @augments OpenAIInput
28
- * @augments AzureOpenAIChatInput
29
- * @example
30
- * ```typescript
31
- * const model = new OpenAIChat({
32
- * prefixMessages: [
33
- * {
34
- * role: "system",
35
- * content: "You are a helpful assistant that answers in pirate language",
36
- * },
37
- * ],
38
- * maxTokens: 50,
39
- * });
40
- *
41
- * const res = await model.call(
42
- * "What would be a good company name for a company that makes colorful socks?"
43
- * );
44
- * console.log({ res });
45
- * ```
46
- */
47
- export class OpenAIChat extends LLM {
48
- static lc_name() {
49
- return "OpenAIChat";
50
- }
51
- get callKeys() {
52
- return [...super.callKeys, "options", "promptIndex"];
53
- }
54
- get lc_secrets() {
55
- return {
56
- openAIApiKey: "OPENAI_API_KEY",
57
- azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
58
- organization: "OPENAI_ORGANIZATION",
59
- };
60
- }
61
- get lc_aliases() {
62
- return {
63
- modelName: "model",
64
- openAIApiKey: "openai_api_key",
65
- azureOpenAIApiVersion: "azure_openai_api_version",
66
- azureOpenAIApiKey: "azure_openai_api_key",
67
- azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
68
- azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
69
- };
70
- }
71
- constructor(fields,
72
- /** @deprecated */
73
- configuration) {
74
- super(fields ?? {});
75
- Object.defineProperty(this, "lc_serializable", {
76
- enumerable: true,
77
- configurable: true,
78
- writable: true,
79
- value: true
80
- });
81
- Object.defineProperty(this, "temperature", {
82
- enumerable: true,
83
- configurable: true,
84
- writable: true,
85
- value: 1
86
- });
87
- Object.defineProperty(this, "topP", {
88
- enumerable: true,
89
- configurable: true,
90
- writable: true,
91
- value: 1
92
- });
93
- Object.defineProperty(this, "frequencyPenalty", {
94
- enumerable: true,
95
- configurable: true,
96
- writable: true,
97
- value: 0
98
- });
99
- Object.defineProperty(this, "presencePenalty", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: 0
104
- });
105
- Object.defineProperty(this, "n", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: 1
110
- });
111
- Object.defineProperty(this, "logitBias", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: void 0
116
- });
117
- Object.defineProperty(this, "maxTokens", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: void 0
122
- });
123
- Object.defineProperty(this, "modelName", {
124
- enumerable: true,
125
- configurable: true,
126
- writable: true,
127
- value: "gpt-3.5-turbo"
128
- });
129
- Object.defineProperty(this, "prefixMessages", {
130
- enumerable: true,
131
- configurable: true,
132
- writable: true,
133
- value: void 0
134
- });
135
- Object.defineProperty(this, "modelKwargs", {
136
- enumerable: true,
137
- configurable: true,
138
- writable: true,
139
- value: void 0
140
- });
141
- Object.defineProperty(this, "timeout", {
142
- enumerable: true,
143
- configurable: true,
144
- writable: true,
145
- value: void 0
146
- });
147
- Object.defineProperty(this, "stop", {
148
- enumerable: true,
149
- configurable: true,
150
- writable: true,
151
- value: void 0
152
- });
153
- Object.defineProperty(this, "user", {
154
- enumerable: true,
155
- configurable: true,
156
- writable: true,
157
- value: void 0
158
- });
159
- Object.defineProperty(this, "streaming", {
160
- enumerable: true,
161
- configurable: true,
162
- writable: true,
163
- value: false
164
- });
165
- Object.defineProperty(this, "openAIApiKey", {
166
- enumerable: true,
167
- configurable: true,
168
- writable: true,
169
- value: void 0
170
- });
171
- Object.defineProperty(this, "azureOpenAIApiVersion", {
172
- enumerable: true,
173
- configurable: true,
174
- writable: true,
175
- value: void 0
176
- });
177
- Object.defineProperty(this, "azureOpenAIApiKey", {
178
- enumerable: true,
179
- configurable: true,
180
- writable: true,
181
- value: void 0
182
- });
183
- Object.defineProperty(this, "azureOpenAIApiInstanceName", {
184
- enumerable: true,
185
- configurable: true,
186
- writable: true,
187
- value: void 0
188
- });
189
- Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
190
- enumerable: true,
191
- configurable: true,
192
- writable: true,
193
- value: void 0
194
- });
195
- Object.defineProperty(this, "azureOpenAIBasePath", {
196
- enumerable: true,
197
- configurable: true,
198
- writable: true,
199
- value: void 0
200
- });
201
- Object.defineProperty(this, "organization", {
202
- enumerable: true,
203
- configurable: true,
204
- writable: true,
205
- value: void 0
206
- });
207
- Object.defineProperty(this, "client", {
208
- enumerable: true,
209
- configurable: true,
210
- writable: true,
211
- value: void 0
212
- });
213
- Object.defineProperty(this, "clientConfig", {
214
- enumerable: true,
215
- configurable: true,
216
- writable: true,
217
- value: void 0
218
- });
219
- this.openAIApiKey =
220
- fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
221
- this.azureOpenAIApiKey =
222
- fields?.azureOpenAIApiKey ??
223
- getEnvironmentVariable("AZURE_OPENAI_API_KEY");
224
- if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
225
- throw new Error("OpenAI or Azure OpenAI API key not found");
226
- }
227
- this.azureOpenAIApiInstanceName =
228
- fields?.azureOpenAIApiInstanceName ??
229
- getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
230
- this.azureOpenAIApiDeploymentName =
231
- (fields?.azureOpenAIApiCompletionsDeploymentName ||
232
- fields?.azureOpenAIApiDeploymentName) ??
233
- (getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
234
- getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
235
- this.azureOpenAIApiVersion =
236
- fields?.azureOpenAIApiVersion ??
237
- getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
238
- this.azureOpenAIBasePath =
239
- fields?.azureOpenAIBasePath ??
240
- getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
241
- this.organization =
242
- fields?.configuration?.organization ??
243
- getEnvironmentVariable("OPENAI_ORGANIZATION");
244
- this.modelName = fields?.modelName ?? this.modelName;
245
- this.prefixMessages = fields?.prefixMessages ?? this.prefixMessages;
246
- this.modelKwargs = fields?.modelKwargs ?? {};
247
- this.timeout = fields?.timeout;
248
- this.temperature = fields?.temperature ?? this.temperature;
249
- this.topP = fields?.topP ?? this.topP;
250
- this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
251
- this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
252
- this.n = fields?.n ?? this.n;
253
- this.logitBias = fields?.logitBias;
254
- this.maxTokens = fields?.maxTokens;
255
- this.stop = fields?.stop;
256
- this.user = fields?.user;
257
- this.streaming = fields?.streaming ?? false;
258
- if (this.n > 1) {
259
- throw new Error("Cannot use n > 1 in OpenAIChat LLM. Use ChatOpenAI Chat Model instead.");
260
- }
261
- if (this.azureOpenAIApiKey) {
262
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
263
- throw new Error("Azure OpenAI API instance name not found");
264
- }
265
- if (!this.azureOpenAIApiDeploymentName) {
266
- throw new Error("Azure OpenAI API deployment name not found");
267
- }
268
- if (!this.azureOpenAIApiVersion) {
269
- throw new Error("Azure OpenAI API version not found");
270
- }
271
- this.openAIApiKey = this.openAIApiKey ?? "";
272
- }
273
- this.clientConfig = {
274
- apiKey: this.openAIApiKey,
275
- organization: this.organization,
276
- baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
277
- dangerouslyAllowBrowser: true,
278
- defaultHeaders: configuration?.baseOptions?.headers ??
279
- fields?.configuration?.baseOptions?.headers,
280
- defaultQuery: configuration?.baseOptions?.params ??
281
- fields?.configuration?.baseOptions?.params,
282
- ...configuration,
283
- ...fields?.configuration,
284
- };
285
- }
286
- /**
287
- * Get the parameters used to invoke the model
288
- */
289
- invocationParams(options) {
290
- return {
291
- model: this.modelName,
292
- temperature: this.temperature,
293
- top_p: this.topP,
294
- frequency_penalty: this.frequencyPenalty,
295
- presence_penalty: this.presencePenalty,
296
- n: this.n,
297
- logit_bias: this.logitBias,
298
- max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
299
- stop: options?.stop ?? this.stop,
300
- user: this.user,
301
- stream: this.streaming,
302
- ...this.modelKwargs,
303
- };
304
- }
305
- /** @ignore */
306
- _identifyingParams() {
307
- return {
308
- model_name: this.modelName,
309
- ...this.invocationParams(),
310
- ...this.clientConfig,
311
- };
312
- }
313
- /**
314
- * Get the identifying parameters for the model
315
- */
316
- identifyingParams() {
317
- return {
318
- model_name: this.modelName,
319
- ...this.invocationParams(),
320
- ...this.clientConfig,
321
- };
322
- }
323
- /**
324
- * Formats the messages for the OpenAI API.
325
- * @param prompt The prompt to be formatted.
326
- * @returns Array of formatted messages.
327
- */
328
- formatMessages(prompt) {
329
- const message = {
330
- role: "user",
331
- content: prompt,
332
- };
333
- return this.prefixMessages ? [...this.prefixMessages, message] : [message];
334
- }
335
- async *_streamResponseChunks(prompt, options, runManager) {
336
- const params = {
337
- ...this.invocationParams(options),
338
- messages: this.formatMessages(prompt),
339
- stream: true,
340
- };
341
- const stream = await this.completionWithRetry(params, options);
342
- for await (const data of stream) {
343
- const choice = data?.choices[0];
344
- if (!choice) {
345
- continue;
346
- }
347
- const { delta } = choice;
348
- const generationChunk = new GenerationChunk({
349
- text: delta.content ?? "",
350
- });
351
- yield generationChunk;
352
- const newTokenIndices = {
353
- prompt: options.promptIndex ?? 0,
354
- completion: choice.index ?? 0,
355
- };
356
- // eslint-disable-next-line no-void
357
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices);
358
- }
359
- if (options.signal?.aborted) {
360
- throw new Error("AbortError");
361
- }
362
- }
363
- /** @ignore */
364
- async _call(prompt, options, runManager) {
365
- const params = this.invocationParams(options);
366
- if (params.stream) {
367
- const stream = await this._streamResponseChunks(prompt, options, runManager);
368
- let finalChunk;
369
- for await (const chunk of stream) {
370
- if (finalChunk === undefined) {
371
- finalChunk = chunk;
372
- }
373
- else {
374
- finalChunk = finalChunk.concat(chunk);
375
- }
376
- }
377
- return finalChunk?.text ?? "";
378
- }
379
- else {
380
- const response = await this.completionWithRetry({
381
- ...params,
382
- stream: false,
383
- messages: this.formatMessages(prompt),
384
- }, {
385
- signal: options.signal,
386
- ...options.options,
387
- });
388
- return response?.choices[0]?.message?.content ?? "";
389
- }
390
- }
391
- async completionWithRetry(request, options) {
392
- const requestOptions = this._getClientOptions(options);
393
- return this.caller.call(async () => {
394
- try {
395
- const res = await this.client.chat.completions.create(request, requestOptions);
396
- return res;
397
- }
398
- catch (e) {
399
- const error = wrapOpenAIClientError(e);
400
- throw error;
401
- }
402
- });
403
- }
404
- /** @ignore */
405
- _getClientOptions(options) {
406
- if (!this.client) {
407
- const openAIEndpointConfig = {
408
- azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
409
- azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
410
- azureOpenAIApiKey: this.azureOpenAIApiKey,
411
- azureOpenAIBasePath: this.azureOpenAIBasePath,
412
- baseURL: this.clientConfig.baseURL,
413
- };
414
- const endpoint = getEndpoint(openAIEndpointConfig);
415
- const params = {
416
- ...this.clientConfig,
417
- baseURL: endpoint,
418
- timeout: this.timeout,
419
- maxRetries: 0,
420
- };
421
- if (!params.baseURL) {
422
- delete params.baseURL;
423
- }
424
- this.client = new OpenAIClient(params);
425
- }
426
- const requestOptions = {
427
- ...this.clientConfig,
428
- ...options,
429
- };
430
- if (this.azureOpenAIApiKey) {
431
- requestOptions.headers = {
432
- "api-key": this.azureOpenAIApiKey,
433
- ...requestOptions.headers,
434
- };
435
- requestOptions.query = {
436
- "api-version": this.azureOpenAIApiVersion,
437
- ...requestOptions.query,
438
- };
439
- }
440
- return requestOptions;
441
- }
442
- _llmType() {
443
- return "openai";
444
- }
445
- }
4
+ export { OpenAIChat };
446
5
  /**
447
6
  * PromptLayer wrapper to OpenAIChat
448
7
  */