langchain 0.0.197-rc.1 → 0.0.197
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/dist/chains/openai_moderation.cjs +2 -2
- package/dist/chains/openai_moderation.d.ts +1 -1
- package/dist/chains/openai_moderation.js +1 -1
- package/dist/chat_models/anthropic.cjs +351 -15
- package/dist/chat_models/anthropic.d.ts +157 -1
- package/dist/chat_models/anthropic.js +348 -1
- package/dist/chat_models/cloudflare_workersai.cjs +5 -0
- package/dist/chat_models/cloudflare_workersai.d.ts +3 -0
- package/dist/chat_models/cloudflare_workersai.js +5 -0
- package/dist/chat_models/fireworks.d.ts +1 -1
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
- package/dist/chat_models/minimax.d.ts +1 -1
- package/dist/chat_models/openai.cjs +698 -4
- package/dist/chat_models/openai.d.ts +137 -4
- package/dist/chat_models/openai.js +695 -2
- package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
- package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
- package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
- package/dist/embeddings/openai.cjs +240 -2
- package/dist/embeddings/openai.d.ts +82 -1
- package/dist/embeddings/openai.js +239 -1
- package/dist/experimental/openai_assistant/index.cjs +3 -3
- package/dist/experimental/openai_assistant/index.d.ts +1 -1
- package/dist/experimental/openai_assistant/index.js +1 -1
- package/dist/experimental/openai_assistant/schema.d.ts +1 -1
- package/dist/experimental/openai_files/index.cjs +2 -2
- package/dist/experimental/openai_files/index.d.ts +1 -1
- package/dist/experimental/openai_files/index.js +1 -1
- package/dist/llms/fireworks.d.ts +1 -1
- package/dist/llms/openai-chat.cjs +445 -3
- package/dist/llms/openai-chat.d.ts +123 -4
- package/dist/llms/openai-chat.js +443 -2
- package/dist/llms/openai.cjs +530 -6
- package/dist/llms/openai.d.ts +123 -4
- package/dist/llms/openai.js +525 -2
- package/dist/schema/index.d.ts +1 -1
- package/dist/tools/convert_to_openai.cjs +38 -4
- package/dist/tools/convert_to_openai.d.ts +11 -1
- package/dist/tools/convert_to_openai.js +35 -1
- package/dist/types/openai-types.d.ts +133 -1
- package/dist/util/env.cjs +9 -70
- package/dist/util/env.d.ts +1 -21
- package/dist/util/env.js +1 -62
- package/dist/util/openai-format-fndef.cjs +81 -0
- package/dist/util/openai-format-fndef.d.ts +44 -0
- package/dist/util/openai-format-fndef.js +77 -0
- package/dist/util/openai.cjs +18 -2
- package/dist/util/openai.d.ts +1 -1
- package/dist/util/openai.js +17 -1
- package/dist/util/openapi.d.ts +2 -2
- package/dist/util/prompt-layer.d.ts +1 -1
- package/package.json +3 -6
package/dist/llms/openai.js
CHANGED
|
@@ -1,7 +1,530 @@
|
|
|
1
|
-
import { OpenAI } from "
|
|
1
|
+
import { OpenAI as OpenAIClient } from "openai";
|
|
2
|
+
import { calculateMaxTokens } from "../base_language/count_tokens.js";
|
|
3
|
+
import { GenerationChunk } from "../schema/index.js";
|
|
4
|
+
import { getEndpoint } from "../util/azure.js";
|
|
5
|
+
import { chunkArray } from "../util/chunk.js";
|
|
2
6
|
import { getEnvironmentVariable } from "../util/env.js";
|
|
3
7
|
import { promptLayerTrackRequest } from "../util/prompt-layer.js";
|
|
4
|
-
|
|
8
|
+
import { BaseLLM } from "./base.js";
|
|
9
|
+
import { OpenAIChat } from "./openai-chat.js";
|
|
10
|
+
import { wrapOpenAIClientError } from "../util/openai.js";
|
|
11
|
+
/**
|
|
12
|
+
* Wrapper around OpenAI large language models.
|
|
13
|
+
*
|
|
14
|
+
* To use you should have the `openai` package installed, with the
|
|
15
|
+
* `OPENAI_API_KEY` environment variable set.
|
|
16
|
+
*
|
|
17
|
+
* To use with Azure you should have the `openai` package installed, with the
|
|
18
|
+
* `AZURE_OPENAI_API_KEY`,
|
|
19
|
+
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
20
|
+
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
21
|
+
* and `AZURE_OPENAI_API_VERSION` environment variable set.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Any parameters that are valid to be passed to {@link
|
|
25
|
+
* https://platform.openai.com/docs/api-reference/completions/create |
|
|
26
|
+
* `openai.createCompletion`} can be passed through {@link modelKwargs}, even
|
|
27
|
+
* if not explicitly available on this class.
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const model = new OpenAI({
|
|
31
|
+
* modelName: "gpt-4",
|
|
32
|
+
* temperature: 0.7,
|
|
33
|
+
* maxTokens: 1000,
|
|
34
|
+
* maxRetries: 5,
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* const res = await model.call(
|
|
38
|
+
* "Question: What would be a good company name for a company that makes colorful socks?\nAnswer:"
|
|
39
|
+
* );
|
|
40
|
+
* console.log({ res });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export class OpenAI extends BaseLLM {
|
|
44
|
+
static lc_name() {
|
|
45
|
+
return "OpenAI";
|
|
46
|
+
}
|
|
47
|
+
get callKeys() {
|
|
48
|
+
return [...super.callKeys, "options"];
|
|
49
|
+
}
|
|
50
|
+
get lc_secrets() {
|
|
51
|
+
return {
|
|
52
|
+
openAIApiKey: "OPENAI_API_KEY",
|
|
53
|
+
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
|
|
54
|
+
organization: "OPENAI_ORGANIZATION",
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
get lc_aliases() {
|
|
58
|
+
return {
|
|
59
|
+
modelName: "model",
|
|
60
|
+
openAIApiKey: "openai_api_key",
|
|
61
|
+
azureOpenAIApiVersion: "azure_openai_api_version",
|
|
62
|
+
azureOpenAIApiKey: "azure_openai_api_key",
|
|
63
|
+
azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
|
|
64
|
+
azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
constructor(fields,
|
|
68
|
+
/** @deprecated */
|
|
69
|
+
configuration) {
|
|
70
|
+
if ((fields?.modelName?.startsWith("gpt-3.5-turbo") ||
|
|
71
|
+
fields?.modelName?.startsWith("gpt-4")) &&
|
|
72
|
+
!fields?.modelName?.includes("-instruct")) {
|
|
73
|
+
// eslint-disable-next-line no-constructor-return
|
|
74
|
+
return new OpenAIChat(fields, configuration);
|
|
75
|
+
}
|
|
76
|
+
super(fields ?? {});
|
|
77
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
writable: true,
|
|
81
|
+
value: true
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(this, "temperature", {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: 0.7
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(this, "maxTokens", {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
configurable: true,
|
|
92
|
+
writable: true,
|
|
93
|
+
value: 256
|
|
94
|
+
});
|
|
95
|
+
Object.defineProperty(this, "topP", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: 1
|
|
100
|
+
});
|
|
101
|
+
Object.defineProperty(this, "frequencyPenalty", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: 0
|
|
106
|
+
});
|
|
107
|
+
Object.defineProperty(this, "presencePenalty", {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: true,
|
|
111
|
+
value: 0
|
|
112
|
+
});
|
|
113
|
+
Object.defineProperty(this, "n", {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
configurable: true,
|
|
116
|
+
writable: true,
|
|
117
|
+
value: 1
|
|
118
|
+
});
|
|
119
|
+
Object.defineProperty(this, "bestOf", {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
configurable: true,
|
|
122
|
+
writable: true,
|
|
123
|
+
value: void 0
|
|
124
|
+
});
|
|
125
|
+
Object.defineProperty(this, "logitBias", {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: true,
|
|
128
|
+
writable: true,
|
|
129
|
+
value: void 0
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(this, "modelName", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: true,
|
|
134
|
+
writable: true,
|
|
135
|
+
value: "gpt-3.5-turbo-instruct"
|
|
136
|
+
});
|
|
137
|
+
Object.defineProperty(this, "modelKwargs", {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
configurable: true,
|
|
140
|
+
writable: true,
|
|
141
|
+
value: void 0
|
|
142
|
+
});
|
|
143
|
+
Object.defineProperty(this, "batchSize", {
|
|
144
|
+
enumerable: true,
|
|
145
|
+
configurable: true,
|
|
146
|
+
writable: true,
|
|
147
|
+
value: 20
|
|
148
|
+
});
|
|
149
|
+
Object.defineProperty(this, "timeout", {
|
|
150
|
+
enumerable: true,
|
|
151
|
+
configurable: true,
|
|
152
|
+
writable: true,
|
|
153
|
+
value: void 0
|
|
154
|
+
});
|
|
155
|
+
Object.defineProperty(this, "stop", {
|
|
156
|
+
enumerable: true,
|
|
157
|
+
configurable: true,
|
|
158
|
+
writable: true,
|
|
159
|
+
value: void 0
|
|
160
|
+
});
|
|
161
|
+
Object.defineProperty(this, "user", {
|
|
162
|
+
enumerable: true,
|
|
163
|
+
configurable: true,
|
|
164
|
+
writable: true,
|
|
165
|
+
value: void 0
|
|
166
|
+
});
|
|
167
|
+
Object.defineProperty(this, "streaming", {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
configurable: true,
|
|
170
|
+
writable: true,
|
|
171
|
+
value: false
|
|
172
|
+
});
|
|
173
|
+
Object.defineProperty(this, "openAIApiKey", {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
configurable: true,
|
|
176
|
+
writable: true,
|
|
177
|
+
value: void 0
|
|
178
|
+
});
|
|
179
|
+
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
180
|
+
enumerable: true,
|
|
181
|
+
configurable: true,
|
|
182
|
+
writable: true,
|
|
183
|
+
value: void 0
|
|
184
|
+
});
|
|
185
|
+
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
186
|
+
enumerable: true,
|
|
187
|
+
configurable: true,
|
|
188
|
+
writable: true,
|
|
189
|
+
value: void 0
|
|
190
|
+
});
|
|
191
|
+
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
configurable: true,
|
|
194
|
+
writable: true,
|
|
195
|
+
value: void 0
|
|
196
|
+
});
|
|
197
|
+
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
198
|
+
enumerable: true,
|
|
199
|
+
configurable: true,
|
|
200
|
+
writable: true,
|
|
201
|
+
value: void 0
|
|
202
|
+
});
|
|
203
|
+
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
204
|
+
enumerable: true,
|
|
205
|
+
configurable: true,
|
|
206
|
+
writable: true,
|
|
207
|
+
value: void 0
|
|
208
|
+
});
|
|
209
|
+
Object.defineProperty(this, "organization", {
|
|
210
|
+
enumerable: true,
|
|
211
|
+
configurable: true,
|
|
212
|
+
writable: true,
|
|
213
|
+
value: void 0
|
|
214
|
+
});
|
|
215
|
+
Object.defineProperty(this, "client", {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
configurable: true,
|
|
218
|
+
writable: true,
|
|
219
|
+
value: void 0
|
|
220
|
+
});
|
|
221
|
+
Object.defineProperty(this, "clientConfig", {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
configurable: true,
|
|
224
|
+
writable: true,
|
|
225
|
+
value: void 0
|
|
226
|
+
});
|
|
227
|
+
this.openAIApiKey =
|
|
228
|
+
fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
|
|
229
|
+
this.azureOpenAIApiKey =
|
|
230
|
+
fields?.azureOpenAIApiKey ??
|
|
231
|
+
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
|
232
|
+
if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
|
|
233
|
+
throw new Error("OpenAI or Azure OpenAI API key not found");
|
|
234
|
+
}
|
|
235
|
+
this.azureOpenAIApiInstanceName =
|
|
236
|
+
fields?.azureOpenAIApiInstanceName ??
|
|
237
|
+
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
238
|
+
this.azureOpenAIApiDeploymentName =
|
|
239
|
+
(fields?.azureOpenAIApiCompletionsDeploymentName ||
|
|
240
|
+
fields?.azureOpenAIApiDeploymentName) ??
|
|
241
|
+
(getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
|
|
242
|
+
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
|
|
243
|
+
this.azureOpenAIApiVersion =
|
|
244
|
+
fields?.azureOpenAIApiVersion ??
|
|
245
|
+
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
|
|
246
|
+
this.azureOpenAIBasePath =
|
|
247
|
+
fields?.azureOpenAIBasePath ??
|
|
248
|
+
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
249
|
+
this.organization =
|
|
250
|
+
fields?.configuration?.organization ??
|
|
251
|
+
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
252
|
+
this.modelName = fields?.modelName ?? this.modelName;
|
|
253
|
+
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
254
|
+
this.batchSize = fields?.batchSize ?? this.batchSize;
|
|
255
|
+
this.timeout = fields?.timeout;
|
|
256
|
+
this.temperature = fields?.temperature ?? this.temperature;
|
|
257
|
+
this.maxTokens = fields?.maxTokens ?? this.maxTokens;
|
|
258
|
+
this.topP = fields?.topP ?? this.topP;
|
|
259
|
+
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
|
|
260
|
+
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
|
|
261
|
+
this.n = fields?.n ?? this.n;
|
|
262
|
+
this.bestOf = fields?.bestOf ?? this.bestOf;
|
|
263
|
+
this.logitBias = fields?.logitBias;
|
|
264
|
+
this.stop = fields?.stop;
|
|
265
|
+
this.user = fields?.user;
|
|
266
|
+
this.streaming = fields?.streaming ?? false;
|
|
267
|
+
if (this.streaming && this.bestOf && this.bestOf > 1) {
|
|
268
|
+
throw new Error("Cannot stream results when bestOf > 1");
|
|
269
|
+
}
|
|
270
|
+
if (this.azureOpenAIApiKey) {
|
|
271
|
+
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
272
|
+
throw new Error("Azure OpenAI API instance name not found");
|
|
273
|
+
}
|
|
274
|
+
if (!this.azureOpenAIApiDeploymentName) {
|
|
275
|
+
throw new Error("Azure OpenAI API deployment name not found");
|
|
276
|
+
}
|
|
277
|
+
if (!this.azureOpenAIApiVersion) {
|
|
278
|
+
throw new Error("Azure OpenAI API version not found");
|
|
279
|
+
}
|
|
280
|
+
this.openAIApiKey = this.openAIApiKey ?? "";
|
|
281
|
+
}
|
|
282
|
+
this.clientConfig = {
|
|
283
|
+
apiKey: this.openAIApiKey,
|
|
284
|
+
organization: this.organization,
|
|
285
|
+
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
|
|
286
|
+
dangerouslyAllowBrowser: true,
|
|
287
|
+
defaultHeaders: configuration?.baseOptions?.headers ??
|
|
288
|
+
fields?.configuration?.baseOptions?.headers,
|
|
289
|
+
defaultQuery: configuration?.baseOptions?.params ??
|
|
290
|
+
fields?.configuration?.baseOptions?.params,
|
|
291
|
+
...configuration,
|
|
292
|
+
...fields?.configuration,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Get the parameters used to invoke the model
|
|
297
|
+
*/
|
|
298
|
+
invocationParams(options) {
|
|
299
|
+
return {
|
|
300
|
+
model: this.modelName,
|
|
301
|
+
temperature: this.temperature,
|
|
302
|
+
max_tokens: this.maxTokens,
|
|
303
|
+
top_p: this.topP,
|
|
304
|
+
frequency_penalty: this.frequencyPenalty,
|
|
305
|
+
presence_penalty: this.presencePenalty,
|
|
306
|
+
n: this.n,
|
|
307
|
+
best_of: this.bestOf,
|
|
308
|
+
logit_bias: this.logitBias,
|
|
309
|
+
stop: options?.stop ?? this.stop,
|
|
310
|
+
user: this.user,
|
|
311
|
+
stream: this.streaming,
|
|
312
|
+
...this.modelKwargs,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
/** @ignore */
|
|
316
|
+
_identifyingParams() {
|
|
317
|
+
return {
|
|
318
|
+
model_name: this.modelName,
|
|
319
|
+
...this.invocationParams(),
|
|
320
|
+
...this.clientConfig,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Get the identifying parameters for the model
|
|
325
|
+
*/
|
|
326
|
+
identifyingParams() {
|
|
327
|
+
return this._identifyingParams();
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Call out to OpenAI's endpoint with k unique prompts
|
|
331
|
+
*
|
|
332
|
+
* @param [prompts] - The prompts to pass into the model.
|
|
333
|
+
* @param [options] - Optional list of stop words to use when generating.
|
|
334
|
+
* @param [runManager] - Optional callback manager to use when generating.
|
|
335
|
+
*
|
|
336
|
+
* @returns The full LLM output.
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* ```ts
|
|
340
|
+
* import { OpenAI } from "langchain/llms/openai";
|
|
341
|
+
* const openai = new OpenAI();
|
|
342
|
+
* const response = await openai.generate(["Tell me a joke."]);
|
|
343
|
+
* ```
|
|
344
|
+
*/
|
|
345
|
+
async _generate(prompts, options, runManager) {
|
|
346
|
+
const subPrompts = chunkArray(prompts, this.batchSize);
|
|
347
|
+
const choices = [];
|
|
348
|
+
const tokenUsage = {};
|
|
349
|
+
const params = this.invocationParams(options);
|
|
350
|
+
if (params.max_tokens === -1) {
|
|
351
|
+
if (prompts.length !== 1) {
|
|
352
|
+
throw new Error("max_tokens set to -1 not supported for multiple inputs");
|
|
353
|
+
}
|
|
354
|
+
params.max_tokens = await calculateMaxTokens({
|
|
355
|
+
prompt: prompts[0],
|
|
356
|
+
// Cast here to allow for other models that may not fit the union
|
|
357
|
+
modelName: this.modelName,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
for (let i = 0; i < subPrompts.length; i += 1) {
|
|
361
|
+
const data = params.stream
|
|
362
|
+
? await (async () => {
|
|
363
|
+
const choices = [];
|
|
364
|
+
let response;
|
|
365
|
+
const stream = await this.completionWithRetry({
|
|
366
|
+
...params,
|
|
367
|
+
stream: true,
|
|
368
|
+
prompt: subPrompts[i],
|
|
369
|
+
}, options);
|
|
370
|
+
for await (const message of stream) {
|
|
371
|
+
// on the first message set the response properties
|
|
372
|
+
if (!response) {
|
|
373
|
+
response = {
|
|
374
|
+
id: message.id,
|
|
375
|
+
object: message.object,
|
|
376
|
+
created: message.created,
|
|
377
|
+
model: message.model,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
// on all messages, update choice
|
|
381
|
+
for (const part of message.choices) {
|
|
382
|
+
if (!choices[part.index]) {
|
|
383
|
+
choices[part.index] = part;
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
const choice = choices[part.index];
|
|
387
|
+
choice.text += part.text;
|
|
388
|
+
choice.finish_reason = part.finish_reason;
|
|
389
|
+
choice.logprobs = part.logprobs;
|
|
390
|
+
}
|
|
391
|
+
void runManager?.handleLLMNewToken(part.text, {
|
|
392
|
+
prompt: Math.floor(part.index / this.n),
|
|
393
|
+
completion: part.index % this.n,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (options.signal?.aborted) {
|
|
398
|
+
throw new Error("AbortError");
|
|
399
|
+
}
|
|
400
|
+
return { ...response, choices };
|
|
401
|
+
})()
|
|
402
|
+
: await this.completionWithRetry({
|
|
403
|
+
...params,
|
|
404
|
+
stream: false,
|
|
405
|
+
prompt: subPrompts[i],
|
|
406
|
+
}, {
|
|
407
|
+
signal: options.signal,
|
|
408
|
+
...options.options,
|
|
409
|
+
});
|
|
410
|
+
choices.push(...data.choices);
|
|
411
|
+
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data.usage
|
|
412
|
+
? data.usage
|
|
413
|
+
: {
|
|
414
|
+
completion_tokens: undefined,
|
|
415
|
+
prompt_tokens: undefined,
|
|
416
|
+
total_tokens: undefined,
|
|
417
|
+
};
|
|
418
|
+
if (completionTokens) {
|
|
419
|
+
tokenUsage.completionTokens =
|
|
420
|
+
(tokenUsage.completionTokens ?? 0) + completionTokens;
|
|
421
|
+
}
|
|
422
|
+
if (promptTokens) {
|
|
423
|
+
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
|
|
424
|
+
}
|
|
425
|
+
if (totalTokens) {
|
|
426
|
+
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const generations = chunkArray(choices, this.n).map((promptChoices) => promptChoices.map((choice) => ({
|
|
430
|
+
text: choice.text ?? "",
|
|
431
|
+
generationInfo: {
|
|
432
|
+
finishReason: choice.finish_reason,
|
|
433
|
+
logprobs: choice.logprobs,
|
|
434
|
+
},
|
|
435
|
+
})));
|
|
436
|
+
return {
|
|
437
|
+
generations,
|
|
438
|
+
llmOutput: { tokenUsage },
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
// TODO(jacoblee): Refactor with _generate(..., {stream: true}) implementation?
|
|
442
|
+
async *_streamResponseChunks(input, options, runManager) {
|
|
443
|
+
const params = {
|
|
444
|
+
...this.invocationParams(options),
|
|
445
|
+
prompt: input,
|
|
446
|
+
stream: true,
|
|
447
|
+
};
|
|
448
|
+
const stream = await this.completionWithRetry(params, options);
|
|
449
|
+
for await (const data of stream) {
|
|
450
|
+
const choice = data?.choices[0];
|
|
451
|
+
if (!choice) {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
const chunk = new GenerationChunk({
|
|
455
|
+
text: choice.text,
|
|
456
|
+
generationInfo: {
|
|
457
|
+
finishReason: choice.finish_reason,
|
|
458
|
+
},
|
|
459
|
+
});
|
|
460
|
+
yield chunk;
|
|
461
|
+
// eslint-disable-next-line no-void
|
|
462
|
+
void runManager?.handleLLMNewToken(chunk.text ?? "");
|
|
463
|
+
}
|
|
464
|
+
if (options.signal?.aborted) {
|
|
465
|
+
throw new Error("AbortError");
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async completionWithRetry(request, options) {
|
|
469
|
+
const requestOptions = this._getClientOptions(options);
|
|
470
|
+
return this.caller.call(async () => {
|
|
471
|
+
try {
|
|
472
|
+
const res = await this.client.completions.create(request, requestOptions);
|
|
473
|
+
return res;
|
|
474
|
+
}
|
|
475
|
+
catch (e) {
|
|
476
|
+
const error = wrapOpenAIClientError(e);
|
|
477
|
+
throw error;
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Calls the OpenAI API with retry logic in case of failures.
|
|
483
|
+
* @param request The request to send to the OpenAI API.
|
|
484
|
+
* @param options Optional configuration for the API call.
|
|
485
|
+
* @returns The response from the OpenAI API.
|
|
486
|
+
*/
|
|
487
|
+
_getClientOptions(options) {
|
|
488
|
+
if (!this.client) {
|
|
489
|
+
const openAIEndpointConfig = {
|
|
490
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
491
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
492
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
493
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
494
|
+
baseURL: this.clientConfig.baseURL,
|
|
495
|
+
};
|
|
496
|
+
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
497
|
+
const params = {
|
|
498
|
+
...this.clientConfig,
|
|
499
|
+
baseURL: endpoint,
|
|
500
|
+
timeout: this.timeout,
|
|
501
|
+
maxRetries: 0,
|
|
502
|
+
};
|
|
503
|
+
if (!params.baseURL) {
|
|
504
|
+
delete params.baseURL;
|
|
505
|
+
}
|
|
506
|
+
this.client = new OpenAIClient(params);
|
|
507
|
+
}
|
|
508
|
+
const requestOptions = {
|
|
509
|
+
...this.clientConfig,
|
|
510
|
+
...options,
|
|
511
|
+
};
|
|
512
|
+
if (this.azureOpenAIApiKey) {
|
|
513
|
+
requestOptions.headers = {
|
|
514
|
+
"api-key": this.azureOpenAIApiKey,
|
|
515
|
+
...requestOptions.headers,
|
|
516
|
+
};
|
|
517
|
+
requestOptions.query = {
|
|
518
|
+
"api-version": this.azureOpenAIApiVersion,
|
|
519
|
+
...requestOptions.query,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
return requestOptions;
|
|
523
|
+
}
|
|
524
|
+
_llmType() {
|
|
525
|
+
return "openai";
|
|
526
|
+
}
|
|
527
|
+
}
|
|
5
528
|
/**
|
|
6
529
|
* PromptLayer wrapper to OpenAI
|
|
7
530
|
* @augments OpenAI
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { OpenAIClient } from "
|
|
1
|
+
import type { OpenAI as OpenAIClient } from "openai";
|
|
2
2
|
import { BaseMessage, HumanMessage, AIMessage, SystemMessage } from "@langchain/core/messages";
|
|
3
3
|
import { Document } from "../document.js";
|
|
4
4
|
import { Serializable } from "../load/serializable.js";
|
|
@@ -1,7 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
5
|
+
/**
|
|
6
|
+
* Formats a `StructuredTool` instance into a format that is compatible
|
|
7
|
+
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
8
|
+
* function to convert the schema of the `StructuredTool` into a JSON
|
|
9
|
+
* schema, which is then used as the parameters for the OpenAI function.
|
|
10
|
+
*/
|
|
11
|
+
function formatToOpenAIFunction(tool) {
|
|
12
|
+
return {
|
|
13
|
+
name: tool.name,
|
|
14
|
+
description: tool.description,
|
|
15
|
+
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
exports.formatToOpenAIFunction = formatToOpenAIFunction;
|
|
19
|
+
function formatToOpenAITool(tool) {
|
|
20
|
+
const schema = (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema);
|
|
21
|
+
return {
|
|
22
|
+
type: "function",
|
|
23
|
+
function: {
|
|
24
|
+
name: tool.name,
|
|
25
|
+
description: tool.description,
|
|
26
|
+
parameters: schema,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.formatToOpenAITool = formatToOpenAITool;
|
|
31
|
+
function formatToOpenAIAssistantTool(tool) {
|
|
32
|
+
return {
|
|
33
|
+
type: "function",
|
|
34
|
+
function: {
|
|
35
|
+
name: tool.name,
|
|
36
|
+
description: tool.description,
|
|
37
|
+
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.formatToOpenAIAssistantTool = formatToOpenAIAssistantTool;
|
|
@@ -1 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import type { OpenAI as OpenAIClient } from "openai";
|
|
2
|
+
import { StructuredTool } from "./base.js";
|
|
3
|
+
/**
|
|
4
|
+
* Formats a `StructuredTool` instance into a format that is compatible
|
|
5
|
+
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
6
|
+
* function to convert the schema of the `StructuredTool` into a JSON
|
|
7
|
+
* schema, which is then used as the parameters for the OpenAI function.
|
|
8
|
+
*/
|
|
9
|
+
export declare function formatToOpenAIFunction(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionCreateParams.Function;
|
|
10
|
+
export declare function formatToOpenAITool(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionTool;
|
|
11
|
+
export declare function formatToOpenAIAssistantTool(tool: StructuredTool): OpenAIClient.Beta.AssistantCreateParams.AssistantToolsFunction;
|
|
@@ -1 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a `StructuredTool` instance into a format that is compatible
|
|
4
|
+
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
5
|
+
* function to convert the schema of the `StructuredTool` into a JSON
|
|
6
|
+
* schema, which is then used as the parameters for the OpenAI function.
|
|
7
|
+
*/
|
|
8
|
+
export function formatToOpenAIFunction(tool) {
|
|
9
|
+
return {
|
|
10
|
+
name: tool.name,
|
|
11
|
+
description: tool.description,
|
|
12
|
+
parameters: zodToJsonSchema(tool.schema),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function formatToOpenAITool(tool) {
|
|
16
|
+
const schema = zodToJsonSchema(tool.schema);
|
|
17
|
+
return {
|
|
18
|
+
type: "function",
|
|
19
|
+
function: {
|
|
20
|
+
name: tool.name,
|
|
21
|
+
description: tool.description,
|
|
22
|
+
parameters: schema,
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function formatToOpenAIAssistantTool(tool) {
|
|
27
|
+
return {
|
|
28
|
+
type: "function",
|
|
29
|
+
function: {
|
|
30
|
+
name: tool.name,
|
|
31
|
+
description: tool.description,
|
|
32
|
+
parameters: zodToJsonSchema(tool.schema),
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|