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
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LLM, BaseLLMParams } from "./base.js";
|
|
2
|
+
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
|
|
3
|
+
import { GenerationChunk } from "../schema/index.js";
|
|
2
4
|
/**
|
|
3
5
|
* Interface for CloudflareWorkersAI input parameters.
|
|
4
6
|
*/
|
|
@@ -7,6 +9,7 @@ export interface CloudflareWorkersAIInput {
|
|
|
7
9
|
cloudflareApiToken?: string;
|
|
8
10
|
model?: string;
|
|
9
11
|
baseUrl?: string;
|
|
12
|
+
streaming?: boolean;
|
|
10
13
|
}
|
|
11
14
|
/**
|
|
12
15
|
* Class representing the CloudflareWorkersAI language model. It extends the LLM (Large
|
|
@@ -18,6 +21,7 @@ export declare class CloudflareWorkersAI extends LLM implements CloudflareWorker
|
|
|
18
21
|
cloudflareAccountId?: string;
|
|
19
22
|
cloudflareApiToken?: string;
|
|
20
23
|
baseUrl: string;
|
|
24
|
+
streaming: boolean;
|
|
21
25
|
static lc_name(): string;
|
|
22
26
|
lc_serializable: boolean;
|
|
23
27
|
constructor(fields?: CloudflareWorkersAIInput & BaseLLMParams);
|
|
@@ -37,13 +41,15 @@ export declare class CloudflareWorkersAI extends LLM implements CloudflareWorker
|
|
|
37
41
|
};
|
|
38
42
|
/** Get the type of LLM. */
|
|
39
43
|
_llmType(): string;
|
|
44
|
+
_request(prompt: string, options: this["ParsedCallOptions"], stream?: boolean): Promise<Response>;
|
|
45
|
+
_streamResponseChunks(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
|
|
40
46
|
/** Call out to CloudflareWorkersAI's complete endpoint.
|
|
41
47
|
Args:
|
|
42
48
|
prompt: The prompt to pass into the model.
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
Returns:
|
|
50
|
+
The string generated by the model.
|
|
45
51
|
Example:
|
|
46
52
|
let response = CloudflareWorkersAI.call("Tell me a joke.");
|
|
47
53
|
*/
|
|
48
|
-
_call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
|
|
54
|
+
_call(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
49
55
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { LLM } from "./base.js";
|
|
2
2
|
import { getEnvironmentVariable } from "../util/env.js";
|
|
3
|
+
import { GenerationChunk } from "../schema/index.js";
|
|
4
|
+
import { convertEventStreamToIterableReadableDataStream } from "../util/event-source-parse.js";
|
|
3
5
|
/**
|
|
4
6
|
* Class representing the CloudflareWorkersAI language model. It extends the LLM (Large
|
|
5
7
|
* Language Model) class, providing a standard interface for interacting
|
|
@@ -35,6 +37,12 @@ export class CloudflareWorkersAI extends LLM {
|
|
|
35
37
|
writable: true,
|
|
36
38
|
value: void 0
|
|
37
39
|
});
|
|
40
|
+
Object.defineProperty(this, "streaming", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: false
|
|
45
|
+
});
|
|
38
46
|
Object.defineProperty(this, "lc_serializable", {
|
|
39
47
|
enumerable: true,
|
|
40
48
|
configurable: true,
|
|
@@ -42,6 +50,7 @@ export class CloudflareWorkersAI extends LLM {
|
|
|
42
50
|
value: true
|
|
43
51
|
});
|
|
44
52
|
this.model = fields?.model ?? this.model;
|
|
53
|
+
this.streaming = fields?.streaming ?? this.streaming;
|
|
45
54
|
this.cloudflareAccountId =
|
|
46
55
|
fields?.cloudflareAccountId ??
|
|
47
56
|
getEnvironmentVariable("CLOUDFLARE_ACCOUNT_ID");
|
|
@@ -84,23 +93,15 @@ export class CloudflareWorkersAI extends LLM {
|
|
|
84
93
|
_llmType() {
|
|
85
94
|
return "cloudflare";
|
|
86
95
|
}
|
|
87
|
-
|
|
88
|
-
Args:
|
|
89
|
-
prompt: The prompt to pass into the model.
|
|
90
|
-
Returns:
|
|
91
|
-
The string generated by the model.
|
|
92
|
-
Example:
|
|
93
|
-
let response = CloudflareWorkersAI.call("Tell me a joke.");
|
|
94
|
-
*/
|
|
95
|
-
async _call(prompt, options) {
|
|
96
|
+
async _request(prompt, options, stream) {
|
|
96
97
|
this.validateEnvironment();
|
|
97
98
|
const url = `${this.baseUrl}/${this.model}`;
|
|
98
99
|
const headers = {
|
|
99
100
|
Authorization: `Bearer ${this.cloudflareApiToken}`,
|
|
100
101
|
"Content-Type": "application/json",
|
|
101
102
|
};
|
|
102
|
-
const data = { prompt };
|
|
103
|
-
|
|
103
|
+
const data = { prompt, stream };
|
|
104
|
+
return this.caller.call(async () => {
|
|
104
105
|
const response = await fetch(url, {
|
|
105
106
|
method: "POST",
|
|
106
107
|
headers,
|
|
@@ -113,8 +114,53 @@ export class CloudflareWorkersAI extends LLM {
|
|
|
113
114
|
error.response = response;
|
|
114
115
|
throw error;
|
|
115
116
|
}
|
|
116
|
-
return response
|
|
117
|
+
return response;
|
|
117
118
|
});
|
|
118
|
-
|
|
119
|
+
}
|
|
120
|
+
async *_streamResponseChunks(prompt, options, runManager) {
|
|
121
|
+
const response = await this._request(prompt, options, true);
|
|
122
|
+
if (!response.body) {
|
|
123
|
+
throw new Error("Empty response from Cloudflare. Please try again.");
|
|
124
|
+
}
|
|
125
|
+
const stream = convertEventStreamToIterableReadableDataStream(response.body);
|
|
126
|
+
for await (const chunk of stream) {
|
|
127
|
+
if (chunk !== "[DONE]") {
|
|
128
|
+
const parsedChunk = JSON.parse(chunk);
|
|
129
|
+
const generationChunk = new GenerationChunk({
|
|
130
|
+
text: parsedChunk.response,
|
|
131
|
+
});
|
|
132
|
+
yield generationChunk;
|
|
133
|
+
// eslint-disable-next-line no-void
|
|
134
|
+
void runManager?.handleLLMNewToken(generationChunk.text ?? "");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** Call out to CloudflareWorkersAI's complete endpoint.
|
|
139
|
+
Args:
|
|
140
|
+
prompt: The prompt to pass into the model.
|
|
141
|
+
Returns:
|
|
142
|
+
The string generated by the model.
|
|
143
|
+
Example:
|
|
144
|
+
let response = CloudflareWorkersAI.call("Tell me a joke.");
|
|
145
|
+
*/
|
|
146
|
+
async _call(prompt, options, runManager) {
|
|
147
|
+
if (!this.streaming) {
|
|
148
|
+
const response = await this._request(prompt, options);
|
|
149
|
+
const responseData = await response.json();
|
|
150
|
+
return responseData.result.response;
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
const stream = this._streamResponseChunks(prompt, options, runManager);
|
|
154
|
+
let finalResult;
|
|
155
|
+
for await (const chunk of stream) {
|
|
156
|
+
if (finalResult === undefined) {
|
|
157
|
+
finalResult = chunk;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
finalResult = finalResult.concat(chunk);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return finalResult?.text ?? "";
|
|
164
|
+
}
|
|
119
165
|
}
|
|
120
166
|
}
|
|
@@ -114,6 +114,8 @@ exports.optionalImportEntrypoints = [
|
|
|
114
114
|
"langchain/chat_models/googlevertexai",
|
|
115
115
|
"langchain/chat_models/googlevertexai/web",
|
|
116
116
|
"langchain/chat_models/googlepalm",
|
|
117
|
+
"langchain/chat_models/iflytek_xinghuo",
|
|
118
|
+
"langchain/chat_models/iflytek_xinghuo/web",
|
|
117
119
|
"langchain/chat_models/llama_cpp",
|
|
118
120
|
"langchain/sql_db",
|
|
119
121
|
"langchain/callbacks/handlers/llmonitor",
|
|
@@ -111,6 +111,8 @@ export const optionalImportEntrypoints = [
|
|
|
111
111
|
"langchain/chat_models/googlevertexai",
|
|
112
112
|
"langchain/chat_models/googlevertexai/web",
|
|
113
113
|
"langchain/chat_models/googlepalm",
|
|
114
|
+
"langchain/chat_models/iflytek_xinghuo",
|
|
115
|
+
"langchain/chat_models/iflytek_xinghuo/web",
|
|
114
116
|
"langchain/chat_models/llama_cpp",
|
|
115
117
|
"langchain/sql_db",
|
|
116
118
|
"langchain/callbacks/handlers/llmonitor",
|
package/dist/load/import_map.cjs
CHANGED
|
@@ -24,8 +24,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
return result;
|
|
25
25
|
};
|
|
26
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
-
exports.
|
|
28
|
-
exports.runnables__remote = exports.evaluation = exports.experimental__chains__violation_of_expectations = exports.experimental__chat_models__bittensor = exports.experimental__plan_and_execute = exports.experimental__generative_agents = exports.experimental__babyagi = exports.experimental__autogpt = exports.util__time = exports.util__math = exports.util__document = exports.storage__in_memory = exports.storage__encoder_backed = exports.stores__message__in_memory = exports.stores__file__in_memory = exports.stores__doc__in_memory = exports.cache = exports.retrievers__vespa = exports.retrievers__score_threshold = exports.retrievers__hyde = exports.retrievers__document_compressors__embeddings_filter = exports.retrievers__document_compressors__chain_extract = exports.retrievers__time_weighted = exports.retrievers__tavily_search_api = exports.retrievers__parent_document = exports.retrievers__multi_vector = exports.retrievers__multi_query = exports.retrievers__document_compressors = exports.retrievers__contextual_compression = exports.retrievers__databerry = exports.retrievers__chaindesk = exports.retrievers__remote = exports.output_parsers = exports.callbacks = exports.schema__storage = exports.schema__runnable = exports.schema__retriever = exports.schema__query_constructor = exports.schema__output_parser = exports.schema__document = exports.schema = exports.chat_models__fake = exports.chat_models__yandex = exports.chat_models__minimax = exports.chat_models__ollama = void 0;
|
|
27
|
+
exports.chat_models__fireworks = exports.chat_models__cloudflare_workersai = exports.chat_models__anthropic = exports.chat_models__openai = exports.chat_models__base = exports.document_transformers__openai_functions = exports.document_loaders__web__sort_xyz_blockchain = exports.document_loaders__web__serpapi = exports.document_loaders__web__searchapi = exports.document_loaders__base = exports.document = exports.memory = exports.text_splitter = exports.vectorstores__xata = exports.vectorstores__vectara = exports.vectorstores__prisma = exports.vectorstores__memory = exports.vectorstores__base = exports.prompts = exports.llms__fake = exports.llms__yandex = exports.llms__fireworks = exports.llms__ollama = exports.llms__cloudflare_workersai = exports.llms__aleph_alpha = exports.llms__ai21 = exports.llms__openai = exports.llms__base = exports.embeddings__minimax = exports.embeddings__openai = exports.embeddings__ollama = exports.embeddings__fake = exports.embeddings__cache_backed = exports.embeddings__base = exports.chains__openai_functions = exports.chains__combine_documents__reduce = exports.chains = exports.tools__render = exports.tools = exports.base_language = exports.agents__openai__output_parser = exports.agents__xml__output_parser = exports.agents__react__output_parser = exports.agents__format_scratchpad__log_to_message = exports.agents__format_scratchpad__xml = exports.agents__format_scratchpad__log = exports.agents__format_scratchpad = exports.agents__toolkits = exports.agents = exports.load__serializable = void 0;
|
|
28
|
+
exports.runnables__remote = exports.evaluation = exports.experimental__chains__violation_of_expectations = exports.experimental__chat_models__bittensor = exports.experimental__plan_and_execute = exports.experimental__generative_agents = exports.experimental__babyagi = exports.experimental__autogpt = exports.util__time = exports.util__math = exports.util__document = exports.storage__in_memory = exports.storage__encoder_backed = exports.stores__message__in_memory = exports.stores__file__in_memory = exports.stores__doc__in_memory = exports.cache = exports.retrievers__vespa = exports.retrievers__score_threshold = exports.retrievers__hyde = exports.retrievers__document_compressors__embeddings_filter = exports.retrievers__document_compressors__chain_extract = exports.retrievers__time_weighted = exports.retrievers__tavily_search_api = exports.retrievers__parent_document = exports.retrievers__multi_vector = exports.retrievers__multi_query = exports.retrievers__document_compressors = exports.retrievers__contextual_compression = exports.retrievers__databerry = exports.retrievers__chaindesk = exports.retrievers__remote = exports.output_parsers = exports.callbacks = exports.schema__storage = exports.schema__runnable = exports.schema__retriever = exports.schema__query_constructor = exports.schema__prompt_template = exports.schema__output_parser = exports.schema__document = exports.schema = exports.chat_models__fake = exports.chat_models__yandex = exports.chat_models__minimax = exports.chat_models__ollama = exports.chat_models__baiduwenxin = void 0;
|
|
29
29
|
exports.load__serializable = __importStar(require("../load/serializable.cjs"));
|
|
30
30
|
exports.agents = __importStar(require("../agents/index.cjs"));
|
|
31
31
|
exports.agents__toolkits = __importStar(require("../agents/toolkits/index.cjs"));
|
|
@@ -40,6 +40,7 @@ exports.base_language = __importStar(require("../base_language/index.cjs"));
|
|
|
40
40
|
exports.tools = __importStar(require("../tools/index.cjs"));
|
|
41
41
|
exports.tools__render = __importStar(require("../tools/render.cjs"));
|
|
42
42
|
exports.chains = __importStar(require("../chains/index.cjs"));
|
|
43
|
+
exports.chains__combine_documents__reduce = __importStar(require("../chains/combine_documents/reduce.cjs"));
|
|
43
44
|
exports.chains__openai_functions = __importStar(require("../chains/openai_functions/index.cjs"));
|
|
44
45
|
exports.embeddings__base = __importStar(require("../embeddings/base.cjs"));
|
|
45
46
|
exports.embeddings__cache_backed = __importStar(require("../embeddings/cache_backed.cjs"));
|
|
@@ -83,6 +84,7 @@ exports.chat_models__fake = __importStar(require("../chat_models/fake.cjs"));
|
|
|
83
84
|
exports.schema = __importStar(require("../schema/index.cjs"));
|
|
84
85
|
exports.schema__document = __importStar(require("../schema/document.cjs"));
|
|
85
86
|
exports.schema__output_parser = __importStar(require("../schema/output_parser.cjs"));
|
|
87
|
+
exports.schema__prompt_template = __importStar(require("../schema/prompt_template.cjs"));
|
|
86
88
|
exports.schema__query_constructor = __importStar(require("../schema/query_constructor.cjs"));
|
|
87
89
|
exports.schema__retriever = __importStar(require("../schema/retriever.cjs"));
|
|
88
90
|
exports.schema__runnable = __importStar(require("../schema/runnable/index.cjs"));
|
|
@@ -12,6 +12,7 @@ export * as base_language from "../base_language/index.js";
|
|
|
12
12
|
export * as tools from "../tools/index.js";
|
|
13
13
|
export * as tools__render from "../tools/render.js";
|
|
14
14
|
export * as chains from "../chains/index.js";
|
|
15
|
+
export * as chains__combine_documents__reduce from "../chains/combine_documents/reduce.js";
|
|
15
16
|
export * as chains__openai_functions from "../chains/openai_functions/index.js";
|
|
16
17
|
export * as embeddings__base from "../embeddings/base.js";
|
|
17
18
|
export * as embeddings__cache_backed from "../embeddings/cache_backed.js";
|
|
@@ -55,6 +56,7 @@ export * as chat_models__fake from "../chat_models/fake.js";
|
|
|
55
56
|
export * as schema from "../schema/index.js";
|
|
56
57
|
export * as schema__document from "../schema/document.js";
|
|
57
58
|
export * as schema__output_parser from "../schema/output_parser.js";
|
|
59
|
+
export * as schema__prompt_template from "../schema/prompt_template.js";
|
|
58
60
|
export * as schema__query_constructor from "../schema/query_constructor.js";
|
|
59
61
|
export * as schema__retriever from "../schema/retriever.js";
|
|
60
62
|
export * as schema__runnable from "../schema/runnable/index.js";
|
package/dist/load/import_map.js
CHANGED
|
@@ -13,6 +13,7 @@ export * as base_language from "../base_language/index.js";
|
|
|
13
13
|
export * as tools from "../tools/index.js";
|
|
14
14
|
export * as tools__render from "../tools/render.js";
|
|
15
15
|
export * as chains from "../chains/index.js";
|
|
16
|
+
export * as chains__combine_documents__reduce from "../chains/combine_documents/reduce.js";
|
|
16
17
|
export * as chains__openai_functions from "../chains/openai_functions/index.js";
|
|
17
18
|
export * as embeddings__base from "../embeddings/base.js";
|
|
18
19
|
export * as embeddings__cache_backed from "../embeddings/cache_backed.js";
|
|
@@ -56,6 +57,7 @@ export * as chat_models__fake from "../chat_models/fake.js";
|
|
|
56
57
|
export * as schema from "../schema/index.js";
|
|
57
58
|
export * as schema__document from "../schema/document.js";
|
|
58
59
|
export * as schema__output_parser from "../schema/output_parser.js";
|
|
60
|
+
export * as schema__prompt_template from "../schema/prompt_template.js";
|
|
59
61
|
export * as schema__query_constructor from "../schema/query_constructor.js";
|
|
60
62
|
export * as schema__retriever from "../schema/retriever.js";
|
|
61
63
|
export * as schema__runnable from "../schema/runnable/index.js";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/utils/parse_partial_json.py
|
|
3
|
+
// MIT License
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.parsePartialJson = void 0;
|
|
6
|
+
function parsePartialJson(s) {
|
|
7
|
+
// Attempt to parse the string as-is.
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(s);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
// Pass
|
|
13
|
+
}
|
|
14
|
+
// Initialize variables.
|
|
15
|
+
let new_s = "";
|
|
16
|
+
const stack = [];
|
|
17
|
+
let isInsideString = false;
|
|
18
|
+
let escaped = false;
|
|
19
|
+
// Process each character in the string one at a time.
|
|
20
|
+
for (let char of s) {
|
|
21
|
+
if (isInsideString) {
|
|
22
|
+
if (char === '"' && !escaped) {
|
|
23
|
+
isInsideString = false;
|
|
24
|
+
}
|
|
25
|
+
else if (char === "\n" && !escaped) {
|
|
26
|
+
char = "\\n"; // Replace the newline character with the escape sequence.
|
|
27
|
+
}
|
|
28
|
+
else if (char === "\\") {
|
|
29
|
+
escaped = !escaped;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
escaped = false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
if (char === '"') {
|
|
37
|
+
isInsideString = true;
|
|
38
|
+
escaped = false;
|
|
39
|
+
}
|
|
40
|
+
else if (char === "{") {
|
|
41
|
+
stack.push("}");
|
|
42
|
+
}
|
|
43
|
+
else if (char === "[") {
|
|
44
|
+
stack.push("]");
|
|
45
|
+
}
|
|
46
|
+
else if (char === "}" || char === "]") {
|
|
47
|
+
if (stack && stack[stack.length - 1] === char) {
|
|
48
|
+
stack.pop();
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
// Mismatched closing character; the input is malformed.
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Append the processed character to the new string.
|
|
57
|
+
new_s += char;
|
|
58
|
+
}
|
|
59
|
+
// If we're still inside a string at the end of processing,
|
|
60
|
+
// we need to close the string.
|
|
61
|
+
if (isInsideString) {
|
|
62
|
+
new_s += '"';
|
|
63
|
+
}
|
|
64
|
+
// Close any remaining open structures in the reverse order that they were opened.
|
|
65
|
+
for (let i = stack.length - 1; i >= 0; i -= 1) {
|
|
66
|
+
new_s += stack[i];
|
|
67
|
+
}
|
|
68
|
+
// Attempt to parse the modified string as JSON.
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(new_s);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
// If we still can't parse the string as JSON, return null to indicate failure.
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.parsePartialJson = parsePartialJson;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parsePartialJson(s: string): any;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Adapted from https://github.com/KillianLucas/open-interpreter/blob/main/interpreter/utils/parse_partial_json.py
|
|
2
|
+
// MIT License
|
|
3
|
+
export function parsePartialJson(s) {
|
|
4
|
+
// Attempt to parse the string as-is.
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(s);
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
// Pass
|
|
10
|
+
}
|
|
11
|
+
// Initialize variables.
|
|
12
|
+
let new_s = "";
|
|
13
|
+
const stack = [];
|
|
14
|
+
let isInsideString = false;
|
|
15
|
+
let escaped = false;
|
|
16
|
+
// Process each character in the string one at a time.
|
|
17
|
+
for (let char of s) {
|
|
18
|
+
if (isInsideString) {
|
|
19
|
+
if (char === '"' && !escaped) {
|
|
20
|
+
isInsideString = false;
|
|
21
|
+
}
|
|
22
|
+
else if (char === "\n" && !escaped) {
|
|
23
|
+
char = "\\n"; // Replace the newline character with the escape sequence.
|
|
24
|
+
}
|
|
25
|
+
else if (char === "\\") {
|
|
26
|
+
escaped = !escaped;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
escaped = false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
if (char === '"') {
|
|
34
|
+
isInsideString = true;
|
|
35
|
+
escaped = false;
|
|
36
|
+
}
|
|
37
|
+
else if (char === "{") {
|
|
38
|
+
stack.push("}");
|
|
39
|
+
}
|
|
40
|
+
else if (char === "[") {
|
|
41
|
+
stack.push("]");
|
|
42
|
+
}
|
|
43
|
+
else if (char === "}" || char === "]") {
|
|
44
|
+
if (stack && stack[stack.length - 1] === char) {
|
|
45
|
+
stack.pop();
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// Mismatched closing character; the input is malformed.
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Append the processed character to the new string.
|
|
54
|
+
new_s += char;
|
|
55
|
+
}
|
|
56
|
+
// If we're still inside a string at the end of processing,
|
|
57
|
+
// we need to close the string.
|
|
58
|
+
if (isInsideString) {
|
|
59
|
+
new_s += '"';
|
|
60
|
+
}
|
|
61
|
+
// Close any remaining open structures in the reverse order that they were opened.
|
|
62
|
+
for (let i = stack.length - 1; i >= 0; i -= 1) {
|
|
63
|
+
new_s += stack[i];
|
|
64
|
+
}
|
|
65
|
+
// Attempt to parse the modified string as JSON.
|
|
66
|
+
try {
|
|
67
|
+
return JSON.parse(new_s);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
// If we still can't parse the string as JSON, return null to indicate failure.
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.JsonKeyOutputFunctionsParser = exports.JsonOutputFunctionsParser = exports.OutputFunctionsParser = void 0;
|
|
4
4
|
const output_parser_js_1 = require("../schema/output_parser.cjs");
|
|
5
|
+
const index_js_1 = require("../util/fast-json-patch/index.cjs");
|
|
6
|
+
const json_js_1 = require("./json.cjs");
|
|
5
7
|
/**
|
|
6
8
|
* Class for parsing the output of an LLM. Can be configured to return
|
|
7
9
|
* only the arguments of the function call in the output.
|
|
@@ -63,12 +65,12 @@ exports.OutputFunctionsParser = OutputFunctionsParser;
|
|
|
63
65
|
* Class for parsing the output of an LLM into a JSON object. Uses an
|
|
64
66
|
* instance of `OutputFunctionsParser` to parse the output.
|
|
65
67
|
*/
|
|
66
|
-
class JsonOutputFunctionsParser extends output_parser_js_1.
|
|
68
|
+
class JsonOutputFunctionsParser extends output_parser_js_1.BaseCumulativeTransformOutputParser {
|
|
67
69
|
static lc_name() {
|
|
68
70
|
return "JsonOutputFunctionsParser";
|
|
69
71
|
}
|
|
70
72
|
constructor(config) {
|
|
71
|
-
super();
|
|
73
|
+
super(config);
|
|
72
74
|
Object.defineProperty(this, "lc_namespace", {
|
|
73
75
|
enumerable: true,
|
|
74
76
|
configurable: true,
|
|
@@ -96,6 +98,31 @@ class JsonOutputFunctionsParser extends output_parser_js_1.BaseLLMOutputParser {
|
|
|
96
98
|
this.argsOnly = config?.argsOnly ?? this.argsOnly;
|
|
97
99
|
this.outputParser = new OutputFunctionsParser(config);
|
|
98
100
|
}
|
|
101
|
+
_diff(prev, next) {
|
|
102
|
+
if (!next) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
const ops = (0, index_js_1.compare)(prev ?? {}, next);
|
|
106
|
+
return ops;
|
|
107
|
+
}
|
|
108
|
+
async parsePartialResult(generations) {
|
|
109
|
+
const generation = generations[0];
|
|
110
|
+
if (!generation.message) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
const { message } = generation;
|
|
114
|
+
const functionCall = message.additional_kwargs.function_call;
|
|
115
|
+
if (!functionCall) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
if (this.argsOnly) {
|
|
119
|
+
return (0, json_js_1.parsePartialJson)(functionCall.arguments);
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
...functionCall,
|
|
123
|
+
arguments: (0, json_js_1.parsePartialJson)(functionCall.arguments),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
99
126
|
/**
|
|
100
127
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
101
128
|
* only the arguments of the function call are returned.
|
|
@@ -114,6 +141,14 @@ class JsonOutputFunctionsParser extends output_parser_js_1.BaseLLMOutputParser {
|
|
|
114
141
|
parsedResult.arguments = JSON.parse(parsedResult.arguments);
|
|
115
142
|
return parsedResult;
|
|
116
143
|
}
|
|
144
|
+
// This method would be called by the default implementation of `parse_result`
|
|
145
|
+
// but we're overriding that method so it's not needed.
|
|
146
|
+
async parse(_text) {
|
|
147
|
+
throw new Error("Not implemented.");
|
|
148
|
+
}
|
|
149
|
+
getFormatInstructions() {
|
|
150
|
+
return "";
|
|
151
|
+
}
|
|
117
152
|
}
|
|
118
153
|
exports.JsonOutputFunctionsParser = JsonOutputFunctionsParser;
|
|
119
154
|
/**
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { JsonSchema7ObjectType } from "zod-to-json-schema/src/parsers/object.js";
|
|
2
2
|
import { ChatGeneration, Generation } from "../schema/index.js";
|
|
3
3
|
import { Optional } from "../types/type-utils.js";
|
|
4
|
-
import { BaseLLMOutputParser } from "../schema/output_parser.js";
|
|
4
|
+
import { BaseCumulativeTransformOutputParser, type BaseCumulativeTransformOutputParserInput, BaseLLMOutputParser } from "../schema/output_parser.js";
|
|
5
|
+
import { type Operation as JSONPatchOperation } from "../util/fast-json-patch/index.js";
|
|
5
6
|
/**
|
|
6
7
|
* Represents optional parameters for a function in a JSON Schema.
|
|
7
8
|
*/
|
|
@@ -16,7 +17,7 @@ export declare class OutputFunctionsParser extends BaseLLMOutputParser<string> {
|
|
|
16
17
|
lc_serializable: boolean;
|
|
17
18
|
argsOnly: boolean;
|
|
18
19
|
constructor(config?: {
|
|
19
|
-
argsOnly
|
|
20
|
+
argsOnly?: boolean;
|
|
20
21
|
});
|
|
21
22
|
/**
|
|
22
23
|
* Parses the output and returns a string representation of the function
|
|
@@ -30,15 +31,17 @@ export declare class OutputFunctionsParser extends BaseLLMOutputParser<string> {
|
|
|
30
31
|
* Class for parsing the output of an LLM into a JSON object. Uses an
|
|
31
32
|
* instance of `OutputFunctionsParser` to parse the output.
|
|
32
33
|
*/
|
|
33
|
-
export declare class JsonOutputFunctionsParser extends
|
|
34
|
+
export declare class JsonOutputFunctionsParser extends BaseCumulativeTransformOutputParser<object> {
|
|
34
35
|
static lc_name(): string;
|
|
35
36
|
lc_namespace: string[];
|
|
36
37
|
lc_serializable: boolean;
|
|
37
38
|
outputParser: OutputFunctionsParser;
|
|
38
39
|
argsOnly: boolean;
|
|
39
40
|
constructor(config?: {
|
|
40
|
-
argsOnly
|
|
41
|
-
});
|
|
41
|
+
argsOnly?: boolean;
|
|
42
|
+
} & BaseCumulativeTransformOutputParserInput);
|
|
43
|
+
protected _diff(prev: JSONPatchOperation | undefined, next: JSONPatchOperation): object | undefined;
|
|
44
|
+
parsePartialResult(generations: ChatGeneration[]): Promise<object | undefined>;
|
|
42
45
|
/**
|
|
43
46
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
44
47
|
* only the arguments of the function call are returned.
|
|
@@ -46,6 +49,8 @@ export declare class JsonOutputFunctionsParser extends BaseLLMOutputParser<objec
|
|
|
46
49
|
* @returns A JSON object representation of the function call or its arguments.
|
|
47
50
|
*/
|
|
48
51
|
parseResult(generations: Generation[] | ChatGeneration[]): Promise<object>;
|
|
52
|
+
parse(_text: string): Promise<object>;
|
|
53
|
+
getFormatInstructions(): string;
|
|
49
54
|
}
|
|
50
55
|
/**
|
|
51
56
|
* Class for parsing the output of an LLM into a JSON object and returning
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { BaseLLMOutputParser } from "../schema/output_parser.js";
|
|
1
|
+
import { BaseCumulativeTransformOutputParser, BaseLLMOutputParser, } from "../schema/output_parser.js";
|
|
2
|
+
import { compare, } from "../util/fast-json-patch/index.js";
|
|
3
|
+
import { parsePartialJson } from "./json.js";
|
|
2
4
|
/**
|
|
3
5
|
* Class for parsing the output of an LLM. Can be configured to return
|
|
4
6
|
* only the arguments of the function call in the output.
|
|
@@ -59,12 +61,12 @@ export class OutputFunctionsParser extends BaseLLMOutputParser {
|
|
|
59
61
|
* Class for parsing the output of an LLM into a JSON object. Uses an
|
|
60
62
|
* instance of `OutputFunctionsParser` to parse the output.
|
|
61
63
|
*/
|
|
62
|
-
export class JsonOutputFunctionsParser extends
|
|
64
|
+
export class JsonOutputFunctionsParser extends BaseCumulativeTransformOutputParser {
|
|
63
65
|
static lc_name() {
|
|
64
66
|
return "JsonOutputFunctionsParser";
|
|
65
67
|
}
|
|
66
68
|
constructor(config) {
|
|
67
|
-
super();
|
|
69
|
+
super(config);
|
|
68
70
|
Object.defineProperty(this, "lc_namespace", {
|
|
69
71
|
enumerable: true,
|
|
70
72
|
configurable: true,
|
|
@@ -92,6 +94,31 @@ export class JsonOutputFunctionsParser extends BaseLLMOutputParser {
|
|
|
92
94
|
this.argsOnly = config?.argsOnly ?? this.argsOnly;
|
|
93
95
|
this.outputParser = new OutputFunctionsParser(config);
|
|
94
96
|
}
|
|
97
|
+
_diff(prev, next) {
|
|
98
|
+
if (!next) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const ops = compare(prev ?? {}, next);
|
|
102
|
+
return ops;
|
|
103
|
+
}
|
|
104
|
+
async parsePartialResult(generations) {
|
|
105
|
+
const generation = generations[0];
|
|
106
|
+
if (!generation.message) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
const { message } = generation;
|
|
110
|
+
const functionCall = message.additional_kwargs.function_call;
|
|
111
|
+
if (!functionCall) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if (this.argsOnly) {
|
|
115
|
+
return parsePartialJson(functionCall.arguments);
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
...functionCall,
|
|
119
|
+
arguments: parsePartialJson(functionCall.arguments),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
95
122
|
/**
|
|
96
123
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
97
124
|
* only the arguments of the function call are returned.
|
|
@@ -110,6 +137,14 @@ export class JsonOutputFunctionsParser extends BaseLLMOutputParser {
|
|
|
110
137
|
parsedResult.arguments = JSON.parse(parsedResult.arguments);
|
|
111
138
|
return parsedResult;
|
|
112
139
|
}
|
|
140
|
+
// This method would be called by the default implementation of `parse_result`
|
|
141
|
+
// but we're overriding that method so it's not needed.
|
|
142
|
+
async parse(_text) {
|
|
143
|
+
throw new Error("Not implemented.");
|
|
144
|
+
}
|
|
145
|
+
getFormatInstructions() {
|
|
146
|
+
return "";
|
|
147
|
+
}
|
|
113
148
|
}
|
|
114
149
|
/**
|
|
115
150
|
* Class for parsing the output of an LLM into a JSON object and returning
|
package/dist/prompts/chat.cjs
CHANGED
|
@@ -374,6 +374,14 @@ class ChatPromptTemplate extends BaseChatPromptTemplate {
|
|
|
374
374
|
};
|
|
375
375
|
return new ChatPromptTemplate(promptDict);
|
|
376
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* Load prompt template from a template f-string
|
|
379
|
+
*/
|
|
380
|
+
static fromTemplate(template) {
|
|
381
|
+
const prompt = prompt_js_1.PromptTemplate.fromTemplate(template);
|
|
382
|
+
const humanTemplate = new HumanMessagePromptTemplate({ prompt });
|
|
383
|
+
return this.fromMessages([humanTemplate]);
|
|
384
|
+
}
|
|
377
385
|
/**
|
|
378
386
|
* Create a chat model-specific prompt from individual chat messages
|
|
379
387
|
* or message-like tuples.
|
package/dist/prompts/chat.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { BaseCallbackConfig } from "../callbacks/manager.js";
|
|
|
2
2
|
import { BaseMessage, BaseMessageLike, BasePromptValue, InputValues, PartialValues } from "../schema/index.js";
|
|
3
3
|
import { Runnable } from "../schema/runnable/index.js";
|
|
4
4
|
import { BasePromptTemplate, BasePromptTemplateInput, BaseStringPromptTemplate, TypedPromptInputValues } from "./base.js";
|
|
5
|
+
import { type ParamsFromFString } from "./prompt.js";
|
|
5
6
|
/**
|
|
6
7
|
* Abstract class that serves as a base for creating message prompt
|
|
7
8
|
* templates. It defines how to format messages for different roles in a
|
|
@@ -170,6 +171,10 @@ export declare class ChatPromptTemplate<RunInput extends InputValues = any, Part
|
|
|
170
171
|
_getPromptType(): "chat";
|
|
171
172
|
formatMessages(values: TypedPromptInputValues<RunInput>): Promise<BaseMessage[]>;
|
|
172
173
|
partial<NewPartialVariableName extends string>(values: PartialValues<NewPartialVariableName>): Promise<ChatPromptTemplate<InputValues<Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>>, any>>;
|
|
174
|
+
/**
|
|
175
|
+
* Load prompt template from a template f-string
|
|
176
|
+
*/
|
|
177
|
+
static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T): ChatPromptTemplate<RunInput extends Symbol ? ParamsFromFString<T> : RunInput, any>;
|
|
173
178
|
/**
|
|
174
179
|
* Create a chat model-specific prompt from individual chat messages
|
|
175
180
|
* or message-like tuples.
|
package/dist/prompts/chat.js
CHANGED
|
@@ -362,6 +362,14 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
|
|
|
362
362
|
};
|
|
363
363
|
return new ChatPromptTemplate(promptDict);
|
|
364
364
|
}
|
|
365
|
+
/**
|
|
366
|
+
* Load prompt template from a template f-string
|
|
367
|
+
*/
|
|
368
|
+
static fromTemplate(template) {
|
|
369
|
+
const prompt = PromptTemplate.fromTemplate(template);
|
|
370
|
+
const humanTemplate = new HumanMessagePromptTemplate({ prompt });
|
|
371
|
+
return this.fromMessages([humanTemplate]);
|
|
372
|
+
}
|
|
365
373
|
/**
|
|
366
374
|
* Create a chat model-specific prompt from individual chat messages
|
|
367
375
|
* or message-like tuples.
|