langchain 0.0.85 → 0.0.87
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/callbacks/handlers/tracer_langchain.cjs +12 -67
- package/dist/callbacks/handlers/tracer_langchain.d.ts +5 -23
- package/dist/callbacks/handlers/tracer_langchain.js +12 -67
- package/dist/chains/conversational_retrieval_chain.cjs +20 -2
- package/dist/chains/conversational_retrieval_chain.d.ts +3 -2
- package/dist/chains/conversational_retrieval_chain.js +20 -2
- package/dist/client/langchainplus.cjs +37 -56
- package/dist/client/langchainplus.d.ts +8 -5
- package/dist/client/langchainplus.js +38 -57
- package/dist/memory/zep.cjs +13 -1
- package/dist/memory/zep.js +14 -2
- package/dist/text_splitter.cjs +406 -79
- package/dist/text_splitter.d.ts +8 -2
- package/dist/text_splitter.js +405 -78
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ const async_caller_js_1 = require("../../util/async_caller.cjs");
|
|
|
5
5
|
const env_js_1 = require("../../util/env.cjs");
|
|
6
6
|
const tracer_js_1 = require("./tracer.cjs");
|
|
7
7
|
class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
8
|
-
constructor({ exampleId,
|
|
8
|
+
constructor({ exampleId, sessionName, callerParams, timeout, } = {}) {
|
|
9
9
|
super();
|
|
10
10
|
Object.defineProperty(this, "name", {
|
|
11
11
|
enumerable: true,
|
|
@@ -33,93 +33,35 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
33
33
|
writable: true,
|
|
34
34
|
value: void 0
|
|
35
35
|
});
|
|
36
|
-
Object.defineProperty(this, "sessionExtra", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: true,
|
|
40
|
-
value: void 0
|
|
41
|
-
});
|
|
42
|
-
Object.defineProperty(this, "session", {
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
writable: true,
|
|
46
|
-
value: void 0
|
|
47
|
-
});
|
|
48
36
|
Object.defineProperty(this, "exampleId", {
|
|
49
37
|
enumerable: true,
|
|
50
38
|
configurable: true,
|
|
51
39
|
writable: true,
|
|
52
40
|
value: void 0
|
|
53
41
|
});
|
|
54
|
-
Object.defineProperty(this, "
|
|
42
|
+
Object.defineProperty(this, "caller", {
|
|
55
43
|
enumerable: true,
|
|
56
44
|
configurable: true,
|
|
57
45
|
writable: true,
|
|
58
46
|
value: void 0
|
|
59
47
|
});
|
|
60
|
-
Object.defineProperty(this, "
|
|
48
|
+
Object.defineProperty(this, "timeout", {
|
|
61
49
|
enumerable: true,
|
|
62
50
|
configurable: true,
|
|
63
51
|
writable: true,
|
|
64
|
-
value:
|
|
52
|
+
value: 5000
|
|
65
53
|
});
|
|
66
54
|
const apiKey = (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY");
|
|
67
55
|
if (apiKey) {
|
|
68
56
|
this.headers["x-api-key"] = apiKey;
|
|
69
57
|
}
|
|
70
|
-
this.tenantId = tenantId ?? (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_TENANT_ID");
|
|
71
58
|
this.sessionName =
|
|
72
|
-
sessionName ?? (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION")
|
|
73
|
-
this.sessionExtra = sessionExtra;
|
|
59
|
+
sessionName ?? (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION");
|
|
74
60
|
this.exampleId = exampleId;
|
|
75
|
-
this.
|
|
76
|
-
|
|
77
|
-
async ensureSession() {
|
|
78
|
-
if (this.session) {
|
|
79
|
-
return this.session;
|
|
80
|
-
}
|
|
81
|
-
const tenantId = await this.ensureTenantId();
|
|
82
|
-
const endpoint = `${this.endpoint}/sessions?upsert=true`;
|
|
83
|
-
const res = await this.caller.call(fetch, endpoint, {
|
|
84
|
-
method: "POST",
|
|
85
|
-
headers: this.headers,
|
|
86
|
-
body: JSON.stringify({
|
|
87
|
-
name: this.sessionName,
|
|
88
|
-
tenant_id: tenantId,
|
|
89
|
-
extra: this.sessionExtra,
|
|
90
|
-
}),
|
|
91
|
-
});
|
|
92
|
-
if (!res.ok) {
|
|
93
|
-
const body = await res.text();
|
|
94
|
-
throw new Error(`Failed to create session: ${res.status} ${res.statusText} ${body}`);
|
|
95
|
-
}
|
|
96
|
-
const session = await res.json();
|
|
97
|
-
this.session = session;
|
|
98
|
-
return session;
|
|
99
|
-
}
|
|
100
|
-
async ensureTenantId() {
|
|
101
|
-
if (this.tenantId) {
|
|
102
|
-
return this.tenantId;
|
|
103
|
-
}
|
|
104
|
-
const endpoint = `${this.endpoint}/tenants`;
|
|
105
|
-
const response = await this.caller.call(fetch, endpoint, {
|
|
106
|
-
method: "GET",
|
|
107
|
-
headers: this.headers,
|
|
108
|
-
});
|
|
109
|
-
if (!response.ok) {
|
|
110
|
-
const body = await response.text();
|
|
111
|
-
throw new Error(`Failed to fetch tenant ID: ${response.status} ${response.statusText} ${body}`);
|
|
112
|
-
}
|
|
113
|
-
const tenants = await response.json();
|
|
114
|
-
if (!tenants || tenants.length === 0) {
|
|
115
|
-
throw new Error(`No tenants found for endpoint ${endpoint}`);
|
|
116
|
-
}
|
|
117
|
-
const tenantId = tenants[0].id;
|
|
118
|
-
this.tenantId = tenantId;
|
|
119
|
-
return tenantId;
|
|
61
|
+
this.timeout = timeout ?? this.timeout;
|
|
62
|
+
this.caller = new async_caller_js_1.AsyncCaller(callerParams ?? { maxRetries: 2 });
|
|
120
63
|
}
|
|
121
64
|
async _convertToCreate(run, example_id = undefined) {
|
|
122
|
-
const session = await this.ensureSession();
|
|
123
65
|
const runExtra = run.extra ?? {};
|
|
124
66
|
runExtra.runtime = await (0, env_js_1.getRuntimeEnvironment)();
|
|
125
67
|
const persistedRun = {
|
|
@@ -135,7 +77,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
135
77
|
error: run.error,
|
|
136
78
|
inputs: run.inputs,
|
|
137
79
|
outputs: run.outputs ?? {},
|
|
138
|
-
|
|
80
|
+
session_name: this.sessionName,
|
|
139
81
|
child_runs: await Promise.all(run.child_runs.map((child_run) => this._convertToCreate(child_run))),
|
|
140
82
|
};
|
|
141
83
|
return persistedRun;
|
|
@@ -147,9 +89,12 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
|
|
|
147
89
|
method: "POST",
|
|
148
90
|
headers: this.headers,
|
|
149
91
|
body: JSON.stringify(persistedRun),
|
|
92
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
150
93
|
});
|
|
94
|
+
// consume the response body to release the connection
|
|
95
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
96
|
+
const body = await response.text();
|
|
151
97
|
if (!response.ok) {
|
|
152
|
-
const body = await response.text();
|
|
153
98
|
throw new Error(`Failed to persist run: ${response.status} ${response.statusText} ${body}`);
|
|
154
99
|
}
|
|
155
100
|
}
|
|
@@ -2,41 +2,23 @@ import { AsyncCaller, AsyncCallerParams } from "../../util/async_caller.js";
|
|
|
2
2
|
import { BaseTracer, Run, BaseRun } from "./tracer.js";
|
|
3
3
|
export interface RunCreate extends BaseRun {
|
|
4
4
|
child_runs: this[];
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
export interface BaseTracerSession {
|
|
8
|
-
start_time: number;
|
|
9
|
-
name?: string;
|
|
10
|
-
}
|
|
11
|
-
export interface BaseTracerSessionV2 extends BaseTracerSession {
|
|
12
|
-
tenant_id: string;
|
|
13
|
-
}
|
|
14
|
-
export interface TracerSessionCreateV2 extends BaseTracerSessionV2 {
|
|
15
|
-
id?: string;
|
|
16
|
-
}
|
|
17
|
-
export interface TracerSession extends BaseTracerSessionV2 {
|
|
18
|
-
id: string;
|
|
5
|
+
session_name?: string;
|
|
19
6
|
}
|
|
20
7
|
export interface LangChainTracerFields {
|
|
21
8
|
exampleId?: string;
|
|
22
|
-
tenantId?: string;
|
|
23
9
|
sessionName?: string;
|
|
24
|
-
sessionExtra?: Record<string, unknown>;
|
|
25
10
|
callerParams?: AsyncCallerParams;
|
|
11
|
+
timeout?: number;
|
|
26
12
|
}
|
|
27
13
|
export declare class LangChainTracer extends BaseTracer implements LangChainTracerFields {
|
|
28
14
|
name: string;
|
|
29
15
|
protected endpoint: string;
|
|
30
16
|
protected headers: Record<string, string>;
|
|
31
|
-
sessionName
|
|
32
|
-
sessionExtra?: LangChainTracerFields["sessionExtra"];
|
|
33
|
-
protected session: TracerSession;
|
|
17
|
+
sessionName?: string;
|
|
34
18
|
exampleId?: string;
|
|
35
|
-
tenantId?: string;
|
|
36
19
|
caller: AsyncCaller;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
protected ensureTenantId(): Promise<string>;
|
|
20
|
+
timeout: number;
|
|
21
|
+
constructor({ exampleId, sessionName, callerParams, timeout, }?: LangChainTracerFields);
|
|
40
22
|
private _convertToCreate;
|
|
41
23
|
protected persistRun(run: Run): Promise<void>;
|
|
42
24
|
}
|
|
@@ -2,7 +2,7 @@ import { AsyncCaller } from "../../util/async_caller.js";
|
|
|
2
2
|
import { getEnvironmentVariable, getRuntimeEnvironment, } from "../../util/env.js";
|
|
3
3
|
import { BaseTracer } from "./tracer.js";
|
|
4
4
|
export class LangChainTracer extends BaseTracer {
|
|
5
|
-
constructor({ exampleId,
|
|
5
|
+
constructor({ exampleId, sessionName, callerParams, timeout, } = {}) {
|
|
6
6
|
super();
|
|
7
7
|
Object.defineProperty(this, "name", {
|
|
8
8
|
enumerable: true,
|
|
@@ -30,93 +30,35 @@ export class LangChainTracer extends BaseTracer {
|
|
|
30
30
|
writable: true,
|
|
31
31
|
value: void 0
|
|
32
32
|
});
|
|
33
|
-
Object.defineProperty(this, "sessionExtra", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: void 0
|
|
38
|
-
});
|
|
39
|
-
Object.defineProperty(this, "session", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
configurable: true,
|
|
42
|
-
writable: true,
|
|
43
|
-
value: void 0
|
|
44
|
-
});
|
|
45
33
|
Object.defineProperty(this, "exampleId", {
|
|
46
34
|
enumerable: true,
|
|
47
35
|
configurable: true,
|
|
48
36
|
writable: true,
|
|
49
37
|
value: void 0
|
|
50
38
|
});
|
|
51
|
-
Object.defineProperty(this, "
|
|
39
|
+
Object.defineProperty(this, "caller", {
|
|
52
40
|
enumerable: true,
|
|
53
41
|
configurable: true,
|
|
54
42
|
writable: true,
|
|
55
43
|
value: void 0
|
|
56
44
|
});
|
|
57
|
-
Object.defineProperty(this, "
|
|
45
|
+
Object.defineProperty(this, "timeout", {
|
|
58
46
|
enumerable: true,
|
|
59
47
|
configurable: true,
|
|
60
48
|
writable: true,
|
|
61
|
-
value:
|
|
49
|
+
value: 5000
|
|
62
50
|
});
|
|
63
51
|
const apiKey = getEnvironmentVariable("LANGCHAIN_API_KEY");
|
|
64
52
|
if (apiKey) {
|
|
65
53
|
this.headers["x-api-key"] = apiKey;
|
|
66
54
|
}
|
|
67
|
-
this.tenantId = tenantId ?? getEnvironmentVariable("LANGCHAIN_TENANT_ID");
|
|
68
55
|
this.sessionName =
|
|
69
|
-
sessionName ?? getEnvironmentVariable("LANGCHAIN_SESSION")
|
|
70
|
-
this.sessionExtra = sessionExtra;
|
|
56
|
+
sessionName ?? getEnvironmentVariable("LANGCHAIN_SESSION");
|
|
71
57
|
this.exampleId = exampleId;
|
|
72
|
-
this.
|
|
73
|
-
|
|
74
|
-
async ensureSession() {
|
|
75
|
-
if (this.session) {
|
|
76
|
-
return this.session;
|
|
77
|
-
}
|
|
78
|
-
const tenantId = await this.ensureTenantId();
|
|
79
|
-
const endpoint = `${this.endpoint}/sessions?upsert=true`;
|
|
80
|
-
const res = await this.caller.call(fetch, endpoint, {
|
|
81
|
-
method: "POST",
|
|
82
|
-
headers: this.headers,
|
|
83
|
-
body: JSON.stringify({
|
|
84
|
-
name: this.sessionName,
|
|
85
|
-
tenant_id: tenantId,
|
|
86
|
-
extra: this.sessionExtra,
|
|
87
|
-
}),
|
|
88
|
-
});
|
|
89
|
-
if (!res.ok) {
|
|
90
|
-
const body = await res.text();
|
|
91
|
-
throw new Error(`Failed to create session: ${res.status} ${res.statusText} ${body}`);
|
|
92
|
-
}
|
|
93
|
-
const session = await res.json();
|
|
94
|
-
this.session = session;
|
|
95
|
-
return session;
|
|
96
|
-
}
|
|
97
|
-
async ensureTenantId() {
|
|
98
|
-
if (this.tenantId) {
|
|
99
|
-
return this.tenantId;
|
|
100
|
-
}
|
|
101
|
-
const endpoint = `${this.endpoint}/tenants`;
|
|
102
|
-
const response = await this.caller.call(fetch, endpoint, {
|
|
103
|
-
method: "GET",
|
|
104
|
-
headers: this.headers,
|
|
105
|
-
});
|
|
106
|
-
if (!response.ok) {
|
|
107
|
-
const body = await response.text();
|
|
108
|
-
throw new Error(`Failed to fetch tenant ID: ${response.status} ${response.statusText} ${body}`);
|
|
109
|
-
}
|
|
110
|
-
const tenants = await response.json();
|
|
111
|
-
if (!tenants || tenants.length === 0) {
|
|
112
|
-
throw new Error(`No tenants found for endpoint ${endpoint}`);
|
|
113
|
-
}
|
|
114
|
-
const tenantId = tenants[0].id;
|
|
115
|
-
this.tenantId = tenantId;
|
|
116
|
-
return tenantId;
|
|
58
|
+
this.timeout = timeout ?? this.timeout;
|
|
59
|
+
this.caller = new AsyncCaller(callerParams ?? { maxRetries: 2 });
|
|
117
60
|
}
|
|
118
61
|
async _convertToCreate(run, example_id = undefined) {
|
|
119
|
-
const session = await this.ensureSession();
|
|
120
62
|
const runExtra = run.extra ?? {};
|
|
121
63
|
runExtra.runtime = await getRuntimeEnvironment();
|
|
122
64
|
const persistedRun = {
|
|
@@ -132,7 +74,7 @@ export class LangChainTracer extends BaseTracer {
|
|
|
132
74
|
error: run.error,
|
|
133
75
|
inputs: run.inputs,
|
|
134
76
|
outputs: run.outputs ?? {},
|
|
135
|
-
|
|
77
|
+
session_name: this.sessionName,
|
|
136
78
|
child_runs: await Promise.all(run.child_runs.map((child_run) => this._convertToCreate(child_run))),
|
|
137
79
|
};
|
|
138
80
|
return persistedRun;
|
|
@@ -144,9 +86,12 @@ export class LangChainTracer extends BaseTracer {
|
|
|
144
86
|
method: "POST",
|
|
145
87
|
headers: this.headers,
|
|
146
88
|
body: JSON.stringify(persistedRun),
|
|
89
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
147
90
|
});
|
|
91
|
+
// consume the response body to release the connection
|
|
92
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
93
|
+
const body = await response.text();
|
|
148
94
|
if (!response.ok) {
|
|
149
|
-
const body = await response.text();
|
|
150
95
|
throw new Error(`Failed to persist run: ${response.status} ${response.statusText} ${body}`);
|
|
151
96
|
}
|
|
152
97
|
}
|
|
@@ -63,16 +63,34 @@ class ConversationalRetrievalQAChain extends base_js_1.BaseChain {
|
|
|
63
63
|
this.returnSourceDocuments =
|
|
64
64
|
fields.returnSourceDocuments ?? this.returnSourceDocuments;
|
|
65
65
|
}
|
|
66
|
+
static getChatHistoryString(chatHistory) {
|
|
67
|
+
if (Array.isArray(chatHistory)) {
|
|
68
|
+
return chatHistory
|
|
69
|
+
.map((chatMessage) => {
|
|
70
|
+
if (chatMessage._getType() === "human") {
|
|
71
|
+
return `Human: ${chatMessage.text}`;
|
|
72
|
+
}
|
|
73
|
+
else if (chatMessage._getType() === "ai") {
|
|
74
|
+
return `Assistant: ${chatMessage.text}`;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return `${chatMessage.text}`;
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
.join("\n");
|
|
81
|
+
}
|
|
82
|
+
return chatHistory;
|
|
83
|
+
}
|
|
66
84
|
/** @ignore */
|
|
67
85
|
async _call(values, runManager) {
|
|
68
86
|
if (!(this.inputKey in values)) {
|
|
69
87
|
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
70
88
|
}
|
|
71
89
|
if (!(this.chatHistoryKey in values)) {
|
|
72
|
-
throw new Error(`
|
|
90
|
+
throw new Error(`Chat history key ${this.chatHistoryKey} not found.`);
|
|
73
91
|
}
|
|
74
92
|
const question = values[this.inputKey];
|
|
75
|
-
const chatHistory = values[this.chatHistoryKey];
|
|
93
|
+
const chatHistory = ConversationalRetrievalQAChain.getChatHistoryString(values[this.chatHistoryKey]);
|
|
76
94
|
let newQuestion = question;
|
|
77
95
|
if (chatHistory.length > 0) {
|
|
78
96
|
const result = await this.questionGeneratorChain.call({
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { BaseLanguageModel } from "../base_language/index.js";
|
|
2
2
|
import { SerializedChatVectorDBQAChain } from "./serde.js";
|
|
3
|
-
import { ChainValues, BaseRetriever } from "../schema/index.js";
|
|
3
|
+
import { ChainValues, BaseRetriever, BaseChatMessage } from "../schema/index.js";
|
|
4
4
|
import { BaseChain, ChainInputs } from "./base.js";
|
|
5
5
|
import { LLMChain } from "./llm_chain.js";
|
|
6
6
|
import { QAChainParams } from "./question_answering/load.js";
|
|
7
7
|
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
8
8
|
export type LoadValues = Record<string, any>;
|
|
9
|
-
export interface ConversationalRetrievalQAChainInput extends
|
|
9
|
+
export interface ConversationalRetrievalQAChainInput extends ChainInputs {
|
|
10
10
|
retriever: BaseRetriever;
|
|
11
11
|
combineDocumentsChain: BaseChain;
|
|
12
12
|
questionGeneratorChain: LLMChain;
|
|
@@ -23,6 +23,7 @@ export declare class ConversationalRetrievalQAChain extends BaseChain implements
|
|
|
23
23
|
questionGeneratorChain: LLMChain;
|
|
24
24
|
returnSourceDocuments: boolean;
|
|
25
25
|
constructor(fields: ConversationalRetrievalQAChainInput);
|
|
26
|
+
static getChatHistoryString(chatHistory: string | BaseChatMessage[]): string;
|
|
26
27
|
/** @ignore */
|
|
27
28
|
_call(values: ChainValues, runManager?: CallbackManagerForChainRun): Promise<ChainValues>;
|
|
28
29
|
_chainType(): string;
|
|
@@ -60,16 +60,34 @@ export class ConversationalRetrievalQAChain extends BaseChain {
|
|
|
60
60
|
this.returnSourceDocuments =
|
|
61
61
|
fields.returnSourceDocuments ?? this.returnSourceDocuments;
|
|
62
62
|
}
|
|
63
|
+
static getChatHistoryString(chatHistory) {
|
|
64
|
+
if (Array.isArray(chatHistory)) {
|
|
65
|
+
return chatHistory
|
|
66
|
+
.map((chatMessage) => {
|
|
67
|
+
if (chatMessage._getType() === "human") {
|
|
68
|
+
return `Human: ${chatMessage.text}`;
|
|
69
|
+
}
|
|
70
|
+
else if (chatMessage._getType() === "ai") {
|
|
71
|
+
return `Assistant: ${chatMessage.text}`;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
return `${chatMessage.text}`;
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
.join("\n");
|
|
78
|
+
}
|
|
79
|
+
return chatHistory;
|
|
80
|
+
}
|
|
63
81
|
/** @ignore */
|
|
64
82
|
async _call(values, runManager) {
|
|
65
83
|
if (!(this.inputKey in values)) {
|
|
66
84
|
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
67
85
|
}
|
|
68
86
|
if (!(this.chatHistoryKey in values)) {
|
|
69
|
-
throw new Error(`
|
|
87
|
+
throw new Error(`Chat history key ${this.chatHistoryKey} not found.`);
|
|
70
88
|
}
|
|
71
89
|
const question = values[this.inputKey];
|
|
72
|
-
const chatHistory = values[this.chatHistoryKey];
|
|
90
|
+
const chatHistory = ConversationalRetrievalQAChain.getChatHistoryString(values[this.chatHistoryKey]);
|
|
73
91
|
let newQuestion = question;
|
|
74
92
|
if (chatHistory.length > 0) {
|
|
75
93
|
const result = await this.questionGeneratorChain.call({
|
|
@@ -9,33 +9,10 @@ const env_js_1 = require("../util/env.cjs");
|
|
|
9
9
|
const isLocalhost = (url) => {
|
|
10
10
|
const strippedUrl = url.replace("http://", "").replace("https://", "");
|
|
11
11
|
const hostname = strippedUrl.split("/")[0].split(":")[0];
|
|
12
|
-
return (hostname === "localhost" ||
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const caller = new async_caller_js_1.AsyncCaller(callerOptions ?? {});
|
|
17
|
-
const url = `${apiUrl}/tenants`;
|
|
18
|
-
let response;
|
|
19
|
-
try {
|
|
20
|
-
response = await caller.call(fetch, url, {
|
|
21
|
-
method: "GET",
|
|
22
|
-
headers: apiKey ? { "x-api-key": apiKey } : undefined,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
catch (err) {
|
|
26
|
-
throw new Error("Unable to get seeded tenant ID. Please manually provide.");
|
|
27
|
-
}
|
|
28
|
-
if (!response.ok) {
|
|
29
|
-
throw new Error(`Failed to fetch seeded tenant ID: ${response.status} ${response.statusText}`);
|
|
30
|
-
}
|
|
31
|
-
const tenants = await response.json();
|
|
32
|
-
if (!Array.isArray(tenants)) {
|
|
33
|
-
throw new Error(`Expected tenants GET request to return an array, but got ${tenants}`);
|
|
34
|
-
}
|
|
35
|
-
if (tenants.length === 0) {
|
|
36
|
-
throw new Error("No seeded tenant found");
|
|
37
|
-
}
|
|
38
|
-
return tenants[0].id;
|
|
12
|
+
return (hostname === "localhost" ||
|
|
13
|
+
hostname === "127.0.0.1" ||
|
|
14
|
+
hostname === "::1" ||
|
|
15
|
+
hostname === "0.0.0.0");
|
|
39
16
|
};
|
|
40
17
|
const stringifyError = (err) => {
|
|
41
18
|
let result;
|
|
@@ -96,39 +73,29 @@ class LangChainPlusClient {
|
|
|
96
73
|
writable: true,
|
|
97
74
|
value: (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") || "http://localhost:1984"
|
|
98
75
|
});
|
|
99
|
-
Object.defineProperty(this, "
|
|
76
|
+
Object.defineProperty(this, "caller", {
|
|
100
77
|
enumerable: true,
|
|
101
78
|
configurable: true,
|
|
102
79
|
writable: true,
|
|
103
80
|
value: void 0
|
|
104
81
|
});
|
|
105
|
-
Object.defineProperty(this, "
|
|
82
|
+
Object.defineProperty(this, "timeout", {
|
|
106
83
|
enumerable: true,
|
|
107
84
|
configurable: true,
|
|
108
85
|
writable: true,
|
|
109
|
-
value:
|
|
86
|
+
value: 10000
|
|
110
87
|
});
|
|
111
88
|
this.apiUrl = config.apiUrl ?? this.apiUrl;
|
|
112
89
|
this.apiKey = config.apiKey;
|
|
113
|
-
const tenantId = config.tenantId ?? (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_TENANT_ID");
|
|
114
|
-
if (tenantId === undefined) {
|
|
115
|
-
throw new Error("No tenant ID provided and no LANGCHAIN_TENANT_ID env var");
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
this.tenantId = tenantId;
|
|
119
|
-
}
|
|
120
90
|
this.validateApiKeyIfHosted();
|
|
91
|
+
this.timeout = config.timeout ?? this.timeout;
|
|
121
92
|
this.caller = new async_caller_js_1.AsyncCaller(config.callerOptions ?? {});
|
|
122
93
|
}
|
|
123
94
|
static async create(config = {}) {
|
|
124
95
|
const apiUrl_ = config.apiUrl ??
|
|
125
96
|
((0, env_js_1.getEnvironmentVariable)("LANGCHAIN_ENDPOINT") || "http://localhost:1984");
|
|
126
97
|
const apiKey_ = config.apiKey ?? (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_API_KEY");
|
|
127
|
-
const tenantId_ = config.tenantId ??
|
|
128
|
-
((0, env_js_1.getEnvironmentVariable)("LANGCHAIN_TENANT_ID") ||
|
|
129
|
-
(await getSeededTenantId(apiUrl_, { apiKey: apiKey_ })));
|
|
130
98
|
return new LangChainPlusClient({
|
|
131
|
-
tenantId: tenantId_,
|
|
132
99
|
apiKey: apiKey_,
|
|
133
100
|
apiUrl: apiUrl_,
|
|
134
101
|
});
|
|
@@ -146,25 +113,28 @@ class LangChainPlusClient {
|
|
|
146
113
|
}
|
|
147
114
|
return headers;
|
|
148
115
|
}
|
|
149
|
-
get queryParams() {
|
|
150
|
-
return new URLSearchParams({ tenant_id: this.tenantId });
|
|
151
|
-
}
|
|
152
116
|
async _get(path, queryParams) {
|
|
153
|
-
const params =
|
|
117
|
+
const params = new URLSearchParams();
|
|
154
118
|
if (queryParams) {
|
|
155
119
|
queryParams.forEach((value, key) => {
|
|
156
120
|
params.append(key, value);
|
|
157
121
|
});
|
|
158
122
|
}
|
|
159
|
-
const url =
|
|
123
|
+
const url = params.toString()
|
|
124
|
+
? `${this.apiUrl}${path}?${params.toString()}`
|
|
125
|
+
: `${this.apiUrl}${path}`;
|
|
160
126
|
const response = await this.caller.call(fetch, url, {
|
|
161
127
|
method: "GET",
|
|
162
128
|
headers: this.headers,
|
|
129
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
163
130
|
});
|
|
131
|
+
// consume the response body to release the connection
|
|
132
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
133
|
+
const json = await response.json();
|
|
164
134
|
if (!response.ok) {
|
|
165
135
|
throw new Error(`Failed to fetch ${path}: ${response.status} ${response.statusText}`);
|
|
166
136
|
}
|
|
167
|
-
return
|
|
137
|
+
return json;
|
|
168
138
|
}
|
|
169
139
|
async readRun(runId) {
|
|
170
140
|
return await this._get(`/runs/${runId}`);
|
|
@@ -229,7 +199,6 @@ class LangChainPlusClient {
|
|
|
229
199
|
formData.append("file", csvFile, fileName);
|
|
230
200
|
formData.append("input_keys", inputKeys.join(","));
|
|
231
201
|
formData.append("output_keys", outputKeys.join(","));
|
|
232
|
-
formData.append("tenant_id", this.tenantId);
|
|
233
202
|
if (description) {
|
|
234
203
|
formData.append("description", description);
|
|
235
204
|
}
|
|
@@ -237,15 +206,17 @@ class LangChainPlusClient {
|
|
|
237
206
|
method: "POST",
|
|
238
207
|
headers: this.headers,
|
|
239
208
|
body: formData,
|
|
209
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
240
210
|
});
|
|
211
|
+
// consume the response body to release the connection
|
|
212
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
213
|
+
const result = await response.json();
|
|
241
214
|
if (!response.ok) {
|
|
242
|
-
const result = await response.json();
|
|
243
215
|
if (result.detail && result.detail.includes("already exists")) {
|
|
244
216
|
throw new Error(`Dataset ${fileName} already exists`);
|
|
245
217
|
}
|
|
246
218
|
throw new Error(`Failed to upload CSV: ${response.status} ${response.statusText}`);
|
|
247
219
|
}
|
|
248
|
-
const result = await response.json();
|
|
249
220
|
return result;
|
|
250
221
|
}
|
|
251
222
|
async createDataset(name, { description }) {
|
|
@@ -255,17 +226,18 @@ class LangChainPlusClient {
|
|
|
255
226
|
body: JSON.stringify({
|
|
256
227
|
name,
|
|
257
228
|
description,
|
|
258
|
-
tenant_id: this.tenantId,
|
|
259
229
|
}),
|
|
230
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
260
231
|
});
|
|
232
|
+
// consume the response body to release the connection
|
|
233
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
234
|
+
const result = await response.json();
|
|
261
235
|
if (!response.ok) {
|
|
262
|
-
const result = await response.json();
|
|
263
236
|
if (result.detail && result.detail.includes("already exists")) {
|
|
264
237
|
throw new Error(`Dataset ${name} already exists`);
|
|
265
238
|
}
|
|
266
239
|
throw new Error(`Failed to create dataset ${response.status} ${response.statusText}`);
|
|
267
240
|
}
|
|
268
|
-
const result = await response.json();
|
|
269
241
|
return result;
|
|
270
242
|
}
|
|
271
243
|
async readDataset({ datasetId, datasetName, }) {
|
|
@@ -325,11 +297,14 @@ class LangChainPlusClient {
|
|
|
325
297
|
const response = await this.caller.call(fetch, this.apiUrl + path, {
|
|
326
298
|
method: "DELETE",
|
|
327
299
|
headers: this.headers,
|
|
300
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
328
301
|
});
|
|
302
|
+
// consume the response body to release the connection
|
|
303
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
304
|
+
const results = await response.json();
|
|
329
305
|
if (!response.ok) {
|
|
330
306
|
throw new Error(`Failed to delete ${path}: ${response.status} ${response.statusText}`);
|
|
331
307
|
}
|
|
332
|
-
const results = await response.json();
|
|
333
308
|
return results;
|
|
334
309
|
}
|
|
335
310
|
async createExample(inputs, outputs, { datasetId, datasetName, createdAt, }) {
|
|
@@ -355,11 +330,14 @@ class LangChainPlusClient {
|
|
|
355
330
|
method: "POST",
|
|
356
331
|
headers: { ...this.headers, "Content-Type": "application/json" },
|
|
357
332
|
body: JSON.stringify(data),
|
|
333
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
358
334
|
});
|
|
335
|
+
// consume the response body to release the connection
|
|
336
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
337
|
+
const result = await response.json();
|
|
359
338
|
if (!response.ok) {
|
|
360
339
|
throw new Error(`Failed to create example: ${response.status} ${response.statusText}`);
|
|
361
340
|
}
|
|
362
|
-
const result = await response.json();
|
|
363
341
|
return result;
|
|
364
342
|
}
|
|
365
343
|
async readExample(exampleId) {
|
|
@@ -392,11 +370,14 @@ class LangChainPlusClient {
|
|
|
392
370
|
const response = await this.caller.call(fetch, this.apiUrl + path, {
|
|
393
371
|
method: "DELETE",
|
|
394
372
|
headers: this.headers,
|
|
373
|
+
signal: AbortSignal.timeout(this.timeout),
|
|
395
374
|
});
|
|
375
|
+
// consume the response body to release the connection
|
|
376
|
+
// https://undici.nodejs.org/#/?id=garbage-collection
|
|
377
|
+
const result = await response.json();
|
|
396
378
|
if (!response.ok) {
|
|
397
379
|
throw new Error(`Failed to delete ${path}: ${response.status} ${response.statusText}`);
|
|
398
380
|
}
|
|
399
|
-
const result = await response.json();
|
|
400
381
|
return result;
|
|
401
382
|
}
|
|
402
383
|
async runLLM(example, tracer, llm, { numRepetitions = 1 }) {
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { BaseRun, Run, RunType } from "../callbacks/handlers/tracer.js";
|
|
2
|
-
import { LangChainTracer
|
|
2
|
+
import { LangChainTracer } 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";
|
|
6
6
|
import { BaseLLM } from "../llms/base.js";
|
|
7
7
|
import { BaseChatModel } from "../chat_models/base.js";
|
|
8
8
|
import { AsyncCallerParams } from "../util/async_caller.js";
|
|
9
|
+
export interface TracerSession {
|
|
10
|
+
id: string;
|
|
11
|
+
tenant_id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
}
|
|
9
14
|
export interface RunResult extends BaseRun {
|
|
10
15
|
name: string;
|
|
11
16
|
session_id: string;
|
|
@@ -57,22 +62,20 @@ export declare function isChain(llm: BaseLanguageModel | (() => Promise<BaseChai
|
|
|
57
62
|
export declare class LangChainPlusClient {
|
|
58
63
|
private apiKey?;
|
|
59
64
|
private apiUrl;
|
|
60
|
-
private tenantId;
|
|
61
65
|
private caller;
|
|
66
|
+
private timeout;
|
|
62
67
|
constructor(config: {
|
|
63
|
-
tenantId?: string;
|
|
64
68
|
apiUrl?: string;
|
|
65
69
|
apiKey?: string;
|
|
70
|
+
timeout?: number;
|
|
66
71
|
callerOptions?: AsyncCallerParams;
|
|
67
72
|
});
|
|
68
73
|
static create(config?: {
|
|
69
74
|
apiUrl?: string;
|
|
70
75
|
apiKey?: string;
|
|
71
|
-
tenantId?: string;
|
|
72
76
|
}): Promise<LangChainPlusClient>;
|
|
73
77
|
private validateApiKeyIfHosted;
|
|
74
78
|
private get headers();
|
|
75
|
-
private get queryParams();
|
|
76
79
|
private _get;
|
|
77
80
|
readRun(runId: string): Promise<Run>;
|
|
78
81
|
listRuns({ sessionId, sessionName, executionOrder, runType, error, }: ListRunsParams): Promise<Run[]>;
|