langchain 0.0.74 → 0.0.76
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/client.cjs +1 -0
- package/client.d.ts +1 -0
- package/client.js +1 -0
- package/dist/base_language/count_tokens.cjs +5 -21
- package/dist/base_language/count_tokens.d.ts +1 -6
- package/dist/base_language/count_tokens.js +4 -19
- package/dist/base_language/index.cjs +16 -24
- package/dist/base_language/index.d.ts +22 -3
- package/dist/base_language/index.js +17 -25
- package/dist/cache/redis.d.ts +3 -1
- package/dist/callbacks/base.d.ts +17 -3
- package/dist/callbacks/handlers/console.cjs +15 -28
- package/dist/callbacks/handlers/console.d.ts +11 -20
- package/dist/callbacks/handlers/console.js +14 -27
- package/dist/callbacks/handlers/initialize.cjs +8 -3
- package/dist/callbacks/handlers/initialize.d.ts +4 -2
- package/dist/callbacks/handlers/initialize.js +6 -2
- package/dist/callbacks/handlers/tracer.cjs +193 -0
- package/dist/callbacks/handlers/tracer.d.ts +65 -0
- package/dist/callbacks/handlers/tracer.js +189 -0
- package/dist/callbacks/handlers/tracer_langchain.cjs +163 -0
- package/dist/callbacks/handlers/tracer_langchain.d.ts +39 -0
- package/dist/callbacks/handlers/tracer_langchain.js +159 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.cjs +202 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +57 -0
- package/dist/callbacks/handlers/tracer_langchain_v1.js +198 -0
- package/dist/callbacks/index.cjs +10 -5
- package/dist/callbacks/index.d.ts +5 -3
- package/dist/callbacks/index.js +5 -3
- package/dist/callbacks/manager.cjs +39 -9
- package/dist/callbacks/manager.d.ts +5 -2
- package/dist/callbacks/manager.js +40 -10
- package/dist/chains/llm_chain.cjs +17 -5
- package/dist/chains/llm_chain.d.ts +9 -3
- package/dist/chains/llm_chain.js +17 -5
- package/dist/chains/question_answering/map_reduce_prompts.cjs +5 -5
- package/dist/chains/question_answering/map_reduce_prompts.d.ts +1 -1
- package/dist/chains/question_answering/map_reduce_prompts.js +1 -1
- package/dist/chains/question_answering/refine_prompts.cjs +5 -5
- package/dist/chains/question_answering/refine_prompts.d.ts +1 -1
- package/dist/chains/question_answering/refine_prompts.js +1 -1
- package/dist/chains/question_answering/stuff_prompts.cjs +2 -2
- package/dist/chains/question_answering/stuff_prompts.d.ts +1 -1
- package/dist/chains/question_answering/stuff_prompts.js +1 -1
- package/dist/chains/sequential_chain.cjs +2 -2
- package/dist/chains/sequential_chain.d.ts +2 -2
- package/dist/chains/sequential_chain.js +2 -2
- package/dist/chains/sql_db/sql_db_prompt.cjs +20 -1
- package/dist/chains/sql_db/sql_db_prompt.d.ts +1 -0
- package/dist/chains/sql_db/sql_db_prompt.js +19 -0
- package/dist/chains/summarization/load.cjs +14 -5
- package/dist/chains/summarization/load.d.ts +7 -2
- package/dist/chains/summarization/load.js +14 -5
- package/dist/chat_models/anthropic.cjs +36 -9
- package/dist/chat_models/anthropic.d.ts +6 -3
- package/dist/chat_models/anthropic.js +36 -9
- package/dist/chat_models/base.cjs +40 -13
- package/dist/chat_models/base.d.ts +14 -7
- package/dist/chat_models/base.js +41 -14
- package/dist/chat_models/openai.cjs +20 -14
- package/dist/chat_models/openai.d.ts +2 -1
- package/dist/chat_models/openai.js +20 -14
- package/dist/client/index.cjs +5 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +1 -0
- package/dist/client/langchainplus.cjs +405 -0
- package/dist/client/langchainplus.d.ts +65 -0
- package/dist/client/langchainplus.js +398 -0
- package/dist/document_loaders/fs/unstructured.cjs +8 -0
- package/dist/document_loaders/fs/unstructured.d.ts +3 -0
- package/dist/document_loaders/fs/unstructured.js +8 -0
- package/dist/document_loaders/web/apify_dataset.cjs +64 -0
- package/dist/document_loaders/web/apify_dataset.d.ts +28 -0
- package/dist/document_loaders/web/apify_dataset.js +60 -0
- package/dist/embeddings/openai.cjs +2 -2
- package/dist/embeddings/openai.js +2 -2
- package/dist/llms/base.cjs +43 -17
- package/dist/llms/base.d.ts +16 -9
- package/dist/llms/base.js +44 -18
- package/dist/llms/cohere.cjs +3 -2
- package/dist/llms/cohere.d.ts +1 -1
- package/dist/llms/cohere.js +3 -2
- package/dist/llms/hf.cjs +2 -2
- package/dist/llms/hf.d.ts +1 -1
- package/dist/llms/hf.js +2 -2
- package/dist/llms/openai-chat.cjs +13 -14
- package/dist/llms/openai-chat.d.ts +2 -1
- package/dist/llms/openai-chat.js +13 -14
- package/dist/llms/openai.cjs +15 -13
- package/dist/llms/openai.d.ts +4 -3
- package/dist/llms/openai.js +15 -13
- package/dist/llms/replicate.cjs +2 -2
- package/dist/llms/replicate.d.ts +1 -1
- package/dist/llms/replicate.js +2 -2
- package/dist/memory/base.cjs +9 -1
- package/dist/memory/base.d.ts +1 -0
- package/dist/memory/base.js +7 -0
- package/dist/memory/entity_memory.cjs +151 -0
- package/dist/memory/entity_memory.d.ts +35 -0
- package/dist/memory/entity_memory.js +147 -0
- package/dist/memory/index.cjs +5 -1
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.js +2 -0
- package/dist/memory/prompt.cjs +84 -1
- package/dist/memory/prompt.d.ts +6 -0
- package/dist/memory/prompt.js +83 -0
- package/dist/memory/stores/entity/in_memory.cjs +32 -0
- package/dist/memory/stores/entity/in_memory.d.ts +10 -0
- package/dist/memory/stores/entity/in_memory.js +28 -0
- package/dist/prompts/index.cjs +6 -1
- package/dist/prompts/index.d.ts +1 -0
- package/dist/prompts/index.js +1 -0
- package/dist/{chains/prompt_selector.d.ts → prompts/selectors/conditional.d.ts} +4 -4
- package/dist/retrievers/document_compressors/chain_extract.cjs +9 -11
- package/dist/retrievers/document_compressors/chain_extract.js +9 -11
- package/dist/schema/index.cjs +13 -1
- package/dist/schema/index.d.ts +19 -0
- package/dist/schema/index.js +11 -0
- package/dist/stores/message/dynamodb.cjs +8 -6
- package/dist/stores/message/dynamodb.js +8 -6
- package/dist/stores/message/redis.cjs +69 -0
- package/dist/stores/message/redis.d.ts +18 -0
- package/dist/stores/message/redis.js +65 -0
- package/dist/stores/message/utils.cjs +30 -15
- package/dist/stores/message/utils.d.ts +4 -2
- package/dist/stores/message/utils.js +28 -14
- package/dist/text_splitter.cjs +3 -23
- package/dist/text_splitter.d.ts +1 -3
- package/dist/text_splitter.js +3 -23
- package/dist/tools/webbrowser.cjs +5 -7
- package/dist/tools/webbrowser.js +3 -5
- package/dist/types/openai-types.d.ts +3 -2
- package/dist/util/async_caller.cjs +16 -0
- package/dist/util/async_caller.d.ts +4 -0
- package/dist/util/async_caller.js +16 -0
- package/dist/util/axios-fetch-adapter.cjs +6 -0
- package/dist/util/axios-fetch-adapter.js +6 -0
- package/dist/util/env.cjs +39 -7
- package/dist/util/env.d.ts +19 -0
- package/dist/util/env.js +32 -6
- package/dist/util/sql_utils.cjs +18 -0
- package/dist/util/sql_utils.js +19 -1
- package/dist/util/tiktoken.cjs +26 -0
- package/dist/util/tiktoken.d.ts +9 -0
- package/dist/util/tiktoken.js +21 -0
- package/dist/vectorstores/redis.cjs +236 -0
- package/dist/vectorstores/redis.d.ts +80 -0
- package/dist/vectorstores/redis.js +232 -0
- package/document_loaders/web/apify_dataset.cjs +1 -0
- package/document_loaders/web/apify_dataset.d.ts +1 -0
- package/document_loaders/web/apify_dataset.js +1 -0
- package/package.json +41 -5
- package/stores/message/redis.cjs +1 -0
- package/stores/message/redis.d.ts +1 -0
- package/stores/message/redis.js +1 -0
- package/vectorstores/redis.cjs +1 -0
- package/vectorstores/redis.d.ts +1 -0
- package/vectorstores/redis.js +1 -0
- package/dist/callbacks/handlers/tracers.cjs +0 -341
- package/dist/callbacks/handlers/tracers.d.ts +0 -100
- package/dist/callbacks/handlers/tracers.js +0 -336
- /package/dist/{chains/prompt_selector.cjs → prompts/selectors/conditional.cjs} +0 -0
- /package/dist/{chains/prompt_selector.js → prompts/selectors/conditional.js} +0 -0
package/client.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/client/index.cjs');
|
package/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/client/index.js'
|
package/client.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/client/index.js'
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateMaxTokens = exports.
|
|
4
|
-
|
|
3
|
+
exports.calculateMaxTokens = exports.getModelContextSize = exports.getEmbeddingContextSize = exports.getModelNameForTiktoken = void 0;
|
|
4
|
+
const tiktoken_js_1 = require("../util/tiktoken.cjs");
|
|
5
|
+
// https://www.npmjs.com/package/js-tiktoken
|
|
5
6
|
const getModelNameForTiktoken = (modelName) => {
|
|
6
7
|
if (modelName.startsWith("gpt-3.5-turbo-")) {
|
|
7
8
|
return "gpt-3.5-turbo";
|
|
@@ -49,31 +50,14 @@ const getModelContextSize = (modelName) => {
|
|
|
49
50
|
}
|
|
50
51
|
};
|
|
51
52
|
exports.getModelContextSize = getModelContextSize;
|
|
52
|
-
const importTiktoken = async () => {
|
|
53
|
-
try {
|
|
54
|
-
const { encoding_for_model } = await import("@dqbd/tiktoken");
|
|
55
|
-
return { encoding_for_model };
|
|
56
|
-
}
|
|
57
|
-
catch (error) {
|
|
58
|
-
console.log(error);
|
|
59
|
-
return { encoding_for_model: null };
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
exports.importTiktoken = importTiktoken;
|
|
63
53
|
const calculateMaxTokens = async ({ prompt, modelName, }) => {
|
|
64
|
-
const { encoding_for_model } = await (0, exports.importTiktoken)();
|
|
65
54
|
// fallback to approximate calculation if tiktoken is not available
|
|
66
55
|
let numTokens = Math.ceil(prompt.length / 4);
|
|
67
56
|
try {
|
|
68
|
-
|
|
69
|
-
const encoding = encoding_for_model((0, exports.getModelNameForTiktoken)(modelName));
|
|
70
|
-
const tokenized = encoding.encode(prompt);
|
|
71
|
-
numTokens = tokenized.length;
|
|
72
|
-
encoding.free();
|
|
73
|
-
}
|
|
57
|
+
numTokens = (await (0, tiktoken_js_1.encodingForModel)(modelName)).encode(prompt).length;
|
|
74
58
|
}
|
|
75
59
|
catch (error) {
|
|
76
|
-
console.warn("Failed to calculate number of tokens
|
|
60
|
+
console.warn("Failed to calculate number of tokens, falling back to approximate count");
|
|
77
61
|
}
|
|
78
62
|
const maxTokens = (0, exports.getModelContextSize)(modelName);
|
|
79
63
|
return maxTokens - numTokens;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TiktokenModel } from "js-tiktoken/lite";
|
|
2
2
|
export declare const getModelNameForTiktoken: (modelName: string) => TiktokenModel;
|
|
3
3
|
export declare const getEmbeddingContextSize: (modelName?: string) => number;
|
|
4
4
|
export declare const getModelContextSize: (modelName: string) => number;
|
|
@@ -6,10 +6,5 @@ interface CalculateMaxTokenProps {
|
|
|
6
6
|
prompt: string;
|
|
7
7
|
modelName: TiktokenModel;
|
|
8
8
|
}
|
|
9
|
-
export declare const importTiktoken: () => Promise<{
|
|
10
|
-
encoding_for_model: typeof import("@dqbd/tiktoken").encoding_for_model;
|
|
11
|
-
} | {
|
|
12
|
-
encoding_for_model: null;
|
|
13
|
-
}>;
|
|
14
9
|
export declare const calculateMaxTokens: ({ prompt, modelName, }: CalculateMaxTokenProps) => Promise<number>;
|
|
15
10
|
export {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { encodingForModel } from "../util/tiktoken.js";
|
|
2
|
+
// https://www.npmjs.com/package/js-tiktoken
|
|
2
3
|
export const getModelNameForTiktoken = (modelName) => {
|
|
3
4
|
if (modelName.startsWith("gpt-3.5-turbo-")) {
|
|
4
5
|
return "gpt-3.5-turbo";
|
|
@@ -43,30 +44,14 @@ export const getModelContextSize = (modelName) => {
|
|
|
43
44
|
return 4097;
|
|
44
45
|
}
|
|
45
46
|
};
|
|
46
|
-
export const importTiktoken = async () => {
|
|
47
|
-
try {
|
|
48
|
-
const { encoding_for_model } = await import("@dqbd/tiktoken");
|
|
49
|
-
return { encoding_for_model };
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
console.log(error);
|
|
53
|
-
return { encoding_for_model: null };
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
47
|
export const calculateMaxTokens = async ({ prompt, modelName, }) => {
|
|
57
|
-
const { encoding_for_model } = await importTiktoken();
|
|
58
48
|
// fallback to approximate calculation if tiktoken is not available
|
|
59
49
|
let numTokens = Math.ceil(prompt.length / 4);
|
|
60
50
|
try {
|
|
61
|
-
|
|
62
|
-
const encoding = encoding_for_model(getModelNameForTiktoken(modelName));
|
|
63
|
-
const tokenized = encoding.encode(prompt);
|
|
64
|
-
numTokens = tokenized.length;
|
|
65
|
-
encoding.free();
|
|
66
|
-
}
|
|
51
|
+
numTokens = (await encodingForModel(modelName)).encode(prompt).length;
|
|
67
52
|
}
|
|
68
53
|
catch (error) {
|
|
69
|
-
console.warn("Failed to calculate number of tokens
|
|
54
|
+
console.warn("Failed to calculate number of tokens, falling back to approximate count");
|
|
70
55
|
}
|
|
71
56
|
const maxTokens = getModelContextSize(modelName);
|
|
72
57
|
return maxTokens - numTokens;
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseLanguageModel = exports.BaseLangChain = void 0;
|
|
4
4
|
const async_caller_js_1 = require("../util/async_caller.cjs");
|
|
5
5
|
const count_tokens_js_1 = require("./count_tokens.cjs");
|
|
6
|
+
const tiktoken_js_1 = require("../util/tiktoken.cjs");
|
|
6
7
|
const getVerbosity = () => false;
|
|
7
8
|
/**
|
|
8
9
|
* Base class for language models, chains, tools.
|
|
@@ -33,6 +34,12 @@ exports.BaseLangChain = BaseLangChain;
|
|
|
33
34
|
* Base class for language models.
|
|
34
35
|
*/
|
|
35
36
|
class BaseLanguageModel extends BaseLangChain {
|
|
37
|
+
/**
|
|
38
|
+
* Keys that the language model accepts as call options.
|
|
39
|
+
*/
|
|
40
|
+
get callKeys() {
|
|
41
|
+
return ["stop", "timeout", "signal"];
|
|
42
|
+
}
|
|
36
43
|
constructor(params) {
|
|
37
44
|
super({
|
|
38
45
|
verbose: params.verbose,
|
|
@@ -54,38 +61,23 @@ class BaseLanguageModel extends BaseLangChain {
|
|
|
54
61
|
writable: true,
|
|
55
62
|
value: void 0
|
|
56
63
|
});
|
|
57
|
-
Object.defineProperty(this, "_registry", {
|
|
58
|
-
enumerable: true,
|
|
59
|
-
configurable: true,
|
|
60
|
-
writable: true,
|
|
61
|
-
value: void 0
|
|
62
|
-
});
|
|
63
64
|
this.caller = new async_caller_js_1.AsyncCaller(params ?? {});
|
|
64
65
|
}
|
|
65
66
|
async getNumTokens(text) {
|
|
66
67
|
// fallback to approximate calculation if tiktoken is not available
|
|
67
68
|
let numTokens = Math.ceil(text.length / 4);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (encoding_for_model) {
|
|
74
|
-
this._encoding = encoding_for_model("modelName" in this
|
|
75
|
-
? (0, count_tokens_js_1.getModelNameForTiktoken)(this.modelName)
|
|
76
|
-
: "gpt2");
|
|
77
|
-
// We need to register a finalizer to free the tokenizer when the
|
|
78
|
-
// model is garbage collected.
|
|
79
|
-
this._registry = new FinalizationRegistry((t) => t.free());
|
|
80
|
-
this._registry.register(this, this._encoding);
|
|
81
|
-
}
|
|
69
|
+
if (!this._encoding) {
|
|
70
|
+
try {
|
|
71
|
+
this._encoding = await (0, tiktoken_js_1.encodingForModel)("modelName" in this
|
|
72
|
+
? (0, count_tokens_js_1.getModelNameForTiktoken)(this.modelName)
|
|
73
|
+
: "gpt2");
|
|
82
74
|
}
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.warn("Failed to calculate number of tokens, falling back to approximate count", error);
|
|
85
77
|
}
|
|
86
78
|
}
|
|
87
|
-
|
|
88
|
-
|
|
79
|
+
if (this._encoding) {
|
|
80
|
+
numTokens = this._encoding.encode(text).length;
|
|
89
81
|
}
|
|
90
82
|
return numTokens;
|
|
91
83
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BasePromptValue, LLMResult } from "../schema/index.js";
|
|
1
|
+
import { BaseChatMessage, BasePromptValue, LLMResult } from "../schema/index.js";
|
|
2
2
|
import { CallbackManager, Callbacks } from "../callbacks/manager.js";
|
|
3
3
|
import { AsyncCaller, AsyncCallerParams } from "../util/async_caller.js";
|
|
4
4
|
export type SerializedLLM = {
|
|
@@ -32,23 +32,42 @@ export interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChai
|
|
|
32
32
|
callbackManager?: CallbackManager;
|
|
33
33
|
}
|
|
34
34
|
export interface BaseLanguageModelCallOptions {
|
|
35
|
+
/**
|
|
36
|
+
* Stop tokens to use for this call.
|
|
37
|
+
* If not provided, the default stop tokens for the model will be used.
|
|
38
|
+
*/
|
|
39
|
+
stop?: string[];
|
|
40
|
+
/**
|
|
41
|
+
* Timeout for this call in milliseconds.
|
|
42
|
+
*/
|
|
43
|
+
timeout?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Abort signal for this call.
|
|
46
|
+
* If provided, the call will be aborted when the signal is aborted.
|
|
47
|
+
*/
|
|
48
|
+
signal?: AbortSignal;
|
|
35
49
|
}
|
|
36
50
|
/**
|
|
37
51
|
* Base class for language models.
|
|
38
52
|
*/
|
|
39
53
|
export declare abstract class BaseLanguageModel extends BaseLangChain implements BaseLanguageModelParams {
|
|
40
54
|
CallOptions: BaseLanguageModelCallOptions;
|
|
55
|
+
/**
|
|
56
|
+
* Keys that the language model accepts as call options.
|
|
57
|
+
*/
|
|
58
|
+
get callKeys(): string[];
|
|
41
59
|
/**
|
|
42
60
|
* The async caller should be used by subclasses to make any async calls,
|
|
43
61
|
* which will thus benefit from the concurrency and retry logic.
|
|
44
62
|
*/
|
|
45
63
|
caller: AsyncCaller;
|
|
46
64
|
constructor(params: BaseLanguageModelParams);
|
|
47
|
-
abstract generatePrompt(promptValues: BasePromptValue[],
|
|
65
|
+
abstract generatePrompt(promptValues: BasePromptValue[], options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<LLMResult>;
|
|
66
|
+
abstract predict(text: string, options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<string>;
|
|
67
|
+
abstract predictMessages(messages: BaseChatMessage[], options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<BaseChatMessage>;
|
|
48
68
|
abstract _modelType(): string;
|
|
49
69
|
abstract _llmType(): string;
|
|
50
70
|
private _encoding?;
|
|
51
|
-
private _registry?;
|
|
52
71
|
getNumTokens(text: string): Promise<number>;
|
|
53
72
|
/**
|
|
54
73
|
* Get the identifying parameters of the LLM.
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AsyncCaller } from "../util/async_caller.js";
|
|
2
|
-
import { getModelNameForTiktoken
|
|
2
|
+
import { getModelNameForTiktoken } from "./count_tokens.js";
|
|
3
|
+
import { encodingForModel } from "../util/tiktoken.js";
|
|
3
4
|
const getVerbosity = () => false;
|
|
4
5
|
/**
|
|
5
6
|
* Base class for language models, chains, tools.
|
|
@@ -29,6 +30,12 @@ export class BaseLangChain {
|
|
|
29
30
|
* Base class for language models.
|
|
30
31
|
*/
|
|
31
32
|
export class BaseLanguageModel extends BaseLangChain {
|
|
33
|
+
/**
|
|
34
|
+
* Keys that the language model accepts as call options.
|
|
35
|
+
*/
|
|
36
|
+
get callKeys() {
|
|
37
|
+
return ["stop", "timeout", "signal"];
|
|
38
|
+
}
|
|
32
39
|
constructor(params) {
|
|
33
40
|
super({
|
|
34
41
|
verbose: params.verbose,
|
|
@@ -50,38 +57,23 @@ export class BaseLanguageModel extends BaseLangChain {
|
|
|
50
57
|
writable: true,
|
|
51
58
|
value: void 0
|
|
52
59
|
});
|
|
53
|
-
Object.defineProperty(this, "_registry", {
|
|
54
|
-
enumerable: true,
|
|
55
|
-
configurable: true,
|
|
56
|
-
writable: true,
|
|
57
|
-
value: void 0
|
|
58
|
-
});
|
|
59
60
|
this.caller = new AsyncCaller(params ?? {});
|
|
60
61
|
}
|
|
61
62
|
async getNumTokens(text) {
|
|
62
63
|
// fallback to approximate calculation if tiktoken is not available
|
|
63
64
|
let numTokens = Math.ceil(text.length / 4);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (encoding_for_model) {
|
|
70
|
-
this._encoding = encoding_for_model("modelName" in this
|
|
71
|
-
? getModelNameForTiktoken(this.modelName)
|
|
72
|
-
: "gpt2");
|
|
73
|
-
// We need to register a finalizer to free the tokenizer when the
|
|
74
|
-
// model is garbage collected.
|
|
75
|
-
this._registry = new FinalizationRegistry((t) => t.free());
|
|
76
|
-
this._registry.register(this, this._encoding);
|
|
77
|
-
}
|
|
65
|
+
if (!this._encoding) {
|
|
66
|
+
try {
|
|
67
|
+
this._encoding = await encodingForModel("modelName" in this
|
|
68
|
+
? getModelNameForTiktoken(this.modelName)
|
|
69
|
+
: "gpt2");
|
|
78
70
|
}
|
|
79
|
-
|
|
80
|
-
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.warn("Failed to calculate number of tokens, falling back to approximate count", error);
|
|
81
73
|
}
|
|
82
74
|
}
|
|
83
|
-
|
|
84
|
-
|
|
75
|
+
if (this._encoding) {
|
|
76
|
+
numTokens = this._encoding.encode(text).length;
|
|
85
77
|
}
|
|
86
78
|
return numTokens;
|
|
87
79
|
}
|
package/dist/cache/redis.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { createCluster, createClient } from "redis";
|
|
2
2
|
import { BaseCache, Generation } from "../schema/index.js";
|
|
3
|
+
type RedisClientType = ReturnType<typeof createClient> | ReturnType<typeof createCluster>;
|
|
3
4
|
export declare class RedisCache extends BaseCache {
|
|
4
5
|
private redisClient;
|
|
5
6
|
constructor(redisClient: RedisClientType);
|
|
6
7
|
lookup(prompt: string, llmKey: string): Promise<Generation[] | null>;
|
|
7
8
|
update(prompt: string, llmKey: string, value: Generation[]): Promise<void>;
|
|
8
9
|
}
|
|
10
|
+
export {};
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AgentAction, AgentFinish, ChainValues, LLMResult } from "../schema/index.js";
|
|
1
|
+
import { AgentAction, AgentFinish, BaseChatMessage, ChainValues, LLMResult } from "../schema/index.js";
|
|
2
2
|
type Error = any;
|
|
3
3
|
export interface BaseCallbackHandlerInput {
|
|
4
4
|
ignoreLLM?: boolean;
|
|
@@ -12,7 +12,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
12
12
|
*/
|
|
13
13
|
handleLLMStart?(llm: {
|
|
14
14
|
name: string;
|
|
15
|
-
}, prompts: string[], runId: string, parentRunId?: string): Promise<void> | void;
|
|
15
|
+
}, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>): Promise<void> | void;
|
|
16
16
|
/**
|
|
17
17
|
* Called when an LLM/ChatModel in `streaming` mode produces a new token
|
|
18
18
|
*/
|
|
@@ -25,6 +25,13 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
25
25
|
* Called at the end of an LLM/ChatModel run, with the output and the run ID.
|
|
26
26
|
*/
|
|
27
27
|
handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string): Promise<void> | void;
|
|
28
|
+
/**
|
|
29
|
+
* Called at the start of a Chat Model run, with the prompt(s)
|
|
30
|
+
* and the run ID.
|
|
31
|
+
*/
|
|
32
|
+
handleChatModelStart?(llm: {
|
|
33
|
+
name: string;
|
|
34
|
+
}, messages: BaseChatMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>): Promise<void> | void;
|
|
28
35
|
/**
|
|
29
36
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
30
37
|
* and the run ID.
|
|
@@ -96,7 +103,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
96
103
|
*/
|
|
97
104
|
handleLLMStart?(llm: {
|
|
98
105
|
name: string;
|
|
99
|
-
}, prompts: string[], runId: string, parentRunId?: string | undefined): void | Promise<void>;
|
|
106
|
+
}, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): void | Promise<void>;
|
|
100
107
|
/**
|
|
101
108
|
* Called when an LLM/ChatModel in `streaming` mode produces a new token
|
|
102
109
|
*/
|
|
@@ -109,6 +116,13 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
109
116
|
* Called at the end of an LLM/ChatModel run, with the output and the run ID.
|
|
110
117
|
*/
|
|
111
118
|
handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string | undefined): void | Promise<void>;
|
|
119
|
+
/**
|
|
120
|
+
* Called at the start of a Chat Model run, with the prompt(s)
|
|
121
|
+
* and the run ID.
|
|
122
|
+
*/
|
|
123
|
+
handleChatModelStart?(llm: {
|
|
124
|
+
name: string;
|
|
125
|
+
}, messages: BaseChatMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): void | Promise<void>;
|
|
112
126
|
/**
|
|
113
127
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
114
128
|
* and the run ID.
|
|
@@ -5,7 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.ConsoleCallbackHandler = void 0;
|
|
7
7
|
const ansi_styles_1 = __importDefault(require("ansi-styles"));
|
|
8
|
-
const
|
|
8
|
+
const tracer_js_1 = require("./tracer.cjs");
|
|
9
9
|
function wrap(style, text) {
|
|
10
10
|
return `${style.open}${text}${style.close}`;
|
|
11
11
|
}
|
|
@@ -25,42 +25,25 @@ function elapsed(run) {
|
|
|
25
25
|
return `${(elapsed / 1000).toFixed(2)}s`;
|
|
26
26
|
}
|
|
27
27
|
const { color } = ansi_styles_1.default;
|
|
28
|
-
class ConsoleCallbackHandler extends
|
|
29
|
-
// boilerplate to work with the base tracer class
|
|
28
|
+
class ConsoleCallbackHandler extends tracer_js_1.BaseTracer {
|
|
30
29
|
constructor() {
|
|
31
|
-
super();
|
|
30
|
+
super(...arguments);
|
|
32
31
|
Object.defineProperty(this, "name", {
|
|
33
32
|
enumerable: true,
|
|
34
33
|
configurable: true,
|
|
35
34
|
writable: true,
|
|
36
35
|
value: "console_callback_handler"
|
|
37
36
|
});
|
|
38
|
-
Object.defineProperty(this, "i", {
|
|
39
|
-
enumerable: true,
|
|
40
|
-
configurable: true,
|
|
41
|
-
writable: true,
|
|
42
|
-
value: 0
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
persistSession(session) {
|
|
46
|
-
// eslint-disable-next-line no-plusplus
|
|
47
|
-
return Promise.resolve({ ...session, id: this.i++ });
|
|
48
37
|
}
|
|
49
38
|
persistRun(_run) {
|
|
50
39
|
return Promise.resolve();
|
|
51
40
|
}
|
|
52
|
-
loadDefaultSession() {
|
|
53
|
-
return this.newSession();
|
|
54
|
-
}
|
|
55
|
-
loadSession(sessionName) {
|
|
56
|
-
return this.newSession(sessionName);
|
|
57
|
-
}
|
|
58
41
|
// utility methods
|
|
59
42
|
getParents(run) {
|
|
60
43
|
const parents = [];
|
|
61
44
|
let currentRun = run;
|
|
62
|
-
while (currentRun.
|
|
63
|
-
const parent = this.runMap.get(currentRun.
|
|
45
|
+
while (currentRun.parent_run_id) {
|
|
46
|
+
const parent = this.runMap.get(currentRun.parent_run_id);
|
|
64
47
|
if (parent) {
|
|
65
48
|
parents.push(parent);
|
|
66
49
|
currentRun = parent;
|
|
@@ -75,7 +58,7 @@ class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
|
|
|
75
58
|
const parents = this.getParents(run).reverse();
|
|
76
59
|
const string = [...parents, run]
|
|
77
60
|
.map((parent, i, arr) => {
|
|
78
|
-
const name = `${parent.execution_order}:${parent.
|
|
61
|
+
const name = `${parent.execution_order}:${parent.run_type}:${parent.name}`;
|
|
79
62
|
return i === arr.length - 1 ? wrap(ansi_styles_1.default.bold, name) : name;
|
|
80
63
|
})
|
|
81
64
|
.join(" > ");
|
|
@@ -96,11 +79,14 @@ class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
|
|
|
96
79
|
}
|
|
97
80
|
onLLMStart(run) {
|
|
98
81
|
const crumbs = this.getBreadcrumbs(run);
|
|
99
|
-
|
|
82
|
+
const inputs = "prompts" in run.inputs
|
|
83
|
+
? { prompts: run.inputs.prompts.map((p) => p.trim()) }
|
|
84
|
+
: run.inputs;
|
|
85
|
+
console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify(inputs, "[inputs]")}`);
|
|
100
86
|
}
|
|
101
87
|
onLLMEnd(run) {
|
|
102
88
|
const crumbs = this.getBreadcrumbs(run);
|
|
103
|
-
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.
|
|
89
|
+
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.outputs, "[response]")}`);
|
|
104
90
|
}
|
|
105
91
|
onLLMError(run) {
|
|
106
92
|
const crumbs = this.getBreadcrumbs(run);
|
|
@@ -108,19 +94,20 @@ class ConsoleCallbackHandler extends tracers_js_1.BaseTracer {
|
|
|
108
94
|
}
|
|
109
95
|
onToolStart(run) {
|
|
110
96
|
const crumbs = this.getBreadcrumbs(run);
|
|
111
|
-
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.
|
|
97
|
+
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.inputs.input?.trim()}"`);
|
|
112
98
|
}
|
|
113
99
|
onToolEnd(run) {
|
|
114
100
|
const crumbs = this.getBreadcrumbs(run);
|
|
115
|
-
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
|
|
101
|
+
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.outputs?.output?.trim()}"`);
|
|
116
102
|
}
|
|
117
103
|
onToolError(run) {
|
|
118
104
|
const crumbs = this.getBreadcrumbs(run);
|
|
119
105
|
console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
120
106
|
}
|
|
121
107
|
onAgentAction(run) {
|
|
108
|
+
const agentRun = run;
|
|
122
109
|
const crumbs = this.getBreadcrumbs(run);
|
|
123
|
-
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(
|
|
110
|
+
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(agentRun.actions[agentRun.actions.length - 1], "[action]")}`);
|
|
124
111
|
}
|
|
125
112
|
}
|
|
126
113
|
exports.ConsoleCallbackHandler = ConsoleCallbackHandler;
|
|
@@ -1,26 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BaseTracer, Run } from "./tracer.js";
|
|
2
2
|
export declare class ConsoleCallbackHandler extends BaseTracer {
|
|
3
3
|
name: "console_callback_handler";
|
|
4
|
-
constructor();
|
|
5
|
-
i: number;
|
|
6
|
-
protected persistSession(session: BaseTracerSession): Promise<{
|
|
7
|
-
id: number;
|
|
8
|
-
start_time: number;
|
|
9
|
-
name?: string | undefined;
|
|
10
|
-
}>;
|
|
11
4
|
protected persistRun(_run: Run): Promise<void>;
|
|
12
|
-
loadDefaultSession(): Promise<import("./tracers.js").TracerSession>;
|
|
13
|
-
loadSession(sessionName: string): Promise<import("./tracers.js").TracerSession>;
|
|
14
5
|
getParents(run: Run): Run[];
|
|
15
6
|
getBreadcrumbs(run: Run): string;
|
|
16
|
-
onChainStart(run:
|
|
17
|
-
onChainEnd(run:
|
|
18
|
-
onChainError(run:
|
|
19
|
-
onLLMStart(run:
|
|
20
|
-
onLLMEnd(run:
|
|
21
|
-
onLLMError(run:
|
|
22
|
-
onToolStart(run:
|
|
23
|
-
onToolEnd(run:
|
|
24
|
-
onToolError(run:
|
|
25
|
-
onAgentAction(run:
|
|
7
|
+
onChainStart(run: Run): void;
|
|
8
|
+
onChainEnd(run: Run): void;
|
|
9
|
+
onChainError(run: Run): void;
|
|
10
|
+
onLLMStart(run: Run): void;
|
|
11
|
+
onLLMEnd(run: Run): void;
|
|
12
|
+
onLLMError(run: Run): void;
|
|
13
|
+
onToolStart(run: Run): void;
|
|
14
|
+
onToolEnd(run: Run): void;
|
|
15
|
+
onToolError(run: Run): void;
|
|
16
|
+
onAgentAction(run: Run): void;
|
|
26
17
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import styles from "ansi-styles";
|
|
2
|
-
import { BaseTracer
|
|
2
|
+
import { BaseTracer } from "./tracer.js";
|
|
3
3
|
function wrap(style, text) {
|
|
4
4
|
return `${style.open}${text}${style.close}`;
|
|
5
5
|
}
|
|
@@ -20,41 +20,24 @@ function elapsed(run) {
|
|
|
20
20
|
}
|
|
21
21
|
const { color } = styles;
|
|
22
22
|
export class ConsoleCallbackHandler extends BaseTracer {
|
|
23
|
-
// boilerplate to work with the base tracer class
|
|
24
23
|
constructor() {
|
|
25
|
-
super();
|
|
24
|
+
super(...arguments);
|
|
26
25
|
Object.defineProperty(this, "name", {
|
|
27
26
|
enumerable: true,
|
|
28
27
|
configurable: true,
|
|
29
28
|
writable: true,
|
|
30
29
|
value: "console_callback_handler"
|
|
31
30
|
});
|
|
32
|
-
Object.defineProperty(this, "i", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
configurable: true,
|
|
35
|
-
writable: true,
|
|
36
|
-
value: 0
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
persistSession(session) {
|
|
40
|
-
// eslint-disable-next-line no-plusplus
|
|
41
|
-
return Promise.resolve({ ...session, id: this.i++ });
|
|
42
31
|
}
|
|
43
32
|
persistRun(_run) {
|
|
44
33
|
return Promise.resolve();
|
|
45
34
|
}
|
|
46
|
-
loadDefaultSession() {
|
|
47
|
-
return this.newSession();
|
|
48
|
-
}
|
|
49
|
-
loadSession(sessionName) {
|
|
50
|
-
return this.newSession(sessionName);
|
|
51
|
-
}
|
|
52
35
|
// utility methods
|
|
53
36
|
getParents(run) {
|
|
54
37
|
const parents = [];
|
|
55
38
|
let currentRun = run;
|
|
56
|
-
while (currentRun.
|
|
57
|
-
const parent = this.runMap.get(currentRun.
|
|
39
|
+
while (currentRun.parent_run_id) {
|
|
40
|
+
const parent = this.runMap.get(currentRun.parent_run_id);
|
|
58
41
|
if (parent) {
|
|
59
42
|
parents.push(parent);
|
|
60
43
|
currentRun = parent;
|
|
@@ -69,7 +52,7 @@ export class ConsoleCallbackHandler extends BaseTracer {
|
|
|
69
52
|
const parents = this.getParents(run).reverse();
|
|
70
53
|
const string = [...parents, run]
|
|
71
54
|
.map((parent, i, arr) => {
|
|
72
|
-
const name = `${parent.execution_order}:${parent.
|
|
55
|
+
const name = `${parent.execution_order}:${parent.run_type}:${parent.name}`;
|
|
73
56
|
return i === arr.length - 1 ? wrap(styles.bold, name) : name;
|
|
74
57
|
})
|
|
75
58
|
.join(" > ");
|
|
@@ -90,11 +73,14 @@ export class ConsoleCallbackHandler extends BaseTracer {
|
|
|
90
73
|
}
|
|
91
74
|
onLLMStart(run) {
|
|
92
75
|
const crumbs = this.getBreadcrumbs(run);
|
|
93
|
-
|
|
76
|
+
const inputs = "prompts" in run.inputs
|
|
77
|
+
? { prompts: run.inputs.prompts.map((p) => p.trim()) }
|
|
78
|
+
: run.inputs;
|
|
79
|
+
console.log(`${wrap(color.green, "[llm/start]")} [${crumbs}] Entering LLM run with input: ${tryJsonStringify(inputs, "[inputs]")}`);
|
|
94
80
|
}
|
|
95
81
|
onLLMEnd(run) {
|
|
96
82
|
const crumbs = this.getBreadcrumbs(run);
|
|
97
|
-
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.
|
|
83
|
+
console.log(`${wrap(color.cyan, "[llm/end]")} [${crumbs}] [${elapsed(run)}] Exiting LLM run with output: ${tryJsonStringify(run.outputs, "[response]")}`);
|
|
98
84
|
}
|
|
99
85
|
onLLMError(run) {
|
|
100
86
|
const crumbs = this.getBreadcrumbs(run);
|
|
@@ -102,18 +88,19 @@ export class ConsoleCallbackHandler extends BaseTracer {
|
|
|
102
88
|
}
|
|
103
89
|
onToolStart(run) {
|
|
104
90
|
const crumbs = this.getBreadcrumbs(run);
|
|
105
|
-
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.
|
|
91
|
+
console.log(`${wrap(color.green, "[tool/start]")} [${crumbs}] Entering Tool run with input: "${run.inputs.input?.trim()}"`);
|
|
106
92
|
}
|
|
107
93
|
onToolEnd(run) {
|
|
108
94
|
const crumbs = this.getBreadcrumbs(run);
|
|
109
|
-
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.output?.trim()}"`);
|
|
95
|
+
console.log(`${wrap(color.cyan, "[tool/end]")} [${crumbs}] [${elapsed(run)}] Exiting Tool run with output: "${run.outputs?.output?.trim()}"`);
|
|
110
96
|
}
|
|
111
97
|
onToolError(run) {
|
|
112
98
|
const crumbs = this.getBreadcrumbs(run);
|
|
113
99
|
console.log(`${wrap(color.red, "[tool/error]")} [${crumbs}] [${elapsed(run)}] Tool run errored with error: ${tryJsonStringify(run.error, "[error]")}`);
|
|
114
100
|
}
|
|
115
101
|
onAgentAction(run) {
|
|
102
|
+
const agentRun = run;
|
|
116
103
|
const crumbs = this.getBreadcrumbs(run);
|
|
117
|
-
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(
|
|
104
|
+
console.log(`${wrap(color.blue, "[agent/action]")} [${crumbs}] Agent selected action: ${tryJsonStringify(agentRun.actions[agentRun.actions.length - 1], "[action]")}`);
|
|
118
105
|
}
|
|
119
106
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getTracingCallbackHandler = void 0;
|
|
4
|
-
const
|
|
3
|
+
exports.getTracingV2CallbackHandler = exports.getTracingCallbackHandler = void 0;
|
|
4
|
+
const tracer_langchain_js_1 = require("./tracer_langchain.cjs");
|
|
5
|
+
const tracer_langchain_v1_js_1 = require("./tracer_langchain_v1.cjs");
|
|
5
6
|
async function getTracingCallbackHandler(session) {
|
|
6
|
-
const tracer = new
|
|
7
|
+
const tracer = new tracer_langchain_v1_js_1.LangChainTracerV1();
|
|
7
8
|
if (session) {
|
|
8
9
|
await tracer.loadSession(session);
|
|
9
10
|
}
|
|
@@ -13,3 +14,7 @@ async function getTracingCallbackHandler(session) {
|
|
|
13
14
|
return tracer;
|
|
14
15
|
}
|
|
15
16
|
exports.getTracingCallbackHandler = getTracingCallbackHandler;
|
|
17
|
+
async function getTracingV2CallbackHandler() {
|
|
18
|
+
return new tracer_langchain_js_1.LangChainTracer();
|
|
19
|
+
}
|
|
20
|
+
exports.getTracingV2CallbackHandler = getTracingV2CallbackHandler;
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
import { LangChainTracer } from "./
|
|
2
|
-
|
|
1
|
+
import { LangChainTracer } from "./tracer_langchain.js";
|
|
2
|
+
import { LangChainTracerV1 } from "./tracer_langchain_v1.js";
|
|
3
|
+
export declare function getTracingCallbackHandler(session?: string): Promise<LangChainTracerV1>;
|
|
4
|
+
export declare function getTracingV2CallbackHandler(): Promise<LangChainTracer>;
|