langchain 0.0.162 → 0.0.164
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 +3 -0
- package/dist/chat_models/bedrock.js +3 -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/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/prompts/selectors/SemanticSimilarityExampleSelector.cjs +9 -2
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +7 -5
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +9 -2
- package/dist/util/bedrock.cjs +2 -1
- package/dist/util/bedrock.js +2 -1
- package/dist/util/stream.cjs +4 -4
- package/dist/util/stream.js +4 -4
- package/dist/vectorstores/prisma.d.ts +1 -1
- package/package.json +1 -1
|
@@ -171,6 +171,9 @@ class ChatBedrock extends base_js_1.SimpleChatModel {
|
|
|
171
171
|
provider,
|
|
172
172
|
});
|
|
173
173
|
const json = await response.json();
|
|
174
|
+
if (!response.ok) {
|
|
175
|
+
throw new Error(`Error ${response.status}: ${json.message ?? JSON.stringify(json)}`);
|
|
176
|
+
}
|
|
174
177
|
const text = bedrock_js_1.BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
|
|
175
178
|
return text;
|
|
176
179
|
}
|
|
@@ -166,6 +166,9 @@ export class ChatBedrock extends SimpleChatModel {
|
|
|
166
166
|
provider,
|
|
167
167
|
});
|
|
168
168
|
const json = await response.json();
|
|
169
|
+
if (!response.ok) {
|
|
170
|
+
throw new Error(`Error ${response.status}: ${json.message ?? JSON.stringify(json)}`);
|
|
171
|
+
}
|
|
169
172
|
const text = BedrockLLMInputOutputAdapter.prepareOutput(provider, json);
|
|
170
173
|
return text;
|
|
171
174
|
}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.NotionAPILoader = exports.isDatabase = exports.isPage = exports.isErrorResponse = exports.isDatabaseResponse = exports.isPageResponse = void 0;
|
|
4
7
|
const client_1 = require("@notionhq/client");
|
|
5
8
|
const notion_to_md_1 = require("notion-to-md");
|
|
6
9
|
const notion_js_1 = require("notion-to-md/build/utils/notion.js");
|
|
10
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
7
11
|
const document_js_1 = require("../../document.cjs");
|
|
8
12
|
const base_js_1 = require("../base.cjs");
|
|
9
13
|
const async_caller_js_1 = require("../../util/async_caller.cjs");
|
|
@@ -84,6 +88,12 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
|
|
|
84
88
|
writable: true,
|
|
85
89
|
value: void 0
|
|
86
90
|
});
|
|
91
|
+
Object.defineProperty(this, "propertiesAsHeader", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: void 0
|
|
96
|
+
});
|
|
87
97
|
this.caller = new async_caller_js_1.AsyncCaller({
|
|
88
98
|
maxConcurrency: 64,
|
|
89
99
|
...options.callerOptions,
|
|
@@ -103,6 +113,7 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
|
|
|
103
113
|
this.documents = [];
|
|
104
114
|
this.rootTitle = "";
|
|
105
115
|
this.onDocumentLoaded = options.onDocumentLoaded ?? ((_ti, _cu) => { });
|
|
116
|
+
this.propertiesAsHeader = options.propertiesAsHeader || false;
|
|
106
117
|
}
|
|
107
118
|
/**
|
|
108
119
|
* Adds a selection of page ids to the pageQueue and removes duplicates.
|
|
@@ -278,14 +289,26 @@ class NotionAPILoader extends base_js_1.BaseDocumentLoader {
|
|
|
278
289
|
pageData,
|
|
279
290
|
this.caller.call(() => (0, notion_js_1.getBlockChildren)(this.notionClient, pageId, null)),
|
|
280
291
|
]);
|
|
281
|
-
if (!(0, client_1.isFullPage)(pageDetails))
|
|
292
|
+
if (!(0, client_1.isFullPage)(pageDetails)) {
|
|
293
|
+
this.pageCompleted.push(pageId);
|
|
282
294
|
return;
|
|
295
|
+
}
|
|
283
296
|
const mdBlocks = await this.loadBlocks(pageBlocks);
|
|
284
297
|
const mdStringObject = this.n2mClient.toMarkdownString(mdBlocks);
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
298
|
+
let pageContent = mdStringObject.parent;
|
|
299
|
+
const metadata = this.parsePageDetails(pageDetails);
|
|
300
|
+
if (this.propertiesAsHeader) {
|
|
301
|
+
pageContent =
|
|
302
|
+
`---\n` +
|
|
303
|
+
`${js_yaml_1.default.dump(metadata.properties)}` +
|
|
304
|
+
`---\n\n` +
|
|
305
|
+
`${pageContent ?? ""}`;
|
|
306
|
+
}
|
|
307
|
+
if (!pageContent) {
|
|
308
|
+
this.pageCompleted.push(pageId);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const pageDocument = new document_js_1.Document({ pageContent, metadata });
|
|
289
312
|
this.documents.push(pageDocument);
|
|
290
313
|
this.pageCompleted.push(pageId);
|
|
291
314
|
this.onDocumentLoaded(this.documents.length, this.pageQueueTotal, this.getTitle(pageDetails) || undefined, this.rootTitle);
|
|
@@ -29,6 +29,7 @@ export type NotionAPILoaderOptions = {
|
|
|
29
29
|
type?: NotionAPIType;
|
|
30
30
|
callerOptions?: ConstructorParameters<typeof AsyncCaller>[0];
|
|
31
31
|
onDocumentLoaded?: OnDocumentLoadedCallback;
|
|
32
|
+
propertiesAsHeader?: boolean;
|
|
32
33
|
};
|
|
33
34
|
/**
|
|
34
35
|
* A class that extends the BaseDocumentLoader class. It represents a
|
|
@@ -45,6 +46,7 @@ export declare class NotionAPILoader extends BaseDocumentLoader {
|
|
|
45
46
|
private documents;
|
|
46
47
|
private rootTitle;
|
|
47
48
|
private onDocumentLoaded;
|
|
49
|
+
private propertiesAsHeader;
|
|
48
50
|
constructor(options: NotionAPILoaderOptions);
|
|
49
51
|
/**
|
|
50
52
|
* Adds a selection of page ids to the pageQueue and removes duplicates.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Client, isFullBlock, isFullPage, iteratePaginatedAPI, APIErrorCode, isNotionClientError, isFullDatabase, } from "@notionhq/client";
|
|
2
2
|
import { NotionToMarkdown } from "notion-to-md";
|
|
3
3
|
import { getBlockChildren } from "notion-to-md/build/utils/notion.js";
|
|
4
|
+
import yaml from "js-yaml";
|
|
4
5
|
import { Document } from "../../document.js";
|
|
5
6
|
import { BaseDocumentLoader } from "../base.js";
|
|
6
7
|
import { AsyncCaller } from "../../util/async_caller.js";
|
|
@@ -76,6 +77,12 @@ export class NotionAPILoader extends BaseDocumentLoader {
|
|
|
76
77
|
writable: true,
|
|
77
78
|
value: void 0
|
|
78
79
|
});
|
|
80
|
+
Object.defineProperty(this, "propertiesAsHeader", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: void 0
|
|
85
|
+
});
|
|
79
86
|
this.caller = new AsyncCaller({
|
|
80
87
|
maxConcurrency: 64,
|
|
81
88
|
...options.callerOptions,
|
|
@@ -95,6 +102,7 @@ export class NotionAPILoader extends BaseDocumentLoader {
|
|
|
95
102
|
this.documents = [];
|
|
96
103
|
this.rootTitle = "";
|
|
97
104
|
this.onDocumentLoaded = options.onDocumentLoaded ?? ((_ti, _cu) => { });
|
|
105
|
+
this.propertiesAsHeader = options.propertiesAsHeader || false;
|
|
98
106
|
}
|
|
99
107
|
/**
|
|
100
108
|
* Adds a selection of page ids to the pageQueue and removes duplicates.
|
|
@@ -270,14 +278,26 @@ export class NotionAPILoader extends BaseDocumentLoader {
|
|
|
270
278
|
pageData,
|
|
271
279
|
this.caller.call(() => getBlockChildren(this.notionClient, pageId, null)),
|
|
272
280
|
]);
|
|
273
|
-
if (!isFullPage(pageDetails))
|
|
281
|
+
if (!isFullPage(pageDetails)) {
|
|
282
|
+
this.pageCompleted.push(pageId);
|
|
274
283
|
return;
|
|
284
|
+
}
|
|
275
285
|
const mdBlocks = await this.loadBlocks(pageBlocks);
|
|
276
286
|
const mdStringObject = this.n2mClient.toMarkdownString(mdBlocks);
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
287
|
+
let pageContent = mdStringObject.parent;
|
|
288
|
+
const metadata = this.parsePageDetails(pageDetails);
|
|
289
|
+
if (this.propertiesAsHeader) {
|
|
290
|
+
pageContent =
|
|
291
|
+
`---\n` +
|
|
292
|
+
`${yaml.dump(metadata.properties)}` +
|
|
293
|
+
`---\n\n` +
|
|
294
|
+
`${pageContent ?? ""}`;
|
|
295
|
+
}
|
|
296
|
+
if (!pageContent) {
|
|
297
|
+
this.pageCompleted.push(pageId);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const pageDocument = new Document({ pageContent, metadata });
|
|
281
301
|
this.documents.push(pageDocument);
|
|
282
302
|
this.pageCompleted.push(pageId);
|
|
283
303
|
this.onDocumentLoaded(this.documents.length, this.pageQueueTotal, this.getTitle(pageDetails) || undefined, this.rootTitle);
|
|
@@ -59,18 +59,19 @@ class Neo4jGraph {
|
|
|
59
59
|
}
|
|
60
60
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
61
|
async query(query, params = {}) {
|
|
62
|
-
const session = this.driver.session({ database: this.database });
|
|
63
62
|
try {
|
|
64
|
-
const result = await
|
|
65
|
-
|
|
63
|
+
const result = await this.driver.executeQuery(query, params, {
|
|
64
|
+
database: this.database,
|
|
65
|
+
});
|
|
66
|
+
return toObjects(result.records);
|
|
66
67
|
}
|
|
67
|
-
|
|
68
|
-
|
|
68
|
+
catch (error) {
|
|
69
|
+
// ignore errors
|
|
69
70
|
}
|
|
71
|
+
return undefined;
|
|
70
72
|
}
|
|
71
73
|
async verifyConnectivity() {
|
|
72
|
-
|
|
73
|
-
await session.close();
|
|
74
|
+
await this.driver.verifyAuthentication();
|
|
74
75
|
}
|
|
75
76
|
async refreshSchema() {
|
|
76
77
|
const nodePropertiesQuery = `
|
|
@@ -99,14 +100,89 @@ class Neo4jGraph {
|
|
|
99
100
|
const relationships = await this.query(relQuery);
|
|
100
101
|
this.schema = `
|
|
101
102
|
Node properties are the following:
|
|
102
|
-
${nodeProperties
|
|
103
|
+
${JSON.stringify(nodeProperties?.map((el) => el.output))}
|
|
103
104
|
|
|
104
105
|
Relationship properties are the following:
|
|
105
|
-
${relationshipsProperties
|
|
106
|
+
${JSON.stringify(relationshipsProperties?.map((el) => el.output))}
|
|
106
107
|
|
|
107
108
|
The relationships are the following:
|
|
108
|
-
${relationships
|
|
109
|
+
${JSON.stringify(relationships?.map((el) => el.output))}
|
|
109
110
|
`;
|
|
110
111
|
}
|
|
112
|
+
async close() {
|
|
113
|
+
await this.driver.close();
|
|
114
|
+
}
|
|
111
115
|
}
|
|
112
116
|
exports.Neo4jGraph = Neo4jGraph;
|
|
117
|
+
function toObjects(records) {
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
119
|
+
const recordValues = records.map((record) => {
|
|
120
|
+
const rObj = record.toObject();
|
|
121
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
122
|
+
const out = {};
|
|
123
|
+
Object.keys(rObj).forEach((key) => {
|
|
124
|
+
out[key] = itemIntToString(rObj[key]);
|
|
125
|
+
});
|
|
126
|
+
return out;
|
|
127
|
+
});
|
|
128
|
+
return recordValues;
|
|
129
|
+
}
|
|
130
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
131
|
+
function itemIntToString(item) {
|
|
132
|
+
if (neo4j_driver_1.default.isInt(item))
|
|
133
|
+
return item.toString();
|
|
134
|
+
if (Array.isArray(item))
|
|
135
|
+
return item.map((ii) => itemIntToString(ii));
|
|
136
|
+
if (["number", "string", "boolean"].indexOf(typeof item) !== -1)
|
|
137
|
+
return item;
|
|
138
|
+
if (item === null)
|
|
139
|
+
return item;
|
|
140
|
+
if (typeof item === "object")
|
|
141
|
+
return objIntToString(item);
|
|
142
|
+
}
|
|
143
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
144
|
+
function objIntToString(obj) {
|
|
145
|
+
const entry = extractFromNeoObjects(obj);
|
|
146
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
147
|
+
let newObj = null;
|
|
148
|
+
if (Array.isArray(entry)) {
|
|
149
|
+
newObj = entry.map((item) => itemIntToString(item));
|
|
150
|
+
}
|
|
151
|
+
else if (entry !== null && typeof entry === "object") {
|
|
152
|
+
newObj = {};
|
|
153
|
+
Object.keys(entry).forEach((key) => {
|
|
154
|
+
newObj[key] = itemIntToString(entry[key]);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return newObj;
|
|
158
|
+
}
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
160
|
+
function extractFromNeoObjects(obj) {
|
|
161
|
+
if (
|
|
162
|
+
// eslint-disable-next-line
|
|
163
|
+
obj instanceof neo4j_driver_1.default.types.Node ||
|
|
164
|
+
// eslint-disable-next-line
|
|
165
|
+
obj instanceof neo4j_driver_1.default.types.Relationship) {
|
|
166
|
+
return obj.properties;
|
|
167
|
+
// eslint-disable-next-line
|
|
168
|
+
}
|
|
169
|
+
else if (obj instanceof neo4j_driver_1.default.types.Path) {
|
|
170
|
+
// eslint-disable-next-line
|
|
171
|
+
return [].concat.apply([], extractPathForRows(obj));
|
|
172
|
+
}
|
|
173
|
+
return obj;
|
|
174
|
+
}
|
|
175
|
+
const extractPathForRows = (path) => {
|
|
176
|
+
let { segments } = path;
|
|
177
|
+
// Zero length path. No relationship, end === start
|
|
178
|
+
if (!Array.isArray(path.segments) || path.segments.length < 1) {
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
180
|
+
segments = [{ ...path, end: null }];
|
|
181
|
+
}
|
|
182
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
183
|
+
return segments.map((segment) => [
|
|
184
|
+
objIntToString(segment.start),
|
|
185
|
+
objIntToString(segment.relationship),
|
|
186
|
+
objIntToString(segment.end),
|
|
187
|
+
].filter((part) => part !== null));
|
|
188
|
+
};
|
|
@@ -11,8 +11,9 @@ export declare class Neo4jGraph {
|
|
|
11
11
|
constructor({ url, username, password, database, }: Neo4jGraphConfig);
|
|
12
12
|
static initialize(config: Neo4jGraphConfig): Promise<Neo4jGraph>;
|
|
13
13
|
getSchema(): string;
|
|
14
|
-
query(query: string, params?: any): Promise<any[]>;
|
|
14
|
+
query(query: string, params?: any): Promise<any[] | undefined>;
|
|
15
15
|
verifyConnectivity(): Promise<void>;
|
|
16
16
|
refreshSchema(): Promise<void>;
|
|
17
|
+
close(): Promise<void>;
|
|
17
18
|
}
|
|
18
19
|
export {};
|
|
@@ -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
|
}
|
|
@@ -39,10 +39,17 @@ class SemanticSimilarityExampleSelector extends base_js_1.BaseExampleSelector {
|
|
|
39
39
|
writable: true,
|
|
40
40
|
value: void 0
|
|
41
41
|
});
|
|
42
|
+
Object.defineProperty(this, "filter", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
42
48
|
this.vectorStore = data.vectorStore;
|
|
43
49
|
this.k = data.k ?? 4;
|
|
44
50
|
this.exampleKeys = data.exampleKeys;
|
|
45
51
|
this.inputKeys = data.inputKeys;
|
|
52
|
+
this.filter = data.filter;
|
|
46
53
|
}
|
|
47
54
|
/**
|
|
48
55
|
* Method that adds a new example to the vectorStore. The example is
|
|
@@ -56,7 +63,7 @@ class SemanticSimilarityExampleSelector extends base_js_1.BaseExampleSelector {
|
|
|
56
63
|
await this.vectorStore.addDocuments([
|
|
57
64
|
new document_js_1.Document({
|
|
58
65
|
pageContent: stringExample,
|
|
59
|
-
metadata:
|
|
66
|
+
metadata: example,
|
|
60
67
|
}),
|
|
61
68
|
]);
|
|
62
69
|
}
|
|
@@ -70,7 +77,7 @@ class SemanticSimilarityExampleSelector extends base_js_1.BaseExampleSelector {
|
|
|
70
77
|
async selectExamples(inputVariables) {
|
|
71
78
|
const inputKeys = this.inputKeys ?? Object.keys(inputVariables);
|
|
72
79
|
const query = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: inputVariables[key] }), {})).join(" ");
|
|
73
|
-
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k);
|
|
80
|
+
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k, this.filter);
|
|
74
81
|
const examples = exampleDocs.map((doc) => doc.metadata);
|
|
75
82
|
if (this.exampleKeys) {
|
|
76
83
|
// If example keys are provided, filter examples to those keys.
|
|
@@ -6,9 +6,10 @@ import { BaseExampleSelector } from "../base.js";
|
|
|
6
6
|
* Interface for the input data of the SemanticSimilarityExampleSelector
|
|
7
7
|
* class.
|
|
8
8
|
*/
|
|
9
|
-
export interface SemanticSimilarityExampleSelectorInput {
|
|
10
|
-
vectorStore:
|
|
9
|
+
export interface SemanticSimilarityExampleSelectorInput<V extends VectorStore = VectorStore> {
|
|
10
|
+
vectorStore: V;
|
|
11
11
|
k?: number;
|
|
12
|
+
filter?: V["FilterType"];
|
|
12
13
|
exampleKeys?: string[];
|
|
13
14
|
inputKeys?: string[];
|
|
14
15
|
}
|
|
@@ -16,12 +17,13 @@ export interface SemanticSimilarityExampleSelectorInput {
|
|
|
16
17
|
* Class that selects examples based on semantic similarity. It extends
|
|
17
18
|
* the BaseExampleSelector class.
|
|
18
19
|
*/
|
|
19
|
-
export declare class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
20
|
-
vectorStore:
|
|
20
|
+
export declare class SemanticSimilarityExampleSelector<V extends VectorStore = VectorStore> extends BaseExampleSelector {
|
|
21
|
+
vectorStore: V;
|
|
21
22
|
k: number;
|
|
22
23
|
exampleKeys?: string[];
|
|
23
24
|
inputKeys?: string[];
|
|
24
|
-
|
|
25
|
+
filter?: V["FilterType"];
|
|
26
|
+
constructor(data: SemanticSimilarityExampleSelectorInput<V>);
|
|
25
27
|
/**
|
|
26
28
|
* Method that adds a new example to the vectorStore. The example is
|
|
27
29
|
* converted to a string and added to the vectorStore as a document.
|
|
@@ -36,10 +36,17 @@ export class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
|
36
36
|
writable: true,
|
|
37
37
|
value: void 0
|
|
38
38
|
});
|
|
39
|
+
Object.defineProperty(this, "filter", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
39
45
|
this.vectorStore = data.vectorStore;
|
|
40
46
|
this.k = data.k ?? 4;
|
|
41
47
|
this.exampleKeys = data.exampleKeys;
|
|
42
48
|
this.inputKeys = data.inputKeys;
|
|
49
|
+
this.filter = data.filter;
|
|
43
50
|
}
|
|
44
51
|
/**
|
|
45
52
|
* Method that adds a new example to the vectorStore. The example is
|
|
@@ -53,7 +60,7 @@ export class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
|
53
60
|
await this.vectorStore.addDocuments([
|
|
54
61
|
new Document({
|
|
55
62
|
pageContent: stringExample,
|
|
56
|
-
metadata:
|
|
63
|
+
metadata: example,
|
|
57
64
|
}),
|
|
58
65
|
]);
|
|
59
66
|
}
|
|
@@ -67,7 +74,7 @@ export class SemanticSimilarityExampleSelector extends BaseExampleSelector {
|
|
|
67
74
|
async selectExamples(inputVariables) {
|
|
68
75
|
const inputKeys = this.inputKeys ?? Object.keys(inputVariables);
|
|
69
76
|
const query = sortedValues(inputKeys.reduce((acc, key) => ({ ...acc, [key]: inputVariables[key] }), {})).join(" ");
|
|
70
|
-
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k);
|
|
77
|
+
const exampleDocs = await this.vectorStore.similaritySearch(query, this.k, this.filter);
|
|
71
78
|
const examples = exampleDocs.map((doc) => doc.metadata);
|
|
72
79
|
if (this.exampleKeys) {
|
|
73
80
|
// If example keys are provided, filter examples to those keys.
|
package/dist/util/bedrock.cjs
CHANGED
|
@@ -48,7 +48,8 @@ class BedrockLLMInputOutputAdapter {
|
|
|
48
48
|
else if (provider === "ai21") {
|
|
49
49
|
return responseBody?.completions?.[0]?.data?.text ?? "";
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
// I haven't been able to get a response with more than one result in it.
|
|
52
|
+
return responseBody.results?.[0]?.outputText;
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
exports.BedrockLLMInputOutputAdapter = BedrockLLMInputOutputAdapter;
|
package/dist/util/bedrock.js
CHANGED
|
@@ -45,6 +45,7 @@ export class BedrockLLMInputOutputAdapter {
|
|
|
45
45
|
else if (provider === "ai21") {
|
|
46
46
|
return responseBody?.completions?.[0]?.data?.text ?? "";
|
|
47
47
|
}
|
|
48
|
-
|
|
48
|
+
// I haven't been able to get a response with more than one result in it.
|
|
49
|
+
return responseBody.results?.[0]?.outputText;
|
|
49
50
|
}
|
|
50
51
|
}
|
package/dist/util/stream.cjs
CHANGED
|
@@ -38,7 +38,7 @@ class IterableReadableStream extends ReadableStream {
|
|
|
38
38
|
const cancelPromise = this.reader.cancel(); // cancel first, but don't await yet
|
|
39
39
|
this.reader.releaseLock(); // release lock first
|
|
40
40
|
await cancelPromise; // now await it
|
|
41
|
-
return { done: true, value: undefined }; // This cast fixes TS typing, and convention is to ignore chunk value anyway
|
|
41
|
+
return { done: true, value: undefined }; // This cast fixes TS typing, and convention is to ignore final chunk value anyway
|
|
42
42
|
}
|
|
43
43
|
[Symbol.asyncIterator]() {
|
|
44
44
|
return this;
|
|
@@ -68,12 +68,12 @@ class IterableReadableStream extends ReadableStream {
|
|
|
68
68
|
return new IterableReadableStream({
|
|
69
69
|
async pull(controller) {
|
|
70
70
|
const { value, done } = await generator.next();
|
|
71
|
+
// When no more data needs to be consumed, close the stream
|
|
71
72
|
if (done) {
|
|
72
73
|
controller.close();
|
|
73
74
|
}
|
|
74
|
-
else if (value)
|
|
75
|
-
|
|
76
|
-
}
|
|
75
|
+
// Fix: `else if (value)` will hang the streaming when nullish value (e.g. empty string) is pulled
|
|
76
|
+
controller.enqueue(value);
|
|
77
77
|
},
|
|
78
78
|
});
|
|
79
79
|
}
|
package/dist/util/stream.js
CHANGED
|
@@ -35,7 +35,7 @@ export class IterableReadableStream extends ReadableStream {
|
|
|
35
35
|
const cancelPromise = this.reader.cancel(); // cancel first, but don't await yet
|
|
36
36
|
this.reader.releaseLock(); // release lock first
|
|
37
37
|
await cancelPromise; // now await it
|
|
38
|
-
return { done: true, value: undefined }; // This cast fixes TS typing, and convention is to ignore chunk value anyway
|
|
38
|
+
return { done: true, value: undefined }; // This cast fixes TS typing, and convention is to ignore final chunk value anyway
|
|
39
39
|
}
|
|
40
40
|
[Symbol.asyncIterator]() {
|
|
41
41
|
return this;
|
|
@@ -65,12 +65,12 @@ export class IterableReadableStream extends ReadableStream {
|
|
|
65
65
|
return new IterableReadableStream({
|
|
66
66
|
async pull(controller) {
|
|
67
67
|
const { value, done } = await generator.next();
|
|
68
|
+
// When no more data needs to be consumed, close the stream
|
|
68
69
|
if (done) {
|
|
69
70
|
controller.close();
|
|
70
71
|
}
|
|
71
|
-
else if (value)
|
|
72
|
-
|
|
73
|
-
}
|
|
72
|
+
// Fix: `else if (value)` will hang the streaming when nullish value (e.g. empty string) is pulled
|
|
73
|
+
controller.enqueue(value);
|
|
74
74
|
},
|
|
75
75
|
});
|
|
76
76
|
}
|
|
@@ -29,7 +29,7 @@ type ObjectIntersect<A, B> = {
|
|
|
29
29
|
type ModelColumns<TModel extends Record<string, unknown>> = {
|
|
30
30
|
[K in keyof TModel]?: true | ColumnSymbol;
|
|
31
31
|
};
|
|
32
|
-
type PrismaSqlFilter<TModel extends Record<string, unknown>> = {
|
|
32
|
+
export type PrismaSqlFilter<TModel extends Record<string, unknown>> = {
|
|
33
33
|
[K in keyof TModel]?: {
|
|
34
34
|
equals?: TModel[K];
|
|
35
35
|
lt?: TModel[K];
|