langchain 0.0.178 → 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.
Files changed (41) hide show
  1. package/chains/combine_documents/reduce.cjs +1 -0
  2. package/chains/combine_documents/reduce.d.ts +1 -0
  3. package/chains/combine_documents/reduce.js +1 -0
  4. package/dist/cache/base.d.ts +1 -1
  5. package/dist/callbacks/index.d.ts +1 -1
  6. package/dist/chains/combine_documents/reduce.cjs +67 -0
  7. package/dist/chains/combine_documents/reduce.d.ts +28 -0
  8. package/dist/chains/combine_documents/reduce.js +62 -0
  9. package/dist/chat_models/iflytek_xinghuo/index.cjs +1 -1
  10. package/dist/chat_models/iflytek_xinghuo/index.js +1 -1
  11. package/dist/load/import_map.cjs +4 -2
  12. package/dist/load/import_map.d.ts +2 -0
  13. package/dist/load/import_map.js +2 -0
  14. package/dist/output_parsers/json.cjs +77 -0
  15. package/dist/output_parsers/json.d.ts +1 -0
  16. package/dist/output_parsers/json.js +73 -0
  17. package/dist/output_parsers/openai_functions.cjs +37 -2
  18. package/dist/output_parsers/openai_functions.d.ts +10 -5
  19. package/dist/output_parsers/openai_functions.js +38 -3
  20. package/dist/schema/index.cjs +33 -1
  21. package/dist/schema/index.d.ts +3 -1
  22. package/dist/schema/index.js +31 -0
  23. package/dist/schema/output_parser.cjs +63 -3
  24. package/dist/schema/output_parser.d.ts +16 -1
  25. package/dist/schema/output_parser.js +59 -0
  26. package/dist/schema/prompt_template.cjs +33 -0
  27. package/dist/schema/prompt_template.d.ts +12 -0
  28. package/dist/schema/prompt_template.js +29 -0
  29. package/dist/storage/convex.d.ts +21 -0
  30. package/dist/stores/message/convex.d.ts +21 -0
  31. package/dist/util/fast-json-patch/index.cjs +1 -0
  32. package/dist/util/fast-json-patch/index.d.ts +1 -0
  33. package/dist/util/fast-json-patch/index.js +1 -0
  34. package/dist/util/fast-json-patch/src/duplex.cjs +237 -0
  35. package/dist/util/fast-json-patch/src/duplex.d.ts +23 -0
  36. package/dist/util/fast-json-patch/src/duplex.js +230 -0
  37. package/dist/vectorstores/convex.d.ts +21 -0
  38. package/package.json +17 -1
  39. package/schema/prompt_template.cjs +1 -0
  40. package/schema/prompt_template.d.ts +1 -0
  41. package/schema/prompt_template.js +1 -0
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/chains/combine_documents/reduce.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/chains/combine_documents/reduce.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/chains/combine_documents/reduce.js'
@@ -12,7 +12,7 @@ import { Generation, StoredGeneration } from "../schema/index.js";
12
12
  export declare const getCacheKey: (...strings: string[]) => string;
