-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathuniversal.ts
882 lines (824 loc) Β· 29.5 KB
/
universal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/* eslint-disable import/no-extraneous-dependencies */
import {
BaseLanguageModelInput,
ToolDefinition,
} from "@langchain/core/language_models/base";
import {
BaseChatModel,
BaseChatModelParams,
BindToolsInput,
type BaseChatModelCallOptions,
} from "@langchain/core/language_models/chat_models";
import { BaseMessage, type AIMessageChunk } from "@langchain/core/messages";
import {
type RunnableBatchOptions,
RunnableBinding,
type RunnableConfig,
type RunnableToolLike,
ensureConfig,
} from "@langchain/core/runnables";
import {
AsyncGeneratorWithSetup,
IterableReadableStream,
} from "@langchain/core/utils/stream";
import {
type LogStreamCallbackHandlerInput,
type RunLogPatch,
type StreamEvent,
} from "@langchain/core/tracers/log_stream";
import { type StructuredToolInterface } from "@langchain/core/tools";
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
import { ChatResult } from "@langchain/core/outputs";
// TODO: remove once `EventStreamCallbackHandlerInput` is exposed in core
interface EventStreamCallbackHandlerInput
extends Omit<LogStreamCallbackHandlerInput, "_schemaFormat"> {}
const _SUPPORTED_PROVIDERS = [
"openai",
"anthropic",
"azure_openai",
"cohere",
"google-vertexai",
"google-vertexai-web",
"google-genai",
"ollama",
"together",
"fireworks",
"mistralai",
"groq",
"bedrock",
"cerebras",
"deepseek",
"xai",
] as const;
export type ChatModelProvider = (typeof _SUPPORTED_PROVIDERS)[number];
export interface ConfigurableChatModelCallOptions
extends BaseChatModelCallOptions {
tools?: (
| StructuredToolInterface
| Record<string, unknown>
| ToolDefinition
| RunnableToolLike
)[];
}
async function _initChatModelHelper(
model: string,
modelProvider?: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params: Record<string, any> = {}
): Promise<BaseChatModel> {
const modelProviderCopy = modelProvider || _inferModelProvider(model);
if (!modelProviderCopy) {
throw new Error(
`Unable to infer model provider for { model: ${model} }, please specify modelProvider directly.`
);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { modelProvider: _unused, ...passedParams } = params;
try {
switch (modelProviderCopy) {
case "openai": {
const { ChatOpenAI } = await import("@langchain/openai");
return new ChatOpenAI({ model, ...passedParams });
}
case "anthropic": {
const { ChatAnthropic } = await import("@langchain/anthropic");
return new ChatAnthropic({ model, ...passedParams });
}
case "azure_openai": {
const { AzureChatOpenAI } = await import("@langchain/openai");
return new AzureChatOpenAI({ model, ...passedParams });
}
case "cohere": {
const { ChatCohere } = await import("@langchain/cohere");
return new ChatCohere({ model, ...passedParams });
}
case "google-vertexai": {
const { ChatVertexAI } = await import("@langchain/google-vertexai");
return new ChatVertexAI({ model, ...passedParams });
}
case "google-vertexai-web": {
const { ChatVertexAI } = await import("@langchain/google-vertexai-web");
return new ChatVertexAI({ model, ...passedParams });
}
case "google-genai": {
const { ChatGoogleGenerativeAI } = await import(
"@langchain/google-genai"
);
return new ChatGoogleGenerativeAI({ model, ...passedParams });
}
case "ollama": {
const { ChatOllama } = await import("@langchain/ollama");
return new ChatOllama({ model, ...passedParams });
}
case "mistralai": {
const { ChatMistralAI } = await import("@langchain/mistralai");
return new ChatMistralAI({ model, ...passedParams });
}
case "groq": {
const { ChatGroq } = await import("@langchain/groq");
return new ChatGroq({ model, ...passedParams });
}
case "cerebras": {
const { ChatCerebras } = await import("@langchain/cerebras");
return new ChatCerebras({ model, ...passedParams });
}
case "bedrock": {
const { ChatBedrockConverse } = await import("@langchain/aws");
return new ChatBedrockConverse({ model, ...passedParams });
}
case "deepseek": {
const { ChatDeepSeek } = await import("@langchain/deepseek");
return new ChatDeepSeek({ model, ...passedParams });
}
case "xai": {
const { ChatXAI } = await import("@langchain/xai");
return new ChatXAI({ model, ...passedParams });
}
case "fireworks": {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Can not install as a proper dependency due to circular dependency
const { ChatFireworks } = await import(
// We can not 'expect-error' because if you explicitly build `@langchain/community`
// this import will be able to be resolved, thus there will be no error. However
// this will never be the case in CI.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Can not install as a proper dependency due to circular dependency
"@langchain/community/chat_models/fireworks"
);
return new ChatFireworks({ model, ...passedParams });
}
case "together": {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Can not install as a proper dependency due to circular dependency
const { ChatTogetherAI } = await import(
// We can not 'expect-error' because if you explicitly build `@langchain/community`
// this import will be able to be resolved, thus there will be no error. However
// this will never be the case in CI.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - Can not install as a proper dependency due to circular dependency
"@langchain/community/chat_models/togetherai"
);
return new ChatTogetherAI({ model, ...passedParams });
}
default: {
const supported = _SUPPORTED_PROVIDERS.join(", ");
throw new Error(
`Unsupported { modelProvider: ${modelProviderCopy} }.\n\nSupported model providers are: ${supported}`
);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if ("code" in e && e.code.includes("ERR_MODULE_NOT_FOUND")) {
const attemptedPackage = new Error(e).message
.split("Error: Cannot find package '")[1]
.split("'")[0];
throw new Error(
`Unable to import ${attemptedPackage}. Please install with ` +
`\`npm install ${attemptedPackage}\` or \`yarn add ${attemptedPackage}\``
);
}
throw e;
}
}
/**
* Attempts to infer the model provider based on the given model name.
*
* @param {string} modelName - The name of the model to infer the provider for.
* @returns {string | undefined} The inferred model provider name, or undefined if unable to infer.
*
* @example
* _inferModelProvider("gpt-4"); // returns "openai"
* _inferModelProvider("claude-2"); // returns "anthropic"
* _inferModelProvider("unknown-model"); // returns undefined
*/
export function _inferModelProvider(modelName: string): string | undefined {
if (
modelName.startsWith("gpt-3") ||
modelName.startsWith("gpt-4") ||
modelName.startsWith("o1") ||
modelName.startsWith("o3") ||
modelName.startsWith("o4")
) {
return "openai";
} else if (modelName.startsWith("claude")) {
return "anthropic";
} else if (modelName.startsWith("command")) {
return "cohere";
} else if (modelName.startsWith("accounts/fireworks")) {
return "fireworks";
} else if (modelName.startsWith("gemini")) {
return "google-vertexai";
} else if (modelName.startsWith("amazon.")) {
return "bedrock";
} else {
return undefined;
}
}
interface ConfigurableModelFields extends BaseChatModelParams {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
defaultConfig?: Record<string, any>;
/**
* @default "any"
*/
configurableFields?: string[] | "any";
/**
* @default ""
*/
configPrefix?: string;
/**
* Methods which should be called after the model is initialized.
* The key will be the method name, and the value will be the arguments.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queuedMethodOperations?: Record<string, any>;
}
/**
* Internal class used to create chat models.
*
* @internal
*/
export class ConfigurableModel<
RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,
CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions
> extends BaseChatModel<CallOptions, AIMessageChunk> {
_llmType(): string {
return "chat_model";
}
lc_namespace = ["langchain", "chat_models"];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_defaultConfig?: Record<string, any> = {};
/**
* @default "any"
*/
_configurableFields: string[] | "any" = "any";
/**
* @default ""
*/
_configPrefix: string;
/**
* Methods which should be called after the model is initialized.
* The key will be the method name, and the value will be the arguments.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_queuedMethodOperations: Record<string, any> = {};
constructor(fields: ConfigurableModelFields) {
super(fields);
this._defaultConfig = fields.defaultConfig ?? {};
if (fields.configurableFields === "any") {
this._configurableFields = "any";
} else {
this._configurableFields = fields.configurableFields ?? [
"model",
"modelProvider",
];
}
if (fields.configPrefix) {
this._configPrefix = fields.configPrefix.endsWith("_")
? fields.configPrefix
: `${fields.configPrefix}_`;
} else {
this._configPrefix = "";
}
this._queuedMethodOperations =
fields.queuedMethodOperations ?? this._queuedMethodOperations;
}
async _model(config?: RunnableConfig) {
const params = { ...this._defaultConfig, ...this._modelParams(config) };
let initializedModel = await _initChatModelHelper(
params.model,
params.modelProvider,
params
);
// Apply queued method operations
const queuedMethodOperationsEntries = Object.entries(
this._queuedMethodOperations
);
if (queuedMethodOperationsEntries.length > 0) {
for (const [method, args] of queuedMethodOperationsEntries) {
if (
method in initializedModel &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
typeof (initializedModel as any)[method] === "function"
) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
initializedModel = await (initializedModel as any)[method](...args);
}
}
}
return initializedModel;
}
async _generate(
messages: BaseMessage[],
options?: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): Promise<ChatResult> {
const model = await this._model(options);
return model._generate(messages, options ?? {}, runManager);
}
override bindTools(
tools: BindToolsInput[],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params?: Record<string, any>
): ConfigurableModel<RunInput, CallOptions> {
this._queuedMethodOperations.bindTools = [tools, params];
return new ConfigurableModel<RunInput, CallOptions>({
defaultConfig: this._defaultConfig,
configurableFields: this._configurableFields,
configPrefix: this._configPrefix,
queuedMethodOperations: this._queuedMethodOperations,
});
}
// Extract the input types from the `BaseModel` class.
withStructuredOutput: BaseChatModel["withStructuredOutput"] = (
schema,
...args
): ReturnType<BaseChatModel["withStructuredOutput"]> => {
this._queuedMethodOperations.withStructuredOutput = [schema, ...args];
return new ConfigurableModel<RunInput, CallOptions>({
defaultConfig: this._defaultConfig,
configurableFields: this._configurableFields,
configPrefix: this._configPrefix,
queuedMethodOperations: this._queuedMethodOperations,
}) as unknown as ReturnType<BaseChatModel["withStructuredOutput"]>;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_modelParams(config?: RunnableConfig): Record<string, any> {
const configurable = config?.configurable ?? {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let modelParams: Record<string, any> = {};
for (const [key, value] of Object.entries(configurable)) {
if (key.startsWith(this._configPrefix)) {
const strippedKey = this._removePrefix(key, this._configPrefix);
modelParams[strippedKey] = value;
}
}
if (this._configurableFields !== "any") {
modelParams = Object.fromEntries(
Object.entries(modelParams).filter(([key]) =>
this._configurableFields.includes(key)
)
);
}
return modelParams;
}
_removePrefix(str: string, prefix: string): string {
return str.startsWith(prefix) ? str.slice(prefix.length) : str;
}
/**
* Bind config to a Runnable, returning a new Runnable.
* @param {RunnableConfig | undefined} [config] - The config to bind.
* @returns {RunnableBinding<RunInput, RunOutput, CallOptions>} A new RunnableBinding with the bound config.
*/
withConfig(
config?: RunnableConfig
): RunnableBinding<RunInput, AIMessageChunk, CallOptions> {
const mergedConfig: RunnableConfig = { ...(config || {}) };
const modelParams = this._modelParams(mergedConfig);
const remainingConfig: RunnableConfig = Object.fromEntries(
Object.entries(mergedConfig).filter(([k]) => k !== "configurable")
);
remainingConfig.configurable = Object.fromEntries(
Object.entries(mergedConfig.configurable || {}).filter(
([k]) =>
this._configPrefix &&
!Object.keys(modelParams).includes(
this._removePrefix(k, this._configPrefix)
)
)
);
const newConfigurableModel = new ConfigurableModel<RunInput, CallOptions>({
defaultConfig: { ...this._defaultConfig, ...modelParams },
configurableFields: Array.isArray(this._configurableFields)
? [...this._configurableFields]
: this._configurableFields,
configPrefix: this._configPrefix,
queuedMethodOperations: this._queuedMethodOperations,
});
return new RunnableBinding<RunInput, AIMessageChunk, CallOptions>({
config: mergedConfig,
bound: newConfigurableModel,
});
}
async invoke(
input: RunInput,
options?: CallOptions
): Promise<AIMessageChunk> {
const model = await this._model(options);
const config = ensureConfig(options);
return model.invoke(input, config);
}
async stream(
input: RunInput,
options?: CallOptions
): Promise<IterableReadableStream<AIMessageChunk>> {
const model = await this._model(options);
const wrappedGenerator = new AsyncGeneratorWithSetup({
generator: await model.stream(input, options),
config: options,
});
await wrappedGenerator.setup;
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
}
async batch(
inputs: RunInput[],
options?: Partial<CallOptions> | Partial<CallOptions>[],
batchOptions?: RunnableBatchOptions & { returnExceptions?: false }
): Promise<AIMessageChunk[]>;
async batch(
inputs: RunInput[],
options?: Partial<CallOptions> | Partial<CallOptions>[],
batchOptions?: RunnableBatchOptions & { returnExceptions: true }
): Promise<(AIMessageChunk | Error)[]>;
async batch(
inputs: RunInput[],
options?: Partial<CallOptions> | Partial<CallOptions>[],
batchOptions?: RunnableBatchOptions
): Promise<(AIMessageChunk | Error)[]>;
async batch(
inputs: RunInput[],
options?: Partial<CallOptions> | Partial<CallOptions>[],
batchOptions?: RunnableBatchOptions
): Promise<(AIMessageChunk | Error)[]> {
// We can super this since the base runnable implementation of
// `.batch` will call `.invoke` on each input.
return super.batch(inputs, options, batchOptions);
}
async *transform(
generator: AsyncGenerator<RunInput>,
options: CallOptions
): AsyncGenerator<AIMessageChunk> {
const model = await this._model(options);
const config = ensureConfig(options);
yield* model.transform(generator, config);
}
async *streamLog(
input: RunInput,
options?: Partial<CallOptions>,
streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">
): AsyncGenerator<RunLogPatch> {
const model = await this._model(options);
const config = ensureConfig(options);
yield* model.streamLog(input, config, {
...streamOptions,
_schemaFormat: "original",
includeNames: streamOptions?.includeNames,
includeTypes: streamOptions?.includeTypes,
includeTags: streamOptions?.includeTags,
excludeNames: streamOptions?.excludeNames,
excludeTypes: streamOptions?.excludeTypes,
excludeTags: streamOptions?.excludeTags,
});
}
streamEvents(
input: RunInput,
options: Partial<CallOptions> & { version: "v1" | "v2" },
streamOptions?: Omit<EventStreamCallbackHandlerInput, "autoClose">
): IterableReadableStream<StreamEvent>;
streamEvents(
input: RunInput,
options: Partial<CallOptions> & {
version: "v1" | "v2";
encoding: "text/event-stream";
},
streamOptions?: Omit<EventStreamCallbackHandlerInput, "autoClose">
): IterableReadableStream<Uint8Array>;
streamEvents(
input: RunInput,
options: Partial<CallOptions> & {
version: "v1" | "v2";
encoding?: "text/event-stream" | undefined;
},
streamOptions?: Omit<EventStreamCallbackHandlerInput, "autoClose">
): IterableReadableStream<StreamEvent | Uint8Array> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const outerThis = this;
async function* wrappedGenerator() {
const model = await outerThis._model(options);
const config = ensureConfig(options);
const eventStream = model.streamEvents(input, config, streamOptions);
for await (const chunk of eventStream) {
yield chunk;
}
}
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator());
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface InitChatModelFields extends Partial<Record<string, any>> {
modelProvider?: string;
configurableFields?: string[] | "any";
configPrefix?: string;
}
export type ConfigurableFields = "any" | string[];
export async function initChatModel<
RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,
CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions
>(
model: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fields?: Partial<Record<string, any>> & {
modelProvider?: string;
configurableFields?: never;
configPrefix?: string;
}
): Promise<ConfigurableModel<RunInput, CallOptions>>;
export async function initChatModel<
RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,
CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions
>(
model: never,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Partial<Record<string, any>> & {
modelProvider?: string;
configurableFields?: never;
configPrefix?: string;
}
): Promise<ConfigurableModel<RunInput, CallOptions>>;
export async function initChatModel<
RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,
CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions
>(
model?: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options?: Partial<Record<string, any>> & {
modelProvider?: string;
configurableFields?: ConfigurableFields;
configPrefix?: string;
}
): Promise<ConfigurableModel<RunInput, CallOptions>>;
// ################################# FOR CONTRIBUTORS #################################
//
// If adding support for a new provider, please append the provider
// name to the supported list in the docstring below.
//
// ####################################################################################
/**
* Initialize a ChatModel from the model name and provider.
* Must have the integration package corresponding to the model provider installed.
*
* @template {extends BaseLanguageModelInput = BaseLanguageModelInput} RunInput - The input type for the model.
* @template {extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions} CallOptions - Call options for the model.
*
* @param {string | ChatModelProvider} [model] - The name of the model, e.g. "gpt-4", "claude-3-opus-20240229".
* Can be prefixed with the model provider, e.g. "openai:gpt-4", "anthropic:claude-3-opus-20240229".
* @param {Object} [fields] - Additional configuration options.
* @param {string} [fields.modelProvider] - The model provider. Supported values include:
* - openai (@langchain/openai)
* - anthropic (@langchain/anthropic)
* - azure_openai (@langchain/openai)
* - google-vertexai (@langchain/google-vertexai)
* - google-vertexai-web (@langchain/google-vertexai-web)
* - google-genai (@langchain/google-genai)
* - bedrock (@langchain/aws)
* - cohere (@langchain/cohere)
* - fireworks (@langchain/community/chat_models/fireworks)
* - together (@langchain/community/chat_models/togetherai)
* - mistralai (@langchain/mistralai)
* - groq (@langchain/groq)
* - ollama (@langchain/ollama)
* - cerebras (@langchain/cerebras)
* - deepseek (@langchain/deepseek)
* - xai (@langchain/xai)
* @param {string[] | "any"} [fields.configurableFields] - Which model parameters are configurable:
* - undefined: No configurable fields.
* - "any": All fields are configurable. (See Security Note in description)
* - string[]: Specified fields are configurable.
* @param {string} [fields.configPrefix] - Prefix for configurable fields at runtime.
* @param {Record<string, any>} [fields.params] - Additional keyword args to pass to the ChatModel constructor.
* @returns {Promise<ConfigurableModel<RunInput, CallOptions>>} A class which extends BaseChatModel.
* @throws {Error} If modelProvider cannot be inferred or isn't supported.
* @throws {Error} If the model provider integration package is not installed.
*
* @example Initialize non-configurable models
* ```typescript
* import { initChatModel } from "langchain/chat_models/universal";
*
* const gpt4 = await initChatModel("openai:gpt-4", {
* temperature: 0.25,
* });
* const gpt4Result = await gpt4.invoke("what's your name");
*
* const claude = await initChatModel("anthropic:claude-3-opus-20240229", {
* temperature: 0.25,
* });
* const claudeResult = await claude.invoke("what's your name");
*
* const gemini = await initChatModel("gemini-1.5-pro", {
* modelProvider: "google-vertexai",
* temperature: 0.25,
* });
* const geminiResult = await gemini.invoke("what's your name");
* ```
*
* @example Create a partially configurable model with no default model
* ```typescript
* import { initChatModel } from "langchain/chat_models/universal";
*
* const configurableModel = await initChatModel(undefined, {
* temperature: 0,
* configurableFields: ["model", "apiKey"],
* });
*
* const gpt4Result = await configurableModel.invoke("what's your name", {
* configurable: {
* model: "gpt-4",
* },
* });
*
* const claudeResult = await configurableModel.invoke("what's your name", {
* configurable: {
* model: "claude-3-5-sonnet-20240620",
* },
* });
* ```
*
* @example Create a fully configurable model with a default model and a config prefix
* ```typescript
* import { initChatModel } from "langchain/chat_models/universal";
*
* const configurableModelWithDefault = await initChatModel("gpt-4", {
* modelProvider: "openai",
* configurableFields: "any",
* configPrefix: "foo",
* temperature: 0,
* });
*
* const openaiResult = await configurableModelWithDefault.invoke(
* "what's your name",
* {
* configurable: {
* foo_apiKey: process.env.OPENAI_API_KEY,
* },
* }
* );
*
* const claudeResult = await configurableModelWithDefault.invoke(
* "what's your name",
* {
* configurable: {
* foo_model: "claude-3-5-sonnet-20240620",
* foo_modelProvider: "anthropic",
* foo_temperature: 0.6,
* foo_apiKey: process.env.ANTHROPIC_API_KEY,
* },
* }
* );
* ```
*
* @example Bind tools to a configurable model:
* ```typescript
* import { initChatModel } from "langchain/chat_models/universal";
* import { z } from "zod";
* import { tool } from "@langchain/core/tools";
*
* const getWeatherTool = tool(
* (input) => {
* // Do something with the input
* return JSON.stringify(input);
* },
* {
* schema: z
* .object({
* location: z
* .string()
* .describe("The city and state, e.g. San Francisco, CA"),
* })
* .describe("Get the current weather in a given location"),
* name: "GetWeather",
* description: "Get the current weather in a given location",
* }
* );
*
* const getPopulationTool = tool(
* (input) => {
* // Do something with the input
* return JSON.stringify(input);
* },
* {
* schema: z
* .object({
* location: z
* .string()
* .describe("The city and state, e.g. San Francisco, CA"),
* })
* .describe("Get the current population in a given location"),
* name: "GetPopulation",
* description: "Get the current population in a given location",
* }
* );
*
* const configurableModel = await initChatModel("gpt-4", {
* configurableFields: ["model", "modelProvider", "apiKey"],
* temperature: 0,
* });
*
* const configurableModelWithTools = configurableModel.bind({
* tools: [getWeatherTool, getPopulationTool],
* });
*
* const configurableToolResult = await configurableModelWithTools.invoke(
* "Which city is hotter today and which is bigger: LA or NY?",
* {
* configurable: {
* apiKey: process.env.OPENAI_API_KEY,
* },
* }
* );
*
* const configurableToolResult2 = await configurableModelWithTools.invoke(
* "Which city is hotter today and which is bigger: LA or NY?",
* {
* configurable: {
* model: "claude-3-5-sonnet-20240620",
* apiKey: process.env.ANTHROPIC_API_KEY,
* },
* }
* );
* ```
*
* @description
* This function initializes a ChatModel based on the provided model name and provider.
* It supports various model providers and allows for runtime configuration of model parameters.
*
* Security Note: Setting `configurableFields` to "any" means fields like api_key, base_url, etc.
* can be altered at runtime, potentially redirecting model requests to a different service/user.
* Make sure that if you're accepting untrusted configurations, you enumerate the
* `configurableFields` explicitly.
*
* The function will attempt to infer the model provider from the model name if not specified.
* Certain model name prefixes are associated with specific providers:
* - gpt-3... or gpt-4... -> openai
* - claude... -> anthropic
* - amazon.... -> bedrock
* - gemini... -> google-vertexai
* - command... -> cohere
* - accounts/fireworks... -> fireworks
*
* @since 0.2.11
* @version 0.2.11
*/
export async function initChatModel<
RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,
CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions
>(
model?: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fields?: Partial<Record<string, any>> & {
modelProvider?: string;
configurableFields?: string[] | "any";
configPrefix?: string;
}
): Promise<ConfigurableModel<RunInput, CallOptions>> {
// eslint-disable-next-line prefer-const
let { configurableFields, configPrefix, modelProvider, ...params } = {
configPrefix: "",
...(fields ?? {}),
};
if (modelProvider === undefined && model?.includes(":")) {
const modelComponents = model.split(":", 2);
if (
_SUPPORTED_PROVIDERS.includes(modelComponents[0] as ChatModelProvider)
) {
// eslint-disable-next-line no-param-reassign
[modelProvider, model] = modelComponents;
}
}
let configurableFieldsCopy = Array.isArray(configurableFields)
? [...configurableFields]
: configurableFields;
if (!model && configurableFieldsCopy === undefined) {
configurableFieldsCopy = ["model", "modelProvider"];
}
if (configPrefix && configurableFieldsCopy === undefined) {
console.warn(
`{ configPrefix: ${configPrefix} } has been set but no fields are configurable. Set ` +
`{ configurableFields: [...] } to specify the model params that are ` +
`configurable.`
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const paramsCopy: Record<string, any> = { ...params };
if (configurableFieldsCopy === undefined) {
return new ConfigurableModel<RunInput, CallOptions>({
defaultConfig: {
...paramsCopy,
model,
modelProvider,
},
configPrefix,
});
} else {
if (model) {
paramsCopy.model = model;
}
if (modelProvider) {
paramsCopy.modelProvider = modelProvider;
}
return new ConfigurableModel<RunInput, CallOptions>({
defaultConfig: paramsCopy,
configPrefix,
configurableFields: configurableFieldsCopy,
});
}
}