@langchain/core 0.3.14 → 0.3.16
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/language_models/chat_models.cjs +4 -0
- package/dist/language_models/chat_models.js +4 -0
- package/dist/runnables/base.cjs +249 -1
- package/dist/runnables/base.d.ts +249 -1
- package/dist/runnables/base.js +249 -1
- package/dist/runnables/router.cjs +23 -0
- package/dist/runnables/router.d.ts +23 -0
- package/dist/runnables/router.js +23 -0
- package/dist/tracers/base.d.ts +6 -0
- package/dist/tracers/tracer_langchain.cjs +7 -1
- package/dist/tracers/tracer_langchain.js +7 -1
- package/package.json +2 -2
|
@@ -132,9 +132,13 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
getLsParams(options) {
|
|
135
|
+
const providerName = this.getName().startsWith("Chat")
|
|
136
|
+
? this.getName().replace("Chat", "")
|
|
137
|
+
: this.getName();
|
|
135
138
|
return {
|
|
136
139
|
ls_model_type: "chat",
|
|
137
140
|
ls_stop: options.stop,
|
|
141
|
+
ls_provider: providerName,
|
|
138
142
|
};
|
|
139
143
|
}
|
|
140
144
|
/** @ignore */
|
|
@@ -128,9 +128,13 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
getLsParams(options) {
|
|
131
|
+
const providerName = this.getName().startsWith("Chat")
|
|
132
|
+
? this.getName().replace("Chat", "")
|
|
133
|
+
: this.getName();
|
|
131
134
|
return {
|
|
132
135
|
ls_model_type: "chat",
|
|
133
136
|
ls_stop: options.stop,
|
|
137
|
+
ls_provider: providerName,
|
|
134
138
|
};
|
|
135
139
|
}
|
|
136
140
|
/** @ignore */
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -746,6 +746,43 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
746
746
|
exports.Runnable = Runnable;
|
|
747
747
|
/**
|
|
748
748
|
* A runnable that delegates calls to another runnable with a set of kwargs.
|
|
749
|
+
* @example
|
|
750
|
+
* ```typescript
|
|
751
|
+
* import {
|
|
752
|
+
* type RunnableConfig,
|
|
753
|
+
* RunnableLambda,
|
|
754
|
+
* } from "@langchain/core/runnables";
|
|
755
|
+
*
|
|
756
|
+
* const enhanceProfile = (
|
|
757
|
+
* profile: Record<string, any>,
|
|
758
|
+
* config?: RunnableConfig
|
|
759
|
+
* ) => {
|
|
760
|
+
* if (config?.configurable?.role) {
|
|
761
|
+
* return { ...profile, role: config.configurable.role };
|
|
762
|
+
* }
|
|
763
|
+
* return profile;
|
|
764
|
+
* };
|
|
765
|
+
*
|
|
766
|
+
* const runnable = RunnableLambda.from(enhanceProfile);
|
|
767
|
+
*
|
|
768
|
+
* // Bind configuration to the runnable to set the user's role dynamically
|
|
769
|
+
* const adminRunnable = runnable.bind({ configurable: { role: "Admin" } });
|
|
770
|
+
* const userRunnable = runnable.bind({ configurable: { role: "User" } });
|
|
771
|
+
*
|
|
772
|
+
* const result1 = await adminRunnable.invoke({
|
|
773
|
+
* name: "Alice",
|
|
774
|
+
* email: "alice@example.com"
|
|
775
|
+
* });
|
|
776
|
+
*
|
|
777
|
+
* // { name: "Alice", email: "alice@example.com", role: "Admin" }
|
|
778
|
+
*
|
|
779
|
+
* const result2 = await userRunnable.invoke({
|
|
780
|
+
* name: "Bob",
|
|
781
|
+
* email: "bob@example.com"
|
|
782
|
+
* });
|
|
783
|
+
*
|
|
784
|
+
* // { name: "Bob", email: "bob@example.com", role: "User" }
|
|
785
|
+
* ```
|
|
749
786
|
*/
|
|
750
787
|
class RunnableBinding extends Runnable {
|
|
751
788
|
static lc_name() {
|
|
@@ -898,6 +935,24 @@ exports.RunnableBinding = RunnableBinding;
|
|
|
898
935
|
/**
|
|
899
936
|
* A runnable that delegates calls to another runnable
|
|
900
937
|
* with each element of the input sequence.
|
|
938
|
+
* @example
|
|
939
|
+
* ```typescript
|
|
940
|
+
* import { RunnableEach, RunnableLambda } from "@langchain/core/runnables";
|
|
941
|
+
*
|
|
942
|
+
* const toUpperCase = (input: string): string => input.toUpperCase();
|
|
943
|
+
* const addGreeting = (input: string): string => `Hello, ${input}!`;
|
|
944
|
+
*
|
|
945
|
+
* const upperCaseLambda = RunnableLambda.from(toUpperCase);
|
|
946
|
+
* const greetingLambda = RunnableLambda.from(addGreeting);
|
|
947
|
+
*
|
|
948
|
+
* const chain = new RunnableEach({
|
|
949
|
+
* bound: upperCaseLambda.pipe(greetingLambda),
|
|
950
|
+
* });
|
|
951
|
+
*
|
|
952
|
+
* const result = await chain.invoke(["alice", "bob", "carol"])
|
|
953
|
+
*
|
|
954
|
+
* // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
|
|
955
|
+
* ```
|
|
901
956
|
*/
|
|
902
957
|
class RunnableEach extends Runnable {
|
|
903
958
|
static lc_name() {
|
|
@@ -974,6 +1029,45 @@ exports.RunnableEach = RunnableEach;
|
|
|
974
1029
|
/**
|
|
975
1030
|
* Base class for runnables that can be retried a
|
|
976
1031
|
* specified number of times.
|
|
1032
|
+
* @example
|
|
1033
|
+
* ```typescript
|
|
1034
|
+
* import {
|
|
1035
|
+
* RunnableLambda,
|
|
1036
|
+
* RunnableRetry,
|
|
1037
|
+
* } from "@langchain/core/runnables";
|
|
1038
|
+
*
|
|
1039
|
+
* // Simulate an API call that fails
|
|
1040
|
+
* const simulateApiCall = (input: string): string => {
|
|
1041
|
+
* console.log(`Attempting API call with input: ${input}`);
|
|
1042
|
+
* throw new Error("API call failed due to network issue");
|
|
1043
|
+
* };
|
|
1044
|
+
*
|
|
1045
|
+
* const apiCallLambda = RunnableLambda.from(simulateApiCall);
|
|
1046
|
+
*
|
|
1047
|
+
* // Apply retry logic using the .withRetry() method
|
|
1048
|
+
* const apiCallWithRetry = apiCallLambda.withRetry({ stopAfterAttempt: 3 });
|
|
1049
|
+
*
|
|
1050
|
+
* // Alternatively, create a RunnableRetry instance manually
|
|
1051
|
+
* const manualRetry = new RunnableRetry({
|
|
1052
|
+
* bound: apiCallLambda,
|
|
1053
|
+
* maxAttemptNumber: 3,
|
|
1054
|
+
* config: {},
|
|
1055
|
+
* });
|
|
1056
|
+
*
|
|
1057
|
+
* // Example invocation using the .withRetry() method
|
|
1058
|
+
* const res = await apiCallWithRetry
|
|
1059
|
+
* .invoke("Request 1")
|
|
1060
|
+
* .catch((error) => {
|
|
1061
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
1062
|
+
* });
|
|
1063
|
+
*
|
|
1064
|
+
* // Example invocation using the manual retry instance
|
|
1065
|
+
* const res2 = await manualRetry
|
|
1066
|
+
* .invoke("Request 2")
|
|
1067
|
+
* .catch((error) => {
|
|
1068
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
1069
|
+
* });
|
|
1070
|
+
* ```
|
|
977
1071
|
*/
|
|
978
1072
|
class RunnableRetry extends RunnableBinding {
|
|
979
1073
|
static lc_name() {
|
|
@@ -1502,7 +1596,30 @@ function assertNonTraceableFunction(func) {
|
|
|
1502
1596
|
}
|
|
1503
1597
|
}
|
|
1504
1598
|
/**
|
|
1505
|
-
* A runnable that
|
|
1599
|
+
* A runnable that wraps an arbitrary function that takes a single argument.
|
|
1600
|
+
* @example
|
|
1601
|
+
* ```typescript
|
|
1602
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
1603
|
+
*
|
|
1604
|
+
* const add = (input: { x: number; y: number }) => input.x + input.y;
|
|
1605
|
+
*
|
|
1606
|
+
* const multiply = (input: { value: number; multiplier: number }) =>
|
|
1607
|
+
* input.value * input.multiplier;
|
|
1608
|
+
*
|
|
1609
|
+
* // Create runnables for the functions
|
|
1610
|
+
* const addLambda = RunnableLambda.from(add);
|
|
1611
|
+
* const multiplyLambda = RunnableLambda.from(multiply);
|
|
1612
|
+
*
|
|
1613
|
+
* // Chain the lambdas for a mathematical operation
|
|
1614
|
+
* const chainedLambda = addLambda.pipe((result) =>
|
|
1615
|
+
* multiplyLambda.invoke({ value: result, multiplier: 2 })
|
|
1616
|
+
* );
|
|
1617
|
+
*
|
|
1618
|
+
* // Example invocation of the chainedLambda
|
|
1619
|
+
* const result = await chainedLambda.invoke({ x: 2, y: 3 });
|
|
1620
|
+
*
|
|
1621
|
+
* // Will log "10" (since (2 + 3) * 2 = 10)
|
|
1622
|
+
* ```
|
|
1506
1623
|
*/
|
|
1507
1624
|
class RunnableLambda extends Runnable {
|
|
1508
1625
|
static lc_name() {
|
|
@@ -1682,6 +1799,39 @@ class RunnableLambda extends Runnable {
|
|
|
1682
1799
|
}
|
|
1683
1800
|
}
|
|
1684
1801
|
exports.RunnableLambda = RunnableLambda;
|
|
1802
|
+
/**
|
|
1803
|
+
* A runnable that runs a mapping of runnables in parallel,
|
|
1804
|
+
* and returns a mapping of their outputs.
|
|
1805
|
+
* @example
|
|
1806
|
+
* ```typescript
|
|
1807
|
+
* import {
|
|
1808
|
+
* RunnableLambda,
|
|
1809
|
+
* RunnableParallel,
|
|
1810
|
+
* } from "@langchain/core/runnables";
|
|
1811
|
+
*
|
|
1812
|
+
* const addYears = (age: number): number => age + 5;
|
|
1813
|
+
* const yearsToFifty = (age: number): number => 50 - age;
|
|
1814
|
+
* const yearsToHundred = (age: number): number => 100 - age;
|
|
1815
|
+
*
|
|
1816
|
+
* const addYearsLambda = RunnableLambda.from(addYears);
|
|
1817
|
+
* const milestoneFiftyLambda = RunnableLambda.from(yearsToFifty);
|
|
1818
|
+
* const milestoneHundredLambda = RunnableLambda.from(yearsToHundred);
|
|
1819
|
+
*
|
|
1820
|
+
* // Pipe will coerce objects into RunnableParallel by default, but we
|
|
1821
|
+
* // explicitly instantiate one here to demonstrate
|
|
1822
|
+
* const sequence = addYearsLambda.pipe(
|
|
1823
|
+
* RunnableParallel.from({
|
|
1824
|
+
* years_to_fifty: milestoneFiftyLambda,
|
|
1825
|
+
* years_to_hundred: milestoneHundredLambda,
|
|
1826
|
+
* })
|
|
1827
|
+
* );
|
|
1828
|
+
*
|
|
1829
|
+
* // Invoke the sequence with a single age input
|
|
1830
|
+
* const res = sequence.invoke(25);
|
|
1831
|
+
*
|
|
1832
|
+
* // { years_to_fifty: 25, years_to_hundred: 75 }
|
|
1833
|
+
* ```
|
|
1834
|
+
*/
|
|
1685
1835
|
class RunnableParallel extends RunnableMap {
|
|
1686
1836
|
}
|
|
1687
1837
|
exports.RunnableParallel = RunnableParallel;
|
|
@@ -1703,6 +1853,55 @@ exports.RunnableParallel = RunnableParallel;
|
|
|
1703
1853
|
* When streaming, fallbacks will only be called on failures during the initial
|
|
1704
1854
|
* stream creation. Errors that occur after a stream starts will not fallback
|
|
1705
1855
|
* to the next Runnable.
|
|
1856
|
+
*
|
|
1857
|
+
* @example
|
|
1858
|
+
* ```typescript
|
|
1859
|
+
* import {
|
|
1860
|
+
* RunnableLambda,
|
|
1861
|
+
* RunnableWithFallbacks,
|
|
1862
|
+
* } from "@langchain/core/runnables";
|
|
1863
|
+
*
|
|
1864
|
+
* const primaryOperation = (input: string): string => {
|
|
1865
|
+
* if (input !== "safe") {
|
|
1866
|
+
* throw new Error("Primary operation failed due to unsafe input");
|
|
1867
|
+
* }
|
|
1868
|
+
* return `Processed: ${input}`;
|
|
1869
|
+
* };
|
|
1870
|
+
*
|
|
1871
|
+
* // Define a fallback operation that processes the input differently
|
|
1872
|
+
* const fallbackOperation = (input: string): string =>
|
|
1873
|
+
* `Fallback processed: ${input}`;
|
|
1874
|
+
*
|
|
1875
|
+
* const primaryRunnable = RunnableLambda.from(primaryOperation);
|
|
1876
|
+
* const fallbackRunnable = RunnableLambda.from(fallbackOperation);
|
|
1877
|
+
*
|
|
1878
|
+
* // Apply the fallback logic using the .withFallbacks() method
|
|
1879
|
+
* const runnableWithFallback = primaryRunnable.withFallbacks([fallbackRunnable]);
|
|
1880
|
+
*
|
|
1881
|
+
* // Alternatively, create a RunnableWithFallbacks instance manually
|
|
1882
|
+
* const manualFallbackChain = new RunnableWithFallbacks({
|
|
1883
|
+
* runnable: primaryRunnable,
|
|
1884
|
+
* fallbacks: [fallbackRunnable],
|
|
1885
|
+
* });
|
|
1886
|
+
*
|
|
1887
|
+
* // Example invocation using .withFallbacks()
|
|
1888
|
+
* const res = await runnableWithFallback
|
|
1889
|
+
* .invoke("unsafe input")
|
|
1890
|
+
* .catch((error) => {
|
|
1891
|
+
* console.error("Failed after all attempts:", error.message);
|
|
1892
|
+
* });
|
|
1893
|
+
*
|
|
1894
|
+
* // "Fallback processed: unsafe input"
|
|
1895
|
+
*
|
|
1896
|
+
* // Example invocation using manual instantiation
|
|
1897
|
+
* const res = await manualFallbackChain
|
|
1898
|
+
* .invoke("safe")
|
|
1899
|
+
* .catch((error) => {
|
|
1900
|
+
* console.error("Failed after all attempts:", error.message);
|
|
1901
|
+
* });
|
|
1902
|
+
*
|
|
1903
|
+
* // "Processed: safe"
|
|
1904
|
+
* ```
|
|
1706
1905
|
*/
|
|
1707
1906
|
class RunnableWithFallbacks extends Runnable {
|
|
1708
1907
|
static lc_name() {
|
|
@@ -1873,6 +2072,34 @@ function _coerceToRunnable(coerceable) {
|
|
|
1873
2072
|
exports._coerceToRunnable = _coerceToRunnable;
|
|
1874
2073
|
/**
|
|
1875
2074
|
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
2075
|
+
* @example
|
|
2076
|
+
* ```typescript
|
|
2077
|
+
* import {
|
|
2078
|
+
* RunnableAssign,
|
|
2079
|
+
* RunnableLambda,
|
|
2080
|
+
* RunnableParallel,
|
|
2081
|
+
* } from "@langchain/core/runnables";
|
|
2082
|
+
*
|
|
2083
|
+
* const calculateAge = (x: { birthYear: number }): { age: number } => {
|
|
2084
|
+
* const currentYear = new Date().getFullYear();
|
|
2085
|
+
* return { age: currentYear - x.birthYear };
|
|
2086
|
+
* };
|
|
2087
|
+
*
|
|
2088
|
+
* const createGreeting = (x: { name: string }): { greeting: string } => {
|
|
2089
|
+
* return { greeting: `Hello, ${x.name}!` };
|
|
2090
|
+
* };
|
|
2091
|
+
*
|
|
2092
|
+
* const mapper = RunnableParallel.from({
|
|
2093
|
+
* age_step: RunnableLambda.from(calculateAge),
|
|
2094
|
+
* greeting_step: RunnableLambda.from(createGreeting),
|
|
2095
|
+
* });
|
|
2096
|
+
*
|
|
2097
|
+
* const runnableAssign = new RunnableAssign({ mapper });
|
|
2098
|
+
*
|
|
2099
|
+
* const res = await runnableAssign.invoke({ name: "Alice", birthYear: 1990 });
|
|
2100
|
+
*
|
|
2101
|
+
* // { name: "Alice", birthYear: 1990, age_step: { age: 34 }, greeting_step: { greeting: "Hello, Alice!" } }
|
|
2102
|
+
* ```
|
|
1876
2103
|
*/
|
|
1877
2104
|
class RunnableAssign extends Runnable {
|
|
1878
2105
|
static lc_name() {
|
|
@@ -1956,6 +2183,27 @@ class RunnableAssign extends Runnable {
|
|
|
1956
2183
|
exports.RunnableAssign = RunnableAssign;
|
|
1957
2184
|
/**
|
|
1958
2185
|
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
2186
|
+
* Useful for streaming, can be automatically created and chained by calling `runnable.pick();`.
|
|
2187
|
+
* @example
|
|
2188
|
+
* ```typescript
|
|
2189
|
+
* import { RunnablePick } from "@langchain/core/runnables";
|
|
2190
|
+
*
|
|
2191
|
+
* const inputData = {
|
|
2192
|
+
* name: "John",
|
|
2193
|
+
* age: 30,
|
|
2194
|
+
* city: "New York",
|
|
2195
|
+
* country: "USA",
|
|
2196
|
+
* email: "john.doe@example.com",
|
|
2197
|
+
* phone: "+1234567890",
|
|
2198
|
+
* };
|
|
2199
|
+
*
|
|
2200
|
+
* const basicInfoRunnable = new RunnablePick(["name", "city"]);
|
|
2201
|
+
*
|
|
2202
|
+
* // Example invocation
|
|
2203
|
+
* const res = await basicInfoRunnable.invoke(inputData);
|
|
2204
|
+
*
|
|
2205
|
+
* // { name: 'John', city: 'New York' }
|
|
2206
|
+
* ```
|
|
1959
2207
|
*/
|
|
1960
2208
|
class RunnablePick extends Runnable {
|
|
1961
2209
|
static lc_name() {
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -317,6 +317,43 @@ export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends Runnabl
|
|
|
317
317
|
};
|
|
318
318
|
/**
|
|
319
319
|
* A runnable that delegates calls to another runnable with a set of kwargs.
|
|
320
|
+
* @example
|
|
321
|
+
* ```typescript
|
|
322
|
+
* import {
|
|
323
|
+
* type RunnableConfig,
|
|
324
|
+
* RunnableLambda,
|
|
325
|
+
* } from "@langchain/core/runnables";
|
|
326
|
+
*
|
|
327
|
+
* const enhanceProfile = (
|
|
328
|
+
* profile: Record<string, any>,
|
|
329
|
+
* config?: RunnableConfig
|
|
330
|
+
* ) => {
|
|
331
|
+
* if (config?.configurable?.role) {
|
|
332
|
+
* return { ...profile, role: config.configurable.role };
|
|
333
|
+
* }
|
|
334
|
+
* return profile;
|
|
335
|
+
* };
|
|
336
|
+
*
|
|
337
|
+
* const runnable = RunnableLambda.from(enhanceProfile);
|
|
338
|
+
*
|
|
339
|
+
* // Bind configuration to the runnable to set the user's role dynamically
|
|
340
|
+
* const adminRunnable = runnable.bind({ configurable: { role: "Admin" } });
|
|
341
|
+
* const userRunnable = runnable.bind({ configurable: { role: "User" } });
|
|
342
|
+
*
|
|
343
|
+
* const result1 = await adminRunnable.invoke({
|
|
344
|
+
* name: "Alice",
|
|
345
|
+
* email: "alice@example.com"
|
|
346
|
+
* });
|
|
347
|
+
*
|
|
348
|
+
* // { name: "Alice", email: "alice@example.com", role: "Admin" }
|
|
349
|
+
*
|
|
350
|
+
* const result2 = await userRunnable.invoke({
|
|
351
|
+
* name: "Bob",
|
|
352
|
+
* email: "bob@example.com"
|
|
353
|
+
* });
|
|
354
|
+
*
|
|
355
|
+
* // { name: "Bob", email: "bob@example.com", role: "User" }
|
|
356
|
+
* ```
|
|
320
357
|
*/
|
|
321
358
|
export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
|
|
322
359
|
static lc_name(): string;
|
|
@@ -374,6 +411,24 @@ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends Ru
|
|
|
374
411
|
/**
|
|
375
412
|
* A runnable that delegates calls to another runnable
|
|
376
413
|
* with each element of the input sequence.
|
|
414
|
+
* @example
|
|
415
|
+
* ```typescript
|
|
416
|
+
* import { RunnableEach, RunnableLambda } from "@langchain/core/runnables";
|
|
417
|
+
*
|
|
418
|
+
* const toUpperCase = (input: string): string => input.toUpperCase();
|
|
419
|
+
* const addGreeting = (input: string): string => `Hello, ${input}!`;
|
|
420
|
+
*
|
|
421
|
+
* const upperCaseLambda = RunnableLambda.from(toUpperCase);
|
|
422
|
+
* const greetingLambda = RunnableLambda.from(addGreeting);
|
|
423
|
+
*
|
|
424
|
+
* const chain = new RunnableEach({
|
|
425
|
+
* bound: upperCaseLambda.pipe(greetingLambda),
|
|
426
|
+
* });
|
|
427
|
+
*
|
|
428
|
+
* const result = await chain.invoke(["alice", "bob", "carol"])
|
|
429
|
+
*
|
|
430
|
+
* // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
|
|
431
|
+
* ```
|
|
377
432
|
*/
|
|
378
433
|
export declare class RunnableEach<RunInputItem, RunOutputItem, CallOptions extends RunnableConfig> extends Runnable<RunInputItem[], RunOutputItem[], CallOptions> {
|
|
379
434
|
static lc_name(): string;
|
|
@@ -423,6 +478,45 @@ export declare class RunnableEach<RunInputItem, RunOutputItem, CallOptions exten
|
|
|
423
478
|
/**
|
|
424
479
|
* Base class for runnables that can be retried a
|
|
425
480
|
* specified number of times.
|
|
481
|
+
* @example
|
|
482
|
+
* ```typescript
|
|
483
|
+
* import {
|
|
484
|
+
* RunnableLambda,
|
|
485
|
+
* RunnableRetry,
|
|
486
|
+
* } from "@langchain/core/runnables";
|
|
487
|
+
*
|
|
488
|
+
* // Simulate an API call that fails
|
|
489
|
+
* const simulateApiCall = (input: string): string => {
|
|
490
|
+
* console.log(`Attempting API call with input: ${input}`);
|
|
491
|
+
* throw new Error("API call failed due to network issue");
|
|
492
|
+
* };
|
|
493
|
+
*
|
|
494
|
+
* const apiCallLambda = RunnableLambda.from(simulateApiCall);
|
|
495
|
+
*
|
|
496
|
+
* // Apply retry logic using the .withRetry() method
|
|
497
|
+
* const apiCallWithRetry = apiCallLambda.withRetry({ stopAfterAttempt: 3 });
|
|
498
|
+
*
|
|
499
|
+
* // Alternatively, create a RunnableRetry instance manually
|
|
500
|
+
* const manualRetry = new RunnableRetry({
|
|
501
|
+
* bound: apiCallLambda,
|
|
502
|
+
* maxAttemptNumber: 3,
|
|
503
|
+
* config: {},
|
|
504
|
+
* });
|
|
505
|
+
*
|
|
506
|
+
* // Example invocation using the .withRetry() method
|
|
507
|
+
* const res = await apiCallWithRetry
|
|
508
|
+
* .invoke("Request 1")
|
|
509
|
+
* .catch((error) => {
|
|
510
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
511
|
+
* });
|
|
512
|
+
*
|
|
513
|
+
* // Example invocation using the manual retry instance
|
|
514
|
+
* const res2 = await manualRetry
|
|
515
|
+
* .invoke("Request 2")
|
|
516
|
+
* .catch((error) => {
|
|
517
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
518
|
+
* });
|
|
519
|
+
* ```
|
|
426
520
|
*/
|
|
427
521
|
export declare class RunnableRetry<RunInput = any, RunOutput = any, CallOptions extends RunnableConfig = RunnableConfig> extends RunnableBinding<RunInput, RunOutput, CallOptions> {
|
|
428
522
|
static lc_name(): string;
|
|
@@ -548,7 +642,30 @@ export declare class RunnableTraceable<RunInput, RunOutput> extends Runnable<Run
|
|
|
548
642
|
static from(func: AnyTraceableFunction): RunnableTraceable<unknown, unknown>;
|
|
549
643
|
}
|
|
550
644
|
/**
|
|
551
|
-
* A runnable that
|
|
645
|
+
* A runnable that wraps an arbitrary function that takes a single argument.
|
|
646
|
+
* @example
|
|
647
|
+
* ```typescript
|
|
648
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
649
|
+
*
|
|
650
|
+
* const add = (input: { x: number; y: number }) => input.x + input.y;
|
|
651
|
+
*
|
|
652
|
+
* const multiply = (input: { value: number; multiplier: number }) =>
|
|
653
|
+
* input.value * input.multiplier;
|
|
654
|
+
*
|
|
655
|
+
* // Create runnables for the functions
|
|
656
|
+
* const addLambda = RunnableLambda.from(add);
|
|
657
|
+
* const multiplyLambda = RunnableLambda.from(multiply);
|
|
658
|
+
*
|
|
659
|
+
* // Chain the lambdas for a mathematical operation
|
|
660
|
+
* const chainedLambda = addLambda.pipe((result) =>
|
|
661
|
+
* multiplyLambda.invoke({ value: result, multiplier: 2 })
|
|
662
|
+
* );
|
|
663
|
+
*
|
|
664
|
+
* // Example invocation of the chainedLambda
|
|
665
|
+
* const result = await chainedLambda.invoke({ x: 2, y: 3 });
|
|
666
|
+
*
|
|
667
|
+
* // Will log "10" (since (2 + 3) * 2 = 10)
|
|
668
|
+
* ```
|
|
552
669
|
*/
|
|
553
670
|
export declare class RunnableLambda<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
|
|
554
671
|
static lc_name(): string;
|
|
@@ -565,6 +682,39 @@ export declare class RunnableLambda<RunInput, RunOutput, CallOptions extends Run
|
|
|
565
682
|
transform(generator: AsyncGenerator<RunInput>, options?: Partial<CallOptions>): AsyncGenerator<RunOutput>;
|
|
566
683
|
stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStream<RunOutput>>;
|
|
567
684
|
}
|
|
685
|
+
/**
|
|
686
|
+
* A runnable that runs a mapping of runnables in parallel,
|
|
687
|
+
* and returns a mapping of their outputs.
|
|
688
|
+
* @example
|
|
689
|
+
* ```typescript
|
|
690
|
+
* import {
|
|
691
|
+
* RunnableLambda,
|
|
692
|
+
* RunnableParallel,
|
|
693
|
+
* } from "@langchain/core/runnables";
|
|
694
|
+
*
|
|
695
|
+
* const addYears = (age: number): number => age + 5;
|
|
696
|
+
* const yearsToFifty = (age: number): number => 50 - age;
|
|
697
|
+
* const yearsToHundred = (age: number): number => 100 - age;
|
|
698
|
+
*
|
|
699
|
+
* const addYearsLambda = RunnableLambda.from(addYears);
|
|
700
|
+
* const milestoneFiftyLambda = RunnableLambda.from(yearsToFifty);
|
|
701
|
+
* const milestoneHundredLambda = RunnableLambda.from(yearsToHundred);
|
|
702
|
+
*
|
|
703
|
+
* // Pipe will coerce objects into RunnableParallel by default, but we
|
|
704
|
+
* // explicitly instantiate one here to demonstrate
|
|
705
|
+
* const sequence = addYearsLambda.pipe(
|
|
706
|
+
* RunnableParallel.from({
|
|
707
|
+
* years_to_fifty: milestoneFiftyLambda,
|
|
708
|
+
* years_to_hundred: milestoneHundredLambda,
|
|
709
|
+
* })
|
|
710
|
+
* );
|
|
711
|
+
*
|
|
712
|
+
* // Invoke the sequence with a single age input
|
|
713
|
+
* const res = sequence.invoke(25);
|
|
714
|
+
*
|
|
715
|
+
* // { years_to_fifty: 25, years_to_hundred: 75 }
|
|
716
|
+
* ```
|
|
717
|
+
*/
|
|
568
718
|
export declare class RunnableParallel<RunInput> extends RunnableMap<RunInput> {
|
|
569
719
|
}
|
|
570
720
|
/**
|
|
@@ -585,6 +735,55 @@ export declare class RunnableParallel<RunInput> extends RunnableMap<RunInput> {
|
|
|
585
735
|
* When streaming, fallbacks will only be called on failures during the initial
|
|
586
736
|
* stream creation. Errors that occur after a stream starts will not fallback
|
|
587
737
|
* to the next Runnable.
|
|
738
|
+
*
|
|
739
|
+
* @example
|
|
740
|
+
* ```typescript
|
|
741
|
+
* import {
|
|
742
|
+
* RunnableLambda,
|
|
743
|
+
* RunnableWithFallbacks,
|
|
744
|
+
* } from "@langchain/core/runnables";
|
|
745
|
+
*
|
|
746
|
+
* const primaryOperation = (input: string): string => {
|
|
747
|
+
* if (input !== "safe") {
|
|
748
|
+
* throw new Error("Primary operation failed due to unsafe input");
|
|
749
|
+
* }
|
|
750
|
+
* return `Processed: ${input}`;
|
|
751
|
+
* };
|
|
752
|
+
*
|
|
753
|
+
* // Define a fallback operation that processes the input differently
|
|
754
|
+
* const fallbackOperation = (input: string): string =>
|
|
755
|
+
* `Fallback processed: ${input}`;
|
|
756
|
+
*
|
|
757
|
+
* const primaryRunnable = RunnableLambda.from(primaryOperation);
|
|
758
|
+
* const fallbackRunnable = RunnableLambda.from(fallbackOperation);
|
|
759
|
+
*
|
|
760
|
+
* // Apply the fallback logic using the .withFallbacks() method
|
|
761
|
+
* const runnableWithFallback = primaryRunnable.withFallbacks([fallbackRunnable]);
|
|
762
|
+
*
|
|
763
|
+
* // Alternatively, create a RunnableWithFallbacks instance manually
|
|
764
|
+
* const manualFallbackChain = new RunnableWithFallbacks({
|
|
765
|
+
* runnable: primaryRunnable,
|
|
766
|
+
* fallbacks: [fallbackRunnable],
|
|
767
|
+
* });
|
|
768
|
+
*
|
|
769
|
+
* // Example invocation using .withFallbacks()
|
|
770
|
+
* const res = await runnableWithFallback
|
|
771
|
+
* .invoke("unsafe input")
|
|
772
|
+
* .catch((error) => {
|
|
773
|
+
* console.error("Failed after all attempts:", error.message);
|
|
774
|
+
* });
|
|
775
|
+
*
|
|
776
|
+
* // "Fallback processed: unsafe input"
|
|
777
|
+
*
|
|
778
|
+
* // Example invocation using manual instantiation
|
|
779
|
+
* const res = await manualFallbackChain
|
|
780
|
+
* .invoke("safe")
|
|
781
|
+
* .catch((error) => {
|
|
782
|
+
* console.error("Failed after all attempts:", error.message);
|
|
783
|
+
* });
|
|
784
|
+
*
|
|
785
|
+
* // "Processed: safe"
|
|
786
|
+
* ```
|
|
588
787
|
*/
|
|
589
788
|
export declare class RunnableWithFallbacks<RunInput, RunOutput> extends Runnable<RunInput, RunOutput> {
|
|
590
789
|
static lc_name(): string;
|
|
@@ -613,6 +812,34 @@ export interface RunnableAssignFields<RunInput> {
|
|
|
613
812
|
}
|
|
614
813
|
/**
|
|
615
814
|
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
815
|
+
* @example
|
|
816
|
+
* ```typescript
|
|
817
|
+
* import {
|
|
818
|
+
* RunnableAssign,
|
|
819
|
+
* RunnableLambda,
|
|
820
|
+
* RunnableParallel,
|
|
821
|
+
* } from "@langchain/core/runnables";
|
|
822
|
+
*
|
|
823
|
+
* const calculateAge = (x: { birthYear: number }): { age: number } => {
|
|
824
|
+
* const currentYear = new Date().getFullYear();
|
|
825
|
+
* return { age: currentYear - x.birthYear };
|
|
826
|
+
* };
|
|
827
|
+
*
|
|
828
|
+
* const createGreeting = (x: { name: string }): { greeting: string } => {
|
|
829
|
+
* return { greeting: `Hello, ${x.name}!` };
|
|
830
|
+
* };
|
|
831
|
+
*
|
|
832
|
+
* const mapper = RunnableParallel.from({
|
|
833
|
+
* age_step: RunnableLambda.from(calculateAge),
|
|
834
|
+
* greeting_step: RunnableLambda.from(createGreeting),
|
|
835
|
+
* });
|
|
836
|
+
*
|
|
837
|
+
* const runnableAssign = new RunnableAssign({ mapper });
|
|
838
|
+
*
|
|
839
|
+
* const res = await runnableAssign.invoke({ name: "Alice", birthYear: 1990 });
|
|
840
|
+
*
|
|
841
|
+
* // { name: "Alice", birthYear: 1990, age_step: { age: 34 }, greeting_step: { greeting: "Hello, Alice!" } }
|
|
842
|
+
* ```
|
|
616
843
|
*/
|
|
617
844
|
export declare class RunnableAssign<RunInput extends Record<string, any> = Record<string, any>, RunOutput extends Record<string, any> = Record<string, any>, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput> implements RunnableAssignFields<RunInput> {
|
|
618
845
|
static lc_name(): string;
|
|
@@ -630,6 +857,27 @@ export interface RunnablePickFields {
|
|
|
630
857
|
}
|
|
631
858
|
/**
|
|
632
859
|
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
860
|
+
* Useful for streaming, can be automatically created and chained by calling `runnable.pick();`.
|
|
861
|
+
* @example
|
|
862
|
+
* ```typescript
|
|
863
|
+
* import { RunnablePick } from "@langchain/core/runnables";
|
|
864
|
+
*
|
|
865
|
+
* const inputData = {
|
|
866
|
+
* name: "John",
|
|
867
|
+
* age: 30,
|
|
868
|
+
* city: "New York",
|
|
869
|
+
* country: "USA",
|
|
870
|
+
* email: "john.doe@example.com",
|
|
871
|
+
* phone: "+1234567890",
|
|
872
|
+
* };
|
|
873
|
+
*
|
|
874
|
+
* const basicInfoRunnable = new RunnablePick(["name", "city"]);
|
|
875
|
+
*
|
|
876
|
+
* // Example invocation
|
|
877
|
+
* const res = await basicInfoRunnable.invoke(inputData);
|
|
878
|
+
*
|
|
879
|
+
* // { name: 'John', city: 'New York' }
|
|
880
|
+
* ```
|
|
633
881
|
*/
|
|
634
882
|
export declare class RunnablePick<RunInput extends Record<string, any> = Record<string, any>, RunOutput extends Record<string, any> | any = Record<string, any> | any, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput> implements RunnablePickFields {
|
|
635
883
|
static lc_name(): string;
|
package/dist/runnables/base.js
CHANGED
|
@@ -738,6 +738,43 @@ export class Runnable extends Serializable {
|
|
|
738
738
|
}
|
|
739
739
|
/**
|
|
740
740
|
* A runnable that delegates calls to another runnable with a set of kwargs.
|
|
741
|
+
* @example
|
|
742
|
+
* ```typescript
|
|
743
|
+
* import {
|
|
744
|
+
* type RunnableConfig,
|
|
745
|
+
* RunnableLambda,
|
|
746
|
+
* } from "@langchain/core/runnables";
|
|
747
|
+
*
|
|
748
|
+
* const enhanceProfile = (
|
|
749
|
+
* profile: Record<string, any>,
|
|
750
|
+
* config?: RunnableConfig
|
|
751
|
+
* ) => {
|
|
752
|
+
* if (config?.configurable?.role) {
|
|
753
|
+
* return { ...profile, role: config.configurable.role };
|
|
754
|
+
* }
|
|
755
|
+
* return profile;
|
|
756
|
+
* };
|
|
757
|
+
*
|
|
758
|
+
* const runnable = RunnableLambda.from(enhanceProfile);
|
|
759
|
+
*
|
|
760
|
+
* // Bind configuration to the runnable to set the user's role dynamically
|
|
761
|
+
* const adminRunnable = runnable.bind({ configurable: { role: "Admin" } });
|
|
762
|
+
* const userRunnable = runnable.bind({ configurable: { role: "User" } });
|
|
763
|
+
*
|
|
764
|
+
* const result1 = await adminRunnable.invoke({
|
|
765
|
+
* name: "Alice",
|
|
766
|
+
* email: "alice@example.com"
|
|
767
|
+
* });
|
|
768
|
+
*
|
|
769
|
+
* // { name: "Alice", email: "alice@example.com", role: "Admin" }
|
|
770
|
+
*
|
|
771
|
+
* const result2 = await userRunnable.invoke({
|
|
772
|
+
* name: "Bob",
|
|
773
|
+
* email: "bob@example.com"
|
|
774
|
+
* });
|
|
775
|
+
*
|
|
776
|
+
* // { name: "Bob", email: "bob@example.com", role: "User" }
|
|
777
|
+
* ```
|
|
741
778
|
*/
|
|
742
779
|
export class RunnableBinding extends Runnable {
|
|
743
780
|
static lc_name() {
|
|
@@ -889,6 +926,24 @@ export class RunnableBinding extends Runnable {
|
|
|
889
926
|
/**
|
|
890
927
|
* A runnable that delegates calls to another runnable
|
|
891
928
|
* with each element of the input sequence.
|
|
929
|
+
* @example
|
|
930
|
+
* ```typescript
|
|
931
|
+
* import { RunnableEach, RunnableLambda } from "@langchain/core/runnables";
|
|
932
|
+
*
|
|
933
|
+
* const toUpperCase = (input: string): string => input.toUpperCase();
|
|
934
|
+
* const addGreeting = (input: string): string => `Hello, ${input}!`;
|
|
935
|
+
*
|
|
936
|
+
* const upperCaseLambda = RunnableLambda.from(toUpperCase);
|
|
937
|
+
* const greetingLambda = RunnableLambda.from(addGreeting);
|
|
938
|
+
*
|
|
939
|
+
* const chain = new RunnableEach({
|
|
940
|
+
* bound: upperCaseLambda.pipe(greetingLambda),
|
|
941
|
+
* });
|
|
942
|
+
*
|
|
943
|
+
* const result = await chain.invoke(["alice", "bob", "carol"])
|
|
944
|
+
*
|
|
945
|
+
* // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
|
|
946
|
+
* ```
|
|
892
947
|
*/
|
|
893
948
|
export class RunnableEach extends Runnable {
|
|
894
949
|
static lc_name() {
|
|
@@ -964,6 +1019,45 @@ export class RunnableEach extends Runnable {
|
|
|
964
1019
|
/**
|
|
965
1020
|
* Base class for runnables that can be retried a
|
|
966
1021
|
* specified number of times.
|
|
1022
|
+
* @example
|
|
1023
|
+
* ```typescript
|
|
1024
|
+
* import {
|
|
1025
|
+
* RunnableLambda,
|
|
1026
|
+
* RunnableRetry,
|
|
1027
|
+
* } from "@langchain/core/runnables";
|
|
1028
|
+
*
|
|
1029
|
+
* // Simulate an API call that fails
|
|
1030
|
+
* const simulateApiCall = (input: string): string => {
|
|
1031
|
+
* console.log(`Attempting API call with input: ${input}`);
|
|
1032
|
+
* throw new Error("API call failed due to network issue");
|
|
1033
|
+
* };
|
|
1034
|
+
*
|
|
1035
|
+
* const apiCallLambda = RunnableLambda.from(simulateApiCall);
|
|
1036
|
+
*
|
|
1037
|
+
* // Apply retry logic using the .withRetry() method
|
|
1038
|
+
* const apiCallWithRetry = apiCallLambda.withRetry({ stopAfterAttempt: 3 });
|
|
1039
|
+
*
|
|
1040
|
+
* // Alternatively, create a RunnableRetry instance manually
|
|
1041
|
+
* const manualRetry = new RunnableRetry({
|
|
1042
|
+
* bound: apiCallLambda,
|
|
1043
|
+
* maxAttemptNumber: 3,
|
|
1044
|
+
* config: {},
|
|
1045
|
+
* });
|
|
1046
|
+
*
|
|
1047
|
+
* // Example invocation using the .withRetry() method
|
|
1048
|
+
* const res = await apiCallWithRetry
|
|
1049
|
+
* .invoke("Request 1")
|
|
1050
|
+
* .catch((error) => {
|
|
1051
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
1052
|
+
* });
|
|
1053
|
+
*
|
|
1054
|
+
* // Example invocation using the manual retry instance
|
|
1055
|
+
* const res2 = await manualRetry
|
|
1056
|
+
* .invoke("Request 2")
|
|
1057
|
+
* .catch((error) => {
|
|
1058
|
+
* console.error("Failed after multiple retries:", error.message);
|
|
1059
|
+
* });
|
|
1060
|
+
* ```
|
|
967
1061
|
*/
|
|
968
1062
|
export class RunnableRetry extends RunnableBinding {
|
|
969
1063
|
static lc_name() {
|
|
@@ -1488,7 +1582,30 @@ function assertNonTraceableFunction(func) {
|
|
|
1488
1582
|
}
|
|
1489
1583
|
}
|
|
1490
1584
|
/**
|
|
1491
|
-
* A runnable that
|
|
1585
|
+
* A runnable that wraps an arbitrary function that takes a single argument.
|
|
1586
|
+
* @example
|
|
1587
|
+
* ```typescript
|
|
1588
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
1589
|
+
*
|
|
1590
|
+
* const add = (input: { x: number; y: number }) => input.x + input.y;
|
|
1591
|
+
*
|
|
1592
|
+
* const multiply = (input: { value: number; multiplier: number }) =>
|
|
1593
|
+
* input.value * input.multiplier;
|
|
1594
|
+
*
|
|
1595
|
+
* // Create runnables for the functions
|
|
1596
|
+
* const addLambda = RunnableLambda.from(add);
|
|
1597
|
+
* const multiplyLambda = RunnableLambda.from(multiply);
|
|
1598
|
+
*
|
|
1599
|
+
* // Chain the lambdas for a mathematical operation
|
|
1600
|
+
* const chainedLambda = addLambda.pipe((result) =>
|
|
1601
|
+
* multiplyLambda.invoke({ value: result, multiplier: 2 })
|
|
1602
|
+
* );
|
|
1603
|
+
*
|
|
1604
|
+
* // Example invocation of the chainedLambda
|
|
1605
|
+
* const result = await chainedLambda.invoke({ x: 2, y: 3 });
|
|
1606
|
+
*
|
|
1607
|
+
* // Will log "10" (since (2 + 3) * 2 = 10)
|
|
1608
|
+
* ```
|
|
1492
1609
|
*/
|
|
1493
1610
|
export class RunnableLambda extends Runnable {
|
|
1494
1611
|
static lc_name() {
|
|
@@ -1667,6 +1784,39 @@ export class RunnableLambda extends Runnable {
|
|
|
1667
1784
|
return IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
|
|
1668
1785
|
}
|
|
1669
1786
|
}
|
|
1787
|
+
/**
|
|
1788
|
+
* A runnable that runs a mapping of runnables in parallel,
|
|
1789
|
+
* and returns a mapping of their outputs.
|
|
1790
|
+
* @example
|
|
1791
|
+
* ```typescript
|
|
1792
|
+
* import {
|
|
1793
|
+
* RunnableLambda,
|
|
1794
|
+
* RunnableParallel,
|
|
1795
|
+
* } from "@langchain/core/runnables";
|
|
1796
|
+
*
|
|
1797
|
+
* const addYears = (age: number): number => age + 5;
|
|
1798
|
+
* const yearsToFifty = (age: number): number => 50 - age;
|
|
1799
|
+
* const yearsToHundred = (age: number): number => 100 - age;
|
|
1800
|
+
*
|
|
1801
|
+
* const addYearsLambda = RunnableLambda.from(addYears);
|
|
1802
|
+
* const milestoneFiftyLambda = RunnableLambda.from(yearsToFifty);
|
|
1803
|
+
* const milestoneHundredLambda = RunnableLambda.from(yearsToHundred);
|
|
1804
|
+
*
|
|
1805
|
+
* // Pipe will coerce objects into RunnableParallel by default, but we
|
|
1806
|
+
* // explicitly instantiate one here to demonstrate
|
|
1807
|
+
* const sequence = addYearsLambda.pipe(
|
|
1808
|
+
* RunnableParallel.from({
|
|
1809
|
+
* years_to_fifty: milestoneFiftyLambda,
|
|
1810
|
+
* years_to_hundred: milestoneHundredLambda,
|
|
1811
|
+
* })
|
|
1812
|
+
* );
|
|
1813
|
+
*
|
|
1814
|
+
* // Invoke the sequence with a single age input
|
|
1815
|
+
* const res = sequence.invoke(25);
|
|
1816
|
+
*
|
|
1817
|
+
* // { years_to_fifty: 25, years_to_hundred: 75 }
|
|
1818
|
+
* ```
|
|
1819
|
+
*/
|
|
1670
1820
|
export class RunnableParallel extends RunnableMap {
|
|
1671
1821
|
}
|
|
1672
1822
|
/**
|
|
@@ -1687,6 +1837,55 @@ export class RunnableParallel extends RunnableMap {
|
|
|
1687
1837
|
* When streaming, fallbacks will only be called on failures during the initial
|
|
1688
1838
|
* stream creation. Errors that occur after a stream starts will not fallback
|
|
1689
1839
|
* to the next Runnable.
|
|
1840
|
+
*
|
|
1841
|
+
* @example
|
|
1842
|
+
* ```typescript
|
|
1843
|
+
* import {
|
|
1844
|
+
* RunnableLambda,
|
|
1845
|
+
* RunnableWithFallbacks,
|
|
1846
|
+
* } from "@langchain/core/runnables";
|
|
1847
|
+
*
|
|
1848
|
+
* const primaryOperation = (input: string): string => {
|
|
1849
|
+
* if (input !== "safe") {
|
|
1850
|
+
* throw new Error("Primary operation failed due to unsafe input");
|
|
1851
|
+
* }
|
|
1852
|
+
* return `Processed: ${input}`;
|
|
1853
|
+
* };
|
|
1854
|
+
*
|
|
1855
|
+
* // Define a fallback operation that processes the input differently
|
|
1856
|
+
* const fallbackOperation = (input: string): string =>
|
|
1857
|
+
* `Fallback processed: ${input}`;
|
|
1858
|
+
*
|
|
1859
|
+
* const primaryRunnable = RunnableLambda.from(primaryOperation);
|
|
1860
|
+
* const fallbackRunnable = RunnableLambda.from(fallbackOperation);
|
|
1861
|
+
*
|
|
1862
|
+
* // Apply the fallback logic using the .withFallbacks() method
|
|
1863
|
+
* const runnableWithFallback = primaryRunnable.withFallbacks([fallbackRunnable]);
|
|
1864
|
+
*
|
|
1865
|
+
* // Alternatively, create a RunnableWithFallbacks instance manually
|
|
1866
|
+
* const manualFallbackChain = new RunnableWithFallbacks({
|
|
1867
|
+
* runnable: primaryRunnable,
|
|
1868
|
+
* fallbacks: [fallbackRunnable],
|
|
1869
|
+
* });
|
|
1870
|
+
*
|
|
1871
|
+
* // Example invocation using .withFallbacks()
|
|
1872
|
+
* const res = await runnableWithFallback
|
|
1873
|
+
* .invoke("unsafe input")
|
|
1874
|
+
* .catch((error) => {
|
|
1875
|
+
* console.error("Failed after all attempts:", error.message);
|
|
1876
|
+
* });
|
|
1877
|
+
*
|
|
1878
|
+
* // "Fallback processed: unsafe input"
|
|
1879
|
+
*
|
|
1880
|
+
* // Example invocation using manual instantiation
|
|
1881
|
+
* const res = await manualFallbackChain
|
|
1882
|
+
* .invoke("safe")
|
|
1883
|
+
* .catch((error) => {
|
|
1884
|
+
* console.error("Failed after all attempts:", error.message);
|
|
1885
|
+
* });
|
|
1886
|
+
*
|
|
1887
|
+
* // "Processed: safe"
|
|
1888
|
+
* ```
|
|
1690
1889
|
*/
|
|
1691
1890
|
export class RunnableWithFallbacks extends Runnable {
|
|
1692
1891
|
static lc_name() {
|
|
@@ -1855,6 +2054,34 @@ export function _coerceToRunnable(coerceable) {
|
|
|
1855
2054
|
}
|
|
1856
2055
|
/**
|
|
1857
2056
|
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
2057
|
+
* @example
|
|
2058
|
+
* ```typescript
|
|
2059
|
+
* import {
|
|
2060
|
+
* RunnableAssign,
|
|
2061
|
+
* RunnableLambda,
|
|
2062
|
+
* RunnableParallel,
|
|
2063
|
+
* } from "@langchain/core/runnables";
|
|
2064
|
+
*
|
|
2065
|
+
* const calculateAge = (x: { birthYear: number }): { age: number } => {
|
|
2066
|
+
* const currentYear = new Date().getFullYear();
|
|
2067
|
+
* return { age: currentYear - x.birthYear };
|
|
2068
|
+
* };
|
|
2069
|
+
*
|
|
2070
|
+
* const createGreeting = (x: { name: string }): { greeting: string } => {
|
|
2071
|
+
* return { greeting: `Hello, ${x.name}!` };
|
|
2072
|
+
* };
|
|
2073
|
+
*
|
|
2074
|
+
* const mapper = RunnableParallel.from({
|
|
2075
|
+
* age_step: RunnableLambda.from(calculateAge),
|
|
2076
|
+
* greeting_step: RunnableLambda.from(createGreeting),
|
|
2077
|
+
* });
|
|
2078
|
+
*
|
|
2079
|
+
* const runnableAssign = new RunnableAssign({ mapper });
|
|
2080
|
+
*
|
|
2081
|
+
* const res = await runnableAssign.invoke({ name: "Alice", birthYear: 1990 });
|
|
2082
|
+
*
|
|
2083
|
+
* // { name: "Alice", birthYear: 1990, age_step: { age: 34 }, greeting_step: { greeting: "Hello, Alice!" } }
|
|
2084
|
+
* ```
|
|
1858
2085
|
*/
|
|
1859
2086
|
export class RunnableAssign extends Runnable {
|
|
1860
2087
|
static lc_name() {
|
|
@@ -1937,6 +2164,27 @@ export class RunnableAssign extends Runnable {
|
|
|
1937
2164
|
}
|
|
1938
2165
|
/**
|
|
1939
2166
|
* A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
|
|
2167
|
+
* Useful for streaming, can be automatically created and chained by calling `runnable.pick();`.
|
|
2168
|
+
* @example
|
|
2169
|
+
* ```typescript
|
|
2170
|
+
* import { RunnablePick } from "@langchain/core/runnables";
|
|
2171
|
+
*
|
|
2172
|
+
* const inputData = {
|
|
2173
|
+
* name: "John",
|
|
2174
|
+
* age: 30,
|
|
2175
|
+
* city: "New York",
|
|
2176
|
+
* country: "USA",
|
|
2177
|
+
* email: "john.doe@example.com",
|
|
2178
|
+
* phone: "+1234567890",
|
|
2179
|
+
* };
|
|
2180
|
+
*
|
|
2181
|
+
* const basicInfoRunnable = new RunnablePick(["name", "city"]);
|
|
2182
|
+
*
|
|
2183
|
+
* // Example invocation
|
|
2184
|
+
* const res = await basicInfoRunnable.invoke(inputData);
|
|
2185
|
+
*
|
|
2186
|
+
* // { name: 'John', city: 'New York' }
|
|
2187
|
+
* ```
|
|
1940
2188
|
*/
|
|
1941
2189
|
export class RunnablePick extends Runnable {
|
|
1942
2190
|
static lc_name() {
|
|
@@ -6,6 +6,29 @@ const config_js_1 = require("./config.cjs");
|
|
|
6
6
|
/**
|
|
7
7
|
* A runnable that routes to a set of runnables based on Input['key'].
|
|
8
8
|
* Returns the output of the selected runnable.
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { RouterRunnable, RunnableLambda } from "@langchain/core/runnables";
|
|
12
|
+
*
|
|
13
|
+
* const router = new RouterRunnable({
|
|
14
|
+
* runnables: {
|
|
15
|
+
* toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),
|
|
16
|
+
* reverseText: RunnableLambda.from((text: string) =>
|
|
17
|
+
* text.split("").reverse().join("")
|
|
18
|
+
* ),
|
|
19
|
+
* },
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* // Invoke the 'reverseText' runnable
|
|
23
|
+
* const result1 = router.invoke({ key: "reverseText", input: "Hello World" });
|
|
24
|
+
*
|
|
25
|
+
* // "dlroW olleH"
|
|
26
|
+
*
|
|
27
|
+
* // Invoke the 'toUpperCase' runnable
|
|
28
|
+
* const result2 = router.invoke({ key: "toUpperCase", input: "Hello World" });
|
|
29
|
+
*
|
|
30
|
+
* // "HELLO WORLD"
|
|
31
|
+
* ```
|
|
9
32
|
*/
|
|
10
33
|
class RouterRunnable extends base_js_1.Runnable {
|
|
11
34
|
static lc_name() {
|
|
@@ -8,6 +8,29 @@ export type RouterInput = {
|
|
|
8
8
|
/**
|
|
9
9
|
* A runnable that routes to a set of runnables based on Input['key'].
|
|
10
10
|
* Returns the output of the selected runnable.
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { RouterRunnable, RunnableLambda } from "@langchain/core/runnables";
|
|
14
|
+
*
|
|
15
|
+
* const router = new RouterRunnable({
|
|
16
|
+
* runnables: {
|
|
17
|
+
* toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),
|
|
18
|
+
* reverseText: RunnableLambda.from((text: string) =>
|
|
19
|
+
* text.split("").reverse().join("")
|
|
20
|
+
* ),
|
|
21
|
+
* },
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Invoke the 'reverseText' runnable
|
|
25
|
+
* const result1 = router.invoke({ key: "reverseText", input: "Hello World" });
|
|
26
|
+
*
|
|
27
|
+
* // "dlroW olleH"
|
|
28
|
+
*
|
|
29
|
+
* // Invoke the 'toUpperCase' runnable
|
|
30
|
+
* const result2 = router.invoke({ key: "toUpperCase", input: "Hello World" });
|
|
31
|
+
*
|
|
32
|
+
* // "HELLO WORLD"
|
|
33
|
+
* ```
|
|
11
34
|
*/
|
|
12
35
|
export declare class RouterRunnable<RunInput extends RouterInput, RunnableInput, RunOutput> extends Runnable<RunInput, RunOutput> {
|
|
13
36
|
static lc_name(): string;
|
package/dist/runnables/router.js
CHANGED
|
@@ -3,6 +3,29 @@ import { ensureConfig } from "./config.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* A runnable that routes to a set of runnables based on Input['key'].
|
|
5
5
|
* Returns the output of the selected runnable.
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { RouterRunnable, RunnableLambda } from "@langchain/core/runnables";
|
|
9
|
+
*
|
|
10
|
+
* const router = new RouterRunnable({
|
|
11
|
+
* runnables: {
|
|
12
|
+
* toUpperCase: RunnableLambda.from((text: string) => text.toUpperCase()),
|
|
13
|
+
* reverseText: RunnableLambda.from((text: string) =>
|
|
14
|
+
* text.split("").reverse().join("")
|
|
15
|
+
* ),
|
|
16
|
+
* },
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Invoke the 'reverseText' runnable
|
|
20
|
+
* const result1 = router.invoke({ key: "reverseText", input: "Hello World" });
|
|
21
|
+
*
|
|
22
|
+
* // "dlroW olleH"
|
|
23
|
+
*
|
|
24
|
+
* // Invoke the 'toUpperCase' runnable
|
|
25
|
+
* const result2 = router.invoke({ key: "toUpperCase", input: "Hello World" });
|
|
26
|
+
*
|
|
27
|
+
* // "HELLO WORLD"
|
|
28
|
+
* ```
|
|
6
29
|
*/
|
|
7
30
|
export class RouterRunnable extends Runnable {
|
|
8
31
|
static lc_name() {
|
package/dist/tracers/base.d.ts
CHANGED
|
@@ -56,6 +56,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
56
56
|
reference_example_id?: string | undefined;
|
|
57
57
|
parent_run_id?: string | undefined;
|
|
58
58
|
tags?: string[] | undefined;
|
|
59
|
+
attachments?: Record<string, [string, Uint8Array]> | undefined;
|
|
59
60
|
};
|
|
60
61
|
protected _endTrace(run: Run): Promise<void>;
|
|
61
62
|
protected _getExecutionOrder(parentRunId: string | undefined): number;
|
|
@@ -88,6 +89,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
88
89
|
reference_example_id?: string | undefined;
|
|
89
90
|
parent_run_id?: string | undefined;
|
|
90
91
|
tags?: string[] | undefined;
|
|
92
|
+
attachments?: Record<string, [string, Uint8Array]> | undefined;
|
|
91
93
|
};
|
|
92
94
|
handleLLMStart(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
93
95
|
/**
|
|
@@ -119,6 +121,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
119
121
|
reference_example_id?: string | undefined;
|
|
120
122
|
parent_run_id?: string | undefined;
|
|
121
123
|
tags?: string[] | undefined;
|
|
124
|
+
attachments?: Record<string, [string, Uint8Array]> | undefined;
|
|
122
125
|
};
|
|
123
126
|
handleChatModelStart(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
124
127
|
handleLLMEnd(output: LLMResult, runId: string): Promise<Run>;
|
|
@@ -152,6 +155,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
152
155
|
reference_example_id?: string | undefined;
|
|
153
156
|
parent_run_id?: string | undefined;
|
|
154
157
|
tags?: string[] | undefined;
|
|
158
|
+
attachments?: Record<string, [string, Uint8Array]> | undefined;
|
|
155
159
|
};
|
|
156
160
|
handleChainStart(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, runType?: string, name?: string): Promise<Run>;
|
|
157
161
|
handleChainEnd(outputs: ChainValues, runId: string, _parentRunId?: string, _tags?: string[], kwargs?: {
|
|
@@ -189,6 +193,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
189
193
|
reference_example_id?: string | undefined;
|
|
190
194
|
parent_run_id?: string | undefined;
|
|
191
195
|
tags?: string[] | undefined;
|
|
196
|
+
attachments?: Record<string, [string, Uint8Array]> | undefined;
|
|
192
197
|
};
|
|
193
198
|
handleToolStart(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
194
199
|
handleToolEnd(output: any, runId: string): Promise<Run>;
|
|
@@ -224,6 +229,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
224
229
|
reference_example_id?: string | undefined;
|
|
225
230
|
parent_run_id?: string | undefined;
|
|
226
231
|
tags?: string[] | undefined;
|
|
232
|
+
attachments?: Record<string, [string, Uint8Array]> | undefined;
|
|
227
233
|
};
|
|
228
234
|
handleRetrieverStart(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
|
|
229
235
|
handleRetrieverEnd(documents: Document<Record<string, unknown>>[], runId: string): Promise<Run>;
|
|
@@ -39,7 +39,13 @@ class LangChainTracer extends base_js_1.BaseTracer {
|
|
|
39
39
|
(0, env_js_1.getEnvironmentVariable)("LANGCHAIN_PROJECT") ??
|
|
40
40
|
(0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION");
|
|
41
41
|
this.exampleId = exampleId;
|
|
42
|
-
|
|
42
|
+
const clientParams = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
|
|
43
|
+
? {
|
|
44
|
+
// LangSmith has its own backgrounding system
|
|
45
|
+
blockOnRootRunFinalization: true,
|
|
46
|
+
}
|
|
47
|
+
: {};
|
|
48
|
+
this.client = client ?? new langsmith_1.Client(clientParams);
|
|
43
49
|
const traceableTree = LangChainTracer.getTraceableRunTree();
|
|
44
50
|
if (traceableTree) {
|
|
45
51
|
this.updateFromRunTree(traceableTree);
|
|
@@ -36,7 +36,13 @@ export class LangChainTracer extends BaseTracer {
|
|
|
36
36
|
getEnvironmentVariable("LANGCHAIN_PROJECT") ??
|
|
37
37
|
getEnvironmentVariable("LANGCHAIN_SESSION");
|
|
38
38
|
this.exampleId = exampleId;
|
|
39
|
-
|
|
39
|
+
const clientParams = getEnvironmentVariable("LANGCHAIN_CALLBACKS_BACKGROUND") === "false"
|
|
40
|
+
? {
|
|
41
|
+
// LangSmith has its own backgrounding system
|
|
42
|
+
blockOnRootRunFinalization: true,
|
|
43
|
+
}
|
|
44
|
+
: {};
|
|
45
|
+
this.client = client ?? new Client(clientParams);
|
|
40
46
|
const traceableTree = LangChainTracer.getTraceableRunTree();
|
|
41
47
|
if (traceableTree) {
|
|
42
48
|
this.updateFromRunTree(traceableTree);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.16",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"camelcase": "6",
|
|
38
38
|
"decamelize": "1.2.0",
|
|
39
39
|
"js-tiktoken": "^1.0.12",
|
|
40
|
-
"langsmith": "^0.
|
|
40
|
+
"langsmith": "^0.2.0",
|
|
41
41
|
"mustache": "^4.2.0",
|
|
42
42
|
"p-queue": "^6.6.2",
|
|
43
43
|
"p-retry": "4",
|