langchain 0.0.163 → 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/bedrock.cjs +3 -0
- package/dist/chat_models/bedrock.js +3 -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/document_loaders/web/notionapi.cjs +28 -5
- package/dist/document_loaders/web/notionapi.d.ts +2 -0
- package/dist/document_loaders/web/notionapi.js +25 -5
- package/dist/embeddings/minimax.cjs +1 -1
- package/dist/embeddings/minimax.js +1 -1
- package/dist/graphs/neo4j_graph.cjs +86 -10
- package/dist/graphs/neo4j_graph.d.ts +2 -1
- package/dist/graphs/neo4j_graph.js +86 -10
- package/dist/llms/bedrock.cjs +3 -0
- package/dist/llms/bedrock.js +3 -0
- 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/util/stream.cjs +4 -4
- package/dist/util/stream.js +4 -4
- 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/dist/vectorstores/prisma.d.ts +1 -1
- 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
|
@@ -53,18 +53,19 @@ export class Neo4jGraph {
|
|
|
53
53
|
}
|
|
54
54
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
55
|
async query(query, params = {}) {
|
|
56
|
-
const session = this.driver.session({ database: this.database });
|
|
57
56
|
try {
|
|
58
|
-
const result = await
|
|
59
|
-
|
|
57
|
+
const result = await this.driver.executeQuery(query, params, {
|
|
58
|
+
database: this.database,
|
|
59
|
+
});
|
|
60
|
+
return toObjects(result.records);
|
|
60
61
|
}
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
catch (error) {
|
|
63
|
+
// ignore errors
|
|
63
64
|
}
|
|
65
|
+
return undefined;
|
|
64
66
|
}
|
|
65
67
|
async verifyConnectivity() {
|
|
66
|
-
|
|
67
|
-
await session.close();
|
|
68
|
+
await this.driver.verifyAuthentication();
|
|
68
69
|
}
|
|
69
70
|
async refreshSchema() {
|
|
70
71
|
const nodePropertiesQuery = `
|
|
@@ -93,13 +94,88 @@ export class Neo4jGraph {
|
|
|
93
94
|
const relationships = await this.query(relQuery);
|
|
94
95
|
this.schema = `
|
|
95
96
|
Node properties are the following:
|
|
96
|
-
${nodeProperties
|
|
97
|
+
${JSON.stringify(nodeProperties?.map((el) => el.output))}
|
|
97
98
|
|
|
98
99
|
Relationship properties are the following:
|
|
99
|
-
${relationshipsProperties
|
|
100
|
+
${JSON.stringify(relationshipsProperties?.map((el) => el.output))}
|
|
100
101
|
|
|
101
102
|
The relationships are the following:
|
|
102
|
-
${relationships
|
|
103
|
+
${JSON.stringify(relationships?.map((el) => el.output))}
|
|
103
104
|
`;
|
|
104
105
|
}
|
|
106
|
+
async close() {
|
|
107
|
+
await this.driver.close();
|
|
108
|
+
}
|
|
105
109
|
}
|
|
110
|
+
function toObjects(records) {
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
+
const recordValues = records.map((record) => {
|
|
113
|
+
const rObj = record.toObject();
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
115
|
+
const out = {};
|
|
116
|
+
Object.keys(rObj).forEach((key) => {
|
|
117
|
+
out[key] = itemIntToString(rObj[key]);
|
|
118
|
+
});
|
|
119
|
+
return out;
|
|
120
|
+
});
|
|
121
|
+
return recordValues;
|
|
122
|
+
}
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
124
|
+
function itemIntToString(item) {
|
|
125
|
+
if (neo4j.isInt(item))
|
|
126
|
+
return item.toString();
|
|
127
|
+
if (Array.isArray(item))
|
|
128
|
+
return item.map((ii) => itemIntToString(ii));
|
|
129
|
+
if (["number", "string", "boolean"].indexOf(typeof item) !== -1)
|
|
130
|
+
return item;
|
|
131
|
+
if (item === null)
|
|
132
|
+
return item;
|
|
133
|
+
if (typeof item === "object")
|
|
134
|
+
return objIntToString(item);
|
|
135
|
+
}
|
|
136
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
137
|
+
function objIntToString(obj) {
|
|
138
|
+
const entry = extractFromNeoObjects(obj);
|
|
139
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
140
|
+
let newObj = null;
|
|
141
|
+
if (Array.isArray(entry)) {
|
|
142
|
+
newObj = entry.map((item) => itemIntToString(item));
|
|
143
|
+
}
|
|
144
|
+
else if (entry !== null && typeof entry === "object") {
|
|
145
|
+
newObj = {};
|
|
146
|
+
Object.keys(entry).forEach((key) => {
|
|
147
|
+
newObj[key] = itemIntToString(entry[key]);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return newObj;
|
|
151
|
+
}
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
function extractFromNeoObjects(obj) {
|
|
154
|
+
if (
|
|
155
|
+
// eslint-disable-next-line
|
|
156
|
+
obj instanceof neo4j.types.Node ||
|
|
157
|
+
// eslint-disable-next-line
|
|
158
|
+
obj instanceof neo4j.types.Relationship) {
|
|
159
|
+
return obj.properties;
|
|
160
|
+
// eslint-disable-next-line
|
|
161
|
+
}
|
|
162
|
+
else if (obj instanceof neo4j.types.Path) {
|
|
163
|
+
// eslint-disable-next-line
|
|
164
|
+
return [].concat.apply([], extractPathForRows(obj));
|
|
165
|
+
}
|
|
166
|
+
return obj;
|
|
167
|
+
}
|
|
168
|
+
const extractPathForRows = (path) => {
|
|
169
|
+
let { segments } = path;
|
|
170
|
+
// Zero length path. No relationship, end === start
|
|
171
|
+
if (!Array.isArray(path.segments) || path.segments.length < 1) {
|
|
172
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
173
|
+
segments = [{ ...path, end: null }];
|
|
174
|
+
}
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
176
|
+
return segments.map((segment) => [
|
|
177
|
+
objIntToString(segment.start),
|
|
178
|
+
objIntToString(segment.relationship),
|
|
179
|
+
objIntToString(segment.end),
|
|
180
|
+
].filter((part) => part !== null));
|
|
181
|
+
};
|
package/dist/llms/bedrock.cjs
CHANGED
|
@@ -130,6 +130,9 @@ class Bedrock extends base_js_1.LLM {
|
|
|
130
130
|
provider,
|
|
131
131
|
});
|
|
132
132
|
const json = await response.json();
|
|
133
|
+
if (!response.ok) {
|
|
134
|
+
throw new Error(`Error ${response.status}: ${json.message ?? JSON.stringify(json)}`);
|
|
135
|
+
}
|
|
133
136
|
const text = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
|
|
134
137
|
return text;
|
|
135
138
|
}
|
package/dist/llms/bedrock.js
CHANGED
|
@@ -127,6 +127,9 @@ export class Bedrock extends LLM {
|
|
|
127
127
|
provider,
|
|
128
128
|
});
|
|
129
129
|
const json = await response.json();
|
|
130
|
+
if (!response.ok) {
|
|
131
|
+
throw new Error(`Error ${response.status}: ${json.message ?? JSON.stringify(json)}`);
|
|
132
|
+
}
|
|
130
133
|
const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
|
|
131
134
|
return text;
|
|
132
135
|
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Portkey = exports.getPortkeySession = exports.PortkeySession = void 0;
|
|
7
|
+
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
+
const portkey_ai_1 = require("portkey-ai");
|
|
9
|
+
const index_js_1 = require("../schema/index.cjs");
|
|
10
|
+
const env_js_1 = require("../util/env.cjs");
|
|
11
|
+
const base_js_1 = require("./base.cjs");
|
|
12
|
+
const readEnv = (env, default_val) => (0, env_js_1.getEnvironmentVariable)(env) ?? default_val;
|
|
13
|
+
class PortkeySession {
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
Object.defineProperty(this, "portkey", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: void 0
|
|
20
|
+
});
|
|
21
|
+
if (!options.apiKey) {
|
|
22
|
+
/* eslint-disable no-param-reassign */
|
|
23
|
+
options.apiKey = readEnv("PORTKEY_API_KEY");
|
|
24
|
+
}
|
|
25
|
+
if (!options.baseURL) {
|
|
26
|
+
/* eslint-disable no-param-reassign */
|
|
27
|
+
options.baseURL = readEnv("PORTKEY_BASE_URL", "https://api.portkey.ai");
|
|
28
|
+
}
|
|
29
|
+
this.portkey = new portkey_ai_1.Portkey({});
|
|
30
|
+
this.portkey.llms = [{}];
|
|
31
|
+
if (!options.apiKey) {
|
|
32
|
+
throw new Error("Set Portkey ApiKey in PORTKEY_API_KEY env variable");
|
|
33
|
+
}
|
|
34
|
+
this.portkey = new portkey_ai_1.Portkey(options);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.PortkeySession = PortkeySession;
|
|
38
|
+
const defaultPortkeySession = [];
|
|
39
|
+
/**
|
|
40
|
+
* Get a session for the Portkey API. If one already exists with the same options,
|
|
41
|
+
* it will be returned. Otherwise, a new session will be created.
|
|
42
|
+
* @param options
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
function getPortkeySession(options = {}) {
|
|
46
|
+
let session = defaultPortkeySession.find((session) => lodash_1.default.isEqual(session.options, options))?.session;
|
|
47
|
+
if (!session) {
|
|
48
|
+
session = new PortkeySession(options);
|
|
49
|
+
defaultPortkeySession.push({ session, options });
|
|
50
|
+
}
|
|
51
|
+
return session;
|
|
52
|
+
}
|
|
53
|
+
exports.getPortkeySession = getPortkeySession;
|
|
54
|
+
class Portkey extends base_js_1.BaseLLM {
|
|
55
|
+
constructor(init) {
|
|
56
|
+
super(init ?? {});
|
|
57
|
+
Object.defineProperty(this, "apiKey", {
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true,
|
|
60
|
+
writable: true,
|
|
61
|
+
value: undefined
|
|
62
|
+
});
|
|
63
|
+
Object.defineProperty(this, "baseURL", {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
configurable: true,
|
|
66
|
+
writable: true,
|
|
67
|
+
value: undefined
|
|
68
|
+
});
|
|
69
|
+
Object.defineProperty(this, "mode", {
|
|
70
|
+
enumerable: true,
|
|
71
|
+
configurable: true,
|
|
72
|
+
writable: true,
|
|
73
|
+
value: undefined
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(this, "llms", {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
configurable: true,
|
|
78
|
+
writable: true,
|
|
79
|
+
value: undefined
|
|
80
|
+
});
|
|
81
|
+
Object.defineProperty(this, "session", {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
configurable: true,
|
|
84
|
+
writable: true,
|
|
85
|
+
value: void 0
|
|
86
|
+
});
|
|
87
|
+
this.apiKey = init?.apiKey;
|
|
88
|
+
this.baseURL = init?.baseURL;
|
|
89
|
+
this.mode = init?.mode;
|
|
90
|
+
this.llms = init?.llms;
|
|
91
|
+
this.session = getPortkeySession({
|
|
92
|
+
apiKey: this.apiKey,
|
|
93
|
+
baseURL: this.baseURL,
|
|
94
|
+
llms: this.llms,
|
|
95
|
+
mode: this.mode,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
_llmType() {
|
|
99
|
+
return "portkey";
|
|
100
|
+
}
|
|
101
|
+
async _generate(prompts, options, _) {
|
|
102
|
+
const choices = [];
|
|
103
|
+
for (let i = 0; i < prompts.length; i += 1) {
|
|
104
|
+
const response = await this.session.portkey.completions.create({
|
|
105
|
+
prompt: prompts[i],
|
|
106
|
+
...options,
|
|
107
|
+
stream: false,
|
|
108
|
+
});
|
|
109
|
+
choices.push(response.choices);
|
|
110
|
+
}
|
|
111
|
+
const generations = choices.map((promptChoices) => promptChoices.map((choice) => ({
|
|
112
|
+
text: choice.text ?? "",
|
|
113
|
+
generationInfo: {
|
|
114
|
+
finishReason: choice.finish_reason,
|
|
115
|
+
logprobs: choice.logprobs,
|
|
116
|
+
},
|
|
117
|
+
})));
|
|
118
|
+
return {
|
|
119
|
+
generations,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
async *_streamResponseChunks(input, options, runManager) {
|
|
123
|
+
const response = await this.session.portkey.completions.create({
|
|
124
|
+
prompt: input,
|
|
125
|
+
...options,
|
|
126
|
+
stream: true,
|
|
127
|
+
});
|
|
128
|
+
for await (const data of response) {
|
|
129
|
+
const choice = data?.choices[0];
|
|
130
|
+
if (!choice) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const chunk = new index_js_1.GenerationChunk({
|
|
134
|
+
text: choice.text ?? "",
|
|
135
|
+
generationInfo: {
|
|
136
|
+
finishReason: choice.finish_reason,
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
yield chunk;
|
|
140
|
+
void runManager?.handleLLMNewToken(chunk.text ?? "");
|
|
141
|
+
}
|
|
142
|
+
if (options.signal?.aborted) {
|
|
143
|
+
throw new Error("AbortError");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.Portkey = Portkey;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LLMOptions, Portkey as _Portkey } from "portkey-ai";
|
|
2
|
+
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";
|
|
3
|
+
import { GenerationChunk, LLMResult } from "../schema/index.js";
|
|
4
|
+
import { BaseLLM } from "./base.js";
|
|
5
|
+
interface PortkeyOptions {
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
baseURL?: string;
|
|
8
|
+
mode?: string;
|
|
9
|
+
llms?: [LLMOptions] | null;
|
|
10
|
+
}
|
|
11
|
+
export declare class PortkeySession {
|
|
12
|
+
portkey: _Portkey;
|
|
13
|
+
constructor(options?: PortkeyOptions);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Get a session for the Portkey API. If one already exists with the same options,
|
|
17
|
+
* it will be returned. Otherwise, a new session will be created.
|
|
18
|
+
* @param options
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
export declare function getPortkeySession(options?: PortkeyOptions): PortkeySession;
|
|
22
|
+
export declare class Portkey extends BaseLLM {
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
baseURL?: string;
|
|
25
|
+
mode?: string;
|
|
26
|
+
llms?: [LLMOptions] | null;
|
|
27
|
+
session: PortkeySession;
|
|
28
|
+
constructor(init?: Partial<Portkey>);
|
|
29
|
+
_llmType(): string;
|
|
30
|
+
_generate(prompts: string[], options: this["ParsedCallOptions"], _?: CallbackManagerForLLMRun): Promise<LLMResult>;
|
|
31
|
+
_streamResponseChunks(input: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import _ from "lodash";
|
|
2
|
+
import { Portkey as _Portkey } from "portkey-ai";
|
|
3
|
+
import { GenerationChunk } from "../schema/index.js";
|
|
4
|
+
import { getEnvironmentVariable } from "../util/env.js";
|
|
5
|
+
import { BaseLLM } from "./base.js";
|
|
6
|
+
const readEnv = (env, default_val) => getEnvironmentVariable(env) ?? default_val;
|
|
7
|
+
export class PortkeySession {
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
Object.defineProperty(this, "portkey", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: void 0
|
|
14
|
+
});
|
|
15
|
+
if (!options.apiKey) {
|
|
16
|
+
/* eslint-disable no-param-reassign */
|
|
17
|
+
options.apiKey = readEnv("PORTKEY_API_KEY");
|
|
18
|
+
}
|
|
19
|
+
if (!options.baseURL) {
|
|
20
|
+
/* eslint-disable no-param-reassign */
|
|
21
|
+
options.baseURL = readEnv("PORTKEY_BASE_URL", "https://api.portkey.ai");
|
|
22
|
+
}
|
|
23
|
+
this.portkey = new _Portkey({});
|
|
24
|
+
this.portkey.llms = [{}];
|
|
25
|
+
if (!options.apiKey) {
|
|
26
|
+
throw new Error("Set Portkey ApiKey in PORTKEY_API_KEY env variable");
|
|
27
|
+
}
|
|
28
|
+
this.portkey = new _Portkey(options);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const defaultPortkeySession = [];
|
|
32
|
+
/**
|
|
33
|
+
* Get a session for the Portkey API. If one already exists with the same options,
|
|
34
|
+
* it will be returned. Otherwise, a new session will be created.
|
|
35
|
+
* @param options
|
|
36
|
+
* @returns
|
|
37
|
+
*/
|
|
38
|
+
export function getPortkeySession(options = {}) {
|
|
39
|
+
let session = defaultPortkeySession.find((session) => _.isEqual(session.options, options))?.session;
|
|
40
|
+
if (!session) {
|
|
41
|
+
session = new PortkeySession(options);
|
|
42
|
+
defaultPortkeySession.push({ session, options });
|
|
43
|
+
}
|
|
44
|
+
return session;
|
|
45
|
+
}
|
|
46
|
+
export class Portkey extends BaseLLM {
|
|
47
|
+
constructor(init) {
|
|
48
|
+
super(init ?? {});
|
|
49
|
+
Object.defineProperty(this, "apiKey", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
configurable: true,
|
|
52
|
+
writable: true,
|
|
53
|
+
value: undefined
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(this, "baseURL", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
configurable: true,
|
|
58
|
+
writable: true,
|
|
59
|
+
value: undefined
|
|
60
|
+
});
|
|
61
|
+
Object.defineProperty(this, "mode", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: undefined
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(this, "llms", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: undefined
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "session", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: void 0
|
|
78
|
+
});
|
|
79
|
+
this.apiKey = init?.apiKey;
|
|
80
|
+
this.baseURL = init?.baseURL;
|
|
81
|
+
this.mode = init?.mode;
|
|
82
|
+
this.llms = init?.llms;
|
|
83
|
+
this.session = getPortkeySession({
|
|
84
|
+
apiKey: this.apiKey,
|
|
85
|
+
baseURL: this.baseURL,
|
|
86
|
+
llms: this.llms,
|
|
87
|
+
mode: this.mode,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
_llmType() {
|
|
91
|
+
return "portkey";
|
|
92
|
+
}
|
|
93
|
+
async _generate(prompts, options, _) {
|
|
94
|
+
const choices = [];
|
|
95
|
+
for (let i = 0; i < prompts.length; i += 1) {
|
|
96
|
+
const response = await this.session.portkey.completions.create({
|
|
97
|
+
prompt: prompts[i],
|
|
98
|
+
...options,
|
|
99
|
+
stream: false,
|
|
100
|
+
});
|
|
101
|
+
choices.push(response.choices);
|
|
102
|
+
}
|
|
103
|
+
const generations = choices.map((promptChoices) => promptChoices.map((choice) => ({
|
|
104
|
+
text: choice.text ?? "",
|
|
105
|
+
generationInfo: {
|
|
106
|
+
finishReason: choice.finish_reason,
|
|
107
|
+
logprobs: choice.logprobs,
|
|
108
|
+
},
|
|
109
|
+
})));
|
|
110
|
+
return {
|
|
111
|
+
generations,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
async *_streamResponseChunks(input, options, runManager) {
|
|
115
|
+
const response = await this.session.portkey.completions.create({
|
|
116
|
+
prompt: input,
|
|
117
|
+
...options,
|
|
118
|
+
stream: true,
|
|
119
|
+
});
|
|
120
|
+
for await (const data of response) {
|
|
121
|
+
const choice = data?.choices[0];
|
|
122
|
+
if (!choice) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const chunk = new GenerationChunk({
|
|
126
|
+
text: choice.text ?? "",
|
|
127
|
+
generationInfo: {
|
|
128
|
+
finishReason: choice.finish_reason,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
yield chunk;
|
|
132
|
+
void runManager?.handleLLMNewToken(chunk.text ?? "");
|
|
133
|
+
}
|
|
134
|
+
if (options.signal?.aborted) {
|
|
135
|
+
throw new Error("AbortError");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -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;
|