langchain 0.0.133 → 0.0.135
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/chat_convo/outputParser.cjs +13 -10
- package/dist/agents/chat_convo/outputParser.js +13 -10
- package/dist/callbacks/base.d.ts +6 -3
- package/dist/callbacks/handlers/tracer.cjs +2 -2
- package/dist/callbacks/handlers/tracer.d.ts +2 -2
- package/dist/callbacks/handlers/tracer.js +2 -2
- package/dist/callbacks/index.cjs +2 -1
- package/dist/callbacks/index.d.ts +1 -1
- package/dist/callbacks/index.js +1 -1
- package/dist/callbacks/manager.cjs +2 -2
- package/dist/callbacks/manager.d.ts +2 -2
- package/dist/callbacks/manager.js +2 -2
- package/dist/chains/sql_db/sql_db_chain.d.ts +1 -1
- package/dist/chains/sql_db/sql_db_prompt.d.ts +6 -6
- package/dist/chat_models/openai.cjs +10 -5
- package/dist/chat_models/openai.js +10 -5
- package/dist/document_loaders/web/recursive_url.cjs +177 -0
- package/dist/document_loaders/web/recursive_url.d.ts +27 -0
- package/dist/document_loaders/web/recursive_url.js +173 -0
- package/dist/hub.cjs +16 -0
- package/dist/hub.d.ts +4 -0
- package/dist/hub.js +11 -0
- package/dist/llms/bedrock.cjs +63 -19
- package/dist/llms/bedrock.d.ts +9 -1
- package/dist/llms/bedrock.js +63 -19
- package/dist/llms/writer.cjs +167 -0
- package/dist/llms/writer.d.ts +60 -0
- package/dist/llms/writer.js +163 -0
- package/dist/load/import_constants.cjs +4 -0
- package/dist/load/import_constants.js +4 -0
- package/dist/load/import_map.cjs +2 -1
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/memory/summary_buffer.d.ts +1 -1
- package/dist/retrievers/score_threshold.cjs +45 -0
- package/dist/retrievers/score_threshold.d.ts +15 -0
- package/dist/retrievers/score_threshold.js +41 -0
- package/dist/sql_db.cjs +8 -1
- package/dist/sql_db.d.ts +1 -0
- package/dist/sql_db.js +8 -1
- package/dist/stores/message/mongodb.cjs +48 -0
- package/dist/stores/message/mongodb.d.ts +15 -0
- package/dist/stores/message/mongodb.js +44 -0
- package/dist/tools/sql.cjs +9 -3
- package/dist/tools/sql.d.ts +0 -1
- package/dist/tools/sql.js +9 -3
- package/dist/util/sql_utils.cjs +8 -2
- package/dist/util/sql_utils.d.ts +2 -1
- package/dist/util/sql_utils.js +8 -2
- package/dist/vectorstores/googlevertexai.cjs +2 -1
- package/dist/vectorstores/googlevertexai.js +2 -1
- package/dist/vectorstores/myscale.cjs +2 -2
- package/dist/vectorstores/myscale.d.ts +1 -1
- package/dist/vectorstores/myscale.js +2 -2
- package/document_loaders/web/recursive_url.cjs +1 -0
- package/document_loaders/web/recursive_url.d.ts +1 -0
- package/document_loaders/web/recursive_url.js +1 -0
- package/hub.cjs +1 -0
- package/hub.d.ts +1 -0
- package/hub.js +1 -0
- package/llms/writer.cjs +1 -0
- package/llms/writer.d.ts +1 -0
- package/llms/writer.js +1 -0
- package/package.json +61 -1
- package/retrievers/score_threshold.cjs +1 -0
- package/retrievers/score_threshold.d.ts +1 -0
- package/retrievers/score_threshold.js +1 -0
- package/stores/message/mongodb.cjs +1 -0
- package/stores/message/mongodb.d.ts +1 -0
- package/stores/message/mongodb.js +1 -0
|
@@ -36,16 +36,19 @@ class ChatConversationalAgentOutputParser extends types_js_1.AgentActionOutputPa
|
|
|
36
36
|
*/
|
|
37
37
|
async parse(text) {
|
|
38
38
|
let jsonOutput = text.trim();
|
|
39
|
-
if (jsonOutput.includes("```json")) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
if (jsonOutput.includes("```json") || jsonOutput.includes("```")) {
|
|
40
|
+
const testString = jsonOutput.includes("```json") ? "```json" : "```";
|
|
41
|
+
const firstIndex = jsonOutput.indexOf(testString);
|
|
42
|
+
const actionInputIndex = jsonOutput.indexOf("action_input");
|
|
43
|
+
if (actionInputIndex > firstIndex) {
|
|
44
|
+
jsonOutput = jsonOutput
|
|
45
|
+
.slice(firstIndex + testString.length)
|
|
46
|
+
.trimStart();
|
|
47
|
+
const lastIndex = jsonOutput.lastIndexOf("```");
|
|
48
|
+
if (lastIndex !== -1) {
|
|
49
|
+
jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
49
52
|
}
|
|
50
53
|
try {
|
|
51
54
|
const response = JSON.parse(jsonOutput);
|
|
@@ -33,16 +33,19 @@ export class ChatConversationalAgentOutputParser extends AgentActionOutputParser
|
|
|
33
33
|
*/
|
|
34
34
|
async parse(text) {
|
|
35
35
|
let jsonOutput = text.trim();
|
|
36
|
-
if (jsonOutput.includes("```json")) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
36
|
+
if (jsonOutput.includes("```json") || jsonOutput.includes("```")) {
|
|
37
|
+
const testString = jsonOutput.includes("```json") ? "```json" : "```";
|
|
38
|
+
const firstIndex = jsonOutput.indexOf(testString);
|
|
39
|
+
const actionInputIndex = jsonOutput.indexOf("action_input");
|
|
40
|
+
if (actionInputIndex > firstIndex) {
|
|
41
|
+
jsonOutput = jsonOutput
|
|
42
|
+
.slice(firstIndex + testString.length)
|
|
43
|
+
.trimStart();
|
|
44
|
+
const lastIndex = jsonOutput.lastIndexOf("```");
|
|
45
|
+
if (lastIndex !== -1) {
|
|
46
|
+
jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
46
49
|
}
|
|
47
50
|
try {
|
|
48
51
|
const response = JSON.parse(jsonOutput);
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentAction, AgentFinish, BaseMessage, ChainValues, LLMResult } from "../schema/index.js";
|
|
1
|
+
import { AgentAction, AgentFinish, BaseMessage, ChainValues, ChatGenerationChunk, GenerationChunk, LLMResult } from "../schema/index.js";
|
|
2
2
|
import { Serializable, Serialized, SerializedNotImplemented } from "../load/serializable.js";
|
|
3
3
|
import { SerializedFields } from "../load/map_keys.js";
|
|
4
4
|
import { Document } from "../document.js";
|
|
@@ -22,6 +22,9 @@ export interface NewTokenIndices {
|
|
|
22
22
|
prompt: number;
|
|
23
23
|
completion: number;
|
|
24
24
|
}
|
|
25
|
+
export type HandleLLMNewTokenCallbackFields = {
|
|
26
|
+
chunk?: GenerationChunk | ChatGenerationChunk;
|
|
27
|
+
};
|
|
25
28
|
/**
|
|
26
29
|
* Abstract class that provides a set of optional methods that can be
|
|
27
30
|
* overridden in derived classes to handle various events during the
|
|
@@ -43,7 +46,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
43
46
|
* idx.completion is the index of the completion that produced the token
|
|
44
47
|
* (if multiple completions per prompt are requested)
|
|
45
48
|
*/
|
|
46
|
-
idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
|
|
49
|
+
idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void> | void;
|
|
47
50
|
/**
|
|
48
51
|
* Called if an LLM/ChatModel run encounters an error
|
|
49
52
|
*/
|
|
@@ -182,7 +185,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
182
185
|
/**
|
|
183
186
|
* Called when an LLM/ChatModel in `streaming` mode produces a new token
|
|
184
187
|
*/
|
|
185
|
-
handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
|
|
188
|
+
handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, fields?: HandleLLMNewTokenCallbackFields | undefined): void | Promise<void>;
|
|
186
189
|
/**
|
|
187
190
|
* Called if an LLM/ChatModel run encounters an error
|
|
188
191
|
*/
|
|
@@ -332,7 +332,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
332
332
|
});
|
|
333
333
|
await this.onText?.(run);
|
|
334
334
|
}
|
|
335
|
-
async handleLLMNewToken(token, idx, runId) {
|
|
335
|
+
async handleLLMNewToken(token, idx, runId, _parentRunId, _tags, fields) {
|
|
336
336
|
const run = this.runMap.get(runId);
|
|
337
337
|
if (!run || run?.run_type !== "llm") {
|
|
338
338
|
return;
|
|
@@ -340,7 +340,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
|
|
|
340
340
|
run.events.push({
|
|
341
341
|
name: "new_token",
|
|
342
342
|
time: Date.now(),
|
|
343
|
-
kwargs: { token, idx },
|
|
343
|
+
kwargs: { token, idx, chunk: fields?.chunk },
|
|
344
344
|
});
|
|
345
345
|
await this.onLLMNewToken?.(run);
|
|
346
346
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { KVMap, BaseRun } from "langsmith/schemas";
|
|
2
2
|
import { AgentAction, AgentFinish, BaseMessage, ChainValues, LLMResult } from "../../schema/index.js";
|
|
3
3
|
import { Serialized } from "../../load/serializable.js";
|
|
4
|
-
import { BaseCallbackHandler, BaseCallbackHandlerInput, NewTokenIndices } from "../base.js";
|
|
4
|
+
import { BaseCallbackHandler, BaseCallbackHandlerInput, HandleLLMNewTokenCallbackFields, NewTokenIndices } from "../base.js";
|
|
5
5
|
import { Document } from "../../document.js";
|
|
6
6
|
export type RunType = string;
|
|
7
7
|
export interface Run extends BaseRun {
|
|
@@ -44,7 +44,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
|
|
|
44
44
|
handleRetrieverEnd(documents: Document<Record<string, unknown>>[], runId: string): Promise<void>;
|
|
45
45
|
handleRetrieverError(error: Error, runId: string): Promise<void>;
|
|
46
46
|
handleText(text: string, runId: string): Promise<void>;
|
|
47
|
-
handleLLMNewToken(token: string, idx: NewTokenIndices, runId: string): Promise<void>;
|
|
47
|
+
handleLLMNewToken(token: string, idx: NewTokenIndices, runId: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void>;
|
|
48
48
|
onLLMStart?(run: Run): void | Promise<void>;
|
|
49
49
|
onLLMEnd?(run: Run): void | Promise<void>;
|
|
50
50
|
onLLMError?(run: Run): void | Promise<void>;
|
|
@@ -329,7 +329,7 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
329
329
|
});
|
|
330
330
|
await this.onText?.(run);
|
|
331
331
|
}
|
|
332
|
-
async handleLLMNewToken(token, idx, runId) {
|
|
332
|
+
async handleLLMNewToken(token, idx, runId, _parentRunId, _tags, fields) {
|
|
333
333
|
const run = this.runMap.get(runId);
|
|
334
334
|
if (!run || run?.run_type !== "llm") {
|
|
335
335
|
return;
|
|
@@ -337,7 +337,7 @@ export class BaseTracer extends BaseCallbackHandler {
|
|
|
337
337
|
run.events.push({
|
|
338
338
|
name: "new_token",
|
|
339
339
|
time: Date.now(),
|
|
340
|
-
kwargs: { token, idx },
|
|
340
|
+
kwargs: { token, idx, chunk: fields?.chunk },
|
|
341
341
|
});
|
|
342
342
|
await this.onLLMNewToken?.(run);
|
|
343
343
|
}
|
package/dist/callbacks/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.consumeCallback = exports.awaitAllCallbacks = exports.traceAsGroup = exports.TraceGroup = exports.CallbackManagerForToolRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForChainRun = exports.CallbackManager = exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = exports.LangChainTracerV1 = exports.LangChainTracer = exports.ConsoleCallbackHandler = exports.BaseTracer = exports.BaseCallbackHandler = void 0;
|
|
3
|
+
exports.consumeCallback = exports.awaitAllCallbacks = exports.traceAsGroup = exports.TraceGroup = exports.CallbackManagerForToolRun = exports.CallbackManagerForLLMRun = exports.CallbackManagerForChainRun = exports.CallbackManagerForRetrieverRun = exports.CallbackManager = exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = exports.LangChainTracerV1 = exports.LangChainTracer = exports.ConsoleCallbackHandler = exports.BaseTracer = exports.BaseCallbackHandler = void 0;
|
|
4
4
|
var base_js_1 = require("./base.cjs");
|
|
5
5
|
Object.defineProperty(exports, "BaseCallbackHandler", { enumerable: true, get: function () { return base_js_1.BaseCallbackHandler; } });
|
|
6
6
|
var tracer_js_1 = require("./handlers/tracer.cjs");
|
|
@@ -16,6 +16,7 @@ Object.defineProperty(exports, "getTracingCallbackHandler", { enumerable: true,
|
|
|
16
16
|
Object.defineProperty(exports, "getTracingV2CallbackHandler", { enumerable: true, get: function () { return initialize_js_1.getTracingV2CallbackHandler; } });
|
|
17
17
|
var manager_js_1 = require("./manager.cjs");
|
|
18
18
|
Object.defineProperty(exports, "CallbackManager", { enumerable: true, get: function () { return manager_js_1.CallbackManager; } });
|
|
19
|
+
Object.defineProperty(exports, "CallbackManagerForRetrieverRun", { enumerable: true, get: function () { return manager_js_1.CallbackManagerForRetrieverRun; } });
|
|
19
20
|
Object.defineProperty(exports, "CallbackManagerForChainRun", { enumerable: true, get: function () { return manager_js_1.CallbackManagerForChainRun; } });
|
|
20
21
|
Object.defineProperty(exports, "CallbackManagerForLLMRun", { enumerable: true, get: function () { return manager_js_1.CallbackManagerForLLMRun; } });
|
|
21
22
|
Object.defineProperty(exports, "CallbackManagerForToolRun", { enumerable: true, get: function () { return manager_js_1.CallbackManagerForToolRun; } });
|
|
@@ -4,5 +4,5 @@ export { ConsoleCallbackHandler } from "./handlers/console.js";
|
|
|
4
4
|
export { LangChainTracer } from "./handlers/tracer_langchain.js";
|
|
5
5
|
export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
|
|
6
6
|
export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
|
|
7
|
-
export { CallbackManager, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, CallbackManagerOptions, Callbacks, TraceGroup, traceAsGroup, } from "./manager.js";
|
|
7
|
+
export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, CallbackManagerOptions, Callbacks, TraceGroup, traceAsGroup, } from "./manager.js";
|
|
8
8
|
export { awaitAllCallbacks, consumeCallback } from "./promises.js";
|
package/dist/callbacks/index.js
CHANGED
|
@@ -4,5 +4,5 @@ export { ConsoleCallbackHandler } from "./handlers/console.js";
|
|
|
4
4
|
export { LangChainTracer } from "./handlers/tracer_langchain.js";
|
|
5
5
|
export { LangChainTracerV1 } from "./handlers/tracer_langchain_v1.js";
|
|
6
6
|
export { getTracingCallbackHandler, getTracingV2CallbackHandler, } from "./handlers/initialize.js";
|
|
7
|
-
export { CallbackManager, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, TraceGroup, traceAsGroup, } from "./manager.js";
|
|
7
|
+
export { CallbackManager, CallbackManagerForRetrieverRun, CallbackManagerForChainRun, CallbackManagerForLLMRun, CallbackManagerForToolRun, TraceGroup, traceAsGroup, } from "./manager.js";
|
|
8
8
|
export { awaitAllCallbacks, consumeCallback } from "./promises.js";
|
|
@@ -137,11 +137,11 @@ class CallbackManagerForRetrieverRun extends BaseRunManager {
|
|
|
137
137
|
}
|
|
138
138
|
exports.CallbackManagerForRetrieverRun = CallbackManagerForRetrieverRun;
|
|
139
139
|
class CallbackManagerForLLMRun extends BaseRunManager {
|
|
140
|
-
async handleLLMNewToken(token, idx
|
|
140
|
+
async handleLLMNewToken(token, idx, _runId, _parentRunId, _tags, fields) {
|
|
141
141
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
142
142
|
if (!handler.ignoreLLM) {
|
|
143
143
|
try {
|
|
144
|
-
await handler.handleLLMNewToken?.(token, idx, this.runId, this._parentRunId, this.tags);
|
|
144
|
+
await handler.handleLLMNewToken?.(token, idx ?? { prompt: 0, completion: 0 }, this.runId, this._parentRunId, this.tags, fields);
|
|
145
145
|
}
|
|
146
146
|
catch (err) {
|
|
147
147
|
console.error(`Error in handler ${handler.constructor.name}, handleLLMNewToken: ${err}`);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AgentAction, AgentFinish, BaseMessage, ChainValues, LLMResult } from "../schema/index.js";
|
|
2
|
-
import { BaseCallbackHandler, CallbackHandlerMethods, NewTokenIndices } from "./base.js";
|
|
2
|
+
import { BaseCallbackHandler, CallbackHandlerMethods, HandleLLMNewTokenCallbackFields, NewTokenIndices } from "./base.js";
|
|
3
3
|
import { LangChainTracerFields } from "./handlers/tracer_langchain.js";
|
|
4
4
|
import { Serialized } from "../load/serializable.js";
|
|
5
5
|
import { Document } from "../document.js";
|
|
@@ -62,7 +62,7 @@ export declare class CallbackManagerForRetrieverRun extends BaseRunManager imple
|
|
|
62
62
|
handleRetrieverError(err: Error | unknown): Promise<void>;
|
|
63
63
|
}
|
|
64
64
|
export declare class CallbackManagerForLLMRun extends BaseRunManager implements BaseCallbackManagerMethods {
|
|
65
|
-
handleLLMNewToken(token: string, idx?: NewTokenIndices): Promise<void>;
|
|
65
|
+
handleLLMNewToken(token: string, idx?: NewTokenIndices, _runId?: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void>;
|
|
66
66
|
handleLLMError(err: Error | unknown): Promise<void>;
|
|
67
67
|
handleLLMEnd(output: LLMResult): Promise<void>;
|
|
68
68
|
}
|
|
@@ -131,11 +131,11 @@ export class CallbackManagerForRetrieverRun extends BaseRunManager {
|
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
export class CallbackManagerForLLMRun extends BaseRunManager {
|
|
134
|
-
async handleLLMNewToken(token, idx
|
|
134
|
+
async handleLLMNewToken(token, idx, _runId, _parentRunId, _tags, fields) {
|
|
135
135
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
136
136
|
if (!handler.ignoreLLM) {
|
|
137
137
|
try {
|
|
138
|
-
await handler.handleLLMNewToken?.(token, idx, this.runId, this._parentRunId, this.tags);
|
|
138
|
+
await handler.handleLLMNewToken?.(token, idx ?? { prompt: 0, completion: 0 }, this.runId, this._parentRunId, this.tags, fields);
|
|
139
139
|
}
|
|
140
140
|
catch (err) {
|
|
141
141
|
console.error(`Error in handler ${handler.constructor.name}, handleLLMNewToken: ${err}`);
|
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
import { PromptTemplate } from "../../prompts/prompt.js";
|
|
2
2
|
export declare const DEFAULT_SQL_DATABASE_PROMPT: PromptTemplate<{
|
|
3
3
|
input: any;
|
|
4
|
+
top_k: any;
|
|
4
5
|
dialect: any;
|
|
5
6
|
table_info: any;
|
|
6
|
-
top_k: any;
|
|
7
7
|
}, any>;
|
|
8
8
|
export declare const SQL_POSTGRES_PROMPT: PromptTemplate<{
|
|
9
9
|
input: any;
|
|
10
|
+
top_k: any;
|
|
10
11
|
dialect: any;
|
|
11
12
|
table_info: any;
|
|
12
|
-
top_k: any;
|
|
13
13
|
}, any>;
|
|
14
14
|
export declare const SQL_SQLITE_PROMPT: PromptTemplate<{
|
|
15
15
|
input: any;
|
|
16
|
+
top_k: any;
|
|
16
17
|
dialect: any;
|
|
17
18
|
table_info: any;
|
|
18
|
-
top_k: any;
|
|
19
19
|
}, any>;
|
|
20
20
|
export declare const SQL_MYSQL_PROMPT: PromptTemplate<{
|
|
21
21
|
input: any;
|
|
22
|
+
top_k: any;
|
|
22
23
|
dialect: any;
|
|
23
24
|
table_info: any;
|
|
24
|
-
top_k: any;
|
|
25
25
|
}, any>;
|
|
26
26
|
export declare const SQL_MSSQL_PROMPT: PromptTemplate<{
|
|
27
27
|
input: any;
|
|
28
|
+
top_k: any;
|
|
28
29
|
dialect: any;
|
|
29
30
|
table_info: any;
|
|
30
|
-
top_k: any;
|
|
31
31
|
}, any>;
|
|
32
32
|
export declare const SQL_SAP_HANA_PROMPT: PromptTemplate<{
|
|
33
33
|
input: any;
|
|
34
|
+
top_k: any;
|
|
34
35
|
dialect: any;
|
|
35
36
|
table_info: any;
|
|
36
|
-
top_k: any;
|
|
37
37
|
}, any>;
|
|
@@ -402,7 +402,10 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
|
|
|
402
402
|
});
|
|
403
403
|
yield generationChunk;
|
|
404
404
|
// eslint-disable-next-line no-void
|
|
405
|
-
void runManager?.handleLLMNewToken(generationChunk.text ?? ""
|
|
405
|
+
void runManager?.handleLLMNewToken(generationChunk.text ?? "", {
|
|
406
|
+
prompt: 0,
|
|
407
|
+
completion: choice.index,
|
|
408
|
+
}, undefined, undefined, undefined, { chunk: generationChunk });
|
|
406
409
|
}
|
|
407
410
|
}
|
|
408
411
|
startStream(request, options) {
|
|
@@ -548,13 +551,15 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
|
|
|
548
551
|
choice.message.function_call.arguments +=
|
|
549
552
|
part.delta?.function_call?.arguments ?? "";
|
|
550
553
|
}
|
|
551
|
-
|
|
554
|
+
const chunk = _convertDeltaToMessageChunk(part.delta, "assistant");
|
|
555
|
+
const generationChunk = new index_js_1.ChatGenerationChunk({
|
|
556
|
+
message: chunk,
|
|
557
|
+
text: chunk.content,
|
|
558
|
+
});
|
|
552
559
|
void runManager?.handleLLMNewToken(part.delta?.content ?? "", {
|
|
553
560
|
prompt: options.promptIndex ?? 0,
|
|
554
561
|
completion: part.index,
|
|
555
|
-
});
|
|
556
|
-
// TODO we don't currently have a callback method for
|
|
557
|
-
// sending the function call arguments
|
|
562
|
+
}, undefined, undefined, undefined, { chunk: generationChunk });
|
|
558
563
|
}
|
|
559
564
|
}
|
|
560
565
|
// when all messages are finished, resolve
|
|
@@ -396,7 +396,10 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
396
396
|
});
|
|
397
397
|
yield generationChunk;
|
|
398
398
|
// eslint-disable-next-line no-void
|
|
399
|
-
void runManager?.handleLLMNewToken(generationChunk.text ?? ""
|
|
399
|
+
void runManager?.handleLLMNewToken(generationChunk.text ?? "", {
|
|
400
|
+
prompt: 0,
|
|
401
|
+
completion: choice.index,
|
|
402
|
+
}, undefined, undefined, undefined, { chunk: generationChunk });
|
|
400
403
|
}
|
|
401
404
|
}
|
|
402
405
|
startStream(request, options) {
|
|
@@ -542,13 +545,15 @@ export class ChatOpenAI extends BaseChatModel {
|
|
|
542
545
|
choice.message.function_call.arguments +=
|
|
543
546
|
part.delta?.function_call?.arguments ?? "";
|
|
544
547
|
}
|
|
545
|
-
|
|
548
|
+
const chunk = _convertDeltaToMessageChunk(part.delta, "assistant");
|
|
549
|
+
const generationChunk = new ChatGenerationChunk({
|
|
550
|
+
message: chunk,
|
|
551
|
+
text: chunk.content,
|
|
552
|
+
});
|
|
546
553
|
void runManager?.handleLLMNewToken(part.delta?.content ?? "", {
|
|
547
554
|
prompt: options.promptIndex ?? 0,
|
|
548
555
|
completion: part.index,
|
|
549
|
-
});
|
|
550
|
-
// TODO we don't currently have a callback method for
|
|
551
|
-
// sending the function call arguments
|
|
556
|
+
}, undefined, undefined, undefined, { chunk: generationChunk });
|
|
552
557
|
}
|
|
553
558
|
}
|
|
554
559
|
// when all messages are finished, resolve
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RecursiveUrlLoader = void 0;
|
|
4
|
+
const jsdom_1 = require("jsdom");
|
|
5
|
+
const async_caller_js_1 = require("../../util/async_caller.cjs");
|
|
6
|
+
const base_js_1 = require("../base.cjs");
|
|
7
|
+
class RecursiveUrlLoader extends base_js_1.BaseDocumentLoader {
|
|
8
|
+
constructor(url, options) {
|
|
9
|
+
super();
|
|
10
|
+
Object.defineProperty(this, "caller", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
writable: true,
|
|
14
|
+
value: void 0
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(this, "url", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "excludeDirs", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: void 0
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "extractor", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: void 0
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(this, "maxDepth", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: void 0
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(this, "timeout", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: void 0
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(this, "preventOutside", {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
configurable: true,
|
|
49
|
+
writable: true,
|
|
50
|
+
value: void 0
|
|
51
|
+
});
|
|
52
|
+
this.caller = new async_caller_js_1.AsyncCaller({
|
|
53
|
+
maxConcurrency: 64,
|
|
54
|
+
maxRetries: 0,
|
|
55
|
+
...options.callerOptions,
|
|
56
|
+
});
|
|
57
|
+
this.url = url;
|
|
58
|
+
this.excludeDirs = options.excludeDirs ?? [];
|
|
59
|
+
this.extractor = options.extractor ?? ((s) => s);
|
|
60
|
+
this.maxDepth = options.maxDepth ?? 2;
|
|
61
|
+
this.timeout = options.timeout ?? 10000;
|
|
62
|
+
this.preventOutside = options.preventOutside ?? true;
|
|
63
|
+
}
|
|
64
|
+
async fetchWithTimeout(resource, options) {
|
|
65
|
+
const { timeout, ...rest } = options;
|
|
66
|
+
return this.caller.call(() => fetch(resource, { ...rest, signal: AbortSignal.timeout(timeout) }));
|
|
67
|
+
}
|
|
68
|
+
getChildLinks(html, baseUrl) {
|
|
69
|
+
const allLinks = Array.from(new jsdom_1.JSDOM(html).window.document.querySelectorAll("a")).map((a) => a.href);
|
|
70
|
+
const absolutePaths = [];
|
|
71
|
+
// eslint-disable-next-line no-script-url
|
|
72
|
+
const invalidPrefixes = ["javascript:", "mailto:", "#"];
|
|
73
|
+
const invalidSuffixes = [
|
|
74
|
+
".css",
|
|
75
|
+
".js",
|
|
76
|
+
".ico",
|
|
77
|
+
".png",
|
|
78
|
+
".jpg",
|
|
79
|
+
".jpeg",
|
|
80
|
+
".gif",
|
|
81
|
+
".svg",
|
|
82
|
+
];
|
|
83
|
+
for (const link of allLinks) {
|
|
84
|
+
if (invalidPrefixes.some((prefix) => link.startsWith(prefix)) ||
|
|
85
|
+
invalidSuffixes.some((suffix) => link.endsWith(suffix)))
|
|
86
|
+
continue;
|
|
87
|
+
if (link.startsWith("http")) {
|
|
88
|
+
const isAllowed = !this.preventOutside || link.startsWith(baseUrl);
|
|
89
|
+
if (isAllowed)
|
|
90
|
+
absolutePaths.push(link);
|
|
91
|
+
}
|
|
92
|
+
else if (link.startsWith("//")) {
|
|
93
|
+
const base = new URL(baseUrl);
|
|
94
|
+
absolutePaths.push(base.protocol + link);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
const newLink = new URL(link, baseUrl).href;
|
|
98
|
+
absolutePaths.push(newLink);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return Array.from(new Set(absolutePaths));
|
|
102
|
+
}
|
|
103
|
+
extractMetadata(rawHtml, url) {
|
|
104
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
105
|
+
const metadata = { source: url };
|
|
106
|
+
const { document } = new jsdom_1.JSDOM(rawHtml).window;
|
|
107
|
+
const title = document.getElementsByTagName("title")[0];
|
|
108
|
+
if (title) {
|
|
109
|
+
metadata.title = title.textContent;
|
|
110
|
+
}
|
|
111
|
+
const description = document.querySelector("meta[name=description]");
|
|
112
|
+
if (description) {
|
|
113
|
+
metadata.description = description.getAttribute("content");
|
|
114
|
+
}
|
|
115
|
+
const html = document.getElementsByTagName("html")[0];
|
|
116
|
+
if (html) {
|
|
117
|
+
metadata.language = html.getAttribute("lang");
|
|
118
|
+
}
|
|
119
|
+
return metadata;
|
|
120
|
+
}
|
|
121
|
+
async getUrlAsDoc(url) {
|
|
122
|
+
let res;
|
|
123
|
+
try {
|
|
124
|
+
res = await this.fetchWithTimeout(url, { timeout: this.timeout });
|
|
125
|
+
res = await res.text();
|
|
126
|
+
}
|
|
127
|
+
catch (e) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
pageContent: this.extractor(res),
|
|
132
|
+
metadata: this.extractMetadata(res, url),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
async getChildUrlsRecursive(inputUrl, visited = new Set(), depth = 0) {
|
|
136
|
+
if (depth > this.maxDepth)
|
|
137
|
+
return [];
|
|
138
|
+
let url = inputUrl;
|
|
139
|
+
if (!inputUrl.endsWith("/"))
|
|
140
|
+
url += "/";
|
|
141
|
+
const isExcluded = this.excludeDirs.some((exDir) => url.startsWith(exDir));
|
|
142
|
+
if (isExcluded)
|
|
143
|
+
return [];
|
|
144
|
+
let res;
|
|
145
|
+
try {
|
|
146
|
+
res = await this.fetchWithTimeout(url, { timeout: this.timeout });
|
|
147
|
+
res = await res.text();
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
return [];
|
|
151
|
+
}
|
|
152
|
+
const childUrls = this.getChildLinks(res, url);
|
|
153
|
+
const results = await Promise.all(childUrls.map((childUrl) => (async () => {
|
|
154
|
+
if (visited.has(childUrl))
|
|
155
|
+
return null;
|
|
156
|
+
visited.add(childUrl);
|
|
157
|
+
const childDoc = await this.getUrlAsDoc(childUrl);
|
|
158
|
+
if (!childDoc)
|
|
159
|
+
return null;
|
|
160
|
+
if (childUrl.endsWith("/")) {
|
|
161
|
+
const childUrlResponses = await this.getChildUrlsRecursive(childUrl, visited, depth + 1);
|
|
162
|
+
return [childDoc, ...childUrlResponses];
|
|
163
|
+
}
|
|
164
|
+
return [childDoc];
|
|
165
|
+
})()));
|
|
166
|
+
return results.flat().filter((docs) => docs !== null);
|
|
167
|
+
}
|
|
168
|
+
async load() {
|
|
169
|
+
const rootDoc = await this.getUrlAsDoc(this.url);
|
|
170
|
+
if (!rootDoc)
|
|
171
|
+
return [];
|
|
172
|
+
const docs = [rootDoc];
|
|
173
|
+
docs.push(...(await this.getChildUrlsRecursive(this.url, new Set([this.url]))));
|
|
174
|
+
return docs;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.RecursiveUrlLoader = RecursiveUrlLoader;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Document } from "../../document.js";
|
|
2
|
+
import { AsyncCaller } from "../../util/async_caller.js";
|
|
3
|
+
import { BaseDocumentLoader, DocumentLoader } from "../base.js";
|
|
4
|
+
export interface RecursiveUrlLoaderOptions {
|
|
5
|
+
excludeDirs?: string[];
|
|
6
|
+
extractor?: (text: string) => string;
|
|
7
|
+
maxDepth?: number;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
preventOutside?: boolean;
|
|
10
|
+
callerOptions?: ConstructorParameters<typeof AsyncCaller>[0];
|
|
11
|
+
}
|
|
12
|
+
export declare class RecursiveUrlLoader extends BaseDocumentLoader implements DocumentLoader {
|
|
13
|
+
private caller;
|
|
14
|
+
private url;
|
|
15
|
+
private excludeDirs;
|
|
16
|
+
private extractor;
|
|
17
|
+
private maxDepth;
|
|
18
|
+
private timeout;
|
|
19
|
+
private preventOutside;
|
|
20
|
+
constructor(url: string, options: RecursiveUrlLoaderOptions);
|
|
21
|
+
private fetchWithTimeout;
|
|
22
|
+
private getChildLinks;
|
|
23
|
+
private extractMetadata;
|
|
24
|
+
private getUrlAsDoc;
|
|
25
|
+
private getChildUrlsRecursive;
|
|
26
|
+
load(): Promise<Document[]>;
|
|
27
|
+
}
|