langchain 0.0.175 → 0.0.177
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/chat_models/bedrock.cjs +25 -4
- package/dist/chat_models/bedrock.d.ts +2 -1
- package/dist/chat_models/bedrock.js +25 -4
- package/dist/chat_models/googlevertexai/common.cjs +46 -7
- package/dist/chat_models/googlevertexai/common.d.ts +7 -2
- package/dist/chat_models/googlevertexai/common.js +47 -8
- package/dist/chat_models/googlevertexai/index.cjs +4 -3
- package/dist/chat_models/googlevertexai/index.js +4 -3
- package/dist/chat_models/googlevertexai/web.cjs +2 -1
- package/dist/chat_models/googlevertexai/web.js +2 -1
- package/dist/chat_models/llama_cpp.cjs +31 -79
- package/dist/chat_models/llama_cpp.d.ts +15 -58
- package/dist/chat_models/llama_cpp.js +32 -80
- package/dist/chat_models/openai.cjs +91 -6
- package/dist/chat_models/openai.d.ts +10 -0
- package/dist/chat_models/openai.js +91 -6
- package/dist/embeddings/googlevertexai.cjs +1 -1
- package/dist/embeddings/googlevertexai.js +1 -1
- package/dist/embeddings/hf.cjs +10 -1
- package/dist/embeddings/hf.d.ts +4 -2
- package/dist/embeddings/hf.js +10 -1
- package/dist/embeddings/llama_cpp.cjs +67 -0
- package/dist/embeddings/llama_cpp.d.ts +26 -0
- package/dist/embeddings/llama_cpp.js +63 -0
- package/dist/embeddings/ollama.cjs +7 -1
- package/dist/embeddings/ollama.js +7 -1
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +2 -2
- package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +1 -1
- package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
- package/dist/experimental/multimodal_embeddings/googlevertexai.js +2 -2
- package/dist/experimental/plan_and_execute/agent_executor.cjs +7 -4
- package/dist/experimental/plan_and_execute/agent_executor.d.ts +4 -3
- package/dist/experimental/plan_and_execute/agent_executor.js +8 -5
- package/dist/experimental/plan_and_execute/prompt.cjs +25 -9
- package/dist/experimental/plan_and_execute/prompt.d.ts +9 -1
- package/dist/experimental/plan_and_execute/prompt.js +23 -8
- package/dist/llms/bedrock.cjs +25 -3
- package/dist/llms/bedrock.d.ts +2 -1
- package/dist/llms/bedrock.js +25 -3
- package/dist/llms/googlevertexai/common.cjs +46 -13
- package/dist/llms/googlevertexai/common.d.ts +8 -3
- package/dist/llms/googlevertexai/common.js +46 -13
- package/dist/llms/googlevertexai/index.cjs +4 -3
- package/dist/llms/googlevertexai/index.js +4 -3
- package/dist/llms/googlevertexai/web.cjs +2 -1
- package/dist/llms/googlevertexai/web.js +2 -1
- package/dist/llms/hf.cjs +10 -1
- package/dist/llms/hf.d.ts +3 -0
- package/dist/llms/hf.js +10 -1
- package/dist/llms/llama_cpp.cjs +25 -65
- package/dist/llms/llama_cpp.d.ts +7 -43
- package/dist/llms/llama_cpp.js +25 -65
- package/dist/load/import_constants.cjs +1 -0
- package/dist/load/import_constants.js +1 -0
- package/dist/prompts/few_shot.cjs +162 -1
- package/dist/prompts/few_shot.d.ts +90 -2
- package/dist/prompts/few_shot.js +160 -0
- package/dist/prompts/index.cjs +2 -1
- package/dist/prompts/index.d.ts +1 -1
- package/dist/prompts/index.js +1 -1
- package/dist/retrievers/zep.cjs +26 -3
- package/dist/retrievers/zep.d.ts +11 -2
- package/dist/retrievers/zep.js +26 -3
- package/dist/types/googlevertexai-types.d.ts +12 -10
- package/dist/util/bedrock.d.ts +2 -0
- package/dist/util/googlevertexai-connection.cjs +298 -10
- package/dist/util/googlevertexai-connection.d.ts +76 -7
- package/dist/util/googlevertexai-connection.js +294 -9
- package/dist/util/googlevertexai-gauth.cjs +36 -0
- package/dist/util/googlevertexai-gauth.d.ts +8 -0
- package/dist/util/googlevertexai-gauth.js +32 -0
- package/dist/util/googlevertexai-webauth.cjs +38 -2
- package/dist/util/googlevertexai-webauth.d.ts +2 -6
- package/dist/util/googlevertexai-webauth.js +38 -2
- package/dist/util/llama_cpp.cjs +34 -0
- package/dist/util/llama_cpp.d.ts +46 -0
- package/dist/util/llama_cpp.js +28 -0
- package/dist/util/openai-format-fndef.cjs +81 -0
- package/dist/util/openai-format-fndef.d.ts +44 -0
- package/dist/util/openai-format-fndef.js +77 -0
- package/dist/util/openapi.d.ts +2 -2
- package/dist/vectorstores/googlevertexai.d.ts +4 -4
- package/dist/vectorstores/pinecone.cjs +5 -5
- package/dist/vectorstores/pinecone.d.ts +2 -2
- package/dist/vectorstores/pinecone.js +5 -5
- package/embeddings/llama_cpp.cjs +1 -0
- package/embeddings/llama_cpp.d.ts +1 -0
- package/embeddings/llama_cpp.js +1 -0
- package/package.json +13 -5
|
@@ -133,6 +133,12 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
|
|
|
133
133
|
writable: true,
|
|
134
134
|
value: new eventstream_codec_1.EventStreamCodec(util_utf8_1.toUtf8, util_utf8_1.fromUtf8)
|
|
135
135
|
});
|
|
136
|
+
Object.defineProperty(this, "streaming", {
|
|
137
|
+
enumerable: true,
|
|
138
|
+
configurable: true,
|
|
139
|
+
writable: true,
|
|
140
|
+
value: false
|
|
141
|
+
});
|
|
136
142
|
this.model = fields?.model ?? this.model;
|
|
137
143
|
const allowedModels = ["ai21", "anthropic", "amazon"];
|
|
138
144
|
if (!allowedModels.includes(this.model.split(".")[0])) {
|
|
@@ -150,6 +156,7 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
|
|
|
150
156
|
this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
|
|
151
157
|
this.stopSequences = fields?.stopSequences;
|
|
152
158
|
this.modelKwargs = fields?.modelKwargs;
|
|
159
|
+
this.streaming = fields?.streaming ?? this.streaming;
|
|
153
160
|
}
|
|
154
161
|
/** Call out to Bedrock service model.
|
|
155
162
|
Arguments:
|
|
@@ -161,10 +168,23 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
|
|
|
161
168
|
Example:
|
|
162
169
|
response = model.call("Tell me a joke.")
|
|
163
170
|
*/
|
|
164
|
-
async _call(messages, options) {
|
|
171
|
+
async _call(messages, options, runManager) {
|
|
165
172
|
const service = "bedrock-runtime";
|
|
166
173
|
const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
|
|
167
174
|
const provider = this.model.split(".")[0];
|
|
175
|
+
if (this.streaming) {
|
|
176
|
+
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
177
|
+
let finalResult;
|
|
178
|
+
for await (const chunk of stream) {
|
|
179
|
+
if (finalResult === undefined) {
|
|
180
|
+
finalResult = chunk;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
finalResult = finalResult.concat(chunk);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return finalResult?.message.content ?? "";
|
|
187
|
+
}
|
|
168
188
|
const response = await this._signedFetch(messages, options, {
|
|
169
189
|
bedrockMethod: "invoke",
|
|
170
190
|
endpointHost,
|
|
@@ -233,7 +253,6 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
|
|
|
233
253
|
event.headers[":content-type"].value !== "application/json") {
|
|
234
254
|
throw Error(`Failed to get event chunk: got ${chunk}`);
|
|
235
255
|
}
|
|
236
|
-
// console.log(decoder.decode(event.body));
|
|
237
256
|
const body = JSON.parse(decoder.decode(event.body));
|
|
238
257
|
if (body.message) {
|
|
239
258
|
throw new Error(body.message);
|
|
@@ -245,7 +264,8 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
|
|
|
245
264
|
text,
|
|
246
265
|
message: new index_js_1.AIMessageChunk({ content: text }),
|
|
247
266
|
});
|
|
248
|
-
|
|
267
|
+
// eslint-disable-next-line no-void
|
|
268
|
+
void runManager?.handleLLMNewToken(text);
|
|
249
269
|
}
|
|
250
270
|
}
|
|
251
271
|
}
|
|
@@ -256,7 +276,8 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
|
|
|
256
276
|
text,
|
|
257
277
|
message: new index_js_1.AIMessageChunk({ content: text }),
|
|
258
278
|
});
|
|
259
|
-
|
|
279
|
+
// eslint-disable-next-line no-void
|
|
280
|
+
void runManager?.handleLLMNewToken(text);
|
|
260
281
|
}
|
|
261
282
|
}
|
|
262
283
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -33,6 +33,7 @@ export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockI
|
|
|
33
33
|
stopSequences?: string[];
|
|
34
34
|
modelKwargs?: Record<string, unknown>;
|
|
35
35
|
codec: EventStreamCodec;
|
|
36
|
+
streaming: boolean;
|
|
36
37
|
get lc_secrets(): {
|
|
37
38
|
[key: string]: string;
|
|
38
39
|
} | undefined;
|
|
@@ -49,7 +50,7 @@ export declare class ChatBedrock extends SimpleChatModel implements BaseBedrockI
|
|
|
49
50
|
Example:
|
|
50
51
|
response = model.call("Tell me a joke.")
|
|
51
52
|
*/
|
|
52
|
-
_call(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<string>;
|
|
53
|
+
_call(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
53
54
|
_signedFetch(messages: BaseMessage[], options: this["ParsedCallOptions"], fields: {
|
|
54
55
|
bedrockMethod: "invoke" | "invoke-with-response-stream";
|
|
55
56
|
endpointHost: string;
|
|
@@ -128,6 +128,12 @@ export class ChatBedrock extends SimpleChatModel {
|
|
|
128
128
|
writable: true,
|
|
129
129
|
value: new EventStreamCodec(toUtf8, fromUtf8)
|
|
130
130
|
});
|
|
131
|
+
Object.defineProperty(this, "streaming", {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: true,
|
|
134
|
+
writable: true,
|
|
135
|
+
value: false
|
|
136
|
+
});
|
|
131
137
|
this.model = fields?.model ?? this.model;
|
|
132
138
|
const allowedModels = ["ai21", "anthropic", "amazon"];
|
|
133
139
|
if (!allowedModels.includes(this.model.split(".")[0])) {
|
|
@@ -145,6 +151,7 @@ export class ChatBedrock extends SimpleChatModel {
|
|
|
145
151
|
this.endpointHost = fields?.endpointHost ?? fields?.endpointUrl;
|
|
146
152
|
this.stopSequences = fields?.stopSequences;
|
|
147
153
|
this.modelKwargs = fields?.modelKwargs;
|
|
154
|
+
this.streaming = fields?.streaming ?? this.streaming;
|
|
148
155
|
}
|
|
149
156
|
/** Call out to Bedrock service model.
|
|
150
157
|
Arguments:
|
|
@@ -156,10 +163,23 @@ export class ChatBedrock extends SimpleChatModel {
|
|
|
156
163
|
Example:
|
|
157
164
|
response = model.call("Tell me a joke.")
|
|
158
165
|
*/
|
|
159
|
-
async _call(messages, options) {
|
|
166
|
+
async _call(messages, options, runManager) {
|
|
160
167
|
const service = "bedrock-runtime";
|
|
161
168
|
const endpointHost = this.endpointHost ?? `${service}.${this.region}.amazonaws.com`;
|
|
162
169
|
const provider = this.model.split(".")[0];
|
|
170
|
+
if (this.streaming) {
|
|
171
|
+
const stream = this._streamResponseChunks(messages, options, runManager);
|
|
172
|
+
let finalResult;
|
|
173
|
+
for await (const chunk of stream) {
|
|
174
|
+
if (finalResult === undefined) {
|
|
175
|
+
finalResult = chunk;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
finalResult = finalResult.concat(chunk);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return finalResult?.message.content ?? "";
|
|
182
|
+
}
|
|
163
183
|
const response = await this._signedFetch(messages, options, {
|
|
164
184
|
bedrockMethod: "invoke",
|
|
165
185
|
endpointHost,
|
|
@@ -228,7 +248,6 @@ export class ChatBedrock extends SimpleChatModel {
|
|
|
228
248
|
event.headers[":content-type"].value !== "application/json") {
|
|
229
249
|
throw Error(`Failed to get event chunk: got ${chunk}`);
|
|
230
250
|
}
|
|
231
|
-
// console.log(decoder.decode(event.body));
|
|
232
251
|
const body = JSON.parse(decoder.decode(event.body));
|
|
233
252
|
if (body.message) {
|
|
234
253
|
throw new Error(body.message);
|
|
@@ -240,7 +259,8 @@ export class ChatBedrock extends SimpleChatModel {
|
|
|
240
259
|
text,
|
|
241
260
|
message: new AIMessageChunk({ content: text }),
|
|
242
261
|
});
|
|
243
|
-
|
|
262
|
+
// eslint-disable-next-line no-void
|
|
263
|
+
void runManager?.handleLLMNewToken(text);
|
|
244
264
|
}
|
|
245
265
|
}
|
|
246
266
|
}
|
|
@@ -251,7 +271,8 @@ export class ChatBedrock extends SimpleChatModel {
|
|
|
251
271
|
text,
|
|
252
272
|
message: new AIMessageChunk({ content: text }),
|
|
253
273
|
});
|
|
254
|
-
|
|
274
|
+
// eslint-disable-next-line no-void
|
|
275
|
+
void runManager?.handleLLMNewToken(text);
|
|
255
276
|
}
|
|
256
277
|
}
|
|
257
278
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -144,6 +144,12 @@ class BaseChatGoogleVertexAI extends base_js_1.BaseChatModel {
|
|
|
144
144
|
writable: true,
|
|
145
145
|
value: void 0
|
|
146
146
|
});
|
|
147
|
+
Object.defineProperty(this, "streamedConnection", {
|
|
148
|
+
enumerable: true,
|
|
149
|
+
configurable: true,
|
|
150
|
+
writable: true,
|
|
151
|
+
value: void 0
|
|
152
|
+
});
|
|
147
153
|
this.model = fields?.model ?? this.model;
|
|
148
154
|
this.temperature = fields?.temperature ?? this.temperature;
|
|
149
155
|
this.maxOutputTokens = fields?.maxOutputTokens ?? this.maxOutputTokens;
|
|
@@ -155,15 +161,31 @@ class BaseChatGoogleVertexAI extends base_js_1.BaseChatModel {
|
|
|
155
161
|
// TODO: Combine the safetyAttributes
|
|
156
162
|
return [];
|
|
157
163
|
}
|
|
158
|
-
|
|
164
|
+
async *_streamResponseChunks(_messages, _options, _runManager) {
|
|
165
|
+
// Make the call as a streaming request
|
|
166
|
+
const instance = this.createInstance(_messages);
|
|
167
|
+
const parameters = this.formatParameters();
|
|
168
|
+
const result = await this.streamedConnection.request([instance], parameters, _options);
|
|
169
|
+
// Get the streaming parser of the response
|
|
170
|
+
const stream = result.data;
|
|
171
|
+
// Loop until the end of the stream
|
|
172
|
+
// During the loop, yield each time we get a chunk from the streaming parser
|
|
173
|
+
// that is either available or added to the queue
|
|
174
|
+
while (!stream.streamDone) {
|
|
175
|
+
const output = await stream.nextChunk();
|
|
176
|
+
const chunk = output !== null
|
|
177
|
+
? BaseChatGoogleVertexAI.convertPredictionChunk(output)
|
|
178
|
+
: new index_js_1.ChatGenerationChunk({
|
|
179
|
+
text: "",
|
|
180
|
+
message: new index_js_1.AIMessageChunk(""),
|
|
181
|
+
generationInfo: { finishReason: "stop" },
|
|
182
|
+
});
|
|
183
|
+
yield chunk;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
159
186
|
async _generate(messages, options) {
|
|
160
187
|
const instance = this.createInstance(messages);
|
|
161
|
-
const parameters =
|
|
162
|
-
temperature: this.temperature,
|
|
163
|
-
topK: this.topK,
|
|
164
|
-
topP: this.topP,
|
|
165
|
-
maxOutputTokens: this.maxOutputTokens,
|
|
166
|
-
};
|
|
188
|
+
const parameters = this.formatParameters();
|
|
167
189
|
const result = await this.connection.request([instance], parameters, options);
|
|
168
190
|
const generations = result?.data?.predictions?.map((prediction) => BaseChatGoogleVertexAI.convertPrediction(prediction)) ?? [];
|
|
169
191
|
return {
|
|
@@ -211,6 +233,14 @@ class BaseChatGoogleVertexAI extends base_js_1.BaseChatModel {
|
|
|
211
233
|
};
|
|
212
234
|
return instance;
|
|
213
235
|
}
|
|
236
|
+
formatParameters() {
|
|
237
|
+
return {
|
|
238
|
+
temperature: this.temperature,
|
|
239
|
+
topK: this.topK,
|
|
240
|
+
topP: this.topP,
|
|
241
|
+
maxOutputTokens: this.maxOutputTokens,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
214
244
|
/**
|
|
215
245
|
* Converts a prediction from the Google Vertex AI chat model to a chat
|
|
216
246
|
* generation.
|
|
@@ -225,5 +255,14 @@ class BaseChatGoogleVertexAI extends base_js_1.BaseChatModel {
|
|
|
225
255
|
generationInfo: prediction,
|
|
226
256
|
};
|
|
227
257
|
}
|
|
258
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
259
|
+
static convertPredictionChunk(output) {
|
|
260
|
+
const generation = BaseChatGoogleVertexAI.convertPrediction(output.outputs[0]);
|
|
261
|
+
return new index_js_1.ChatGenerationChunk({
|
|
262
|
+
text: generation.text,
|
|
263
|
+
message: new index_js_1.AIMessageChunk(generation.message),
|
|
264
|
+
generationInfo: generation.generationInfo,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
228
267
|
}
|
|
229
268
|
exports.BaseChatGoogleVertexAI = BaseChatGoogleVertexAI;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { BaseChatModel } from "../base.js";
|
|
2
|
-
import { BaseMessage, ChatGeneration, ChatMessage, ChatResult, LLMResult } from "../../schema/index.js";
|
|
2
|
+
import { BaseMessage, ChatGeneration, ChatGenerationChunk, ChatMessage, ChatResult, LLMResult } from "../../schema/index.js";
|
|
3
3
|
import { GoogleVertexAILLMConnection } from "../../util/googlevertexai-connection.js";
|
|
4
|
-
import { GoogleVertexAIBaseLLMInput, GoogleVertexAIBasePrediction } from "../../types/googlevertexai-types.js";
|
|
4
|
+
import { GoogleVertexAIBaseLLMInput, GoogleVertexAIBasePrediction, GoogleVertexAIModelParams } from "../../types/googlevertexai-types.js";
|
|
5
5
|
import { BaseLanguageModelCallOptions } from "../../base_language/index.js";
|
|
6
|
+
import { CallbackManagerForLLMRun } from "../../callbacks/index.js";
|
|
6
7
|
/**
|
|
7
8
|
* Represents a single "example" exchange that can be provided to
|
|
8
9
|
* help illustrate what a model response should look like.
|
|
@@ -96,9 +97,11 @@ export declare class BaseChatGoogleVertexAI<AuthOptions> extends BaseChatModel i
|
|
|
96
97
|
topK: number;
|
|
97
98
|
examples: ChatExample[];
|
|
98
99
|
connection: GoogleVertexAILLMConnection<BaseLanguageModelCallOptions, GoogleVertexAIChatInstance, GoogleVertexAIChatPrediction, AuthOptions>;
|
|
100
|
+
streamedConnection: GoogleVertexAILLMConnection<BaseLanguageModelCallOptions, GoogleVertexAIChatInstance, GoogleVertexAIChatPrediction, AuthOptions>;
|
|
99
101
|
get lc_aliases(): Record<string, string>;
|
|
100
102
|
constructor(fields?: GoogleVertexAIChatInput<AuthOptions>);
|
|
101
103
|
_combineLLMOutput(): LLMResult["llmOutput"];
|
|
104
|
+
_streamResponseChunks(_messages: BaseMessage[], _options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
102
105
|
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<ChatResult>;
|
|
103
106
|
_llmType(): string;
|
|
104
107
|
/**
|
|
@@ -107,6 +110,7 @@ export declare class BaseChatGoogleVertexAI<AuthOptions> extends BaseChatModel i
|
|
|
107
110
|
* @returns A new instance of the Google Vertex AI chat model.
|
|
108
111
|
*/
|
|
109
112
|
createInstance(messages: BaseMessage[]): GoogleVertexAIChatInstance;
|
|
113
|
+
formatParameters(): GoogleVertexAIModelParams;
|
|
110
114
|
/**
|
|
111
115
|
* Converts a prediction from the Google Vertex AI chat model to a chat
|
|
112
116
|
* generation.
|
|
@@ -114,5 +118,6 @@ export declare class BaseChatGoogleVertexAI<AuthOptions> extends BaseChatModel i
|
|
|
114
118
|
* @returns The converted chat generation.
|
|
115
119
|
*/
|
|
116
120
|
static convertPrediction(prediction: GoogleVertexAIChatPrediction): ChatGeneration;
|
|
121
|
+
static convertPredictionChunk(output: any): ChatGenerationChunk;
|
|
117
122
|
}
|
|
118
123
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseChatModel } from "../base.js";
|
|
2
|
-
import { AIMessage, ChatMessage, } from "../../schema/index.js";
|
|
2
|
+
import { AIMessage, AIMessageChunk, ChatGenerationChunk, ChatMessage, } from "../../schema/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Represents a chat message in the Google Vertex AI chat model.
|
|
5
5
|
*/
|
|
@@ -140,6 +140,12 @@ export class BaseChatGoogleVertexAI extends BaseChatModel {
|
|
|
140
140
|
writable: true,
|
|
141
141
|
value: void 0
|
|
142
142
|
});
|
|
143
|
+
Object.defineProperty(this, "streamedConnection", {
|
|
144
|
+
enumerable: true,
|
|
145
|
+
configurable: true,
|
|
146
|
+
writable: true,
|
|
147
|
+
value: void 0
|
|
148
|
+
});
|
|
143
149
|
this.model = fields?.model ?? this.model;
|
|
144
150
|
this.temperature = fields?.temperature ?? this.temperature;
|
|
145
151
|
this.maxOutputTokens = fields?.maxOutputTokens ?? this.maxOutputTokens;
|
|
@@ -151,15 +157,31 @@ export class BaseChatGoogleVertexAI extends BaseChatModel {
|
|
|
151
157
|
// TODO: Combine the safetyAttributes
|
|
152
158
|
return [];
|
|
153
159
|
}
|
|
154
|
-
|
|
160
|
+
async *_streamResponseChunks(_messages, _options, _runManager) {
|
|
161
|
+
// Make the call as a streaming request
|
|
162
|
+
const instance = this.createInstance(_messages);
|
|
163
|
+
const parameters = this.formatParameters();
|
|
164
|
+
const result = await this.streamedConnection.request([instance], parameters, _options);
|
|
165
|
+
// Get the streaming parser of the response
|
|
166
|
+
const stream = result.data;
|
|
167
|
+
// Loop until the end of the stream
|
|
168
|
+
// During the loop, yield each time we get a chunk from the streaming parser
|
|
169
|
+
// that is either available or added to the queue
|
|
170
|
+
while (!stream.streamDone) {
|
|
171
|
+
const output = await stream.nextChunk();
|
|
172
|
+
const chunk = output !== null
|
|
173
|
+
? BaseChatGoogleVertexAI.convertPredictionChunk(output)
|
|
174
|
+
: new ChatGenerationChunk({
|
|
175
|
+
text: "",
|
|
176
|
+
message: new AIMessageChunk(""),
|
|
177
|
+
generationInfo: { finishReason: "stop" },
|
|
178
|
+
});
|
|
179
|
+
yield chunk;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
155
182
|
async _generate(messages, options) {
|
|
156
183
|
const instance = this.createInstance(messages);
|
|
157
|
-
const parameters =
|
|
158
|
-
temperature: this.temperature,
|
|
159
|
-
topK: this.topK,
|
|
160
|
-
topP: this.topP,
|
|
161
|
-
maxOutputTokens: this.maxOutputTokens,
|
|
162
|
-
};
|
|
184
|
+
const parameters = this.formatParameters();
|
|
163
185
|
const result = await this.connection.request([instance], parameters, options);
|
|
164
186
|
const generations = result?.data?.predictions?.map((prediction) => BaseChatGoogleVertexAI.convertPrediction(prediction)) ?? [];
|
|
165
187
|
return {
|
|
@@ -207,6 +229,14 @@ export class BaseChatGoogleVertexAI extends BaseChatModel {
|
|
|
207
229
|
};
|
|
208
230
|
return instance;
|
|
209
231
|
}
|
|
232
|
+
formatParameters() {
|
|
233
|
+
return {
|
|
234
|
+
temperature: this.temperature,
|
|
235
|
+
topK: this.topK,
|
|
236
|
+
topP: this.topP,
|
|
237
|
+
maxOutputTokens: this.maxOutputTokens,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
210
240
|
/**
|
|
211
241
|
* Converts a prediction from the Google Vertex AI chat model to a chat
|
|
212
242
|
* generation.
|
|
@@ -221,4 +251,13 @@ export class BaseChatGoogleVertexAI extends BaseChatModel {
|
|
|
221
251
|
generationInfo: prediction,
|
|
222
252
|
};
|
|
223
253
|
}
|
|
254
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
255
|
+
static convertPredictionChunk(output) {
|
|
256
|
+
const generation = BaseChatGoogleVertexAI.convertPrediction(output.outputs[0]);
|
|
257
|
+
return new ChatGenerationChunk({
|
|
258
|
+
text: generation.text,
|
|
259
|
+
message: new AIMessageChunk(generation.message),
|
|
260
|
+
generationInfo: generation.generationInfo,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
224
263
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ChatGoogleVertexAI = void 0;
|
|
4
|
-
const google_auth_library_1 = require("google-auth-library");
|
|
5
4
|
const common_js_1 = require("./common.cjs");
|
|
6
5
|
const googlevertexai_connection_js_1 = require("../../util/googlevertexai-connection.cjs");
|
|
6
|
+
const googlevertexai_gauth_js_1 = require("../../util/googlevertexai-gauth.cjs");
|
|
7
7
|
/**
|
|
8
8
|
* Enables calls to the Google Cloud's Vertex AI API to access
|
|
9
9
|
* Large Language Models in a chat-like fashion.
|
|
@@ -24,11 +24,12 @@ class ChatGoogleVertexAI extends common_js_1.BaseChatGoogleVertexAI {
|
|
|
24
24
|
}
|
|
25
25
|
constructor(fields) {
|
|
26
26
|
super(fields);
|
|
27
|
-
const client = new
|
|
27
|
+
const client = new googlevertexai_gauth_js_1.GAuthClient({
|
|
28
28
|
scopes: "https://www.googleapis.com/auth/cloud-platform",
|
|
29
29
|
...fields?.authOptions,
|
|
30
30
|
});
|
|
31
|
-
this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
|
|
31
|
+
this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
|
|
32
|
+
this.streamedConnection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
exports.ChatGoogleVertexAI = ChatGoogleVertexAI;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { GoogleAuth } from "google-auth-library";
|
|
2
1
|
import { BaseChatGoogleVertexAI } from "./common.js";
|
|
3
2
|
import { GoogleVertexAILLMConnection } from "../../util/googlevertexai-connection.js";
|
|
3
|
+
import { GAuthClient } from "../../util/googlevertexai-gauth.js";
|
|
4
4
|
/**
|
|
5
5
|
* Enables calls to the Google Cloud's Vertex AI API to access
|
|
6
6
|
* Large Language Models in a chat-like fashion.
|
|
@@ -21,10 +21,11 @@ export class ChatGoogleVertexAI extends BaseChatGoogleVertexAI {
|
|
|
21
21
|
}
|
|
22
22
|
constructor(fields) {
|
|
23
23
|
super(fields);
|
|
24
|
-
const client = new
|
|
24
|
+
const client = new GAuthClient({
|
|
25
25
|
scopes: "https://www.googleapis.com/auth/cloud-platform",
|
|
26
26
|
...fields?.authOptions,
|
|
27
27
|
});
|
|
28
|
-
this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
|
|
28
|
+
this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
|
|
29
|
+
this.streamedConnection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
|
|
29
30
|
}
|
|
30
31
|
}
|
|
@@ -25,7 +25,8 @@ class ChatGoogleVertexAI extends common_js_1.BaseChatGoogleVertexAI {
|
|
|
25
25
|
constructor(fields) {
|
|
26
26
|
super(fields);
|
|
27
27
|
const client = new googlevertexai_webauth_js_1.WebGoogleAuth(fields?.authOptions);
|
|
28
|
-
this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
|
|
28
|
+
this.connection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
|
|
29
|
+
this.streamedConnection = new googlevertexai_connection_js_1.GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
32
|
exports.ChatGoogleVertexAI = ChatGoogleVertexAI;
|
|
@@ -22,6 +22,7 @@ export class ChatGoogleVertexAI extends BaseChatGoogleVertexAI {
|
|
|
22
22
|
constructor(fields) {
|
|
23
23
|
super(fields);
|
|
24
24
|
const client = new WebGoogleAuth(fields?.authOptions);
|
|
25
|
-
this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client);
|
|
25
|
+
this.connection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, false);
|
|
26
|
+
this.streamedConnection = new GoogleVertexAILLMConnection({ ...fields, ...this }, this.caller, client, true);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ChatLlamaCpp = void 0;
|
|
4
4
|
const node_llama_cpp_1 = require("node-llama-cpp");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
|
+
const llama_cpp_js_1 = require("../util/llama_cpp.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* To use this model you need to have the `node-llama-cpp` module installed.
|
|
8
9
|
* This can be installed using `npm install -S node-llama-cpp` and the minimum
|
|
@@ -15,73 +16,31 @@ class ChatLlamaCpp extends base_js_1.SimpleChatModel {
|
|
|
15
16
|
}
|
|
16
17
|
constructor(inputs) {
|
|
17
18
|
super(inputs);
|
|
18
|
-
Object.defineProperty(this, "
|
|
19
|
+
Object.defineProperty(this, "maxTokens", {
|
|
19
20
|
enumerable: true,
|
|
20
21
|
configurable: true,
|
|
21
22
|
writable: true,
|
|
22
23
|
value: void 0
|
|
23
24
|
});
|
|
24
|
-
Object.defineProperty(this, "
|
|
25
|
+
Object.defineProperty(this, "temperature", {
|
|
25
26
|
enumerable: true,
|
|
26
27
|
configurable: true,
|
|
27
28
|
writable: true,
|
|
28
29
|
value: void 0
|
|
29
30
|
});
|
|
30
|
-
Object.defineProperty(this, "
|
|
31
|
+
Object.defineProperty(this, "topK", {
|
|
31
32
|
enumerable: true,
|
|
32
33
|
configurable: true,
|
|
33
34
|
writable: true,
|
|
34
35
|
value: void 0
|
|
35
36
|
});
|
|
36
|
-
Object.defineProperty(this, "
|
|
37
|
+
Object.defineProperty(this, "topP", {
|
|
37
38
|
enumerable: true,
|
|
38
39
|
configurable: true,
|
|
39
40
|
writable: true,
|
|
40
41
|
value: void 0
|
|
41
42
|
});
|
|
42
|
-
Object.defineProperty(this, "
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
writable: true,
|
|
46
|
-
value: void 0
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(this, "logitsAll", {
|
|
49
|
-
enumerable: true,
|
|
50
|
-
configurable: true,
|
|
51
|
-
writable: true,
|
|
52
|
-
value: void 0
|
|
53
|
-
});
|
|
54
|
-
Object.defineProperty(this, "lowVram", {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
configurable: true,
|
|
57
|
-
writable: true,
|
|
58
|
-
value: void 0
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(this, "seed", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
configurable: true,
|
|
63
|
-
writable: true,
|
|
64
|
-
value: void 0
|
|
65
|
-
});
|
|
66
|
-
Object.defineProperty(this, "useMlock", {
|
|
67
|
-
enumerable: true,
|
|
68
|
-
configurable: true,
|
|
69
|
-
writable: true,
|
|
70
|
-
value: void 0
|
|
71
|
-
});
|
|
72
|
-
Object.defineProperty(this, "useMmap", {
|
|
73
|
-
enumerable: true,
|
|
74
|
-
configurable: true,
|
|
75
|
-
writable: true,
|
|
76
|
-
value: void 0
|
|
77
|
-
});
|
|
78
|
-
Object.defineProperty(this, "vocabOnly", {
|
|
79
|
-
enumerable: true,
|
|
80
|
-
configurable: true,
|
|
81
|
-
writable: true,
|
|
82
|
-
value: void 0
|
|
83
|
-
});
|
|
84
|
-
Object.defineProperty(this, "modelPath", {
|
|
43
|
+
Object.defineProperty(this, "trimWhitespaceSuffix", {
|
|
85
44
|
enumerable: true,
|
|
86
45
|
configurable: true,
|
|
87
46
|
writable: true,
|
|
@@ -105,47 +64,33 @@ class ChatLlamaCpp extends base_js_1.SimpleChatModel {
|
|
|
105
64
|
writable: true,
|
|
106
65
|
value: void 0
|
|
107
66
|
});
|
|
108
|
-
this.
|
|
109
|
-
this.
|
|
110
|
-
this.
|
|
111
|
-
this.
|
|
112
|
-
this.
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
115
|
-
this.modelPath = inputs.modelPath;
|
|
116
|
-
this.seed = inputs?.seed;
|
|
117
|
-
this.useMlock = inputs?.useMlock;
|
|
118
|
-
this.useMmap = inputs?.useMmap;
|
|
119
|
-
this.vocabOnly = inputs?.vocabOnly;
|
|
120
|
-
this._model = new node_llama_cpp_1.LlamaModel(inputs);
|
|
121
|
-
this._context = new node_llama_cpp_1.LlamaContext({ model: this._model });
|
|
67
|
+
this.maxTokens = inputs?.maxTokens;
|
|
68
|
+
this.temperature = inputs?.temperature;
|
|
69
|
+
this.topK = inputs?.topK;
|
|
70
|
+
this.topP = inputs?.topP;
|
|
71
|
+
this.trimWhitespaceSuffix = inputs?.trimWhitespaceSuffix;
|
|
72
|
+
this._model = (0, llama_cpp_js_1.createLlamaModel)(inputs);
|
|
73
|
+
this._context = (0, llama_cpp_js_1.createLlamaContext)(this._model, inputs);
|
|
122
74
|
this._session = null;
|
|
123
75
|
}
|
|
124
76
|
_llmType() {
|
|
125
77
|
return "llama2_cpp";
|
|
126
78
|
}
|
|
127
|
-
invocationParams() {
|
|
128
|
-
return {
|
|
129
|
-
batchSize: this.batchSize,
|
|
130
|
-
contextSize: this.contextSize,
|
|
131
|
-
embedding: this.embedding,
|
|
132
|
-
f16Kv: this.f16Kv,
|
|
133
|
-
gpuLayers: this.gpuLayers,
|
|
134
|
-
logitsAll: this.logitsAll,
|
|
135
|
-
lowVram: this.lowVram,
|
|
136
|
-
modelPath: this.modelPath,
|
|
137
|
-
seed: this.seed,
|
|
138
|
-
useMlock: this.useMlock,
|
|
139
|
-
useMmap: this.useMmap,
|
|
140
|
-
vocabOnly: this.vocabOnly,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
79
|
/** @ignore */
|
|
144
80
|
_combineLLMOutput() {
|
|
145
81
|
return {};
|
|
146
82
|
}
|
|
83
|
+
invocationParams() {
|
|
84
|
+
return {
|
|
85
|
+
maxTokens: this.maxTokens,
|
|
86
|
+
temperature: this.temperature,
|
|
87
|
+
topK: this.topK,
|
|
88
|
+
topP: this.topP,
|
|
89
|
+
trimWhitespaceSuffix: this.trimWhitespaceSuffix,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
147
92
|
/** @ignore */
|
|
148
|
-
async _call(messages,
|
|
93
|
+
async _call(messages, _options) {
|
|
149
94
|
let prompt = "";
|
|
150
95
|
if (messages.length > 1) {
|
|
151
96
|
// We need to build a new _session
|
|
@@ -159,8 +104,15 @@ class ChatLlamaCpp extends base_js_1.SimpleChatModel {
|
|
|
159
104
|
prompt = messages[0].content;
|
|
160
105
|
}
|
|
161
106
|
try {
|
|
107
|
+
const promptOptions = {
|
|
108
|
+
maxTokens: this?.maxTokens,
|
|
109
|
+
temperature: this?.temperature,
|
|
110
|
+
topK: this?.topK,
|
|
111
|
+
topP: this?.topP,
|
|
112
|
+
trimWhitespaceSuffix: this?.trimWhitespaceSuffix,
|
|
113
|
+
};
|
|
162
114
|
// @ts-expect-error - TS2531: Object is possibly 'null'.
|
|
163
|
-
const completion = await this._session.prompt(prompt,
|
|
115
|
+
const completion = await this._session.prompt(prompt, promptOptions);
|
|
164
116
|
return completion;
|
|
165
117
|
}
|
|
166
118
|
catch (e) {
|