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/dist/llms/base.cjs
CHANGED
|
@@ -5,6 +5,7 @@ const index_js_1 = require("../cache/index.cjs");
|
|
|
5
5
|
const index_js_2 = require("../schema/index.cjs");
|
|
6
6
|
const index_js_3 = require("../base_language/index.cjs");
|
|
7
7
|
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
8
|
+
const base_js_1 = require("../memory/base.cjs");
|
|
8
9
|
/**
|
|
9
10
|
* LLM Wrapper. Provides an {@link call} (an {@link generate}) function that takes in a prompt (or prompts) and returns a string.
|
|
10
11
|
*/
|
|
@@ -27,17 +28,25 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
|
|
|
27
28
|
this.cache = undefined;
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
|
-
async generatePrompt(promptValues,
|
|
31
|
+
async generatePrompt(promptValues, options, callbacks) {
|
|
31
32
|
const prompts = promptValues.map((promptValue) => promptValue.toString());
|
|
32
|
-
return this.generate(prompts,
|
|
33
|
+
return this.generate(prompts, options, callbacks);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get the parameters used to invoke the model
|
|
37
|
+
*/
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
+
invocationParams() {
|
|
40
|
+
return {};
|
|
33
41
|
}
|
|
34
42
|
/** @ignore */
|
|
35
|
-
async _generateUncached(prompts,
|
|
43
|
+
async _generateUncached(prompts, options, callbacks) {
|
|
36
44
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(callbacks, this.callbacks, { verbose: this.verbose });
|
|
37
|
-
const
|
|
45
|
+
const invocationParams = { invocation_params: this?.invocationParams() };
|
|
46
|
+
const runManager = await callbackManager_?.handleLLMStart({ name: this._llmType() }, prompts, undefined, undefined, invocationParams);
|
|
38
47
|
let output;
|
|
39
48
|
try {
|
|
40
|
-
output = await this._generate(prompts,
|
|
49
|
+
output = await this._generate(prompts, options, runManager);
|
|
41
50
|
}
|
|
42
51
|
catch (err) {
|
|
43
52
|
await runManager?.handleLLMError(err);
|
|
@@ -56,16 +65,29 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
|
|
|
56
65
|
/**
|
|
57
66
|
* Run the LLM on the given propmts an input, handling caching.
|
|
58
67
|
*/
|
|
59
|
-
async generate(prompts,
|
|
68
|
+
async generate(prompts, options, callbacks) {
|
|
60
69
|
if (!Array.isArray(prompts)) {
|
|
61
70
|
throw new Error("Argument 'prompts' is expected to be a string[]");
|
|
62
71
|
}
|
|
72
|
+
let parsedOptions;
|
|
73
|
+
if (Array.isArray(options)) {
|
|
74
|
+
parsedOptions = { stop: options };
|
|
75
|
+
}
|
|
76
|
+
else if (options?.timeout && !options.signal) {
|
|
77
|
+
parsedOptions = {
|
|
78
|
+
...options,
|
|
79
|
+
signal: AbortSignal.timeout(options.timeout),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
parsedOptions = options ?? {};
|
|
84
|
+
}
|
|
63
85
|
if (!this.cache) {
|
|
64
|
-
return this._generateUncached(prompts,
|
|
86
|
+
return this._generateUncached(prompts, parsedOptions, callbacks);
|
|
65
87
|
}
|
|
66
88
|
const { cache } = this;
|
|
67
89
|
const params = this.serialize();
|
|
68
|
-
params.stop = stop;
|
|
90
|
+
params.stop = parsedOptions.stop ?? params.stop;
|
|
69
91
|
const llmStringKey = `${Object.entries(params).sort()}`;
|
|
70
92
|
const missingPromptIndices = [];
|
|
71
93
|
const generations = await Promise.all(prompts.map(async (prompt, index) => {
|
|
@@ -77,7 +99,7 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
|
|
|
77
99
|
}));
|
|
78
100
|
let llmOutput = {};
|
|
79
101
|
if (missingPromptIndices.length > 0) {
|
|
80
|
-
const results = await this._generateUncached(missingPromptIndices.map((i) => prompts[i]),
|
|
102
|
+
const results = await this._generateUncached(missingPromptIndices.map((i) => prompts[i]), parsedOptions, callbacks);
|
|
81
103
|
await Promise.all(results.generations.map(async (generation, index) => {
|
|
82
104
|
const promptIndex = missingPromptIndices[index];
|
|
83
105
|
generations[promptIndex] = generation;
|
|
@@ -90,10 +112,18 @@ class BaseLLM extends index_js_3.BaseLanguageModel {
|
|
|
90
112
|
/**
|
|
91
113
|
* Convenience wrapper for {@link generate} that takes in a single string prompt and returns a single string output.
|
|
92
114
|
*/
|
|
93
|
-
async call(prompt,
|
|
94
|
-
const { generations } = await this.generate([prompt],
|
|
115
|
+
async call(prompt, options, callbacks) {
|
|
116
|
+
const { generations } = await this.generate([prompt], options ?? {}, callbacks);
|
|
95
117
|
return generations[0][0].text;
|
|
96
118
|
}
|
|
119
|
+
async predict(text, options, callbacks) {
|
|
120
|
+
return this.call(text, options, callbacks);
|
|
121
|
+
}
|
|
122
|
+
async predictMessages(messages, options, callbacks) {
|
|
123
|
+
const text = (0, base_js_1.getBufferString)(messages);
|
|
124
|
+
const prediction = await this.call(text, options, callbacks);
|
|
125
|
+
return new index_js_2.AIChatMessage(prediction);
|
|
126
|
+
}
|
|
97
127
|
/**
|
|
98
128
|
* Get the identifying parameters of the LLM.
|
|
99
129
|
*/
|
|
@@ -140,12 +170,8 @@ exports.BaseLLM = BaseLLM;
|
|
|
140
170
|
* @augments BaseLLM
|
|
141
171
|
*/
|
|
142
172
|
class LLM extends BaseLLM {
|
|
143
|
-
async _generate(prompts,
|
|
144
|
-
const generations = [];
|
|
145
|
-
for (let i = 0; i < prompts.length; i += 1) {
|
|
146
|
-
const text = await this._call(prompts[i], stop, runManager);
|
|
147
|
-
generations.push([{ text }]);
|
|
148
|
-
}
|
|
173
|
+
async _generate(prompts, options, runManager) {
|
|
174
|
+
const generations = await Promise.all(prompts.map((prompt) => this._call(prompt, options, runManager).then((text) => [{ text }])));
|
|
149
175
|
return { generations };
|
|
150
176
|
}
|
|
151
177
|
}
|
package/dist/llms/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseCache, BasePromptValue, LLMResult } from "../schema/index.js";
|
|
1
|
+
import { BaseCache, BaseChatMessage, BasePromptValue, LLMResult } from "../schema/index.js";
|
|
2
2
|
import { BaseLanguageModel, BaseLanguageModelCallOptions, BaseLanguageModelParams } from "../base_language/index.js";
|
|
3
3
|
import { CallbackManagerForLLMRun, Callbacks } from "../callbacks/manager.js";
|
|
4
4
|
export type SerializedLLM = {
|
|
@@ -18,24 +18,31 @@ export interface BaseLLMCallOptions extends BaseLanguageModelCallOptions {
|
|
|
18
18
|
* LLM Wrapper. Provides an {@link call} (an {@link generate}) function that takes in a prompt (or prompts) and returns a string.
|
|
19
19
|
*/
|
|
20
20
|
export declare abstract class BaseLLM extends BaseLanguageModel {
|
|
21
|
-
CallOptions:
|
|
21
|
+
CallOptions: BaseLLMCallOptions;
|
|
22
|
+
ParsedCallOptions: Omit<this["CallOptions"], "timeout">;
|
|
22
23
|
cache?: BaseCache;
|
|
23
24
|
constructor({ cache, concurrency, ...rest }: BaseLLMParams);
|
|
24
|
-
generatePrompt(promptValues: BasePromptValue[],
|
|
25
|
+
generatePrompt(promptValues: BasePromptValue[], options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<LLMResult>;
|
|
25
26
|
/**
|
|
26
27
|
* Run the LLM on the given prompts and input.
|
|
27
28
|
*/
|
|
28
|
-
abstract _generate(prompts: string[],
|
|
29
|
+
abstract _generate(prompts: string[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<LLMResult>;
|
|
30
|
+
/**
|
|
31
|
+
* Get the parameters used to invoke the model
|
|
32
|
+
*/
|
|
33
|
+
invocationParams(): any;
|
|
29
34
|
/** @ignore */
|
|
30
|
-
_generateUncached(prompts: string[],
|
|
35
|
+
_generateUncached(prompts: string[], options: this["CallOptions"], callbacks?: Callbacks): Promise<LLMResult>;
|
|
31
36
|
/**
|
|
32
37
|
* Run the LLM on the given propmts an input, handling caching.
|
|
33
38
|
*/
|
|
34
|
-
generate(prompts: string[],
|
|
39
|
+
generate(prompts: string[], options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<LLMResult>;
|
|
35
40
|
/**
|
|
36
41
|
* Convenience wrapper for {@link generate} that takes in a single string prompt and returns a single string output.
|
|
37
42
|
*/
|
|
38
|
-
call(prompt: string,
|
|
43
|
+
call(prompt: string, options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<string>;
|
|
44
|
+
predict(text: string, options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<string>;
|
|
45
|
+
predictMessages(messages: BaseChatMessage[], options?: string[] | this["CallOptions"], callbacks?: Callbacks): Promise<BaseChatMessage>;
|
|
39
46
|
/**
|
|
40
47
|
* Get the identifying parameters of the LLM.
|
|
41
48
|
*/
|
|
@@ -65,6 +72,6 @@ export declare abstract class LLM extends BaseLLM {
|
|
|
65
72
|
/**
|
|
66
73
|
* Run the LLM on the given prompt and input.
|
|
67
74
|
*/
|
|
68
|
-
abstract _call(prompt: string,
|
|
69
|
-
_generate(prompts: string[],
|
|
75
|
+
abstract _call(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
76
|
+
_generate(prompts: string[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<LLMResult>;
|
|
70
77
|
}
|
package/dist/llms/base.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { InMemoryCache } from "../cache/index.js";
|
|
2
|
-
import { RUN_KEY, } from "../schema/index.js";
|
|
2
|
+
import { AIChatMessage, RUN_KEY, } from "../schema/index.js";
|
|
3
3
|
import { BaseLanguageModel, } from "../base_language/index.js";
|
|
4
4
|
import { CallbackManager, } from "../callbacks/manager.js";
|
|
5
|
+
import { getBufferString } from "../memory/base.js";
|
|
5
6
|
/**
|
|
6
7
|
* LLM Wrapper. Provides an {@link call} (an {@link generate}) function that takes in a prompt (or prompts) and returns a string.
|
|
7
8
|
*/
|
|
@@ -24,17 +25,25 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
24
25
|
this.cache = undefined;
|
|
25
26
|
}
|
|
26
27
|
}
|
|
27
|
-
async generatePrompt(promptValues,
|
|
28
|
+
async generatePrompt(promptValues, options, callbacks) {
|
|
28
29
|
const prompts = promptValues.map((promptValue) => promptValue.toString());
|
|
29
|
-
return this.generate(prompts,
|
|
30
|
+
return this.generate(prompts, options, callbacks);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the parameters used to invoke the model
|
|
34
|
+
*/
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
invocationParams() {
|
|
37
|
+
return {};
|
|
30
38
|
}
|
|
31
39
|
/** @ignore */
|
|
32
|
-
async _generateUncached(prompts,
|
|
40
|
+
async _generateUncached(prompts, options, callbacks) {
|
|
33
41
|
const callbackManager_ = await CallbackManager.configure(callbacks, this.callbacks, { verbose: this.verbose });
|
|
34
|
-
const
|
|
42
|
+
const invocationParams = { invocation_params: this?.invocationParams() };
|
|
43
|
+
const runManager = await callbackManager_?.handleLLMStart({ name: this._llmType() }, prompts, undefined, undefined, invocationParams);
|
|
35
44
|
let output;
|
|
36
45
|
try {
|
|
37
|
-
output = await this._generate(prompts,
|
|
46
|
+
output = await this._generate(prompts, options, runManager);
|
|
38
47
|
}
|
|
39
48
|
catch (err) {
|
|
40
49
|
await runManager?.handleLLMError(err);
|
|
@@ -53,16 +62,29 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
53
62
|
/**
|
|
54
63
|
* Run the LLM on the given propmts an input, handling caching.
|
|
55
64
|
*/
|
|
56
|
-
async generate(prompts,
|
|
65
|
+
async generate(prompts, options, callbacks) {
|
|
57
66
|
if (!Array.isArray(prompts)) {
|
|
58
67
|
throw new Error("Argument 'prompts' is expected to be a string[]");
|
|
59
68
|
}
|
|
69
|
+
let parsedOptions;
|
|
70
|
+
if (Array.isArray(options)) {
|
|
71
|
+
parsedOptions = { stop: options };
|
|
72
|
+
}
|
|
73
|
+
else if (options?.timeout && !options.signal) {
|
|
74
|
+
parsedOptions = {
|
|
75
|
+
...options,
|
|
76
|
+
signal: AbortSignal.timeout(options.timeout),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
parsedOptions = options ?? {};
|
|
81
|
+
}
|
|
60
82
|
if (!this.cache) {
|
|
61
|
-
return this._generateUncached(prompts,
|
|
83
|
+
return this._generateUncached(prompts, parsedOptions, callbacks);
|
|
62
84
|
}
|
|
63
85
|
const { cache } = this;
|
|
64
86
|
const params = this.serialize();
|
|
65
|
-
params.stop = stop;
|
|
87
|
+
params.stop = parsedOptions.stop ?? params.stop;
|
|
66
88
|
const llmStringKey = `${Object.entries(params).sort()}`;
|
|
67
89
|
const missingPromptIndices = [];
|
|
68
90
|
const generations = await Promise.all(prompts.map(async (prompt, index) => {
|
|
@@ -74,7 +96,7 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
74
96
|
}));
|
|
75
97
|
let llmOutput = {};
|
|
76
98
|
if (missingPromptIndices.length > 0) {
|
|
77
|
-
const results = await this._generateUncached(missingPromptIndices.map((i) => prompts[i]),
|
|
99
|
+
const results = await this._generateUncached(missingPromptIndices.map((i) => prompts[i]), parsedOptions, callbacks);
|
|
78
100
|
await Promise.all(results.generations.map(async (generation, index) => {
|
|
79
101
|
const promptIndex = missingPromptIndices[index];
|
|
80
102
|
generations[promptIndex] = generation;
|
|
@@ -87,10 +109,18 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
87
109
|
/**
|
|
88
110
|
* Convenience wrapper for {@link generate} that takes in a single string prompt and returns a single string output.
|
|
89
111
|
*/
|
|
90
|
-
async call(prompt,
|
|
91
|
-
const { generations } = await this.generate([prompt],
|
|
112
|
+
async call(prompt, options, callbacks) {
|
|
113
|
+
const { generations } = await this.generate([prompt], options ?? {}, callbacks);
|
|
92
114
|
return generations[0][0].text;
|
|
93
115
|
}
|
|
116
|
+
async predict(text, options, callbacks) {
|
|
117
|
+
return this.call(text, options, callbacks);
|
|
118
|
+
}
|
|
119
|
+
async predictMessages(messages, options, callbacks) {
|
|
120
|
+
const text = getBufferString(messages);
|
|
121
|
+
const prediction = await this.call(text, options, callbacks);
|
|
122
|
+
return new AIChatMessage(prediction);
|
|
123
|
+
}
|
|
94
124
|
/**
|
|
95
125
|
* Get the identifying parameters of the LLM.
|
|
96
126
|
*/
|
|
@@ -136,12 +166,8 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
136
166
|
* @augments BaseLLM
|
|
137
167
|
*/
|
|
138
168
|
export class LLM extends BaseLLM {
|
|
139
|
-
async _generate(prompts,
|
|
140
|
-
const generations = [];
|
|
141
|
-
for (let i = 0; i < prompts.length; i += 1) {
|
|
142
|
-
const text = await this._call(prompts[i], stop, runManager);
|
|
143
|
-
generations.push([{ text }]);
|
|
144
|
-
}
|
|
169
|
+
async _generate(prompts, options, runManager) {
|
|
170
|
+
const generations = await Promise.all(prompts.map((prompt) => this._call(prompt, options, runManager).then((text) => [{ text }])));
|
|
145
171
|
return { generations };
|
|
146
172
|
}
|
|
147
173
|
}
|
package/dist/llms/cohere.cjs
CHANGED
|
@@ -45,15 +45,16 @@ class Cohere extends base_js_1.LLM {
|
|
|
45
45
|
return "cohere";
|
|
46
46
|
}
|
|
47
47
|
/** @ignore */
|
|
48
|
-
async _call(prompt,
|
|
48
|
+
async _call(prompt, options) {
|
|
49
49
|
const { cohere } = await Cohere.imports();
|
|
50
50
|
cohere.init(this.apiKey);
|
|
51
51
|
// Hit the `generate` endpoint on the `large` model
|
|
52
|
-
const generateResponse = await this.caller.
|
|
52
|
+
const generateResponse = await this.caller.callWithOptions({ signal: options.signal }, cohere.generate.bind(cohere), {
|
|
53
53
|
prompt,
|
|
54
54
|
model: this.model,
|
|
55
55
|
max_tokens: this.maxTokens,
|
|
56
56
|
temperature: this.temperature,
|
|
57
|
+
end_sequences: options.stop,
|
|
57
58
|
});
|
|
58
59
|
try {
|
|
59
60
|
return generateResponse.body.generations[0].text;
|
package/dist/llms/cohere.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare class Cohere extends LLM implements CohereInput {
|
|
|
18
18
|
constructor(fields?: CohereInput);
|
|
19
19
|
_llmType(): string;
|
|
20
20
|
/** @ignore */
|
|
21
|
-
_call(prompt: string,
|
|
21
|
+
_call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
|
|
22
22
|
/** @ignore */
|
|
23
23
|
static imports(): Promise<{
|
|
24
24
|
cohere: typeof import("cohere-ai");
|
package/dist/llms/cohere.js
CHANGED
|
@@ -42,15 +42,16 @@ export class Cohere extends LLM {
|
|
|
42
42
|
return "cohere";
|
|
43
43
|
}
|
|
44
44
|
/** @ignore */
|
|
45
|
-
async _call(prompt,
|
|
45
|
+
async _call(prompt, options) {
|
|
46
46
|
const { cohere } = await Cohere.imports();
|
|
47
47
|
cohere.init(this.apiKey);
|
|
48
48
|
// Hit the `generate` endpoint on the `large` model
|
|
49
|
-
const generateResponse = await this.caller.
|
|
49
|
+
const generateResponse = await this.caller.callWithOptions({ signal: options.signal }, cohere.generate.bind(cohere), {
|
|
50
50
|
prompt,
|
|
51
51
|
model: this.model,
|
|
52
52
|
max_tokens: this.maxTokens,
|
|
53
53
|
temperature: this.temperature,
|
|
54
|
+
end_sequences: options.stop,
|
|
54
55
|
});
|
|
55
56
|
try {
|
|
56
57
|
return generateResponse.body.generations[0].text;
|
package/dist/llms/hf.cjs
CHANGED
|
@@ -67,10 +67,10 @@ class HuggingFaceInference extends base_js_1.LLM {
|
|
|
67
67
|
return "huggingface_hub";
|
|
68
68
|
}
|
|
69
69
|
/** @ignore */
|
|
70
|
-
async _call(prompt,
|
|
70
|
+
async _call(prompt, options) {
|
|
71
71
|
const { HfInference } = await HuggingFaceInference.imports();
|
|
72
72
|
const hf = new HfInference(this.apiKey);
|
|
73
|
-
const res = await this.caller.
|
|
73
|
+
const res = await this.caller.callWithOptions({ signal: options.signal }, hf.textGeneration.bind(hf), {
|
|
74
74
|
model: this.model,
|
|
75
75
|
parameters: {
|
|
76
76
|
// make it behave similar to openai, returning only the generated text
|
package/dist/llms/hf.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export declare class HuggingFaceInference extends LLM implements HFInput {
|
|
|
28
28
|
constructor(fields?: Partial<HFInput> & BaseLLMParams);
|
|
29
29
|
_llmType(): string;
|
|
30
30
|
/** @ignore */
|
|
31
|
-
_call(prompt: string,
|
|
31
|
+
_call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
|
|
32
32
|
/** @ignore */
|
|
33
33
|
static imports(): Promise<{
|
|
34
34
|
HfInference: typeof import("@huggingface/inference").HfInference;
|
package/dist/llms/hf.js
CHANGED
|
@@ -64,10 +64,10 @@ export class HuggingFaceInference extends LLM {
|
|
|
64
64
|
return "huggingface_hub";
|
|
65
65
|
}
|
|
66
66
|
/** @ignore */
|
|
67
|
-
async _call(prompt,
|
|
67
|
+
async _call(prompt, options) {
|
|
68
68
|
const { HfInference } = await HuggingFaceInference.imports();
|
|
69
69
|
const hf = new HfInference(this.apiKey);
|
|
70
|
-
const res = await this.caller.
|
|
70
|
+
const res = await this.caller.callWithOptions({ signal: options.signal }, hf.textGeneration.bind(hf), {
|
|
71
71
|
model: this.model,
|
|
72
72
|
parameters: {
|
|
73
73
|
// make it behave similar to openai, returning only the generated text
|
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.PromptLayerOpenAIChat = exports.OpenAIChat = void 0;
|
|
7
|
-
const browser_or_node_1 = require("browser-or-node");
|
|
8
7
|
const openai_1 = require("openai");
|
|
8
|
+
const env_js_1 = require("../util/env.cjs");
|
|
9
9
|
const axios_fetch_adapter_js_1 = __importDefault(require("../util/axios-fetch-adapter.cjs"));
|
|
10
10
|
const base_js_1 = require("./base.cjs");
|
|
11
11
|
/**
|
|
@@ -31,6 +31,9 @@ const base_js_1 = require("./base.cjs");
|
|
|
31
31
|
* @augments AzureOpenAIChatInput
|
|
32
32
|
*/
|
|
33
33
|
class OpenAIChat extends base_js_1.LLM {
|
|
34
|
+
get callKeys() {
|
|
35
|
+
return ["stop", "signal", "timeout", "options"];
|
|
36
|
+
}
|
|
34
37
|
constructor(fields, configuration) {
|
|
35
38
|
super(fields ?? {});
|
|
36
39
|
Object.defineProperty(this, "temperature", {
|
|
@@ -255,16 +258,8 @@ class OpenAIChat extends base_js_1.LLM {
|
|
|
255
258
|
return this.prefixMessages ? [...this.prefixMessages, message] : [message];
|
|
256
259
|
}
|
|
257
260
|
/** @ignore */
|
|
258
|
-
async _call(prompt,
|
|
259
|
-
const stop =
|
|
260
|
-
? stopOrOptions
|
|
261
|
-
: stopOrOptions?.stop;
|
|
262
|
-
const options = Array.isArray(stopOrOptions)
|
|
263
|
-
? {}
|
|
264
|
-
: stopOrOptions?.options ?? {};
|
|
265
|
-
if (this.stop && stop) {
|
|
266
|
-
throw new Error("Stop found in input and default params");
|
|
267
|
-
}
|
|
261
|
+
async _call(prompt, options, runManager) {
|
|
262
|
+
const { stop } = options;
|
|
268
263
|
const params = this.invocationParams();
|
|
269
264
|
params.stop = stop ?? params.stop;
|
|
270
265
|
const data = params.stream
|
|
@@ -276,7 +271,8 @@ class OpenAIChat extends base_js_1.LLM {
|
|
|
276
271
|
...params,
|
|
277
272
|
messages: this.formatMessages(prompt),
|
|
278
273
|
}, {
|
|
279
|
-
|
|
274
|
+
signal: options.signal,
|
|
275
|
+
...options.options,
|
|
280
276
|
adapter: axios_fetch_adapter_js_1.default,
|
|
281
277
|
responseType: "stream",
|
|
282
278
|
onmessage: (event) => {
|
|
@@ -340,7 +336,10 @@ class OpenAIChat extends base_js_1.LLM {
|
|
|
340
336
|
: await this.completionWithRetry({
|
|
341
337
|
...params,
|
|
342
338
|
messages: this.formatMessages(prompt),
|
|
343
|
-
},
|
|
339
|
+
}, {
|
|
340
|
+
signal: options.signal,
|
|
341
|
+
...options.options,
|
|
342
|
+
});
|
|
344
343
|
return data.choices[0].message?.content ?? "";
|
|
345
344
|
}
|
|
346
345
|
/** @ignore */
|
|
@@ -360,7 +359,7 @@ class OpenAIChat extends base_js_1.LLM {
|
|
|
360
359
|
this.client = new openai_1.OpenAIApi(clientConfig);
|
|
361
360
|
}
|
|
362
361
|
const axiosOptions = {
|
|
363
|
-
adapter:
|
|
362
|
+
adapter: (0, env_js_1.isNode)() ? undefined : axios_fetch_adapter_js_1.default,
|
|
364
363
|
...this.clientConfig.baseOptions,
|
|
365
364
|
...options,
|
|
366
365
|
};
|
|
@@ -28,6 +28,7 @@ export { OpenAICallOptions, OpenAIChatInput, AzureOpenAIInput };
|
|
|
28
28
|
*/
|
|
29
29
|
export declare class OpenAIChat extends LLM implements OpenAIChatInput, AzureOpenAIInput {
|
|
30
30
|
CallOptions: OpenAICallOptions;
|
|
31
|
+
get callKeys(): (keyof OpenAICallOptions)[];
|
|
31
32
|
temperature: number;
|
|
32
33
|
topP: number;
|
|
33
34
|
frequencyPenalty: number;
|
|
@@ -104,7 +105,7 @@ export declare class OpenAIChat extends LLM implements OpenAIChatInput, AzureOpe
|
|
|
104
105
|
};
|
|
105
106
|
private formatMessages;
|
|
106
107
|
/** @ignore */
|
|
107
|
-
_call(prompt: string,
|
|
108
|
+
_call(prompt: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
108
109
|
/** @ignore */
|
|
109
110
|
completionWithRetry(request: CreateChatCompletionRequest, options?: StreamingAxiosConfiguration): Promise<CreateChatCompletionResponse>;
|
|
110
111
|
_llmType(): string;
|
package/dist/llms/openai-chat.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isNode } from "browser-or-node";
|
|
2
1
|
import { Configuration, OpenAIApi, } from "openai";
|
|
2
|
+
import { isNode } from "../util/env.js";
|
|
3
3
|
import fetchAdapter from "../util/axios-fetch-adapter.js";
|
|
4
4
|
import { LLM } from "./base.js";
|
|
5
5
|
/**
|
|
@@ -25,6 +25,9 @@ import { LLM } from "./base.js";
|
|
|
25
25
|
* @augments AzureOpenAIChatInput
|
|
26
26
|
*/
|
|
27
27
|
export class OpenAIChat extends LLM {
|
|
28
|
+
get callKeys() {
|
|
29
|
+
return ["stop", "signal", "timeout", "options"];
|
|
30
|
+
}
|
|
28
31
|
constructor(fields, configuration) {
|
|
29
32
|
super(fields ?? {});
|
|
30
33
|
Object.defineProperty(this, "temperature", {
|
|
@@ -249,16 +252,8 @@ export class OpenAIChat extends LLM {
|
|
|
249
252
|
return this.prefixMessages ? [...this.prefixMessages, message] : [message];
|
|
250
253
|
}
|
|
251
254
|
/** @ignore */
|
|
252
|
-
async _call(prompt,
|
|
253
|
-
const stop =
|
|
254
|
-
? stopOrOptions
|
|
255
|
-
: stopOrOptions?.stop;
|
|
256
|
-
const options = Array.isArray(stopOrOptions)
|
|
257
|
-
? {}
|
|
258
|
-
: stopOrOptions?.options ?? {};
|
|
259
|
-
if (this.stop && stop) {
|
|
260
|
-
throw new Error("Stop found in input and default params");
|
|
261
|
-
}
|
|
255
|
+
async _call(prompt, options, runManager) {
|
|
256
|
+
const { stop } = options;
|
|
262
257
|
const params = this.invocationParams();
|
|
263
258
|
params.stop = stop ?? params.stop;
|
|
264
259
|
const data = params.stream
|
|
@@ -270,7 +265,8 @@ export class OpenAIChat extends LLM {
|
|
|
270
265
|
...params,
|
|
271
266
|
messages: this.formatMessages(prompt),
|
|
272
267
|
}, {
|
|
273
|
-
|
|
268
|
+
signal: options.signal,
|
|
269
|
+
...options.options,
|
|
274
270
|
adapter: fetchAdapter,
|
|
275
271
|
responseType: "stream",
|
|
276
272
|
onmessage: (event) => {
|
|
@@ -334,7 +330,10 @@ export class OpenAIChat extends LLM {
|
|
|
334
330
|
: await this.completionWithRetry({
|
|
335
331
|
...params,
|
|
336
332
|
messages: this.formatMessages(prompt),
|
|
337
|
-
},
|
|
333
|
+
}, {
|
|
334
|
+
signal: options.signal,
|
|
335
|
+
...options.options,
|
|
336
|
+
});
|
|
338
337
|
return data.choices[0].message?.content ?? "";
|
|
339
338
|
}
|
|
340
339
|
/** @ignore */
|
|
@@ -354,7 +353,7 @@ export class OpenAIChat extends LLM {
|
|
|
354
353
|
this.client = new OpenAIApi(clientConfig);
|
|
355
354
|
}
|
|
356
355
|
const axiosOptions = {
|
|
357
|
-
adapter: isNode ? undefined : fetchAdapter,
|
|
356
|
+
adapter: isNode() ? undefined : fetchAdapter,
|
|
358
357
|
...this.clientConfig.baseOptions,
|
|
359
358
|
...options,
|
|
360
359
|
};
|
package/dist/llms/openai.cjs
CHANGED
|
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.PromptLayerOpenAIChat = exports.OpenAIChat = exports.PromptLayerOpenAI = exports.OpenAI = void 0;
|
|
7
|
-
const browser_or_node_1 = require("browser-or-node");
|
|
8
7
|
const openai_1 = require("openai");
|
|
8
|
+
const env_js_1 = require("../util/env.cjs");
|
|
9
9
|
const axios_fetch_adapter_js_1 = __importDefault(require("../util/axios-fetch-adapter.cjs"));
|
|
10
10
|
const chunk_js_1 = require("../util/chunk.cjs");
|
|
11
11
|
const base_js_1 = require("./base.cjs");
|
|
@@ -30,6 +30,9 @@ const openai_chat_js_1 = require("./openai-chat.cjs");
|
|
|
30
30
|
* if not explicitly available on this class.
|
|
31
31
|
*/
|
|
32
32
|
class OpenAI extends base_js_1.BaseLLM {
|
|
33
|
+
get callKeys() {
|
|
34
|
+
return ["stop", "signal", "timeout", "options"];
|
|
35
|
+
}
|
|
33
36
|
constructor(fields, configuration) {
|
|
34
37
|
if (fields?.modelName?.startsWith("gpt-3.5-turbo") ||
|
|
35
38
|
fields?.modelName?.startsWith("gpt-4") ||
|
|
@@ -264,8 +267,8 @@ class OpenAI extends base_js_1.BaseLLM {
|
|
|
264
267
|
/**
|
|
265
268
|
* Call out to OpenAI's endpoint with k unique prompts
|
|
266
269
|
*
|
|
267
|
-
* @param prompts - The prompts to pass into the model.
|
|
268
|
-
* @param [
|
|
270
|
+
* @param [prompts] - The prompts to pass into the model.
|
|
271
|
+
* @param [options] - Optional list of stop words to use when generating.
|
|
269
272
|
* @param [runManager] - Optional callback manager to use when generating.
|
|
270
273
|
*
|
|
271
274
|
* @returns The full LLM output.
|
|
@@ -277,13 +280,8 @@ class OpenAI extends base_js_1.BaseLLM {
|
|
|
277
280
|
* const response = await openai.generate(["Tell me a joke."]);
|
|
278
281
|
* ```
|
|
279
282
|
*/
|
|
280
|
-
async _generate(prompts,
|
|
281
|
-
const stop =
|
|
282
|
-
? stopOrOptions
|
|
283
|
-
: stopOrOptions?.stop;
|
|
284
|
-
const options = Array.isArray(stopOrOptions)
|
|
285
|
-
? {}
|
|
286
|
-
: stopOrOptions?.options ?? {};
|
|
283
|
+
async _generate(prompts, options, runManager) {
|
|
284
|
+
const { stop } = options;
|
|
287
285
|
const subPrompts = (0, chunk_js_1.chunkArray)(prompts, this.batchSize);
|
|
288
286
|
const choices = [];
|
|
289
287
|
const tokenUsage = {};
|
|
@@ -313,7 +311,8 @@ class OpenAI extends base_js_1.BaseLLM {
|
|
|
313
311
|
...params,
|
|
314
312
|
prompt: subPrompts[i],
|
|
315
313
|
}, {
|
|
316
|
-
|
|
314
|
+
signal: options.signal,
|
|
315
|
+
...options.options,
|
|
317
316
|
adapter: axios_fetch_adapter_js_1.default,
|
|
318
317
|
responseType: "stream",
|
|
319
318
|
onmessage: (event) => {
|
|
@@ -374,7 +373,10 @@ class OpenAI extends base_js_1.BaseLLM {
|
|
|
374
373
|
: await this.completionWithRetry({
|
|
375
374
|
...params,
|
|
376
375
|
prompt: subPrompts[i],
|
|
377
|
-
},
|
|
376
|
+
}, {
|
|
377
|
+
signal: options.signal,
|
|
378
|
+
...options.options,
|
|
379
|
+
});
|
|
378
380
|
choices.push(...data.choices);
|
|
379
381
|
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data.usage ?? {};
|
|
380
382
|
if (completionTokens) {
|
|
@@ -417,7 +419,7 @@ class OpenAI extends base_js_1.BaseLLM {
|
|
|
417
419
|
this.client = new openai_1.OpenAIApi(clientConfig);
|
|
418
420
|
}
|
|
419
421
|
const axiosOptions = {
|
|
420
|
-
adapter:
|
|
422
|
+
adapter: (0, env_js_1.isNode)() ? undefined : axios_fetch_adapter_js_1.default,
|
|
421
423
|
...this.clientConfig.baseOptions,
|
|
422
424
|
...options,
|
|
423
425
|
};
|
package/dist/llms/openai.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export { OpenAICallOptions, AzureOpenAIInput, OpenAIInput };
|
|
|
25
25
|
*/
|
|
26
26
|
export declare class OpenAI extends BaseLLM implements OpenAIInput, AzureOpenAIInput {
|
|
27
27
|
CallOptions: OpenAICallOptions;
|
|
28
|
+
get callKeys(): (keyof OpenAICallOptions)[];
|
|
28
29
|
temperature: number;
|
|
29
30
|
maxTokens: number;
|
|
30
31
|
topP: number;
|
|
@@ -112,8 +113,8 @@ export declare class OpenAI extends BaseLLM implements OpenAIInput, AzureOpenAII
|
|
|
112
113
|
/**
|
|
113
114
|
* Call out to OpenAI's endpoint with k unique prompts
|
|
114
115
|
*
|
|
115
|
-
* @param prompts - The prompts to pass into the model.
|
|
116
|
-
* @param [
|
|
116
|
+
* @param [prompts] - The prompts to pass into the model.
|
|
117
|
+
* @param [options] - Optional list of stop words to use when generating.
|
|
117
118
|
* @param [runManager] - Optional callback manager to use when generating.
|
|
118
119
|
*
|
|
119
120
|
* @returns The full LLM output.
|
|
@@ -125,7 +126,7 @@ export declare class OpenAI extends BaseLLM implements OpenAIInput, AzureOpenAII
|
|
|
125
126
|
* const response = await openai.generate(["Tell me a joke."]);
|
|
126
127
|
* ```
|
|
127
128
|
*/
|
|
128
|
-
_generate(prompts: string[],
|
|
129
|
+
_generate(prompts: string[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<LLMResult>;
|
|
129
130
|
/** @ignore */
|
|
130
131
|
completionWithRetry(request: CreateCompletionRequest, options?: StreamingAxiosConfiguration): Promise<CreateCompletionResponse>;
|
|
131
132
|
_llmType(): string;
|