langchain 0.0.177 → 0.0.179
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/chains/combine_documents/reduce.cjs +1 -0
- package/chains/combine_documents/reduce.d.ts +1 -0
- package/chains/combine_documents/reduce.js +1 -0
- package/chat_models/iflytek_xinghuo/web.cjs +1 -0
- package/chat_models/iflytek_xinghuo/web.d.ts +1 -0
- package/chat_models/iflytek_xinghuo/web.js +1 -0
- package/chat_models/iflytek_xinghuo.cjs +1 -0
- package/chat_models/iflytek_xinghuo.d.ts +1 -0
- package/chat_models/iflytek_xinghuo.js +1 -0
- package/dist/cache/base.d.ts +1 -1
- package/dist/callbacks/index.d.ts +1 -1
- package/dist/chains/combine_documents/reduce.cjs +67 -0
- package/dist/chains/combine_documents/reduce.d.ts +28 -0
- package/dist/chains/combine_documents/reduce.js +62 -0
- package/dist/chat_models/cloudflare_workersai.cjs +70 -24
- package/dist/chat_models/cloudflare_workersai.d.ts +6 -2
- package/dist/chat_models/cloudflare_workersai.js +71 -25
- package/dist/chat_models/iflytek_xinghuo/common.cjs +335 -0
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +165 -0
- package/dist/chat_models/iflytek_xinghuo/common.js +331 -0
- package/dist/chat_models/iflytek_xinghuo/index.cjs +35 -0
- package/dist/chat_models/iflytek_xinghuo/index.d.ts +5 -0
- package/dist/chat_models/iflytek_xinghuo/index.js +28 -0
- package/dist/chat_models/iflytek_xinghuo/web.cjs +30 -0
- package/dist/chat_models/iflytek_xinghuo/web.d.ts +5 -0
- package/dist/chat_models/iflytek_xinghuo/web.js +26 -0
- package/dist/graphs/neo4j_graph.cjs +36 -5
- package/dist/graphs/neo4j_graph.js +14 -3
- package/dist/llms/cloudflare_workersai.cjs +59 -13
- package/dist/llms/cloudflare_workersai.d.ts +9 -3
- package/dist/llms/cloudflare_workersai.js +59 -13
- package/dist/load/import_constants.cjs +2 -0
- package/dist/load/import_constants.js +2 -0
- package/dist/load/import_map.cjs +4 -2
- package/dist/load/import_map.d.ts +2 -0
- package/dist/load/import_map.js +2 -0
- package/dist/output_parsers/json.cjs +77 -0
- package/dist/output_parsers/json.d.ts +1 -0
- package/dist/output_parsers/json.js +73 -0
- package/dist/output_parsers/openai_functions.cjs +37 -2
- package/dist/output_parsers/openai_functions.d.ts +10 -5
- package/dist/output_parsers/openai_functions.js +38 -3
- package/dist/prompts/chat.cjs +8 -0
- package/dist/prompts/chat.d.ts +5 -0
- package/dist/prompts/chat.js +8 -0
- package/dist/schema/index.cjs +33 -1
- package/dist/schema/index.d.ts +3 -1
- package/dist/schema/index.js +31 -0
- package/dist/schema/output_parser.cjs +63 -3
- package/dist/schema/output_parser.d.ts +16 -1
- package/dist/schema/output_parser.js +59 -0
- package/dist/schema/prompt_template.cjs +33 -0
- package/dist/schema/prompt_template.d.ts +12 -0
- package/dist/schema/prompt_template.js +29 -0
- package/dist/storage/convex.d.ts +21 -0
- package/dist/stores/message/convex.d.ts +21 -0
- package/dist/util/event-source-parse.cjs +20 -1
- package/dist/util/event-source-parse.d.ts +2 -0
- package/dist/util/event-source-parse.js +18 -0
- package/dist/util/fast-json-patch/index.cjs +1 -0
- package/dist/util/fast-json-patch/index.d.ts +1 -0
- package/dist/util/fast-json-patch/index.js +1 -0
- package/dist/util/fast-json-patch/src/duplex.cjs +237 -0
- package/dist/util/fast-json-patch/src/duplex.d.ts +23 -0
- package/dist/util/fast-json-patch/src/duplex.js +230 -0
- package/dist/util/iflytek_websocket_stream.cjs +81 -0
- package/dist/util/iflytek_websocket_stream.d.ts +27 -0
- package/dist/util/iflytek_websocket_stream.js +77 -0
- package/dist/vectorstores/convex.d.ts +21 -0
- package/package.json +38 -1
- package/schema/prompt_template.cjs +1 -0
- package/schema/prompt_template.d.ts +1 -0
- package/schema/prompt_template.js +1 -0
package/dist/schema/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Docstore = exports.BaseEntityStore = exports.BaseFileStore = exports.BaseCache = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = exports.BasePromptValue = exports.mapStoredMessageToChatMessage = exports.ChatGenerationChunk = exports.ChatMessageChunk = exports.coerceMessageLikeToMessage = exports.isBaseMessage = exports.ChatMessage = exports.FunctionMessageChunk = exports.FunctionMessage = exports.SystemChatMessage = exports.AIChatMessage = exports.HumanChatMessage = exports.BaseChatMessage = exports.SystemMessageChunk = exports.SystemMessage = exports.AIMessageChunk = exports.AIMessage = exports.HumanMessageChunk = exports.HumanMessage = exports.BaseMessageChunk = exports.BaseMessage = exports.GenerationChunk = exports.RUN_KEY = void 0;
|
|
3
|
+
exports.Docstore = exports.BaseEntityStore = exports.BaseFileStore = exports.BaseCache = exports.BaseListChatMessageHistory = exports.BaseChatMessageHistory = exports.BasePromptValue = exports.mapStoredMessageToChatMessage = exports.ChatGenerationChunk = exports.ChatMessageChunk = exports.coerceMessageLikeToMessage = exports.isBaseMessageChunk = exports.isBaseMessage = exports.ChatMessage = exports.FunctionMessageChunk = exports.FunctionMessage = exports.SystemChatMessage = exports.AIChatMessage = exports.HumanChatMessage = exports.BaseChatMessage = exports.SystemMessageChunk = exports.SystemMessage = exports.AIMessageChunk = exports.AIMessage = exports.HumanMessageChunk = exports.HumanMessage = exports.BaseMessageChunk = exports.BaseMessage = exports.GenerationChunk = exports.RUN_KEY = void 0;
|
|
4
4
|
const serializable_js_1 = require("../load/serializable.cjs");
|
|
5
5
|
exports.RUN_KEY = "__run";
|
|
6
6
|
/**
|
|
@@ -105,6 +105,33 @@ class BaseMessage extends serializable_js_1.Serializable {
|
|
|
105
105
|
.kwargs,
|
|
106
106
|
};
|
|
107
107
|
}
|
|
108
|
+
toChunk() {
|
|
109
|
+
const type = this._getType();
|
|
110
|
+
if (type === "human") {
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
112
|
+
return new HumanMessageChunk({ ...this });
|
|
113
|
+
}
|
|
114
|
+
else if (type === "ai") {
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
116
|
+
return new AIMessageChunk({ ...this });
|
|
117
|
+
}
|
|
118
|
+
else if (type === "system") {
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
120
|
+
return new SystemMessageChunk({ ...this });
|
|
121
|
+
}
|
|
122
|
+
else if (type === "function") {
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
124
|
+
return new FunctionMessageChunk({ ...this });
|
|
125
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
126
|
+
}
|
|
127
|
+
else if (ChatMessage.isInstance(this)) {
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
129
|
+
return new ChatMessageChunk({ ...this });
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
throw new Error("Unknown message type.");
|
|
133
|
+
}
|
|
134
|
+
}
|
|
108
135
|
}
|
|
109
136
|
exports.BaseMessage = BaseMessage;
|
|
110
137
|
/**
|
|
@@ -326,6 +353,11 @@ function isBaseMessage(messageLike) {
|
|
|
326
353
|
return typeof messageLike?._getType === "function";
|
|
327
354
|
}
|
|
328
355
|
exports.isBaseMessage = isBaseMessage;
|
|
356
|
+
function isBaseMessageChunk(messageLike) {
|
|
357
|
+
return (isBaseMessage(messageLike) &&
|
|
358
|
+
typeof messageLike.concat === "function");
|
|
359
|
+
}
|
|
360
|
+
exports.isBaseMessageChunk = isBaseMessageChunk;
|
|
329
361
|
function coerceMessageLikeToMessage(messageLike) {
|
|
330
362
|
if (typeof messageLike === "string") {
|
|
331
363
|
return new HumanMessage(messageLike);
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -103,6 +103,7 @@ export declare abstract class BaseMessage extends Serializable implements BaseMe
|
|
|
103
103
|
/** @deprecated */
|
|
104
104
|
kwargs?: Record<string, unknown>);
|
|
105
105
|
toDict(): StoredMessage;
|
|
106
|
+
toChunk(): BaseMessageChunk;
|
|
106
107
|
}
|
|
107
108
|
/**
|
|
108
109
|
* Represents a chunk of a message, which can be concatenated with other
|
|
@@ -219,6 +220,7 @@ export type BaseMessageLike = BaseMessage | [
|
|
|
219
220
|
string
|
|
220
221
|
] | string;
|
|
221
222
|
export declare function isBaseMessage(messageLike?: unknown): messageLike is BaseMessage;
|
|
223
|
+
export declare function isBaseMessageChunk(messageLike?: unknown): messageLike is BaseMessageChunk;
|
|
222
224
|
export declare function coerceMessageLikeToMessage(messageLike: BaseMessageLike): BaseMessage;
|
|
223
225
|
/**
|
|
224
226
|
* Represents a chunk of a chat message, which can be concatenated with
|
|
@@ -243,7 +245,7 @@ export declare class ChatGenerationChunk extends GenerationChunk implements Chat
|
|
|
243
245
|
constructor(fields: ChatGenerationChunkFields);
|
|
244
246
|
concat(chunk: ChatGenerationChunk): ChatGenerationChunk;
|
|
245
247
|
}
|
|
246
|
-
export declare function mapStoredMessageToChatMessage(message: StoredMessage): HumanMessage | AIMessage | SystemMessage | FunctionMessage
|
|
248
|
+
export declare function mapStoredMessageToChatMessage(message: StoredMessage): ChatMessage | HumanMessage | AIMessage | SystemMessage | FunctionMessage;
|
|
247
249
|
export interface ChatResult {
|
|
248
250
|
generations: ChatGeneration[];
|
|
249
251
|
llmOutput?: Record<string, any>;
|
package/dist/schema/index.js
CHANGED
|
@@ -101,6 +101,33 @@ export class BaseMessage extends Serializable {
|
|
|
101
101
|
.kwargs,
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
+
toChunk() {
|
|
105
|
+
const type = this._getType();
|
|
106
|
+
if (type === "human") {
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
108
|
+
return new HumanMessageChunk({ ...this });
|
|
109
|
+
}
|
|
110
|
+
else if (type === "ai") {
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
112
|
+
return new AIMessageChunk({ ...this });
|
|
113
|
+
}
|
|
114
|
+
else if (type === "system") {
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
116
|
+
return new SystemMessageChunk({ ...this });
|
|
117
|
+
}
|
|
118
|
+
else if (type === "function") {
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
120
|
+
return new FunctionMessageChunk({ ...this });
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
122
|
+
}
|
|
123
|
+
else if (ChatMessage.isInstance(this)) {
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
125
|
+
return new ChatMessageChunk({ ...this });
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
throw new Error("Unknown message type.");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
104
131
|
}
|
|
105
132
|
/**
|
|
106
133
|
* Represents a chunk of a message, which can be concatenated with other
|
|
@@ -310,6 +337,10 @@ export class ChatMessage extends BaseMessage {
|
|
|
310
337
|
export function isBaseMessage(messageLike) {
|
|
311
338
|
return typeof messageLike?._getType === "function";
|
|
312
339
|
}
|
|
340
|
+
export function isBaseMessageChunk(messageLike) {
|
|
341
|
+
return (isBaseMessage(messageLike) &&
|
|
342
|
+
typeof messageLike.concat === "function");
|
|
343
|
+
}
|
|
313
344
|
export function coerceMessageLikeToMessage(messageLike) {
|
|
314
345
|
if (typeof messageLike === "string") {
|
|
315
346
|
return new HumanMessage(messageLike);
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OutputParserException = exports.BytesOutputParser = exports.StringOutputParser = exports.BaseTransformOutputParser = exports.BaseOutputParser = exports.BaseLLMOutputParser = void 0;
|
|
4
|
-
const index_js_1 = require("./
|
|
3
|
+
exports.OutputParserException = exports.BytesOutputParser = exports.StringOutputParser = exports.BaseCumulativeTransformOutputParser = exports.BaseTransformOutputParser = exports.BaseOutputParser = exports.BaseLLMOutputParser = void 0;
|
|
4
|
+
const index_js_1 = require("./index.cjs");
|
|
5
|
+
const index_js_2 = require("./runnable/index.cjs");
|
|
6
|
+
const index_js_3 = require("../util/@cfworker/json-schema/index.cjs");
|
|
5
7
|
/**
|
|
6
8
|
* Abstract base class for parsing the output of a Large Language Model
|
|
7
9
|
* (LLM) call. It provides methods for parsing the result of an LLM call
|
|
8
10
|
* and invoking the parser with a given input.
|
|
9
11
|
*/
|
|
10
|
-
class BaseLLMOutputParser extends
|
|
12
|
+
class BaseLLMOutputParser extends index_js_2.Runnable {
|
|
11
13
|
/**
|
|
12
14
|
* Parses the result of an LLM call with a given prompt. By default, it
|
|
13
15
|
* simply calls `parseResult`.
|
|
@@ -86,6 +88,64 @@ class BaseTransformOutputParser extends BaseOutputParser {
|
|
|
86
88
|
}
|
|
87
89
|
}
|
|
88
90
|
exports.BaseTransformOutputParser = BaseTransformOutputParser;
|
|
91
|
+
/**
|
|
92
|
+
* A base class for output parsers that can handle streaming input. It
|
|
93
|
+
* extends the `BaseTransformOutputParser` class and provides a method for
|
|
94
|
+
* converting parsed outputs into a diff format.
|
|
95
|
+
*/
|
|
96
|
+
class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
|
|
97
|
+
constructor(fields) {
|
|
98
|
+
super(fields);
|
|
99
|
+
Object.defineProperty(this, "diff", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
value: false
|
|
104
|
+
});
|
|
105
|
+
this.diff = fields?.diff ?? this.diff;
|
|
106
|
+
}
|
|
107
|
+
async *_transform(inputGenerator) {
|
|
108
|
+
let prevParsed;
|
|
109
|
+
let accGen;
|
|
110
|
+
for await (const chunk of inputGenerator) {
|
|
111
|
+
let chunkGen;
|
|
112
|
+
if ((0, index_js_1.isBaseMessageChunk)(chunk)) {
|
|
113
|
+
chunkGen = new index_js_1.ChatGenerationChunk({
|
|
114
|
+
message: chunk,
|
|
115
|
+
text: chunk.content,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else if ((0, index_js_1.isBaseMessage)(chunk)) {
|
|
119
|
+
chunkGen = new index_js_1.ChatGenerationChunk({
|
|
120
|
+
message: chunk.toChunk(),
|
|
121
|
+
text: chunk.content,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
chunkGen = new index_js_1.GenerationChunk({ text: chunk });
|
|
126
|
+
}
|
|
127
|
+
if (accGen === undefined) {
|
|
128
|
+
accGen = chunkGen;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
accGen = accGen.concat(chunkGen);
|
|
132
|
+
}
|
|
133
|
+
const parsed = await this.parsePartialResult([accGen]);
|
|
134
|
+
if (parsed !== undefined &&
|
|
135
|
+
parsed !== null &&
|
|
136
|
+
!(0, index_js_3.deepCompareStrict)(parsed, prevParsed)) {
|
|
137
|
+
if (this.diff) {
|
|
138
|
+
yield this._diff(prevParsed, parsed);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
yield parsed;
|
|
142
|
+
}
|
|
143
|
+
prevParsed = parsed;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.BaseCumulativeTransformOutputParser = BaseCumulativeTransformOutputParser;
|
|
89
149
|
/**
|
|
90
150
|
* OutputParser that parses LLMResult into the top likely string.
|
|
91
151
|
*/
|
|
@@ -77,7 +77,7 @@ export declare abstract class BaseOutputParser<T = unknown> extends BaseLLMOutpu
|
|
|
77
77
|
* Class to parse the output of an LLM call that also allows streaming inputs.
|
|
78
78
|
*/
|
|
79
79
|
export declare abstract class BaseTransformOutputParser<T = unknown> extends BaseOutputParser<T> {
|
|
80
|
-
_transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
|
|
80
|
+
protected _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
|
|
81
81
|
/**
|
|
82
82
|
* Transforms an asynchronous generator of input into an asynchronous
|
|
83
83
|
* generator of parsed output.
|
|
@@ -87,6 +87,21 @@ export declare abstract class BaseTransformOutputParser<T = unknown> extends Bas
|
|
|
87
87
|
*/
|
|
88
88
|
transform(inputGenerator: AsyncGenerator<string | BaseMessage>, options: BaseCallbackConfig): AsyncGenerator<T>;
|
|
89
89
|
}
|
|
90
|
+
export type BaseCumulativeTransformOutputParserInput = {
|
|
91
|
+
diff?: boolean;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* A base class for output parsers that can handle streaming input. It
|
|
95
|
+
* extends the `BaseTransformOutputParser` class and provides a method for
|
|
96
|
+
* converting parsed outputs into a diff format.
|
|
97
|
+
*/
|
|
98
|
+
export declare abstract class BaseCumulativeTransformOutputParser<T = unknown> extends BaseTransformOutputParser<T> {
|
|
99
|
+
protected diff: boolean;
|
|
100
|
+
constructor(fields?: BaseCumulativeTransformOutputParserInput);
|
|
101
|
+
protected abstract _diff(prev: any | undefined, next: any): any;
|
|
102
|
+
abstract parsePartialResult(generations: Generation[] | ChatGeneration[]): Promise<T | undefined>;
|
|
103
|
+
protected _transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
|
|
104
|
+
}
|
|
90
105
|
/**
|
|
91
106
|
* OutputParser that parses LLMResult into the top likely string.
|
|
92
107
|
*/
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { isBaseMessage, isBaseMessageChunk, ChatGenerationChunk, GenerationChunk, } from "./index.js";
|
|
1
2
|
import { Runnable } from "./runnable/index.js";
|
|
3
|
+
import { deepCompareStrict } from "../util/@cfworker/json-schema/index.js";
|
|
2
4
|
/**
|
|
3
5
|
* Abstract base class for parsing the output of a Large Language Model
|
|
4
6
|
* (LLM) call. It provides methods for parsing the result of an LLM call
|
|
@@ -80,6 +82,63 @@ export class BaseTransformOutputParser extends BaseOutputParser {
|
|
|
80
82
|
});
|
|
81
83
|
}
|
|
82
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* A base class for output parsers that can handle streaming input. It
|
|
87
|
+
* extends the `BaseTransformOutputParser` class and provides a method for
|
|
88
|
+
* converting parsed outputs into a diff format.
|
|
89
|
+
*/
|
|
90
|
+
export class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
|
|
91
|
+
constructor(fields) {
|
|
92
|
+
super(fields);
|
|
93
|
+
Object.defineProperty(this, "diff", {
|
|
94
|
+
enumerable: true,
|
|
95
|
+
configurable: true,
|
|
96
|
+
writable: true,
|
|
97
|
+
value: false
|
|
98
|
+
});
|
|
99
|
+
this.diff = fields?.diff ?? this.diff;
|
|
100
|
+
}
|
|
101
|
+
async *_transform(inputGenerator) {
|
|
102
|
+
let prevParsed;
|
|
103
|
+
let accGen;
|
|
104
|
+
for await (const chunk of inputGenerator) {
|
|
105
|
+
let chunkGen;
|
|
106
|
+
if (isBaseMessageChunk(chunk)) {
|
|
107
|
+
chunkGen = new ChatGenerationChunk({
|
|
108
|
+
message: chunk,
|
|
109
|
+
text: chunk.content,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
else if (isBaseMessage(chunk)) {
|
|
113
|
+
chunkGen = new ChatGenerationChunk({
|
|
114
|
+
message: chunk.toChunk(),
|
|
115
|
+
text: chunk.content,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
chunkGen = new GenerationChunk({ text: chunk });
|
|
120
|
+
}
|
|
121
|
+
if (accGen === undefined) {
|
|
122
|
+
accGen = chunkGen;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
accGen = accGen.concat(chunkGen);
|
|
126
|
+
}
|
|
127
|
+
const parsed = await this.parsePartialResult([accGen]);
|
|
128
|
+
if (parsed !== undefined &&
|
|
129
|
+
parsed !== null &&
|
|
130
|
+
!deepCompareStrict(parsed, prevParsed)) {
|
|
131
|
+
if (this.diff) {
|
|
132
|
+
yield this._diff(prevParsed, parsed);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
yield parsed;
|
|
136
|
+
}
|
|
137
|
+
prevParsed = parsed;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
83
142
|
/**
|
|
84
143
|
* OutputParser that parses LLMResult into the top likely string.
|
|
85
144
|
*/
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatDocument = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Formats a document using a given prompt template.
|
|
6
|
+
*
|
|
7
|
+
* @async
|
|
8
|
+
* @param {Document} document - The document to format.
|
|
9
|
+
* @param {BasePromptTemplate} prompt - The prompt template to use for formatting.
|
|
10
|
+
* @returns {Promise<string>} A Promise that resolves to the formatted document as a string.
|
|
11
|
+
* @throws {Error} If the document is missing required metadata variables specified in the prompt template.
|
|
12
|
+
*/
|
|
13
|
+
const formatDocument = async (document, prompt) => {
|
|
14
|
+
const baseInfo = {
|
|
15
|
+
pageContent: document.pageContent,
|
|
16
|
+
...document.metadata,
|
|
17
|
+
};
|
|
18
|
+
const variables = new Set(prompt.inputVariables);
|
|
19
|
+
const requiredMetadata = new Set(prompt.inputVariables
|
|
20
|
+
.map((v) => (v !== "pageContent" ? v : null))
|
|
21
|
+
.filter((v) => v !== null));
|
|
22
|
+
const missingMetadata = [];
|
|
23
|
+
for (const variable of variables) {
|
|
24
|
+
if (!(variable in baseInfo) && variable !== "pageContent") {
|
|
25
|
+
missingMetadata.push(variable);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (missingMetadata.length) {
|
|
29
|
+
throw new Error(`Document prompt requires documents to have metadata variables: ${JSON.stringify(requiredMetadata)}. Received document with missing metadata: ${JSON.stringify(missingMetadata)}`);
|
|
30
|
+
}
|
|
31
|
+
return prompt.format(baseInfo);
|
|
32
|
+
};
|
|
33
|
+
exports.formatDocument = formatDocument;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Document } from "../document.js";
|
|
2
|
+
import { BasePromptTemplate } from "../prompts/base.js";
|
|
3
|
+
/**
|
|
4
|
+
* Formats a document using a given prompt template.
|
|
5
|
+
*
|
|
6
|
+
* @async
|
|
7
|
+
* @param {Document} document - The document to format.
|
|
8
|
+
* @param {BasePromptTemplate} prompt - The prompt template to use for formatting.
|
|
9
|
+
* @returns {Promise<string>} A Promise that resolves to the formatted document as a string.
|
|
10
|
+
* @throws {Error} If the document is missing required metadata variables specified in the prompt template.
|
|
11
|
+
*/
|
|
12
|
+
export declare const formatDocument: (document: Document, prompt: BasePromptTemplate) => Promise<string>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a document using a given prompt template.
|
|
3
|
+
*
|
|
4
|
+
* @async
|
|
5
|
+
* @param {Document} document - The document to format.
|
|
6
|
+
* @param {BasePromptTemplate} prompt - The prompt template to use for formatting.
|
|
7
|
+
* @returns {Promise<string>} A Promise that resolves to the formatted document as a string.
|
|
8
|
+
* @throws {Error} If the document is missing required metadata variables specified in the prompt template.
|
|
9
|
+
*/
|
|
10
|
+
export const formatDocument = async (document, prompt) => {
|
|
11
|
+
const baseInfo = {
|
|
12
|
+
pageContent: document.pageContent,
|
|
13
|
+
...document.metadata,
|
|
14
|
+
};
|
|
15
|
+
const variables = new Set(prompt.inputVariables);
|
|
16
|
+
const requiredMetadata = new Set(prompt.inputVariables
|
|
17
|
+
.map((v) => (v !== "pageContent" ? v : null))
|
|
18
|
+
.filter((v) => v !== null));
|
|
19
|
+
const missingMetadata = [];
|
|
20
|
+
for (const variable of variables) {
|
|
21
|
+
if (!(variable in baseInfo) && variable !== "pageContent") {
|
|
22
|
+
missingMetadata.push(variable);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (missingMetadata.length) {
|
|
26
|
+
throw new Error(`Document prompt requires documents to have metadata variables: ${JSON.stringify(requiredMetadata)}. Received document with missing metadata: ${JSON.stringify(missingMetadata)}`);
|
|
27
|
+
}
|
|
28
|
+
return prompt.format(baseInfo);
|
|
29
|
+
};
|
package/dist/storage/convex.d.ts
CHANGED
|
@@ -21,12 +21,33 @@ export type ConvexKVStoreConfig<DataModel extends GenericDataModel, TableName ex
|
|
|
21
21
|
key: string;
|
|
22
22
|
}>> = {
|
|
23
23
|
readonly ctx: GenericActionCtx<DataModel>;
|
|
24
|
+
/**
|
|
25
|
+
* Defaults to "cache"
|
|
26
|
+
*/
|
|
24
27
|
readonly table?: TableName;
|
|
28
|
+
/**
|
|
29
|
+
* Defaults to "byKey"
|
|
30
|
+
*/
|
|
25
31
|
readonly index?: IndexName;
|
|
32
|
+
/**
|
|
33
|
+
* Defaults to "key"
|
|
34
|
+
*/
|
|
26
35
|
readonly keyField?: KeyFieldName;
|
|
36
|
+
/**
|
|
37
|
+
* Defaults to "value"
|
|
38
|
+
*/
|
|
27
39
|
readonly valueField?: ValueFieldName;
|
|
40
|
+
/**
|
|
41
|
+
* Defaults to `internal.langchain.db.upsert`
|
|
42
|
+
*/
|
|
28
43
|
readonly upsert?: UpsertMutation;
|
|
44
|
+
/**
|
|
45
|
+
* Defaults to `internal.langchain.db.lookup`
|
|
46
|
+
*/
|
|
29
47
|
readonly lookup?: LookupQuery;
|
|
48
|
+
/**
|
|
49
|
+
* Defaults to `internal.langchain.db.deleteMany`
|
|
50
|
+
*/
|
|
30
51
|
readonly deleteMany?: DeleteManyMutation;
|
|
31
52
|
};
|
|
32
53
|
/**
|
|
@@ -21,12 +21,33 @@ export type ConvexChatMessageHistoryInput<DataModel extends GenericDataModel, Ta
|
|
|
21
21
|
}> = any> = {
|
|
22
22
|
readonly ctx: GenericActionCtx<DataModel>;
|
|
23
23
|
readonly sessionId: DocumentByName<DataModel, TableName>[SessionIdFieldName];
|
|
24
|
+
/**
|
|
25
|
+
* Defaults to "messages"
|
|
26
|
+
*/
|
|
24
27
|
readonly table?: TableName;
|
|
28
|
+
/**
|
|
29
|
+
* Defaults to "bySessionId"
|
|
30
|
+
*/
|
|
25
31
|
readonly index?: IndexName;
|
|
32
|
+
/**
|
|
33
|
+
* Defaults to "sessionId"
|
|
34
|
+
*/
|
|
26
35
|
readonly sessionIdField?: SessionIdFieldName;
|
|
36
|
+
/**
|
|
37
|
+
* Defaults to "message"
|
|
38
|
+
*/
|
|
27
39
|
readonly messageTextFieldName?: MessageTextFieldName;
|
|
40
|
+
/**
|
|
41
|
+
* Defaults to `internal.langchain.db.insert`
|
|
42
|
+
*/
|
|
28
43
|
readonly insert?: InsertMutation;
|
|
44
|
+
/**
|
|
45
|
+
* Defaults to `internal.langchain.db.lookup`
|
|
46
|
+
*/
|
|
29
47
|
readonly lookup?: LookupQuery;
|
|
48
|
+
/**
|
|
49
|
+
* Defaults to `internal.langchain.db.deleteMany`
|
|
50
|
+
*/
|
|
30
51
|
readonly deleteMany?: DeleteManyMutation;
|
|
31
52
|
};
|
|
32
53
|
export declare class ConvexChatMessageHistory<DataModel extends GenericDataModel, SessionIdFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "sessionId", TableName extends TableNamesInDataModel<DataModel> = "messages", IndexName extends VectorIndexNames<NamedTableInfo<DataModel, TableName>> = "bySessionId", MessageTextFieldName extends FieldPaths<NamedTableInfo<DataModel, TableName>> = "message", InsertMutation extends FunctionReference<"mutation", "internal", {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMessages = exports.getLines = exports.getBytes = exports.EventStreamContentType = void 0;
|
|
3
|
+
exports.convertEventStreamToIterableReadableDataStream = exports.getMessages = exports.getLines = exports.getBytes = exports.EventStreamContentType = void 0;
|
|
4
|
+
const stream_js_1 = require("./stream.cjs");
|
|
4
5
|
exports.EventStreamContentType = "text/event-stream";
|
|
5
6
|
function isNodeJSReadable(x) {
|
|
6
7
|
return x != null && typeof x === "object" && "on" in x;
|
|
@@ -199,6 +200,24 @@ function newMessage() {
|
|
|
199
200
|
retry: undefined,
|
|
200
201
|
};
|
|
201
202
|
}
|
|
203
|
+
function convertEventStreamToIterableReadableDataStream(stream) {
|
|
204
|
+
const dataStream = new ReadableStream({
|
|
205
|
+
async start(controller) {
|
|
206
|
+
const enqueueLine = getMessages((msg) => {
|
|
207
|
+
if (msg.data)
|
|
208
|
+
controller.enqueue(msg.data);
|
|
209
|
+
});
|
|
210
|
+
const onLine = (line, fieldLength, flush) => {
|
|
211
|
+
enqueueLine(line, fieldLength, flush);
|
|
212
|
+
if (flush)
|
|
213
|
+
controller.close();
|
|
214
|
+
};
|
|
215
|
+
await getBytes(stream, getLines(onLine));
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
return stream_js_1.IterableReadableStream.fromReadableStream(dataStream);
|
|
219
|
+
}
|
|
220
|
+
exports.convertEventStreamToIterableReadableDataStream = convertEventStreamToIterableReadableDataStream;
|
|
202
221
|
function isEmpty(message) {
|
|
203
222
|
return (message.data === "" &&
|
|
204
223
|
message.event === "" &&
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IterableReadableStream } from "./stream.js";
|
|
1
2
|
export declare const EventStreamContentType = "text/event-stream";
|
|
2
3
|
/**
|
|
3
4
|
* Represents a message sent in an event stream
|
|
@@ -35,3 +36,4 @@ export declare function getLines(onLine: (line: Uint8Array, fieldLength: number,
|
|
|
35
36
|
* @returns A function that should be called for each incoming line buffer.
|
|
36
37
|
*/
|
|
37
38
|
export declare function getMessages(onMessage?: (msg: EventSourceMessage) => void, onId?: (id: string) => void, onRetry?: (retry: number) => void): (line: Uint8Array, fieldLength: number, flush?: boolean) => void;
|
|
39
|
+
export declare function convertEventStreamToIterableReadableDataStream(stream: ReadableStream): IterableReadableStream<any>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { IterableReadableStream } from "./stream.js";
|
|
1
2
|
export const EventStreamContentType = "text/event-stream";
|
|
2
3
|
function isNodeJSReadable(x) {
|
|
3
4
|
return x != null && typeof x === "object" && "on" in x;
|
|
@@ -193,6 +194,23 @@ function newMessage() {
|
|
|
193
194
|
retry: undefined,
|
|
194
195
|
};
|
|
195
196
|
}
|
|
197
|
+
export function convertEventStreamToIterableReadableDataStream(stream) {
|
|
198
|
+
const dataStream = new ReadableStream({
|
|
199
|
+
async start(controller) {
|
|
200
|
+
const enqueueLine = getMessages((msg) => {
|
|
201
|
+
if (msg.data)
|
|
202
|
+
controller.enqueue(msg.data);
|
|
203
|
+
});
|
|
204
|
+
const onLine = (line, fieldLength, flush) => {
|
|
205
|
+
enqueueLine(line, fieldLength, flush);
|
|
206
|
+
if (flush)
|
|
207
|
+
controller.close();
|
|
208
|
+
};
|
|
209
|
+
await getBytes(stream, getLines(onLine));
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
return IterableReadableStream.fromReadableStream(dataStream);
|
|
213
|
+
}
|
|
196
214
|
function isEmpty(message) {
|
|
197
215
|
return (message.data === "" &&
|
|
198
216
|
message.event === "" &&
|
|
@@ -28,6 +28,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.unescapePathComponent = exports.escapePathComponent = exports.deepClone = exports.JsonPatchError = void 0;
|
|
30
30
|
__exportStar(require("./src/core.cjs"), exports);
|
|
31
|
+
__exportStar(require("./src/duplex.cjs"), exports);
|
|
31
32
|
var helpers_js_1 = require("./src/helpers.cjs");
|
|
32
33
|
Object.defineProperty(exports, "JsonPatchError", { enumerable: true, get: function () { return helpers_js_1.PatchError; } });
|
|
33
34
|
Object.defineProperty(exports, "deepClone", { enumerable: true, get: function () { return helpers_js_1._deepClone; } });
|