langchain 0.0.164 → 0.0.165
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/chat_models/portkey.cjs +1 -0
- package/chat_models/portkey.d.ts +1 -0
- package/chat_models/portkey.js +1 -0
- package/dist/chat_models/portkey.cjs +159 -0
- package/dist/chat_models/portkey.d.ts +17 -0
- package/dist/chat_models/portkey.js +155 -0
- package/dist/embeddings/minimax.cjs +1 -1
- package/dist/embeddings/minimax.js +1 -1
- package/dist/llms/portkey.cjs +147 -0
- package/dist/llms/portkey.d.ts +33 -0
- package/dist/llms/portkey.js +138 -0
- package/dist/llms/sagemaker_endpoint.cjs +76 -14
- package/dist/llms/sagemaker_endpoint.d.ts +39 -20
- package/dist/llms/sagemaker_endpoint.js +77 -15
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/output_parsers/list.cjs +1 -1
- package/dist/output_parsers/list.js +1 -1
- package/dist/vectorstores/cassandra.cjs +212 -0
- package/dist/vectorstores/cassandra.d.ts +98 -0
- package/dist/vectorstores/cassandra.js +208 -0
- package/dist/vectorstores/mongodb_atlas.cjs +29 -39
- package/dist/vectorstores/mongodb_atlas.js +29 -39
- package/llms/portkey.cjs +1 -0
- package/llms/portkey.d.ts +1 -0
- package/llms/portkey.js +1 -0
- package/package.json +42 -2
- package/vectorstores/cassandra.cjs +1 -0
- package/vectorstores/cassandra.d.ts +1 -0
- package/vectorstores/cassandra.js +1 -0
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SageMakerEndpoint = exports.BaseSageMakerContentHandler = void 0;
|
|
4
4
|
const client_sagemaker_runtime_1 = require("@aws-sdk/client-sagemaker-runtime");
|
|
5
|
+
const index_js_1 = require("../schema/index.cjs");
|
|
5
6
|
const base_js_1 = require("./base.cjs");
|
|
6
7
|
/**
|
|
7
8
|
* A handler class to transform input from LLM to a format that SageMaker
|
|
@@ -32,14 +33,12 @@ const base_js_1 = require("./base.cjs");
|
|
|
32
33
|
*/
|
|
33
34
|
class BaseSageMakerContentHandler {
|
|
34
35
|
constructor() {
|
|
35
|
-
/** The MIME type of the input data passed to endpoint */
|
|
36
36
|
Object.defineProperty(this, "contentType", {
|
|
37
37
|
enumerable: true,
|
|
38
38
|
configurable: true,
|
|
39
39
|
writable: true,
|
|
40
40
|
value: "text/plain"
|
|
41
41
|
});
|
|
42
|
-
/** The MIME type of the response data returned from endpoint */
|
|
43
42
|
Object.defineProperty(this, "accepts", {
|
|
44
43
|
enumerable: true,
|
|
45
44
|
configurable: true,
|
|
@@ -51,16 +50,17 @@ class BaseSageMakerContentHandler {
|
|
|
51
50
|
exports.BaseSageMakerContentHandler = BaseSageMakerContentHandler;
|
|
52
51
|
/**
|
|
53
52
|
* The SageMakerEndpoint class is used to interact with SageMaker
|
|
54
|
-
* Inference Endpoint models. It
|
|
55
|
-
*
|
|
56
|
-
* SageMaker endpoint using the provided content handler. The class uses
|
|
57
|
-
* AWS client for authentication, which automatically loads credentials.
|
|
53
|
+
* Inference Endpoint models. It uses the AWS client for authentication,
|
|
54
|
+
* which automatically loads credentials.
|
|
58
55
|
* If a specific credential profile is to be used, the name of the profile
|
|
59
56
|
* from the ~/.aws/credentials file must be passed. The credentials or
|
|
60
57
|
* roles used should have the required policies to access the SageMaker
|
|
61
58
|
* endpoint.
|
|
62
59
|
*/
|
|
63
60
|
class SageMakerEndpoint extends base_js_1.LLM {
|
|
61
|
+
static lc_name() {
|
|
62
|
+
return "SageMakerEndpoint";
|
|
63
|
+
}
|
|
64
64
|
get lc_secrets() {
|
|
65
65
|
return {
|
|
66
66
|
"clientOptions.credentials.accessKeyId": "AWS_ACCESS_KEY_ID",
|
|
@@ -69,39 +69,44 @@ class SageMakerEndpoint extends base_js_1.LLM {
|
|
|
69
69
|
};
|
|
70
70
|
}
|
|
71
71
|
constructor(fields) {
|
|
72
|
-
super(fields
|
|
72
|
+
super(fields);
|
|
73
73
|
Object.defineProperty(this, "endpointName", {
|
|
74
74
|
enumerable: true,
|
|
75
75
|
configurable: true,
|
|
76
76
|
writable: true,
|
|
77
77
|
value: void 0
|
|
78
78
|
});
|
|
79
|
-
Object.defineProperty(this, "
|
|
79
|
+
Object.defineProperty(this, "modelKwargs", {
|
|
80
80
|
enumerable: true,
|
|
81
81
|
configurable: true,
|
|
82
82
|
writable: true,
|
|
83
83
|
value: void 0
|
|
84
84
|
});
|
|
85
|
-
Object.defineProperty(this, "
|
|
85
|
+
Object.defineProperty(this, "endpointKwargs", {
|
|
86
86
|
enumerable: true,
|
|
87
87
|
configurable: true,
|
|
88
88
|
writable: true,
|
|
89
89
|
value: void 0
|
|
90
90
|
});
|
|
91
|
-
Object.defineProperty(this, "
|
|
91
|
+
Object.defineProperty(this, "client", {
|
|
92
92
|
enumerable: true,
|
|
93
93
|
configurable: true,
|
|
94
94
|
writable: true,
|
|
95
95
|
value: void 0
|
|
96
96
|
});
|
|
97
|
-
Object.defineProperty(this, "
|
|
97
|
+
Object.defineProperty(this, "contentHandler", {
|
|
98
98
|
enumerable: true,
|
|
99
99
|
configurable: true,
|
|
100
100
|
writable: true,
|
|
101
101
|
value: void 0
|
|
102
102
|
});
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
Object.defineProperty(this, "streaming", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
108
|
+
});
|
|
109
|
+
if (!fields.clientOptions.region) {
|
|
105
110
|
throw new Error(`Please pass a "clientOptions" object with a "region" field to the constructor`);
|
|
106
111
|
}
|
|
107
112
|
const endpointName = fields?.endpointName;
|
|
@@ -116,13 +121,33 @@ class SageMakerEndpoint extends base_js_1.LLM {
|
|
|
116
121
|
this.contentHandler = fields.contentHandler;
|
|
117
122
|
this.endpointKwargs = fields.endpointKwargs;
|
|
118
123
|
this.modelKwargs = fields.modelKwargs;
|
|
124
|
+
this.streaming = fields.streaming ?? false;
|
|
119
125
|
this.client = new client_sagemaker_runtime_1.SageMakerRuntimeClient(fields.clientOptions);
|
|
120
126
|
}
|
|
121
127
|
_llmType() {
|
|
122
128
|
return "sagemaker_endpoint";
|
|
123
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Calls the SageMaker endpoint and retrieves the result.
|
|
132
|
+
* @param {string} prompt The input prompt.
|
|
133
|
+
* @param {this["ParsedCallOptions"]} options Parsed call options.
|
|
134
|
+
* @param {CallbackManagerForLLMRun} _runManager Optional run manager.
|
|
135
|
+
* @returns {Promise<string>} A promise that resolves to the generated string.
|
|
136
|
+
*/
|
|
124
137
|
/** @ignore */
|
|
125
|
-
async _call(prompt, options) {
|
|
138
|
+
async _call(prompt, options, _runManager) {
|
|
139
|
+
return this.streaming
|
|
140
|
+
? await this.streamingCall(prompt, options)
|
|
141
|
+
: await this.noStreamingCall(prompt, options);
|
|
142
|
+
}
|
|
143
|
+
async streamingCall(prompt, options) {
|
|
144
|
+
const chunks = [];
|
|
145
|
+
for await (const chunk of this._streamResponseChunks(prompt, options)) {
|
|
146
|
+
chunks.push(chunk.text);
|
|
147
|
+
}
|
|
148
|
+
return chunks.join("");
|
|
149
|
+
}
|
|
150
|
+
async noStreamingCall(prompt, options) {
|
|
126
151
|
const body = await this.contentHandler.transformInput(prompt, this.modelKwargs ?? {});
|
|
127
152
|
const { contentType, accepts } = this.contentHandler;
|
|
128
153
|
const response = await this.caller.call(() => this.client.send(new client_sagemaker_runtime_1.InvokeEndpointCommand({
|
|
@@ -137,5 +162,42 @@ class SageMakerEndpoint extends base_js_1.LLM {
|
|
|
137
162
|
}
|
|
138
163
|
return this.contentHandler.transformOutput(response.Body);
|
|
139
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Streams response chunks from the SageMaker endpoint.
|
|
167
|
+
* @param {string} prompt The input prompt.
|
|
168
|
+
* @param {this["ParsedCallOptions"]} options Parsed call options.
|
|
169
|
+
* @returns {AsyncGenerator<GenerationChunk>} An asynchronous generator yielding generation chunks.
|
|
170
|
+
*/
|
|
171
|
+
async *_streamResponseChunks(prompt, options) {
|
|
172
|
+
const body = await this.contentHandler.transformInput(prompt, this.modelKwargs ?? {});
|
|
173
|
+
const { contentType, accepts } = this.contentHandler;
|
|
174
|
+
const stream = await this.caller.call(() => this.client.send(new client_sagemaker_runtime_1.InvokeEndpointWithResponseStreamCommand({
|
|
175
|
+
EndpointName: this.endpointName,
|
|
176
|
+
Body: body,
|
|
177
|
+
ContentType: contentType,
|
|
178
|
+
Accept: accepts,
|
|
179
|
+
...this.endpointKwargs,
|
|
180
|
+
}), { abortSignal: options.signal }));
|
|
181
|
+
if (!stream.Body) {
|
|
182
|
+
throw new Error("Inference result missing Body");
|
|
183
|
+
}
|
|
184
|
+
for await (const chunk of stream.Body) {
|
|
185
|
+
if (chunk.PayloadPart && chunk.PayloadPart.Bytes) {
|
|
186
|
+
yield new index_js_1.GenerationChunk({
|
|
187
|
+
text: await this.contentHandler.transformOutput(chunk.PayloadPart.Bytes),
|
|
188
|
+
generationInfo: {
|
|
189
|
+
...chunk,
|
|
190
|
+
response: undefined,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
else if (chunk.InternalStreamFailure) {
|
|
195
|
+
throw new Error(chunk.InternalStreamFailure.message);
|
|
196
|
+
}
|
|
197
|
+
else if (chunk.ModelStreamError) {
|
|
198
|
+
throw new Error(chunk.ModelStreamError.message);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
140
202
|
}
|
|
141
203
|
exports.SageMakerEndpoint = SageMakerEndpoint;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { SageMakerRuntimeClient, SageMakerRuntimeClientConfig } from "@aws-sdk/client-sagemaker-runtime";
|
|
2
|
-
import {
|
|
2
|
+
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
|
|
3
|
+
import { GenerationChunk } from "../schema/index.js";
|
|
4
|
+
import { BaseLLMCallOptions, BaseLLMParams, LLM } from "./base.js";
|
|
3
5
|
/**
|
|
4
6
|
* A handler class to transform input from LLM to a format that SageMaker
|
|
5
7
|
* endpoint expects. Similarily, the class also handles transforming output from
|
|
@@ -28,22 +30,22 @@ import { LLM, BaseLLMParams } from "./base.js";
|
|
|
28
30
|
* ```
|
|
29
31
|
*/
|
|
30
32
|
export declare abstract class BaseSageMakerContentHandler<InputType, OutputType> {
|
|
31
|
-
/** The MIME type of the input data passed to endpoint */
|
|
32
33
|
contentType: string;
|
|
33
|
-
/** The MIME type of the response data returned from endpoint */
|
|
34
34
|
accepts: string;
|
|
35
35
|
/**
|
|
36
|
-
* Transforms the
|
|
37
|
-
*
|
|
38
|
-
*
|
|
36
|
+
* Transforms the prompt and model arguments into a specific format for sending to SageMaker.
|
|
37
|
+
* @param {InputType} prompt The prompt to be transformed.
|
|
38
|
+
* @param {Record<string, unknown>} modelKwargs Additional arguments.
|
|
39
|
+
* @returns {Promise<Uint8Array>} A promise that resolves to the formatted data for sending.
|
|
39
40
|
*/
|
|
40
41
|
abstract transformInput(prompt: InputType, modelKwargs: Record<string, unknown>): Promise<Uint8Array>;
|
|
41
42
|
/**
|
|
42
|
-
* Transforms
|
|
43
|
+
* Transforms SageMaker output into a desired format.
|
|
44
|
+
* @param {Uint8Array} output The raw output from SageMaker.
|
|
45
|
+
* @returns {Promise<OutputType>} A promise that resolves to the transformed data.
|
|
43
46
|
*/
|
|
44
47
|
abstract transformOutput(output: Uint8Array): Promise<OutputType>;
|
|
45
48
|
}
|
|
46
|
-
/** Content handler for LLM class. */
|
|
47
49
|
export type SageMakerLLMContentHandler = BaseSageMakerContentHandler<string, string>;
|
|
48
50
|
/**
|
|
49
51
|
* The SageMakerEndpointInput interface defines the input parameters for
|
|
@@ -61,11 +63,6 @@ export interface SageMakerEndpointInput extends BaseLLMParams {
|
|
|
61
63
|
* Options passed to the SageMaker client.
|
|
62
64
|
*/
|
|
63
65
|
clientOptions: SageMakerRuntimeClientConfig;
|
|
64
|
-
/**
|
|
65
|
-
* The content handler class that provides an input and output transform
|
|
66
|
-
* functions to handle formats between LLM and the endpoint.
|
|
67
|
-
*/
|
|
68
|
-
contentHandler: SageMakerLLMContentHandler;
|
|
69
66
|
/**
|
|
70
67
|
* Key word arguments to pass to the model.
|
|
71
68
|
*/
|
|
@@ -74,29 +71,51 @@ export interface SageMakerEndpointInput extends BaseLLMParams {
|
|
|
74
71
|
* Optional attributes passed to the InvokeEndpointCommand
|
|
75
72
|
*/
|
|
76
73
|
endpointKwargs?: Record<string, unknown>;
|
|
74
|
+
/**
|
|
75
|
+
* The content handler class that provides an input and output transform
|
|
76
|
+
* functions to handle formats between LLM and the endpoint.
|
|
77
|
+
*/
|
|
78
|
+
contentHandler: SageMakerLLMContentHandler;
|
|
79
|
+
streaming?: boolean;
|
|
77
80
|
}
|
|
78
81
|
/**
|
|
79
82
|
* The SageMakerEndpoint class is used to interact with SageMaker
|
|
80
|
-
* Inference Endpoint models. It
|
|
81
|
-
*
|
|
82
|
-
* SageMaker endpoint using the provided content handler. The class uses
|
|
83
|
-
* AWS client for authentication, which automatically loads credentials.
|
|
83
|
+
* Inference Endpoint models. It uses the AWS client for authentication,
|
|
84
|
+
* which automatically loads credentials.
|
|
84
85
|
* If a specific credential profile is to be used, the name of the profile
|
|
85
86
|
* from the ~/.aws/credentials file must be passed. The credentials or
|
|
86
87
|
* roles used should have the required policies to access the SageMaker
|
|
87
88
|
* endpoint.
|
|
88
89
|
*/
|
|
89
|
-
export declare class SageMakerEndpoint extends LLM {
|
|
90
|
+
export declare class SageMakerEndpoint extends LLM<BaseLLMCallOptions> {
|
|
91
|
+
static lc_name(): string;
|
|
90
92
|
get lc_secrets(): {
|
|
91
93
|
[key: string]: string;
|
|
92
94
|
} | undefined;
|
|
93
95
|
endpointName: string;
|
|
94
|
-
contentHandler: SageMakerLLMContentHandler;
|
|
95
96
|
modelKwargs?: Record<string, unknown>;
|
|
96
97
|
endpointKwargs?: Record<string, unknown>;
|
|
97
98
|
client: SageMakerRuntimeClient;
|
|
99
|
+
contentHandler: SageMakerLLMContentHandler;
|
|
100
|
+
streaming: boolean;
|
|
98
101
|
constructor(fields: SageMakerEndpointInput);
|
|
99
102
|
_llmType(): string;
|
|
103
|
+
/**
|
|
104
|
+
* Calls the SageMaker endpoint and retrieves the result.
|
|
105
|
+
* @param {string} prompt The input prompt.
|
|
106
|
+
* @param {this["ParsedCallOptions"]} options Parsed call options.
|
|
107
|
+
* @param {CallbackManagerForLLMRun} _runManager Optional run manager.
|
|
108
|
+
* @returns {Promise<string>} A promise that resolves to the generated string.
|
|
109
|
+
*/
|
|
100
110
|
/** @ignore */
|
|
101
|
-
_call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
|
|
111
|
+
_call(prompt: string, options: this["ParsedCallOptions"], _runManager?: CallbackManagerForLLMRun): Promise<string>;
|
|
112
|
+
private streamingCall;
|
|
113
|
+
private noStreamingCall;
|
|
114
|
+
/**
|
|
115
|
+
* Streams response chunks from the SageMaker endpoint.
|
|
116
|
+
* @param {string} prompt The input prompt.
|
|
117
|
+
* @param {this["ParsedCallOptions"]} options Parsed call options.
|
|
118
|
+
* @returns {AsyncGenerator<GenerationChunk>} An asynchronous generator yielding generation chunks.
|
|
119
|
+
*/
|
|
120
|
+
_streamResponseChunks(prompt: string, options: this["ParsedCallOptions"]): AsyncGenerator<GenerationChunk>;
|
|
102
121
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InvokeEndpointCommand, InvokeEndpointWithResponseStreamCommand, SageMakerRuntimeClient, } from "@aws-sdk/client-sagemaker-runtime";
|
|
2
|
+
import { GenerationChunk } from "../schema/index.js";
|
|
2
3
|
import { LLM } from "./base.js";
|
|
3
4
|
/**
|
|
4
5
|
* A handler class to transform input from LLM to a format that SageMaker
|
|
@@ -29,14 +30,12 @@ import { LLM } from "./base.js";
|
|
|
29
30
|
*/
|
|
30
31
|
export class BaseSageMakerContentHandler {
|
|
31
32
|
constructor() {
|
|
32
|
-
/** The MIME type of the input data passed to endpoint */
|
|
33
33
|
Object.defineProperty(this, "contentType", {
|
|
34
34
|
enumerable: true,
|
|
35
35
|
configurable: true,
|
|
36
36
|
writable: true,
|
|
37
37
|
value: "text/plain"
|
|
38
38
|
});
|
|
39
|
-
/** The MIME type of the response data returned from endpoint */
|
|
40
39
|
Object.defineProperty(this, "accepts", {
|
|
41
40
|
enumerable: true,
|
|
42
41
|
configurable: true,
|
|
@@ -47,16 +46,17 @@ export class BaseSageMakerContentHandler {
|
|
|
47
46
|
}
|
|
48
47
|
/**
|
|
49
48
|
* The SageMakerEndpoint class is used to interact with SageMaker
|
|
50
|
-
* Inference Endpoint models. It
|
|
51
|
-
*
|
|
52
|
-
* SageMaker endpoint using the provided content handler. The class uses
|
|
53
|
-
* AWS client for authentication, which automatically loads credentials.
|
|
49
|
+
* Inference Endpoint models. It uses the AWS client for authentication,
|
|
50
|
+
* which automatically loads credentials.
|
|
54
51
|
* If a specific credential profile is to be used, the name of the profile
|
|
55
52
|
* from the ~/.aws/credentials file must be passed. The credentials or
|
|
56
53
|
* roles used should have the required policies to access the SageMaker
|
|
57
54
|
* endpoint.
|
|
58
55
|
*/
|
|
59
56
|
export class SageMakerEndpoint extends LLM {
|
|
57
|
+
static lc_name() {
|
|
58
|
+
return "SageMakerEndpoint";
|
|
59
|
+
}
|
|
60
60
|
get lc_secrets() {
|
|
61
61
|
return {
|
|
62
62
|
"clientOptions.credentials.accessKeyId": "AWS_ACCESS_KEY_ID",
|
|
@@ -65,39 +65,44 @@ export class SageMakerEndpoint extends LLM {
|
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
constructor(fields) {
|
|
68
|
-
super(fields
|
|
68
|
+
super(fields);
|
|
69
69
|
Object.defineProperty(this, "endpointName", {
|
|
70
70
|
enumerable: true,
|
|
71
71
|
configurable: true,
|
|
72
72
|
writable: true,
|
|
73
73
|
value: void 0
|
|
74
74
|
});
|
|
75
|
-
Object.defineProperty(this, "
|
|
75
|
+
Object.defineProperty(this, "modelKwargs", {
|
|
76
76
|
enumerable: true,
|
|
77
77
|
configurable: true,
|
|
78
78
|
writable: true,
|
|
79
79
|
value: void 0
|
|
80
80
|
});
|
|
81
|
-
Object.defineProperty(this, "
|
|
81
|
+
Object.defineProperty(this, "endpointKwargs", {
|
|
82
82
|
enumerable: true,
|
|
83
83
|
configurable: true,
|
|
84
84
|
writable: true,
|
|
85
85
|
value: void 0
|
|
86
86
|
});
|
|
87
|
-
Object.defineProperty(this, "
|
|
87
|
+
Object.defineProperty(this, "client", {
|
|
88
88
|
enumerable: true,
|
|
89
89
|
configurable: true,
|
|
90
90
|
writable: true,
|
|
91
91
|
value: void 0
|
|
92
92
|
});
|
|
93
|
-
Object.defineProperty(this, "
|
|
93
|
+
Object.defineProperty(this, "contentHandler", {
|
|
94
94
|
enumerable: true,
|
|
95
95
|
configurable: true,
|
|
96
96
|
writable: true,
|
|
97
97
|
value: void 0
|
|
98
98
|
});
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
Object.defineProperty(this, "streaming", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
value: void 0
|
|
104
|
+
});
|
|
105
|
+
if (!fields.clientOptions.region) {
|
|
101
106
|
throw new Error(`Please pass a "clientOptions" object with a "region" field to the constructor`);
|
|
102
107
|
}
|
|
103
108
|
const endpointName = fields?.endpointName;
|
|
@@ -112,13 +117,33 @@ export class SageMakerEndpoint extends LLM {
|
|
|
112
117
|
this.contentHandler = fields.contentHandler;
|
|
113
118
|
this.endpointKwargs = fields.endpointKwargs;
|
|
114
119
|
this.modelKwargs = fields.modelKwargs;
|
|
120
|
+
this.streaming = fields.streaming ?? false;
|
|
115
121
|
this.client = new SageMakerRuntimeClient(fields.clientOptions);
|
|
116
122
|
}
|
|
117
123
|
_llmType() {
|
|
118
124
|
return "sagemaker_endpoint";
|
|
119
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Calls the SageMaker endpoint and retrieves the result.
|
|
128
|
+
* @param {string} prompt The input prompt.
|
|
129
|
+
* @param {this["ParsedCallOptions"]} options Parsed call options.
|
|
130
|
+
* @param {CallbackManagerForLLMRun} _runManager Optional run manager.
|
|
131
|
+
* @returns {Promise<string>} A promise that resolves to the generated string.
|
|
132
|
+
*/
|
|
120
133
|
/** @ignore */
|
|
121
|
-
async _call(prompt, options) {
|
|
134
|
+
async _call(prompt, options, _runManager) {
|
|
135
|
+
return this.streaming
|
|
136
|
+
? await this.streamingCall(prompt, options)
|
|
137
|
+
: await this.noStreamingCall(prompt, options);
|
|
138
|
+
}
|
|
139
|
+
async streamingCall(prompt, options) {
|
|
140
|
+
const chunks = [];
|
|
141
|
+
for await (const chunk of this._streamResponseChunks(prompt, options)) {
|
|
142
|
+
chunks.push(chunk.text);
|
|
143
|
+
}
|
|
144
|
+
return chunks.join("");
|
|
145
|
+
}
|
|
146
|
+
async noStreamingCall(prompt, options) {
|
|
122
147
|
const body = await this.contentHandler.transformInput(prompt, this.modelKwargs ?? {});
|
|
123
148
|
const { contentType, accepts } = this.contentHandler;
|
|
124
149
|
const response = await this.caller.call(() => this.client.send(new InvokeEndpointCommand({
|
|
@@ -133,4 +158,41 @@ export class SageMakerEndpoint extends LLM {
|
|
|
133
158
|
}
|
|
134
159
|
return this.contentHandler.transformOutput(response.Body);
|
|
135
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Streams response chunks from the SageMaker endpoint.
|
|
163
|
+
* @param {string} prompt The input prompt.
|
|
164
|
+
* @param {this["ParsedCallOptions"]} options Parsed call options.
|
|
165
|
+
* @returns {AsyncGenerator<GenerationChunk>} An asynchronous generator yielding generation chunks.
|
|
166
|
+
*/
|
|
167
|
+
async *_streamResponseChunks(prompt, options) {
|
|
168
|
+
const body = await this.contentHandler.transformInput(prompt, this.modelKwargs ?? {});
|
|
169
|
+
const { contentType, accepts } = this.contentHandler;
|
|
170
|
+
const stream = await this.caller.call(() => this.client.send(new InvokeEndpointWithResponseStreamCommand({
|
|
171
|
+
EndpointName: this.endpointName,
|
|
172
|
+
Body: body,
|
|
173
|
+
ContentType: contentType,
|
|
174
|
+
Accept: accepts,
|
|
175
|
+
...this.endpointKwargs,
|
|
176
|
+
}), { abortSignal: options.signal }));
|
|
177
|
+
if (!stream.Body) {
|
|
178
|
+
throw new Error("Inference result missing Body");
|
|
179
|
+
}
|
|
180
|
+
for await (const chunk of stream.Body) {
|
|
181
|
+
if (chunk.PayloadPart && chunk.PayloadPart.Bytes) {
|
|
182
|
+
yield new GenerationChunk({
|
|
183
|
+
text: await this.contentHandler.transformOutput(chunk.PayloadPart.Bytes),
|
|
184
|
+
generationInfo: {
|
|
185
|
+
...chunk,
|
|
186
|
+
response: undefined,
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
else if (chunk.InternalStreamFailure) {
|
|
191
|
+
throw new Error(chunk.InternalStreamFailure.message);
|
|
192
|
+
}
|
|
193
|
+
else if (chunk.ModelStreamError) {
|
|
194
|
+
throw new Error(chunk.ModelStreamError.message);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
136
198
|
}
|
|
@@ -37,8 +37,10 @@ exports.optionalImportEntrypoints = [
|
|
|
37
37
|
"langchain/llms/bedrock",
|
|
38
38
|
"langchain/llms/llama_cpp",
|
|
39
39
|
"langchain/llms/writer",
|
|
40
|
+
"langchain/llms/portkey",
|
|
40
41
|
"langchain/prompts/load",
|
|
41
42
|
"langchain/vectorstores/analyticdb",
|
|
43
|
+
"langchain/vectorstores/cassandra",
|
|
42
44
|
"langchain/vectorstores/elasticsearch",
|
|
43
45
|
"langchain/vectorstores/cloudflare_vectorize",
|
|
44
46
|
"langchain/vectorstores/chroma",
|
|
@@ -101,6 +103,7 @@ exports.optionalImportEntrypoints = [
|
|
|
101
103
|
"langchain/document_loaders/fs/openai_whisper_audio",
|
|
102
104
|
"langchain/document_transformers/html_to_text",
|
|
103
105
|
"langchain/document_transformers/mozilla_readability",
|
|
106
|
+
"langchain/chat_models/portkey",
|
|
104
107
|
"langchain/chat_models/bedrock",
|
|
105
108
|
"langchain/chat_models/googlevertexai",
|
|
106
109
|
"langchain/chat_models/googlevertexai/web",
|
|
@@ -34,8 +34,10 @@ export const optionalImportEntrypoints = [
|
|
|
34
34
|
"langchain/llms/bedrock",
|
|
35
35
|
"langchain/llms/llama_cpp",
|
|
36
36
|
"langchain/llms/writer",
|
|
37
|
+
"langchain/llms/portkey",
|
|
37
38
|
"langchain/prompts/load",
|
|
38
39
|
"langchain/vectorstores/analyticdb",
|
|
40
|
+
"langchain/vectorstores/cassandra",
|
|
39
41
|
"langchain/vectorstores/elasticsearch",
|
|
40
42
|
"langchain/vectorstores/cloudflare_vectorize",
|
|
41
43
|
"langchain/vectorstores/chroma",
|
|
@@ -98,6 +100,7 @@ export const optionalImportEntrypoints = [
|
|
|
98
100
|
"langchain/document_loaders/fs/openai_whisper_audio",
|
|
99
101
|
"langchain/document_transformers/html_to_text",
|
|
100
102
|
"langchain/document_transformers/mozilla_readability",
|
|
103
|
+
"langchain/chat_models/portkey",
|
|
101
104
|
"langchain/chat_models/bedrock",
|
|
102
105
|
"langchain/chat_models/googlevertexai",
|
|
103
106
|
"langchain/chat_models/googlevertexai/web",
|
|
@@ -119,7 +119,7 @@ class CustomListOutputParser extends ListOutputParser {
|
|
|
119
119
|
* @returns A string containing instructions on the expected format of the response.
|
|
120
120
|
*/
|
|
121
121
|
getFormatInstructions() {
|
|
122
|
-
return `Your response should be a list of ${this.length} items separated by "${this.separator}" (eg: \`foo${this.separator} bar${this.separator} baz\`)`;
|
|
122
|
+
return `Your response should be a list of ${this.length === undefined ? "" : `${this.length} `}items separated by "${this.separator}" (eg: \`foo${this.separator} bar${this.separator} baz\`)`;
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
exports.CustomListOutputParser = CustomListOutputParser;
|
|
@@ -114,6 +114,6 @@ export class CustomListOutputParser extends ListOutputParser {
|
|
|
114
114
|
* @returns A string containing instructions on the expected format of the response.
|
|
115
115
|
*/
|
|
116
116
|
getFormatInstructions() {
|
|
117
|
-
return `Your response should be a list of ${this.length} items separated by "${this.separator}" (eg: \`foo${this.separator} bar${this.separator} baz\`)`;
|
|
117
|
+
return `Your response should be a list of ${this.length === undefined ? "" : `${this.length} `}items separated by "${this.separator}" (eg: \`foo${this.separator} bar${this.separator} baz\`)`;
|
|
118
118
|
}
|
|
119
119
|
}
|