@langchain/core 0.2.22-rc.1 → 0.2.23
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/runnables/base.cjs
CHANGED
|
@@ -111,10 +111,11 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
111
111
|
* @returns A new RunnableWithFallbacks.
|
|
112
112
|
*/
|
|
113
113
|
withFallbacks(fields) {
|
|
114
|
+
const fallbacks = Array.isArray(fields) ? fields : fields.fallbacks;
|
|
114
115
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
115
116
|
return new RunnableWithFallbacks({
|
|
116
117
|
runnable: this,
|
|
117
|
-
fallbacks
|
|
118
|
+
fallbacks,
|
|
118
119
|
});
|
|
119
120
|
}
|
|
120
121
|
_getOptionsList(options, length = 0) {
|
|
@@ -1673,6 +1674,22 @@ class RunnableParallel extends RunnableMap {
|
|
|
1673
1674
|
exports.RunnableParallel = RunnableParallel;
|
|
1674
1675
|
/**
|
|
1675
1676
|
* A Runnable that can fallback to other Runnables if it fails.
|
|
1677
|
+
* External APIs (e.g., APIs for a language model) may at times experience
|
|
1678
|
+
* degraded performance or even downtime.
|
|
1679
|
+
*
|
|
1680
|
+
* In these cases, it can be useful to have a fallback Runnable that can be
|
|
1681
|
+
* used in place of the original Runnable (e.g., fallback to another LLM provider).
|
|
1682
|
+
*
|
|
1683
|
+
* Fallbacks can be defined at the level of a single Runnable, or at the level
|
|
1684
|
+
* of a chain of Runnables. Fallbacks are tried in order until one succeeds or
|
|
1685
|
+
* all fail.
|
|
1686
|
+
*
|
|
1687
|
+
* While you can instantiate a `RunnableWithFallbacks` directly, it is usually
|
|
1688
|
+
* more convenient to use the `withFallbacks` method on an existing Runnable.
|
|
1689
|
+
*
|
|
1690
|
+
* When streaming, fallbacks will only be called on failures during the initial
|
|
1691
|
+
* stream creation. Errors that occur after a stream starts will not fallback
|
|
1692
|
+
* to the next Runnable.
|
|
1676
1693
|
*/
|
|
1677
1694
|
class RunnableWithFallbacks extends Runnable {
|
|
1678
1695
|
static lc_name() {
|
|
@@ -1738,6 +1755,51 @@ class RunnableWithFallbacks extends Runnable {
|
|
|
1738
1755
|
await runManager?.handleChainError(firstError);
|
|
1739
1756
|
throw firstError;
|
|
1740
1757
|
}
|
|
1758
|
+
async *_streamIterator(input, options) {
|
|
1759
|
+
const config = (0, config_js_1.ensureConfig)(options);
|
|
1760
|
+
const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(options);
|
|
1761
|
+
const { runId, ...otherConfigFields } = config;
|
|
1762
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
|
|
1763
|
+
let firstError;
|
|
1764
|
+
let stream;
|
|
1765
|
+
for (const runnable of this.runnables()) {
|
|
1766
|
+
config?.signal?.throwIfAborted();
|
|
1767
|
+
const childConfig = (0, config_js_1.patchConfig)(otherConfigFields, {
|
|
1768
|
+
callbacks: runManager?.getChild(),
|
|
1769
|
+
});
|
|
1770
|
+
try {
|
|
1771
|
+
stream = await runnable.stream(input, childConfig);
|
|
1772
|
+
break;
|
|
1773
|
+
}
|
|
1774
|
+
catch (e) {
|
|
1775
|
+
if (firstError === undefined) {
|
|
1776
|
+
firstError = e;
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
if (stream === undefined) {
|
|
1781
|
+
const error = firstError ?? new Error("No error stored at end of fallback.");
|
|
1782
|
+
await runManager?.handleChainError(error);
|
|
1783
|
+
throw error;
|
|
1784
|
+
}
|
|
1785
|
+
let output;
|
|
1786
|
+
try {
|
|
1787
|
+
for await (const chunk of stream) {
|
|
1788
|
+
yield chunk;
|
|
1789
|
+
try {
|
|
1790
|
+
output = output === undefined ? output : (0, stream_js_1.concat)(output, chunk);
|
|
1791
|
+
}
|
|
1792
|
+
catch (e) {
|
|
1793
|
+
output = undefined;
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
catch (e) {
|
|
1798
|
+
await runManager?.handleChainError(e);
|
|
1799
|
+
throw e;
|
|
1800
|
+
}
|
|
1801
|
+
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1802
|
+
}
|
|
1741
1803
|
async batch(inputs, options, batchOptions) {
|
|
1742
1804
|
if (batchOptions?.returnExceptions) {
|
|
1743
1805
|
throw new Error("Not implemented.");
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -67,7 +67,7 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
67
67
|
*/
|
|
68
68
|
withFallbacks(fields: {
|
|
69
69
|
fallbacks: Runnable<RunInput, RunOutput>[];
|
|
70
|
-
}): RunnableWithFallbacks<RunInput, RunOutput>;
|
|
70
|
+
} | Runnable<RunInput, RunOutput>[]): RunnableWithFallbacks<RunInput, RunOutput>;
|
|
71
71
|
protected _getOptionsList<O extends CallOptions & {
|
|
72
72
|
runType?: string;
|
|
73
73
|
}>(options: Partial<O> | Partial<O>[], length?: number): Partial<O>[];
|
|
@@ -568,6 +568,22 @@ export declare class RunnableParallel<RunInput> extends RunnableMap<RunInput> {
|
|
|
568
568
|
}
|
|
569
569
|
/**
|
|
570
570
|
* A Runnable that can fallback to other Runnables if it fails.
|
|
571
|
+
* External APIs (e.g., APIs for a language model) may at times experience
|
|
572
|
+
* degraded performance or even downtime.
|
|
573
|
+
*
|
|
574
|
+
* In these cases, it can be useful to have a fallback Runnable that can be
|
|
575
|
+
* used in place of the original Runnable (e.g., fallback to another LLM provider).
|
|
576
|
+
*
|
|
577
|
+
* Fallbacks can be defined at the level of a single Runnable, or at the level
|
|
578
|
+
* of a chain of Runnables. Fallbacks are tried in order until one succeeds or
|
|
579
|
+
* all fail.
|
|
580
|
+
*
|
|
581
|
+
* While you can instantiate a `RunnableWithFallbacks` directly, it is usually
|
|
582
|
+
* more convenient to use the `withFallbacks` method on an existing Runnable.
|
|
583
|
+
*
|
|
584
|
+
* When streaming, fallbacks will only be called on failures during the initial
|
|
585
|
+
* stream creation. Errors that occur after a stream starts will not fallback
|
|
586
|
+
* to the next Runnable.
|
|
571
587
|
*/
|
|
572
588
|
export declare class RunnableWithFallbacks<RunInput, RunOutput> extends Runnable<RunInput, RunOutput> {
|
|
573
589
|
static lc_name(): string;
|
|
@@ -581,6 +597,7 @@ export declare class RunnableWithFallbacks<RunInput, RunOutput> extends Runnable
|
|
|
581
597
|
});
|
|
582
598
|
runnables(): Generator<Runnable<RunInput, RunOutput, RunnableConfig>, void, unknown>;
|
|
583
599
|
invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunOutput>;
|
|
600
|
+
_streamIterator(input: RunInput, options?: Partial<RunnableConfig> | undefined): AsyncGenerator<RunOutput>;
|
|
584
601
|
batch(inputs: RunInput[], options?: Partial<RunnableConfig> | Partial<RunnableConfig>[], batchOptions?: RunnableBatchOptions & {
|
|
585
602
|
returnExceptions?: false;
|
|
586
603
|
}): Promise<RunOutput[]>;
|
package/dist/runnables/base.js
CHANGED
|
@@ -104,10 +104,11 @@ export class Runnable extends Serializable {
|
|
|
104
104
|
* @returns A new RunnableWithFallbacks.
|
|
105
105
|
*/
|
|
106
106
|
withFallbacks(fields) {
|
|
107
|
+
const fallbacks = Array.isArray(fields) ? fields : fields.fallbacks;
|
|
107
108
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
108
109
|
return new RunnableWithFallbacks({
|
|
109
110
|
runnable: this,
|
|
110
|
-
fallbacks
|
|
111
|
+
fallbacks,
|
|
111
112
|
});
|
|
112
113
|
}
|
|
113
114
|
_getOptionsList(options, length = 0) {
|
|
@@ -1657,6 +1658,22 @@ export class RunnableParallel extends RunnableMap {
|
|
|
1657
1658
|
}
|
|
1658
1659
|
/**
|
|
1659
1660
|
* A Runnable that can fallback to other Runnables if it fails.
|
|
1661
|
+
* External APIs (e.g., APIs for a language model) may at times experience
|
|
1662
|
+
* degraded performance or even downtime.
|
|
1663
|
+
*
|
|
1664
|
+
* In these cases, it can be useful to have a fallback Runnable that can be
|
|
1665
|
+
* used in place of the original Runnable (e.g., fallback to another LLM provider).
|
|
1666
|
+
*
|
|
1667
|
+
* Fallbacks can be defined at the level of a single Runnable, or at the level
|
|
1668
|
+
* of a chain of Runnables. Fallbacks are tried in order until one succeeds or
|
|
1669
|
+
* all fail.
|
|
1670
|
+
*
|
|
1671
|
+
* While you can instantiate a `RunnableWithFallbacks` directly, it is usually
|
|
1672
|
+
* more convenient to use the `withFallbacks` method on an existing Runnable.
|
|
1673
|
+
*
|
|
1674
|
+
* When streaming, fallbacks will only be called on failures during the initial
|
|
1675
|
+
* stream creation. Errors that occur after a stream starts will not fallback
|
|
1676
|
+
* to the next Runnable.
|
|
1660
1677
|
*/
|
|
1661
1678
|
export class RunnableWithFallbacks extends Runnable {
|
|
1662
1679
|
static lc_name() {
|
|
@@ -1722,6 +1739,51 @@ export class RunnableWithFallbacks extends Runnable {
|
|
|
1722
1739
|
await runManager?.handleChainError(firstError);
|
|
1723
1740
|
throw firstError;
|
|
1724
1741
|
}
|
|
1742
|
+
async *_streamIterator(input, options) {
|
|
1743
|
+
const config = ensureConfig(options);
|
|
1744
|
+
const callbackManager_ = await getCallbackManagerForConfig(options);
|
|
1745
|
+
const { runId, ...otherConfigFields } = config;
|
|
1746
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
|
|
1747
|
+
let firstError;
|
|
1748
|
+
let stream;
|
|
1749
|
+
for (const runnable of this.runnables()) {
|
|
1750
|
+
config?.signal?.throwIfAborted();
|
|
1751
|
+
const childConfig = patchConfig(otherConfigFields, {
|
|
1752
|
+
callbacks: runManager?.getChild(),
|
|
1753
|
+
});
|
|
1754
|
+
try {
|
|
1755
|
+
stream = await runnable.stream(input, childConfig);
|
|
1756
|
+
break;
|
|
1757
|
+
}
|
|
1758
|
+
catch (e) {
|
|
1759
|
+
if (firstError === undefined) {
|
|
1760
|
+
firstError = e;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
if (stream === undefined) {
|
|
1765
|
+
const error = firstError ?? new Error("No error stored at end of fallback.");
|
|
1766
|
+
await runManager?.handleChainError(error);
|
|
1767
|
+
throw error;
|
|
1768
|
+
}
|
|
1769
|
+
let output;
|
|
1770
|
+
try {
|
|
1771
|
+
for await (const chunk of stream) {
|
|
1772
|
+
yield chunk;
|
|
1773
|
+
try {
|
|
1774
|
+
output = output === undefined ? output : concat(output, chunk);
|
|
1775
|
+
}
|
|
1776
|
+
catch (e) {
|
|
1777
|
+
output = undefined;
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
catch (e) {
|
|
1782
|
+
await runManager?.handleChainError(e);
|
|
1783
|
+
throw e;
|
|
1784
|
+
}
|
|
1785
|
+
await runManager?.handleChainEnd(_coerceToDict(output, "output"));
|
|
1786
|
+
}
|
|
1725
1787
|
async batch(inputs, options, batchOptions) {
|
|
1726
1788
|
if (batchOptions?.returnExceptions) {
|
|
1727
1789
|
throw new Error("Not implemented.");
|
|
@@ -18,6 +18,7 @@ exports.EventStreamContentType = "text/event-stream";
|
|
|
18
18
|
async function getBytes(
|
|
19
19
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
20
|
stream, onChunk) {
|
|
21
|
+
// TODO: Use Async iteration for both cases?
|
|
21
22
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
22
23
|
if (stream instanceof ReadableStream) {
|
|
23
24
|
const reader = stream.getReader();
|
|
@@ -15,6 +15,7 @@ export const EventStreamContentType = "text/event-stream";
|
|
|
15
15
|
export async function getBytes(
|
|
16
16
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
17
|
stream, onChunk) {
|
|
18
|
+
// TODO: Use Async iteration for both cases?
|
|
18
19
|
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
19
20
|
if (stream instanceof ReadableStream) {
|
|
20
21
|
const reader = stream.getReader();
|