langchain 0.0.78 → 0.0.80
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/chains/query_constructor/prompt.cjs +5 -5
- package/dist/chains/query_constructor/prompt.d.ts +2 -2
- package/dist/chains/query_constructor/prompt.js +5 -5
- package/dist/chains/sql_db/sql_db_chain.cjs +0 -3
- package/dist/chains/sql_db/sql_db_chain.js +0 -3
- package/dist/chains/vector_db_qa.cjs +1 -1
- package/dist/chains/vector_db_qa.js +1 -1
- package/dist/client/langchainplus.cjs +143 -52
- package/dist/client/langchainplus.d.ts +72 -15
- package/dist/client/langchainplus.js +144 -53
- package/dist/document_loaders/index.cjs +1 -3
- package/dist/document_loaders/index.d.ts +0 -1
- package/dist/document_loaders/index.js +0 -1
- package/dist/document_loaders/web/github.cjs +38 -23
- package/dist/document_loaders/web/github.d.ts +5 -2
- package/dist/document_loaders/web/github.js +38 -23
- package/dist/llms/googlevertexai.cjs +97 -0
- package/dist/llms/googlevertexai.d.ts +43 -0
- package/dist/llms/googlevertexai.js +93 -0
- package/dist/prompts/selectors/conditional.cjs +4 -0
- package/dist/prompts/selectors/conditional.d.ts +5 -0
- package/dist/prompts/selectors/conditional.js +4 -0
- package/dist/retrievers/metal.d.ts +2 -1
- package/dist/stores/message/redis.cjs +1 -10
- package/dist/stores/message/redis.js +1 -10
- package/dist/text_splitter.cjs +11 -4
- package/dist/text_splitter.d.ts +7 -2
- package/dist/text_splitter.js +11 -4
- package/dist/types/googlevertexai-types.cjs +2 -0
- package/dist/types/googlevertexai-types.d.ts +47 -0
- package/dist/types/googlevertexai-types.js +1 -0
- package/dist/util/googlevertexai-connection.cjs +66 -0
- package/dist/util/googlevertexai-connection.d.ts +13 -0
- package/dist/util/googlevertexai-connection.js +62 -0
- package/dist/vectorstores/chroma.cjs +34 -7
- package/dist/vectorstores/chroma.d.ts +5 -1
- package/dist/vectorstores/chroma.js +34 -7
- package/dist/vectorstores/milvus.cjs +9 -30
- package/dist/vectorstores/milvus.d.ts +0 -3
- package/dist/vectorstores/milvus.js +9 -30
- package/llms/googlevertexai.cjs +1 -0
- package/llms/googlevertexai.d.ts +1 -0
- package/llms/googlevertexai.js +1 -0
- package/package.json +21 -5
|
@@ -5,8 +5,8 @@ const prompt_js_1 = require("../../prompts/prompt.cjs");
|
|
|
5
5
|
exports.SONG_DATA_SOURCE = `\
|
|
6
6
|
\`\`\`json
|
|
7
7
|
{
|
|
8
|
-
content: "Lyrics of a song",
|
|
9
|
-
attributes: {
|
|
8
|
+
"content": "Lyrics of a song",
|
|
9
|
+
"attributes": {
|
|
10
10
|
"artist": {
|
|
11
11
|
"type": "string",
|
|
12
12
|
"description": "Name of the song artist"
|
|
@@ -95,7 +95,7 @@ A comparison statement takes the form: \`comp(attr, val)\`:
|
|
|
95
95
|
A logical operation statement takes the form \`op(statement1, statement2, ...)\`:
|
|
96
96
|
- \`op\` ({allowed_operators}): logical operator
|
|
97
97
|
- \`statement1\`, \`statement2\`, ... (comparison statements or logical operation \
|
|
98
|
-
statements): one or more statements to
|
|
98
|
+
statements): one or more statements to apply the operation to
|
|
99
99
|
|
|
100
100
|
Make sure that you only use the comparators and logical operators listed above and \
|
|
101
101
|
no others.
|
|
@@ -115,8 +115,8 @@ exports.DEFAULT_SUFFIX = `\
|
|
|
115
115
|
Data Source:
|
|
116
116
|
\`\`\`json
|
|
117
117
|
{{{{
|
|
118
|
-
content: {content},
|
|
119
|
-
attributes: {attributes}
|
|
118
|
+
"content": {content},
|
|
119
|
+
"attributes": {attributes}
|
|
120
120
|
}}}}
|
|
121
121
|
\`\`\`
|
|
122
122
|
|
|
@@ -10,6 +10,6 @@ export declare const DEFAULT_EXAMPLES: {
|
|
|
10
10
|
}[];
|
|
11
11
|
export declare const EXAMPLE_PROMPT_TEMPLATE = "<< Example {i}. >>\nData Source:\n{data_source}\n\nUser Query:\n{user_query}\n\nStructured Request:\n{structured_request}\n";
|
|
12
12
|
export declare const EXAMPLE_PROMPT: PromptTemplate;
|
|
13
|
-
export declare const DEFAULT_SCHEMA = "<< Structured Request Schema >>\nWhen responding use a markdown code snippet with a JSON object formatted in the following schema:\n\n```json\n{{{{\n \"query\": string \\ text string to compare to document contents\n \"filter\": string \\ logical condition statement for filtering documents\n}}}}\n```\n\nThe query string should contain only text that is expected to match the contents of documents. Any conditions in the filter should not be mentioned in the query as well.\n\nA logical condition statement is composed of one or more comparison and logical operation statements.\n\nA comparison statement takes the form: `comp(attr, val)`:\n- `comp` ({allowed_comparators}): comparator\n- `attr` (string): name of attribute to apply the comparison to\n- `val` (string): is the comparison value\n\nA logical operation statement takes the form `op(statement1, statement2, ...)`:\n- `op` ({allowed_operators}): logical operator\n- `statement1`, `statement2`, ... (comparison statements or logical operation statements): one or more statements to
|
|
13
|
+
export declare const DEFAULT_SCHEMA = "<< Structured Request Schema >>\nWhen responding use a markdown code snippet with a JSON object formatted in the following schema:\n\n```json\n{{{{\n \"query\": string \\ text string to compare to document contents\n \"filter\": string \\ logical condition statement for filtering documents\n}}}}\n```\n\nThe query string should contain only text that is expected to match the contents of documents. Any conditions in the filter should not be mentioned in the query as well.\n\nA logical condition statement is composed of one or more comparison and logical operation statements.\n\nA comparison statement takes the form: `comp(attr, val)`:\n- `comp` ({allowed_comparators}): comparator\n- `attr` (string): name of attribute to apply the comparison to\n- `val` (string): is the comparison value\n\nA logical operation statement takes the form `op(statement1, statement2, ...)`:\n- `op` ({allowed_operators}): logical operator\n- `statement1`, `statement2`, ... (comparison statements or logical operation statements): one or more statements to apply the operation to\n\nMake sure that you only use the comparators and logical operators listed above and no others.\nMake sure that filters only refer to attributes that exist in the data source.\nMake sure that filters take into account the descriptions of attributes and only make comparisons that are feasible given the type of data being stored.\nMake sure that filters are only used as needed. If there are no filters that should be applied return \"NO_FILTER\" for the filter value.";
|
|
14
14
|
export declare const DEFAULT_PREFIX = "Your goal is to structure the user's query to match the request schema provided below.\n\n{schema}";
|
|
15
|
-
export declare const DEFAULT_SUFFIX = "<< Example {i}. >>\nData Source:\n```json\n{{{{\n content: {content},\n attributes: {attributes}\n}}}}\n```\n\nUser Query:\n{{query}}\n\nStructured Request:\n";
|
|
15
|
+
export declare const DEFAULT_SUFFIX = "<< Example {i}. >>\nData Source:\n```json\n{{{{\n \"content\": {content},\n \"attributes\": {attributes}\n}}}}\n```\n\nUser Query:\n{{query}}\n\nStructured Request:\n";
|
|
@@ -2,8 +2,8 @@ import { PromptTemplate } from "../../prompts/prompt.js";
|
|
|
2
2
|
export const SONG_DATA_SOURCE = `\
|
|
3
3
|
\`\`\`json
|
|
4
4
|
{
|
|
5
|
-
content: "Lyrics of a song",
|
|
6
|
-
attributes: {
|
|
5
|
+
"content": "Lyrics of a song",
|
|
6
|
+
"attributes": {
|
|
7
7
|
"artist": {
|
|
8
8
|
"type": "string",
|
|
9
9
|
"description": "Name of the song artist"
|
|
@@ -92,7 +92,7 @@ A comparison statement takes the form: \`comp(attr, val)\`:
|
|
|
92
92
|
A logical operation statement takes the form \`op(statement1, statement2, ...)\`:
|
|
93
93
|
- \`op\` ({allowed_operators}): logical operator
|
|
94
94
|
- \`statement1\`, \`statement2\`, ... (comparison statements or logical operation \
|
|
95
|
-
statements): one or more statements to
|
|
95
|
+
statements): one or more statements to apply the operation to
|
|
96
96
|
|
|
97
97
|
Make sure that you only use the comparators and logical operators listed above and \
|
|
98
98
|
no others.
|
|
@@ -112,8 +112,8 @@ export const DEFAULT_SUFFIX = `\
|
|
|
112
112
|
Data Source:
|
|
113
113
|
\`\`\`json
|
|
114
114
|
{{{{
|
|
115
|
-
content: {content},
|
|
116
|
-
attributes: {attributes}
|
|
115
|
+
"content": {content},
|
|
116
|
+
"attributes": {attributes}
|
|
117
117
|
}}}}
|
|
118
118
|
\`\`\`
|
|
119
119
|
|
|
@@ -96,13 +96,10 @@ class SqlDatabaseChain extends base_js_1.BaseChain {
|
|
|
96
96
|
stop: ["\nSQLResult:"],
|
|
97
97
|
};
|
|
98
98
|
await this.verifyNumberOfTokens(inputText, tableInfo);
|
|
99
|
-
const intermediateStep = [];
|
|
100
99
|
const sqlCommand = await llmChain.predict(llmInputs, runManager?.getChild());
|
|
101
|
-
intermediateStep.push(sqlCommand);
|
|
102
100
|
let queryResult = "";
|
|
103
101
|
try {
|
|
104
102
|
queryResult = await this.database.appDataSource.query(sqlCommand);
|
|
105
|
-
intermediateStep.push(queryResult);
|
|
106
103
|
}
|
|
107
104
|
catch (error) {
|
|
108
105
|
console.error(error);
|
|
@@ -93,13 +93,10 @@ export class SqlDatabaseChain extends BaseChain {
|
|
|
93
93
|
stop: ["\nSQLResult:"],
|
|
94
94
|
};
|
|
95
95
|
await this.verifyNumberOfTokens(inputText, tableInfo);
|
|
96
|
-
const intermediateStep = [];
|
|
97
96
|
const sqlCommand = await llmChain.predict(llmInputs, runManager?.getChild());
|
|
98
|
-
intermediateStep.push(sqlCommand);
|
|
99
97
|
let queryResult = "";
|
|
100
98
|
try {
|
|
101
99
|
queryResult = await this.database.appDataSource.query(sqlCommand);
|
|
102
|
-
intermediateStep.push(queryResult);
|
|
103
100
|
}
|
|
104
101
|
catch (error) {
|
|
105
102
|
console.error(error);
|
|
@@ -55,7 +55,7 @@ class VectorDBQAChain extends base_js_1.BaseChain {
|
|
|
55
55
|
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
56
56
|
}
|
|
57
57
|
const question = values[this.inputKey];
|
|
58
|
-
const docs = await this.vectorstore.similaritySearch(question, this.k);
|
|
58
|
+
const docs = await this.vectorstore.similaritySearch(question, this.k, values.filter);
|
|
59
59
|
const inputs = { question, input_documents: docs };
|
|
60
60
|
const result = await this.combineDocumentsChain.call(inputs, runManager?.getChild());
|
|
61
61
|
if (this.returnSourceDocuments) {
|
|
@@ -52,7 +52,7 @@ export class VectorDBQAChain extends BaseChain {
|
|
|
52
52
|
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
53
53
|
}
|
|
54
54
|
const question = values[this.inputKey];
|
|
55
|
-
const docs = await this.vectorstore.similaritySearch(question, this.k);
|
|
55
|
+
const docs = await this.vectorstore.similaritySearch(question, this.k, values.filter);
|
|
56
56
|
const inputs = { question, input_documents: docs };
|
|
57
57
|
const result = await this.combineDocumentsChain.call(inputs, runManager?.getChild());
|
|
58
58
|
if (this.returnSourceDocuments) {
|
|
@@ -10,7 +10,7 @@ const isLocalhost = (url) => {
|
|
|
10
10
|
const hostname = strippedUrl.split("/")[0].split(":")[0];
|
|
11
11
|
return (hostname === "localhost" || hostname === "127.0.0.1" || hostname === "::1");
|
|
12
12
|
};
|
|
13
|
-
const getSeededTenantId = async (apiUrl, apiKey, callerOptions
|
|
13
|
+
const getSeededTenantId = async (apiUrl, { apiKey, callerOptions, }) => {
|
|
14
14
|
// Get the tenant ID from the seeded tenant
|
|
15
15
|
const caller = new async_caller_js_1.AsyncCaller(callerOptions ?? {});
|
|
16
16
|
const url = `${apiUrl}/tenants`;
|
|
@@ -18,7 +18,7 @@ const getSeededTenantId = async (apiUrl, apiKey, callerOptions = undefined) => {
|
|
|
18
18
|
try {
|
|
19
19
|
response = await caller.call(fetch, url, {
|
|
20
20
|
method: "GET",
|
|
21
|
-
headers: apiKey ? {
|
|
21
|
+
headers: apiKey ? { "x-api-key": apiKey } : undefined,
|
|
22
22
|
});
|
|
23
23
|
}
|
|
24
24
|
catch (err) {
|
|
@@ -82,18 +82,24 @@ async function getModelOrFactoryType(llm) {
|
|
|
82
82
|
throw new Error("Unknown model or factory type");
|
|
83
83
|
}
|
|
84
84
|
class LangChainPlusClient {
|
|
85
|
-
constructor(
|
|
85
|
+
constructor(config) {
|
|
86
86
|
Object.defineProperty(this, "apiKey", {
|
|
87
87
|
enumerable: true,
|
|
88
88
|
configurable: true,
|
|
89
89
|
writable: true,
|
|
90
|
-
value:
|
|
90
|
+
value: typeof process !== "undefined"
|
|
91
|
+
? // eslint-disable-next-line no-process-env
|
|
92
|
+
process.env?.LANGCHAIN_API_KEY
|
|
93
|
+
: undefined
|
|
91
94
|
});
|
|
92
95
|
Object.defineProperty(this, "apiUrl", {
|
|
93
96
|
enumerable: true,
|
|
94
97
|
configurable: true,
|
|
95
98
|
writable: true,
|
|
96
|
-
value:
|
|
99
|
+
value: (typeof process !== "undefined"
|
|
100
|
+
? // eslint-disable-next-line no-process-env
|
|
101
|
+
process.env?.LANGCHAIN_ENDPOINT
|
|
102
|
+
: undefined) || "http://localhost:8000"
|
|
97
103
|
});
|
|
98
104
|
Object.defineProperty(this, "tenantId", {
|
|
99
105
|
enumerable: true,
|
|
@@ -107,15 +113,45 @@ class LangChainPlusClient {
|
|
|
107
113
|
writable: true,
|
|
108
114
|
value: void 0
|
|
109
115
|
});
|
|
110
|
-
this.apiUrl = apiUrl;
|
|
111
|
-
this.apiKey = apiKey;
|
|
112
|
-
|
|
116
|
+
this.apiUrl = config.apiUrl ?? this.apiUrl;
|
|
117
|
+
this.apiKey = config.apiKey;
|
|
118
|
+
const tenantId = config.tenantId ??
|
|
119
|
+
(typeof process !== "undefined"
|
|
120
|
+
? // eslint-disable-next-line no-process-env
|
|
121
|
+
process.env?.LANGCHAIN_TENANT_ID
|
|
122
|
+
: undefined);
|
|
123
|
+
if (tenantId === undefined) {
|
|
124
|
+
throw new Error("No tenant ID provided and no LANGCHAIN_TENANT_ID env var");
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
this.tenantId = tenantId;
|
|
128
|
+
}
|
|
113
129
|
this.validateApiKeyIfHosted();
|
|
114
|
-
this.caller = new async_caller_js_1.AsyncCaller(callerOptions ?? {});
|
|
130
|
+
this.caller = new async_caller_js_1.AsyncCaller(config.callerOptions ?? {});
|
|
115
131
|
}
|
|
116
|
-
static async create(
|
|
117
|
-
const
|
|
118
|
-
|
|
132
|
+
static async create(config = {}) {
|
|
133
|
+
const apiUrl_ = config.apiUrl ??
|
|
134
|
+
((typeof process !== "undefined"
|
|
135
|
+
? // eslint-disable-next-line no-process-env
|
|
136
|
+
process.env?.LANGCHAIN_ENDPOINT
|
|
137
|
+
: undefined) ||
|
|
138
|
+
"http://localhost:8000");
|
|
139
|
+
const apiKey_ = config.apiKey ??
|
|
140
|
+
(typeof process !== "undefined"
|
|
141
|
+
? // eslint-disable-next-line no-process-env
|
|
142
|
+
process.env?.LANGCHAIN_API_KEY
|
|
143
|
+
: undefined);
|
|
144
|
+
const tenantId_ = config.tenantId ??
|
|
145
|
+
((typeof process !== "undefined"
|
|
146
|
+
? // eslint-disable-next-line no-process-env
|
|
147
|
+
process.env?.LANGCHAIN_TENANT_ID
|
|
148
|
+
: undefined) ||
|
|
149
|
+
(await getSeededTenantId(apiUrl_, { apiKey: apiKey_ })));
|
|
150
|
+
return new LangChainPlusClient({
|
|
151
|
+
tenantId: tenantId_,
|
|
152
|
+
apiKey: apiKey_,
|
|
153
|
+
apiUrl: apiUrl_,
|
|
154
|
+
});
|
|
119
155
|
}
|
|
120
156
|
validateApiKeyIfHosted() {
|
|
121
157
|
const isLocal = isLocalhost(this.apiUrl);
|
|
@@ -126,24 +162,21 @@ class LangChainPlusClient {
|
|
|
126
162
|
get headers() {
|
|
127
163
|
const headers = {};
|
|
128
164
|
if (this.apiKey) {
|
|
129
|
-
headers
|
|
165
|
+
headers["x-api-key"] = `${this.apiKey}`;
|
|
130
166
|
}
|
|
131
167
|
return headers;
|
|
132
168
|
}
|
|
133
169
|
get queryParams() {
|
|
134
|
-
return { tenant_id: this.tenantId };
|
|
135
|
-
}
|
|
136
|
-
async _get(path, queryParams
|
|
137
|
-
const params =
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
? `${queryString}&${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
|
|
143
|
-
: `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`;
|
|
144
|
-
}
|
|
170
|
+
return new URLSearchParams({ tenant_id: this.tenantId });
|
|
171
|
+
}
|
|
172
|
+
async _get(path, queryParams) {
|
|
173
|
+
const params = this.queryParams;
|
|
174
|
+
if (queryParams) {
|
|
175
|
+
queryParams.forEach((value, key) => {
|
|
176
|
+
params.append(key, value);
|
|
177
|
+
});
|
|
145
178
|
}
|
|
146
|
-
const url = `${this.apiUrl}${path}
|
|
179
|
+
const url = `${this.apiUrl}${path}?${params.toString()}`;
|
|
147
180
|
const response = await this.caller.call(fetch, url, {
|
|
148
181
|
method: "GET",
|
|
149
182
|
headers: this.headers,
|
|
@@ -153,14 +186,73 @@ class LangChainPlusClient {
|
|
|
153
186
|
}
|
|
154
187
|
return response.json();
|
|
155
188
|
}
|
|
156
|
-
async
|
|
189
|
+
async readRun(runId) {
|
|
190
|
+
return await this._get(`/runs/${runId}`);
|
|
191
|
+
}
|
|
192
|
+
async listRuns({ sessionId, sessionName, executionOrder = 1, runType, error, }) {
|
|
193
|
+
const queryParams = new URLSearchParams();
|
|
194
|
+
let sessionId_ = sessionId;
|
|
195
|
+
if (sessionName) {
|
|
196
|
+
if (sessionId) {
|
|
197
|
+
throw new Error("Only one of session_id or session_name may be given");
|
|
198
|
+
}
|
|
199
|
+
sessionId_ = (await this.readSession({ sessionName })).id;
|
|
200
|
+
}
|
|
201
|
+
if (sessionId_) {
|
|
202
|
+
queryParams.append("session", sessionId_);
|
|
203
|
+
}
|
|
204
|
+
if (executionOrder) {
|
|
205
|
+
queryParams.append("execution_order", executionOrder.toString());
|
|
206
|
+
}
|
|
207
|
+
if (runType) {
|
|
208
|
+
queryParams.append("run_type", runType);
|
|
209
|
+
}
|
|
210
|
+
if (error !== undefined) {
|
|
211
|
+
queryParams.append("error", error.toString());
|
|
212
|
+
}
|
|
213
|
+
return this._get("/runs", queryParams);
|
|
214
|
+
}
|
|
215
|
+
async readSession({ sessionId, sessionName, }) {
|
|
216
|
+
let path = "/sessions";
|
|
217
|
+
const params = new URLSearchParams();
|
|
218
|
+
if (sessionId !== undefined && sessionName !== undefined) {
|
|
219
|
+
throw new Error("Must provide either sessionName or sessionId, not both");
|
|
220
|
+
}
|
|
221
|
+
else if (sessionId !== undefined) {
|
|
222
|
+
path += `/${sessionId}`;
|
|
223
|
+
}
|
|
224
|
+
else if (sessionName !== undefined) {
|
|
225
|
+
params.append("name", sessionName);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
throw new Error("Must provide sessionName or sessionId");
|
|
229
|
+
}
|
|
230
|
+
const response = await this._get(path, params);
|
|
231
|
+
let result;
|
|
232
|
+
if (Array.isArray(response)) {
|
|
233
|
+
if (response.length === 0) {
|
|
234
|
+
throw new Error(`Session[id=${sessionId}, name=${sessionName}] not found`);
|
|
235
|
+
}
|
|
236
|
+
result = response[0];
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
result = response;
|
|
240
|
+
}
|
|
241
|
+
return result;
|
|
242
|
+
}
|
|
243
|
+
async listSessions() {
|
|
244
|
+
return this._get("/sessions");
|
|
245
|
+
}
|
|
246
|
+
async uploadCsv({ csvFile, fileName, inputKeys, outputKeys, description, }) {
|
|
157
247
|
const url = `${this.apiUrl}/datasets/upload`;
|
|
158
248
|
const formData = new FormData();
|
|
159
249
|
formData.append("file", csvFile, fileName);
|
|
160
250
|
formData.append("input_keys", inputKeys.join(","));
|
|
161
251
|
formData.append("output_keys", outputKeys.join(","));
|
|
162
|
-
formData.append("description", description);
|
|
163
252
|
formData.append("tenant_id", this.tenantId);
|
|
253
|
+
if (description) {
|
|
254
|
+
formData.append("description", description);
|
|
255
|
+
}
|
|
164
256
|
const response = await this.caller.call(fetch, url, {
|
|
165
257
|
method: "POST",
|
|
166
258
|
headers: this.headers,
|
|
@@ -176,7 +268,7 @@ class LangChainPlusClient {
|
|
|
176
268
|
const result = await response.json();
|
|
177
269
|
return result;
|
|
178
270
|
}
|
|
179
|
-
async createDataset(name, description) {
|
|
271
|
+
async createDataset(name, { description }) {
|
|
180
272
|
const response = await this.caller.call(fetch, `${this.apiUrl}/datasets`, {
|
|
181
273
|
method: "POST",
|
|
182
274
|
headers: { ...this.headers, "Content-Type": "application/json" },
|
|
@@ -196,10 +288,10 @@ class LangChainPlusClient {
|
|
|
196
288
|
const result = await response.json();
|
|
197
289
|
return result;
|
|
198
290
|
}
|
|
199
|
-
async readDataset(datasetId, datasetName) {
|
|
291
|
+
async readDataset({ datasetId, datasetName, }) {
|
|
200
292
|
let path = "/datasets";
|
|
201
|
-
//
|
|
202
|
-
const params = { limit: 1 };
|
|
293
|
+
// limit to 1 result
|
|
294
|
+
const params = new URLSearchParams({ limit: "1" });
|
|
203
295
|
if (datasetId !== undefined && datasetName !== undefined) {
|
|
204
296
|
throw new Error("Must provide either datasetName or datasetId, not both");
|
|
205
297
|
}
|
|
@@ -207,7 +299,7 @@ class LangChainPlusClient {
|
|
|
207
299
|
path += `/${datasetId}`;
|
|
208
300
|
}
|
|
209
301
|
else if (datasetName !== undefined) {
|
|
210
|
-
params.name
|
|
302
|
+
params.append("name", datasetName);
|
|
211
303
|
}
|
|
212
304
|
else {
|
|
213
305
|
throw new Error("Must provide datasetName or datasetId");
|
|
@@ -225,24 +317,23 @@ class LangChainPlusClient {
|
|
|
225
317
|
}
|
|
226
318
|
return result;
|
|
227
319
|
}
|
|
228
|
-
async listDatasets(limit = 100) {
|
|
320
|
+
async listDatasets({ limit = 100, } = {}) {
|
|
229
321
|
const path = "/datasets";
|
|
230
|
-
|
|
231
|
-
const params = { limit };
|
|
322
|
+
const params = new URLSearchParams({ limit: limit.toString() });
|
|
232
323
|
const response = await this._get(path, params);
|
|
233
324
|
if (!Array.isArray(response)) {
|
|
234
325
|
throw new Error(`Expected ${path} to return an array, but got ${response}`);
|
|
235
326
|
}
|
|
236
327
|
return response;
|
|
237
328
|
}
|
|
238
|
-
async deleteDataset(datasetId, datasetName) {
|
|
329
|
+
async deleteDataset({ datasetId, datasetName, }) {
|
|
239
330
|
let path = "/datasets";
|
|
240
331
|
let datasetId_ = datasetId;
|
|
241
332
|
if (datasetId !== undefined && datasetName !== undefined) {
|
|
242
333
|
throw new Error("Must provide either datasetName or datasetId, not both");
|
|
243
334
|
}
|
|
244
335
|
else if (datasetName !== undefined) {
|
|
245
|
-
const dataset = await this.readDataset(
|
|
336
|
+
const dataset = await this.readDataset({ datasetName });
|
|
246
337
|
datasetId_ = dataset.id;
|
|
247
338
|
}
|
|
248
339
|
if (datasetId_ !== undefined) {
|
|
@@ -261,7 +352,7 @@ class LangChainPlusClient {
|
|
|
261
352
|
const results = await response.json();
|
|
262
353
|
return results;
|
|
263
354
|
}
|
|
264
|
-
async createExample(inputs, outputs
|
|
355
|
+
async createExample(inputs, outputs, { datasetId, datasetName, createdAt, }) {
|
|
265
356
|
let datasetId_ = datasetId;
|
|
266
357
|
if (datasetId_ === undefined && datasetName === undefined) {
|
|
267
358
|
throw new Error("Must provide either datasetName or datasetId");
|
|
@@ -270,7 +361,7 @@ class LangChainPlusClient {
|
|
|
270
361
|
throw new Error("Must provide either datasetName or datasetId, not both");
|
|
271
362
|
}
|
|
272
363
|
else if (datasetId_ === undefined) {
|
|
273
|
-
const dataset = await this.readDataset(
|
|
364
|
+
const dataset = await this.readDataset({ datasetName });
|
|
274
365
|
datasetId_ = dataset.id;
|
|
275
366
|
}
|
|
276
367
|
const createdAt_ = createdAt || new Date();
|
|
@@ -295,7 +386,7 @@ class LangChainPlusClient {
|
|
|
295
386
|
const path = `/examples/${exampleId}`;
|
|
296
387
|
return await this._get(path);
|
|
297
388
|
}
|
|
298
|
-
async listExamples(datasetId
|
|
389
|
+
async listExamples({ datasetId, datasetName, } = {}) {
|
|
299
390
|
let datasetId_;
|
|
300
391
|
if (datasetId !== undefined && datasetName !== undefined) {
|
|
301
392
|
throw new Error("Must provide either datasetName or datasetId, not both");
|
|
@@ -304,15 +395,13 @@ class LangChainPlusClient {
|
|
|
304
395
|
datasetId_ = datasetId;
|
|
305
396
|
}
|
|
306
397
|
else if (datasetName !== undefined) {
|
|
307
|
-
const dataset = await this.readDataset(
|
|
398
|
+
const dataset = await this.readDataset({ datasetName });
|
|
308
399
|
datasetId_ = dataset.id;
|
|
309
400
|
}
|
|
310
401
|
else {
|
|
311
402
|
throw new Error("Must provide a datasetName or datasetId");
|
|
312
403
|
}
|
|
313
|
-
const response = await this._get("/examples", {
|
|
314
|
-
dataset: datasetId_,
|
|
315
|
-
});
|
|
404
|
+
const response = await this._get("/examples", new URLSearchParams({ dataset: datasetId_ }));
|
|
316
405
|
if (!Array.isArray(response)) {
|
|
317
406
|
throw new Error(`Expected /examples to return an array, but got ${response}`);
|
|
318
407
|
}
|
|
@@ -330,7 +419,7 @@ class LangChainPlusClient {
|
|
|
330
419
|
const result = await response.json();
|
|
331
420
|
return result;
|
|
332
421
|
}
|
|
333
|
-
async runLLM(example, tracer, llm, numRepetitions = 1) {
|
|
422
|
+
async runLLM(example, tracer, llm, { numRepetitions = 1 }) {
|
|
334
423
|
const results = await Promise.all(Array.from({ length: numRepetitions }).map(async () => {
|
|
335
424
|
try {
|
|
336
425
|
const prompt = example.inputs.prompt;
|
|
@@ -343,7 +432,7 @@ class LangChainPlusClient {
|
|
|
343
432
|
}));
|
|
344
433
|
return results;
|
|
345
434
|
}
|
|
346
|
-
async runChain(example, tracer, chainFactory, numRepetitions = 1) {
|
|
435
|
+
async runChain(example, tracer, chainFactory, { numRepetitions = 1, }) {
|
|
347
436
|
const results = await Promise.all(Array.from({ length: numRepetitions }).map(async () => {
|
|
348
437
|
try {
|
|
349
438
|
const chain = await chainFactory();
|
|
@@ -356,7 +445,7 @@ class LangChainPlusClient {
|
|
|
356
445
|
}));
|
|
357
446
|
return results;
|
|
358
447
|
}
|
|
359
|
-
async runChatModel(example, tracer, chatModel, numRepetitions = 1) {
|
|
448
|
+
async runChatModel(example, tracer, chatModel, { numRepetitions = 1, }) {
|
|
360
449
|
const results = await Promise.all(Array.from({ length: numRepetitions }).map(async () => {
|
|
361
450
|
try {
|
|
362
451
|
const messages = example.inputs.messages;
|
|
@@ -369,8 +458,8 @@ class LangChainPlusClient {
|
|
|
369
458
|
}));
|
|
370
459
|
return results;
|
|
371
460
|
}
|
|
372
|
-
async runOnDataset(datasetName, llmOrChainFactory, numRepetitions = 1, sessionName =
|
|
373
|
-
const examples = await this.listExamples(
|
|
461
|
+
async runOnDataset(datasetName, llmOrChainFactory, { numRepetitions = 1, sessionName, } = {}) {
|
|
462
|
+
const examples = await this.listExamples({ datasetName });
|
|
374
463
|
let sessionName_;
|
|
375
464
|
if (sessionName === undefined) {
|
|
376
465
|
const currentTime = new Date().toISOString();
|
|
@@ -388,17 +477,19 @@ class LangChainPlusClient {
|
|
|
388
477
|
});
|
|
389
478
|
if (modelOrFactoryType === "llm") {
|
|
390
479
|
const llm = llmOrChainFactory;
|
|
391
|
-
const llmResult = await this.runLLM(example, tracer, llm,
|
|
480
|
+
const llmResult = await this.runLLM(example, tracer, llm, {
|
|
481
|
+
numRepetitions,
|
|
482
|
+
});
|
|
392
483
|
results[example.id] = llmResult;
|
|
393
484
|
}
|
|
394
485
|
else if (modelOrFactoryType === "chainFactory") {
|
|
395
486
|
const chainFactory = llmOrChainFactory;
|
|
396
|
-
const chainResult = await this.runChain(example, tracer, chainFactory, numRepetitions);
|
|
487
|
+
const chainResult = await this.runChain(example, tracer, chainFactory, { numRepetitions });
|
|
397
488
|
results[example.id] = chainResult;
|
|
398
489
|
}
|
|
399
490
|
else if (modelOrFactoryType === "chatModel") {
|
|
400
491
|
const chatModel = llmOrChainFactory;
|
|
401
|
-
const chatModelResult = await this.runChatModel(example, tracer, chatModel, numRepetitions);
|
|
492
|
+
const chatModelResult = await this.runChatModel(example, tracer, chatModel, { numRepetitions });
|
|
402
493
|
results[example.id] = chatModelResult;
|
|
403
494
|
}
|
|
404
495
|
else {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseRun } from "../callbacks/handlers/tracer.js";
|
|
2
|
-
import { LangChainTracer } from "../callbacks/handlers/tracer_langchain.js";
|
|
1
|
+
import { BaseRun, Run, RunType } from "../callbacks/handlers/tracer.js";
|
|
2
|
+
import { LangChainTracer, TracerSession } from "../callbacks/handlers/tracer_langchain.js";
|
|
3
3
|
import { ChainValues, LLMResult, RunInputs, RunOutputs } from "../schema/index.js";
|
|
4
4
|
import { BaseLanguageModel } from "../base_language/index.js";
|
|
5
5
|
import { BaseChain } from "../chains/base.js";
|
|
@@ -36,6 +36,20 @@ export interface Example extends BaseExample {
|
|
|
36
36
|
modified_at: string;
|
|
37
37
|
runs: RunResult[];
|
|
38
38
|
}
|
|
39
|
+
interface ListRunsParams {
|
|
40
|
+
sessionId?: string;
|
|
41
|
+
sessionName?: string;
|
|
42
|
+
executionOrder?: number;
|
|
43
|
+
runType?: RunType;
|
|
44
|
+
error?: boolean;
|
|
45
|
+
}
|
|
46
|
+
interface UploadCSVParams {
|
|
47
|
+
csvFile: Blob;
|
|
48
|
+
fileName: string;
|
|
49
|
+
inputKeys: string[];
|
|
50
|
+
outputKeys: string[];
|
|
51
|
+
description?: string;
|
|
52
|
+
}
|
|
39
53
|
export type DatasetRunResults = Record<string, (string | LLMResult | ChainValues)[]>;
|
|
40
54
|
export declare function isLLM(llm: BaseLanguageModel | (() => Promise<BaseChain>)): llm is BaseLLM;
|
|
41
55
|
export declare function isChatModel(llm: BaseLanguageModel | (() => Promise<BaseChain>)): llm is BaseChatModel;
|
|
@@ -45,23 +59,66 @@ export declare class LangChainPlusClient {
|
|
|
45
59
|
private apiUrl;
|
|
46
60
|
private tenantId;
|
|
47
61
|
private caller;
|
|
48
|
-
constructor(
|
|
49
|
-
|
|
62
|
+
constructor(config: {
|
|
63
|
+
tenantId?: string;
|
|
64
|
+
apiUrl?: string;
|
|
65
|
+
apiKey?: string;
|
|
66
|
+
callerOptions?: AsyncCallerParams;
|
|
67
|
+
});
|
|
68
|
+
static create(config?: {
|
|
69
|
+
apiUrl?: string;
|
|
70
|
+
apiKey?: string;
|
|
71
|
+
tenantId?: string;
|
|
72
|
+
}): Promise<LangChainPlusClient>;
|
|
50
73
|
private validateApiKeyIfHosted;
|
|
51
74
|
private get headers();
|
|
52
75
|
private get queryParams();
|
|
53
76
|
private _get;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
readRun(runId: string): Promise<Run>;
|
|
78
|
+
listRuns({ sessionId, sessionName, executionOrder, runType, error, }: ListRunsParams): Promise<Run[]>;
|
|
79
|
+
readSession({ sessionId, sessionName, }: {
|
|
80
|
+
sessionId?: string;
|
|
81
|
+
sessionName?: string;
|
|
82
|
+
}): Promise<TracerSession>;
|
|
83
|
+
listSessions(): Promise<TracerSession[]>;
|
|
84
|
+
uploadCsv({ csvFile, fileName, inputKeys, outputKeys, description, }: UploadCSVParams): Promise<Dataset>;
|
|
85
|
+
createDataset(name: string, { description }: {
|
|
86
|
+
description?: string;
|
|
87
|
+
}): Promise<Dataset>;
|
|
88
|
+
readDataset({ datasetId, datasetName, }: {
|
|
89
|
+
datasetId?: string;
|
|
90
|
+
datasetName?: string;
|
|
91
|
+
}): Promise<Dataset>;
|
|
92
|
+
listDatasets({ limit, }?: {
|
|
93
|
+
limit?: number;
|
|
94
|
+
}): Promise<Dataset[]>;
|
|
95
|
+
deleteDataset({ datasetId, datasetName, }: {
|
|
96
|
+
datasetId?: string;
|
|
97
|
+
datasetName?: string;
|
|
98
|
+
}): Promise<Dataset>;
|
|
99
|
+
createExample(inputs: RunInputs, outputs: RunOutputs, { datasetId, datasetName, createdAt, }: {
|
|
100
|
+
datasetId?: string;
|
|
101
|
+
datasetName?: string;
|
|
102
|
+
createdAt?: Date;
|
|
103
|
+
}): Promise<Example>;
|
|
60
104
|
readExample(exampleId: string): Promise<Example>;
|
|
61
|
-
listExamples(datasetId
|
|
105
|
+
listExamples({ datasetId, datasetName, }?: {
|
|
106
|
+
datasetId?: string;
|
|
107
|
+
datasetName?: string;
|
|
108
|
+
}): Promise<Example[]>;
|
|
62
109
|
deleteExample(exampleId: string): Promise<Example>;
|
|
63
|
-
protected runLLM(example: Example, tracer: LangChainTracer, llm: BaseLLM, numRepetitions
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
110
|
+
protected runLLM(example: Example, tracer: LangChainTracer, llm: BaseLLM, { numRepetitions }: {
|
|
111
|
+
numRepetitions?: number;
|
|
112
|
+
}): Promise<(LLMResult | string)[]>;
|
|
113
|
+
protected runChain(example: Example, tracer: LangChainTracer, chainFactory: () => Promise<BaseChain>, { numRepetitions, }: {
|
|
114
|
+
numRepetitions?: number;
|
|
115
|
+
}): Promise<(ChainValues | string)[]>;
|
|
116
|
+
protected runChatModel(example: Example, tracer: LangChainTracer, chatModel: BaseChatModel, { numRepetitions, }: {
|
|
117
|
+
numRepetitions?: number;
|
|
118
|
+
}): Promise<(LLMResult | string)[]>;
|
|
119
|
+
runOnDataset(datasetName: string, llmOrChainFactory: BaseLanguageModel | (() => Promise<BaseChain>), { numRepetitions, sessionName, }?: {
|
|
120
|
+
numRepetitions?: number;
|
|
121
|
+
sessionName?: string;
|
|
122
|
+
}): Promise<DatasetRunResults>;
|
|
67
123
|
}
|
|
124
|
+
export {};
|