13
13
  export declare function deserializeStoredGeneration(storedGeneration: StoredGeneration): {
14
14
  text: string;
15
- message: import("../schema/index.js").HumanMessage | import("../schema/index.js").AIMessage | import("../schema/index.js").SystemMessage | import("../schema/index.js").FunctionMessage | import("../schema/index.js").ChatMessage;
15
+ message: import("../schema/index.js").ChatMessage | import("../schema/index.js").HumanMessage | import("../schema/index.js").AIMessage | import("../schema/index.js").SystemMessage | import("../schema/index.js").FunctionMessage;
16
16
  } | {
17
17
  text: string;
18
18
  message?: undefined;
@@ -5,5 +5,5 @@ export { RunCollectorCallbackHandler } from "./handlers/run_collector.js";
5
5
  export { LangChainTracer } from "./handlers/tracer_langchain.js";
6
6
  export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
7
7
  export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
8
- export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, type CallbackManagerOptions, type Callbacks, TraceGroup, traceAsGroup, } from "./manager.js";
8
+ export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, type CallbackManagerOptions, type Callbacks, type BaseCallbackConfig, TraceGroup, traceAsGroup, } from "./manager.js";
9
9
  export { awaitAllCallbacks, consumeCallback } from "./promises.js";
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.collapseDocs = exports.splitListOfDocs = void 0;
4
+ /**
5
+ * Splits a list of documents into sublists based on a maximum token limit.
6
+ *
7
+ * @param {Document[]} docs - The list of documents to be split.
8
+ * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.
9
+ * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.
10
+ *
11
+ * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.
12
+ *
13
+ * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
14
+ */
15
+ function splitListOfDocs(docs, lengthFunc, tokenMax) {
16
+ const newResultDocList = [];
17
+ let subResultDocs = [];
18
+ for (const doc of docs) {
19
+ subResultDocs.push(doc);
20
+ const numTokens = lengthFunc(subResultDocs);
21
+ if (numTokens > tokenMax) {
22
+ if (subResultDocs.length === 1) {
23
+ throw new Error("A single document was longer than the context length, we cannot handle this.");
24
+ }
25
+ newResultDocList.push(subResultDocs.slice(0, -1));
26
+ subResultDocs = subResultDocs.slice(-1);
27
+ }
28
+ }
29
+ newResultDocList.push(subResultDocs);
30
+ return newResultDocList;
31
+ }
32
+ exports.splitListOfDocs = splitListOfDocs;
33
+ /**
34
+ * Collapses a list of documents into a single document.
35
+ *
36
+ * This function takes a list of documents and a function to combine the content of these documents.
37
+ * It combines the content of the documents using the provided function and merges the metadata of all documents.
38
+ * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.
39
+ *
40
+ * @param {Document[]} docs - The list of documents to be collapsed.
41
+ * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.
42
+ *
43
+ * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.
44
+ *
45
+ * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.
46
+ */
47
+ async function collapseDocs(docs, combineDocumentFunc) {
48
+ const result = await combineDocumentFunc(docs);
49
+ const combinedMetadata = {};
50
+ for (const key in docs[0].metadata) {
51
+ if (key in docs[0].metadata) {
52
+ combinedMetadata[key] = String(docs[0].metadata[key]);
53
+ }
54
+ }
55
+ for (const doc of docs.slice(1)) {
56
+ for (const key in doc.metadata) {
57
+ if (key in combinedMetadata) {
58
+ combinedMetadata[key] += `, ${doc.metadata[key]}`;
59
+ }
60
+ else {
61
+ combinedMetadata[key] = String(doc.metadata[key]);
62
+ }
63
+ }
64
+ }
65
+ return { pageContent: result, metadata: combinedMetadata };
66
+ }
67
+ exports.collapseDocs = collapseDocs;
@@ -0,0 +1,28 @@
1
+ import { Document } from "../../document.js";
2
+ /**
3
+ * Splits a list of documents into sublists based on a maximum token limit.
4
+ *
5
+ * @param {Document[]} docs - The list of documents to be split.
6
+ * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.
7
+ * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.
8
+ *
9
+ * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.
10
+ *
11
+ * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
12
+ */
13
+ export declare function splitListOfDocs(docs: Document[], lengthFunc: (...args: any[]) => any, tokenMax: number): Document[][];
14
+ /**
15
+ * Collapses a list of documents into a single document.
16
+ *
17
+ * This function takes a list of documents and a function to combine the content of these documents.
18
+ * It combines the content of the documents using the provided function and merges the metadata of all documents.
19
+ * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.
20
+ *
21
+ * @param {Document[]} docs - The list of documents to be collapsed.
22
+ * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.
23
+ *
24
+ * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.
25
+ *
26
+ * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.
27
+ */
28
+ export declare function collapseDocs(docs: Document[], combineDocumentFunc: (docs: Document[]) => Promise<string>): Promise<Document>;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Splits a list of documents into sublists based on a maximum token limit.
3
+ *
4
+ * @param {Document[]} docs - The list of documents to be split.
5
+ * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.
6
+ * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.
7
+ *
8
+ * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.
9
+ *
10
+ * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.
11
+ */
12
+ export function splitListOfDocs(docs, lengthFunc, tokenMax) {
13
+ const newResultDocList = [];
14
+ let subResultDocs = [];
15
+ for (const doc of docs) {
16
+ subResultDocs.push(doc);
17
+ const numTokens = lengthFunc(subResultDocs);
18
+ if (numTokens > tokenMax) {
19
+ if (subResultDocs.length === 1) {
20
+ throw new Error("A single document was longer than the context length, we cannot handle this.");
21
+ }
22
+ newResultDocList.push(subResultDocs.slice(0, -1));
23
+ subResultDocs = subResultDocs.slice(-1);
24
+ }
25
+ }
26
+ newResultDocList.push(subResultDocs);
27
+ return newResultDocList;
28
+ }
29
+ /**
30
+ * Collapses a list of documents into a single document.
31
+ *
32
+ * This function takes a list of documents and a function to combine the content of these documents.
33
+ * It combines the content of the documents using the provided function and merges the metadata of all documents.
34
+ * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.
35
+ *
36
+ * @param {Document[]} docs - The list of documents to be collapsed.
37
+ * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.
38
+ *
39
+ * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.
40
+ *
41
+ * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.
42
+ */
43
+ export async function collapseDocs(docs, combineDocumentFunc) {
44
+ const result = await combineDocumentFunc(docs);
45
+ const combinedMetadata = {};
46
+ for (const key in docs[0].metadata) {
47
+ if (key in docs[0].metadata) {
48
+ combinedMetadata[key] = String(docs[0].metadata[key]);
49
+ }
50
+ }
51
+ for (const doc of docs.slice(1)) {
52
+ for (const key in doc.metadata) {
53
+ if (key in combinedMetadata) {
54
+ combinedMetadata[key] += `, ${doc.metadata[key]}`;
55
+ }
56
+ else {
57
+ combinedMetadata[key] = String(doc.metadata[key]);
58
+ }
59
+ }
60
+ }
61
+ return { pageContent: result, metadata: combinedMetadata };
62
+ }
@@ -19,7 +19,7 @@ class ChatIflytekXinghuo extends common_js_1.BaseChatIflytekXinghuo {
19
19
  const host = "spark-api.xf-yun.com";
20
20
  const date = new Date().toUTCString();
21
21
  const url = `GET /${this.version}/chat HTTP/1.1`;
22
- const { createHmac } = await import("crypto");
22
+ const { createHmac } = await import("node:crypto");
23
23
  const hash = createHmac("sha256", this.iflytekApiSecret)
24
24
  .update(`host: ${host}\ndate: ${date}\n${url}`)
25
25
  .digest("base64");
@@ -13,7 +13,7 @@ export class ChatIflytekXinghuo extends BaseChatIflytekXinghuo {
13
13
  const host = "spark-api.xf-yun.com";
14
14
  const date = new Date().toUTCString();
15
15
  const url = `GET /${this.version}/chat HTTP/1.1`;
16
- const { createHmac } = await import("crypto");
16
+ const { createHmac } = await import("node:crypto");
17
17
  const hash = createHmac("sha256", this.iflytekApiSecret)
18
18
  .update(`host: ${host}\ndate: ${date}\n${url}`)
19
19
  .digest("base64");
@@ -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.chat_models__baiduwenxin = 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 = 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__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";
@@ -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.BaseLLMOutputParser {
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: boolean;
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 BaseLLMOutputParser<object> {
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: boolean;
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 BaseLLMOutputParser {
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