langchain 0.0.75 → 0.0.76

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.
Files changed (114) hide show
  1. package/dist/base_language/count_tokens.cjs +5 -21
  2. package/dist/base_language/count_tokens.d.ts +1 -6
  3. package/dist/base_language/count_tokens.js +4 -19
  4. package/dist/base_language/index.cjs +10 -24
  5. package/dist/base_language/index.d.ts +3 -2
  6. package/dist/base_language/index.js +11 -25
  7. package/dist/cache/redis.d.ts +3 -1
  8. package/dist/callbacks/base.d.ts +4 -4
  9. package/dist/callbacks/handlers/initialize.cjs +2 -9
  10. package/dist/callbacks/handlers/initialize.d.ts +1 -1
  11. package/dist/callbacks/handlers/initialize.js +2 -9
  12. package/dist/callbacks/handlers/tracer.cjs +11 -22
  13. package/dist/callbacks/handlers/tracer.d.ts +2 -2
  14. package/dist/callbacks/handlers/tracer.js +11 -22
  15. package/dist/callbacks/handlers/tracer_langchain.cjs +52 -91
  16. package/dist/callbacks/handlers/tracer_langchain.d.ts +12 -9
  17. package/dist/callbacks/handlers/tracer_langchain.js +52 -68
  18. package/dist/callbacks/handlers/tracer_langchain_v1.cjs +5 -1
  19. package/dist/callbacks/handlers/tracer_langchain_v1.js +5 -1
  20. package/dist/callbacks/manager.cjs +10 -10
  21. package/dist/callbacks/manager.d.ts +2 -2
  22. package/dist/callbacks/manager.js +10 -10
  23. package/dist/chains/question_answering/map_reduce_prompts.cjs +5 -5
  24. package/dist/chains/question_answering/map_reduce_prompts.d.ts +1 -1
  25. package/dist/chains/question_answering/map_reduce_prompts.js +1 -1
  26. package/dist/chains/question_answering/refine_prompts.cjs +5 -5
  27. package/dist/chains/question_answering/refine_prompts.d.ts +1 -1
  28. package/dist/chains/question_answering/refine_prompts.js +1 -1
  29. package/dist/chains/question_answering/stuff_prompts.cjs +2 -2
  30. package/dist/chains/question_answering/stuff_prompts.d.ts +1 -1
  31. package/dist/chains/question_answering/stuff_prompts.js +1 -1
  32. package/dist/chains/sequential_chain.cjs +2 -2
  33. package/dist/chains/sequential_chain.d.ts +2 -2
  34. package/dist/chains/sequential_chain.js +2 -2
  35. package/dist/chains/sql_db/sql_db_prompt.cjs +20 -1
  36. package/dist/chains/sql_db/sql_db_prompt.d.ts +1 -0
  37. package/dist/chains/sql_db/sql_db_prompt.js +19 -0
  38. package/dist/chat_models/base.cjs +17 -1
  39. package/dist/chat_models/base.d.ts +6 -0
  40. package/dist/chat_models/base.js +18 -2
  41. package/dist/chat_models/openai.cjs +2 -2
  42. package/dist/chat_models/openai.js +2 -2
  43. package/dist/client/langchainplus.cjs +90 -29
  44. package/dist/client/langchainplus.d.ts +7 -5
  45. package/dist/client/langchainplus.js +90 -29
  46. package/dist/document_loaders/fs/unstructured.cjs +8 -0
  47. package/dist/document_loaders/fs/unstructured.d.ts +3 -0
  48. package/dist/document_loaders/fs/unstructured.js +8 -0
  49. package/dist/document_loaders/web/apify_dataset.cjs +64 -0
  50. package/dist/document_loaders/web/apify_dataset.d.ts +28 -0
  51. package/dist/document_loaders/web/apify_dataset.js +60 -0
  52. package/dist/embeddings/openai.cjs +2 -2
  53. package/dist/embeddings/openai.js +2 -2
  54. package/dist/llms/base.cjs +18 -1
  55. package/dist/llms/base.d.ts +7 -1
  56. package/dist/llms/base.js +19 -2
  57. package/dist/llms/openai-chat.cjs +2 -2
  58. package/dist/llms/openai-chat.js +2 -2
  59. package/dist/llms/openai.cjs +2 -2
  60. package/dist/llms/openai.js +2 -2
  61. package/dist/memory/base.cjs +9 -1
  62. package/dist/memory/base.d.ts +1 -0
  63. package/dist/memory/base.js +7 -0
  64. package/dist/memory/entity_memory.cjs +151 -0
  65. package/dist/memory/entity_memory.d.ts +35 -0
  66. package/dist/memory/entity_memory.js +147 -0
  67. package/dist/memory/index.cjs +5 -1
  68. package/dist/memory/index.d.ts +2 -0
  69. package/dist/memory/index.js +2 -0
  70. package/dist/memory/prompt.cjs +84 -1
  71. package/dist/memory/prompt.d.ts +6 -0
  72. package/dist/memory/prompt.js +83 -0
  73. package/dist/memory/stores/entity/in_memory.cjs +32 -0
  74. package/dist/memory/stores/entity/in_memory.d.ts +10 -0
  75. package/dist/memory/stores/entity/in_memory.js +28 -0
  76. package/dist/prompts/index.cjs +6 -1
  77. package/dist/prompts/index.d.ts +1 -0
  78. package/dist/prompts/index.js +1 -0
  79. package/dist/{chains/prompt_selector.d.ts → prompts/selectors/conditional.d.ts} +4 -4
  80. package/dist/schema/index.cjs +13 -1
  81. package/dist/schema/index.d.ts +17 -0
  82. package/dist/schema/index.js +11 -0
  83. package/dist/stores/message/dynamodb.cjs +8 -6
  84. package/dist/stores/message/dynamodb.js +8 -6
  85. package/dist/stores/message/redis.cjs +5 -3
  86. package/dist/stores/message/redis.js +5 -3
  87. package/dist/stores/message/utils.cjs +30 -15
  88. package/dist/stores/message/utils.d.ts +4 -2
  89. package/dist/stores/message/utils.js +28 -14
  90. package/dist/text_splitter.cjs +3 -23
  91. package/dist/text_splitter.d.ts +1 -3
  92. package/dist/text_splitter.js +3 -23
  93. package/dist/tools/webbrowser.cjs +5 -7
  94. package/dist/tools/webbrowser.js +3 -5
  95. package/dist/util/env.cjs +39 -7
  96. package/dist/util/env.d.ts +19 -0
  97. package/dist/util/env.js +32 -6
  98. package/dist/util/sql_utils.cjs +18 -0
  99. package/dist/util/sql_utils.js +19 -1
  100. package/dist/util/tiktoken.cjs +26 -0
  101. package/dist/util/tiktoken.d.ts +9 -0
  102. package/dist/util/tiktoken.js +21 -0
  103. package/dist/vectorstores/redis.cjs +236 -0
  104. package/dist/vectorstores/redis.d.ts +80 -0
  105. package/dist/vectorstores/redis.js +232 -0
  106. package/document_loaders/web/apify_dataset.cjs +1 -0
  107. package/document_loaders/web/apify_dataset.d.ts +1 -0
  108. package/document_loaders/web/apify_dataset.js +1 -0
  109. package/package.json +25 -5
  110. package/vectorstores/redis.cjs +1 -0
  111. package/vectorstores/redis.d.ts +1 -0
  112. package/vectorstores/redis.js +1 -0
  113. /package/dist/{chains/prompt_selector.cjs → prompts/selectors/conditional.cjs} +0 -0
  114. /package/dist/{chains/prompt_selector.js → prompts/selectors/conditional.js} +0 -0
@@ -1,33 +1,10 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
3
  exports.LangChainTracer = void 0;
27
- const uuid = __importStar(require("uuid"));
4
+ const env_js_1 = require("../../util/env.cjs");
28
5
  const tracer_js_1 = require("./tracer.cjs");
29
6
  class LangChainTracer extends tracer_js_1.BaseTracer {
30
- constructor(exampleId, tenantId) {
7
+ constructor({ exampleId, tenantId, sessionName, sessionExtra, } = {}) {
31
8
  super();
32
9
  Object.defineProperty(this, "name", {
33
10
  enumerable: true,
@@ -52,6 +29,18 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
52
29
  "Content-Type": "application/json",
53
30
  }
54
31
  });
32
+ Object.defineProperty(this, "sessionName", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ Object.defineProperty(this, "sessionExtra", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
55
44
  Object.defineProperty(this, "session", {
56
45
  enumerable: true,
57
46
  configurable: true,
@@ -81,57 +70,51 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
81
70
  ? // eslint-disable-next-line no-process-env
82
71
  process.env?.LANGCHAIN_TENANT_ID
83
72
  : undefined);
73
+ this.sessionName =
74
+ sessionName ??
75
+ (typeof process !== "undefined"
76
+ ? // eslint-disable-next-line no-process-env
77
+ process.env?.LANGCHAIN_SESSION
78
+ : undefined) ??
79
+ "default";
80
+ this.sessionExtra = sessionExtra;
84
81
  this.exampleId = exampleId;
85
82
  }
86
- async newSession(sessionName) {
87
- const tenantId = this.tenantId ?? (await this.updateTenantId());
88
- const sessionCreate = {
89
- start_time: Date.now(),
90
- name: sessionName,
91
- tenant_id: tenantId,
92
- };
93
- const session = await this.persistSession(sessionCreate);
94
- this.session = session;
95
- return session;
96
- }
97
- async loadSession(sessionName) {
98
- const endpoint = `${this.endpoint}/sessions?name=${sessionName}`;
99
- return this._handleSessionResponse(endpoint);
100
- }
101
- async loadDefaultSession() {
102
- const endpoint = `${this.endpoint}/sessions?name=default`;
103
- return this._handleSessionResponse(endpoint);
104
- }
105
- async persistSession(sessionCreate) {
106
- const endpoint = `${this.endpoint}/sessions`;
107
- const tenant_id = this.tenantId ?? (await this.updateTenantId());
108
- const response = await fetch(endpoint, {
83
+ async ensureSession() {
84
+ if (this.session) {
85
+ return this.session;
86
+ }
87
+ const tenantId = await this.ensureTenantId();
88
+ const endpoint = `${this.endpoint}/sessions?upsert=true`;
89
+ const res = await fetch(endpoint, {
109
90
  method: "POST",
110
91
  headers: this.headers,
111
- body: JSON.stringify(sessionCreate),
92
+ body: JSON.stringify({
93
+ name: this.sessionName,
94
+ tenant_id: tenantId,
95
+ extra: this.sessionExtra,
96
+ }),
112
97
  });
113
- if (!response.ok) {
114
- if (sessionCreate.name !== undefined) {
115
- return await this.loadSession(sessionCreate.name);
116
- }
117
- else {
118
- return await this.loadDefaultSession();
119
- }
98
+ if (!res.ok) {
99
+ const body = await res.text();
100
+ throw new Error(`Failed to create session: ${res.status} ${res.statusText} ${body}`);
120
101
  }
121
- return {
122
- id: (await response.json()).id,
123
- tenant_id,
124
- ...sessionCreate,
125
- };
102
+ const session = await res.json();
103
+ this.session = session;
104
+ return session;
126
105
  }
127
- async updateTenantId() {
106
+ async ensureTenantId() {
107
+ if (this.tenantId) {
108
+ return this.tenantId;
109
+ }
128
110
  const endpoint = `${this.endpoint}/tenants`;
129
111
  const response = await fetch(endpoint, {
130
112
  method: "GET",
131
113
  headers: this.headers,
132
114
  });
133
115
  if (!response.ok) {
134
- throw new Error(`Failed to fetch tenant ID: ${response.status} ${response.statusText}`);
116
+ const body = await response.text();
117
+ throw new Error(`Failed to fetch tenant ID: ${response.status} ${response.statusText} ${body}`);
135
118
  }
136
119
  const tenants = await response.json();
137
120
  if (!tenants || tenants.length === 0) {
@@ -141,33 +124,10 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
141
124
  this.tenantId = tenantId;
142
125
  return tenantId;
143
126
  }
144
- async _handleSessionResponse(endpoint) {
145
- const tenantId = this.tenantId ?? (await this.updateTenantId());
146
- const configured_endpoint = `${endpoint}&tenant_id=${this.tenantId}`;
147
- const response = await fetch(configured_endpoint, {
148
- method: "GET",
149
- headers: this.headers,
150
- });
151
- let tracerSession;
152
- if (!response.ok) {
153
- throw new Error(`Failed to fetch session: ${response.status} ${response.statusText}`);
154
- }
155
- const resp = (await response.json());
156
- if (resp.length === 0) {
157
- tracerSession = {
158
- id: uuid.v4(),
159
- start_time: Date.now(),
160
- tenant_id: tenantId,
161
- };
162
- this.session = tracerSession;
163
- return tracerSession;
164
- }
165
- [tracerSession] = resp;
166
- this.session = tracerSession;
167
- return tracerSession;
168
- }
169
127
  async _convertToCreate(run, example_id = undefined) {
170
- const session = this.session ?? (await this.loadDefaultSession());
128
+ const session = await this.ensureSession();
129
+ const runExtra = run.extra ?? {};
130
+ runExtra.runtime = await (0, env_js_1.getRuntimeEnvironment)();
171
131
  const persistedRun = {
172
132
  id: run.id,
173
133
  name: run.name,
@@ -175,7 +135,7 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
175
135
  end_time: run.end_time,
176
136
  run_type: run.run_type,
177
137
  reference_example_id: example_id,
178
- extra: run.extra ?? {},
138
+ extra: runExtra,
179
139
  execution_order: run.execution_order,
180
140
  serialized: run.serialized,
181
141
  error: run.error,
@@ -195,7 +155,8 @@ class LangChainTracer extends tracer_js_1.BaseTracer {
195
155
  body: JSON.stringify(persistedRun),
196
156
  });
197
157
  if (!response.ok) {
198
- console.error(`Failed to persist run: ${response.status} ${response.statusText}`);
158
+ const body = await response.text();
159
+ throw new Error(`Failed to persist run: ${response.status} ${response.statusText} ${body}`);
199
160
  }
200
161
  }
201
162
  }
@@ -1,5 +1,4 @@
1
1
  import { BaseTracer, Run, BaseRun } from "./tracer.js";
2
- import { Optional } from "../../types/type-utils.js";
3
2
  export interface RunCreate extends BaseRun {
4
3
  child_runs: this[];
5
4
  session_id: string;
@@ -17,20 +16,24 @@ export interface TracerSessionCreateV2 extends BaseTracerSessionV2 {
17
16
  export interface TracerSession extends BaseTracerSessionV2 {
18
17
  id: string;
19
18
  }
20
- export declare class LangChainTracer extends BaseTracer {
19
+ export interface LangChainTracerFields {
20
+ exampleId?: string;
21
+ tenantId?: string;
22
+ sessionName?: string;
23
+ sessionExtra?: Record<string, unknown>;
24
+ }
25
+ export declare class LangChainTracer extends BaseTracer implements LangChainTracerFields {
21
26
  name: string;
22
27
  protected endpoint: string;
23
28
  protected headers: Record<string, string>;
29
+ sessionName: string;
30
+ sessionExtra?: LangChainTracerFields["sessionExtra"];
24
31
  protected session: TracerSession;
25
32
  exampleId?: string;
26
33
  tenantId?: string;
27
- constructor(exampleId?: string, tenantId?: string);
28
- newSession(sessionName?: string): Promise<TracerSession>;
29
- loadSession(sessionName: string): Promise<TracerSession>;
30
- loadDefaultSession(): Promise<TracerSession>;
31
- protected persistSession(sessionCreate: Optional<BaseTracerSessionV2, "tenant_id">): Promise<TracerSession>;
32
- updateTenantId(): Promise<string>;
33
- protected _handleSessionResponse(endpoint: string): Promise<TracerSession>;
34
+ constructor({ exampleId, tenantId, sessionName, sessionExtra, }?: LangChainTracerFields);
35
+ protected ensureSession(): Promise<TracerSession>;
36
+ protected ensureTenantId(): Promise<string>;
34
37
  private _convertToCreate;
35
38
  protected persistRun(run: Run): Promise<void>;
36
39
  }
@@ -1,7 +1,7 @@
1
- import * as uuid from "uuid";
1
+ import { getRuntimeEnvironment } from "../../util/env.js";
2
2
  import { BaseTracer } from "./tracer.js";
3
3
  export class LangChainTracer extends BaseTracer {
4
- constructor(exampleId, tenantId) {
4
+ constructor({ exampleId, tenantId, sessionName, sessionExtra, } = {}) {
5
5
  super();
6
6
  Object.defineProperty(this, "name", {
7
7
  enumerable: true,
@@ -26,6 +26,18 @@ export class LangChainTracer extends BaseTracer {
26
26
  "Content-Type": "application/json",
27
27
  }
28
28
  });
29
+ Object.defineProperty(this, "sessionName", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
35
+ Object.defineProperty(this, "sessionExtra", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
29
41
  Object.defineProperty(this, "session", {
30
42
  enumerable: true,
31
43
  configurable: true,
@@ -55,57 +67,51 @@ export class LangChainTracer extends BaseTracer {
55
67
  ? // eslint-disable-next-line no-process-env
56
68
  process.env?.LANGCHAIN_TENANT_ID
57
69
  : undefined);
70
+ this.sessionName =
71
+ sessionName ??
72
+ (typeof process !== "undefined"
73
+ ? // eslint-disable-next-line no-process-env
74
+ process.env?.LANGCHAIN_SESSION
75
+ : undefined) ??
76
+ "default";
77
+ this.sessionExtra = sessionExtra;
58
78
  this.exampleId = exampleId;
59
79
  }
60
- async newSession(sessionName) {
61
- const tenantId = this.tenantId ?? (await this.updateTenantId());
62
- const sessionCreate = {
63
- start_time: Date.now(),
64
- name: sessionName,
65
- tenant_id: tenantId,
66
- };
67
- const session = await this.persistSession(sessionCreate);
68
- this.session = session;
69
- return session;
70
- }
71
- async loadSession(sessionName) {
72
- const endpoint = `${this.endpoint}/sessions?name=${sessionName}`;
73
- return this._handleSessionResponse(endpoint);
74
- }
75
- async loadDefaultSession() {
76
- const endpoint = `${this.endpoint}/sessions?name=default`;
77
- return this._handleSessionResponse(endpoint);
78
- }
79
- async persistSession(sessionCreate) {
80
- const endpoint = `${this.endpoint}/sessions`;
81
- const tenant_id = this.tenantId ?? (await this.updateTenantId());
82
- const response = await fetch(endpoint, {
80
+ async ensureSession() {
81
+ if (this.session) {
82
+ return this.session;
83
+ }
84
+ const tenantId = await this.ensureTenantId();
85
+ const endpoint = `${this.endpoint}/sessions?upsert=true`;
86
+ const res = await fetch(endpoint, {
83
87
  method: "POST",
84
88
  headers: this.headers,
85
- body: JSON.stringify(sessionCreate),
89
+ body: JSON.stringify({
90
+ name: this.sessionName,
91
+ tenant_id: tenantId,
92
+ extra: this.sessionExtra,
93
+ }),
86
94
  });
87
- if (!response.ok) {
88
- if (sessionCreate.name !== undefined) {
89
- return await this.loadSession(sessionCreate.name);
90
- }
91
- else {
92
- return await this.loadDefaultSession();
93
- }
95
+ if (!res.ok) {
96
+ const body = await res.text();
97
+ throw new Error(`Failed to create session: ${res.status} ${res.statusText} ${body}`);
94
98
  }
95
- return {
96
- id: (await response.json()).id,
97
- tenant_id,
98
- ...sessionCreate,
99
- };
99
+ const session = await res.json();
100
+ this.session = session;
101
+ return session;
100
102
  }
101
- async updateTenantId() {
103
+ async ensureTenantId() {
104
+ if (this.tenantId) {
105
+ return this.tenantId;
106
+ }
102
107
  const endpoint = `${this.endpoint}/tenants`;
103
108
  const response = await fetch(endpoint, {
104
109
  method: "GET",
105
110
  headers: this.headers,
106
111
  });
107
112
  if (!response.ok) {
108
- throw new Error(`Failed to fetch tenant ID: ${response.status} ${response.statusText}`);
113
+ const body = await response.text();
114
+ throw new Error(`Failed to fetch tenant ID: ${response.status} ${response.statusText} ${body}`);
109
115
  }
110
116
  const tenants = await response.json();
111
117
  if (!tenants || tenants.length === 0) {
@@ -115,33 +121,10 @@ export class LangChainTracer extends BaseTracer {
115
121
  this.tenantId = tenantId;
116
122
  return tenantId;
117
123
  }
118
- async _handleSessionResponse(endpoint) {
119
- const tenantId = this.tenantId ?? (await this.updateTenantId());
120
- const configured_endpoint = `${endpoint}&tenant_id=${this.tenantId}`;
121
- const response = await fetch(configured_endpoint, {
122
- method: "GET",
123
- headers: this.headers,
124
- });
125
- let tracerSession;
126
- if (!response.ok) {
127
- throw new Error(`Failed to fetch session: ${response.status} ${response.statusText}`);
128
- }
129
- const resp = (await response.json());
130
- if (resp.length === 0) {
131
- tracerSession = {
132
- id: uuid.v4(),
133
- start_time: Date.now(),
134
- tenant_id: tenantId,
135
- };
136
- this.session = tracerSession;
137
- return tracerSession;
138
- }
139
- [tracerSession] = resp;
140
- this.session = tracerSession;
141
- return tracerSession;
142
- }
143
124
  async _convertToCreate(run, example_id = undefined) {
144
- const session = this.session ?? (await this.loadDefaultSession());
125
+ const session = await this.ensureSession();
126
+ const runExtra = run.extra ?? {};
127
+ runExtra.runtime = await getRuntimeEnvironment();
145
128
  const persistedRun = {
146
129
  id: run.id,
147
130
  name: run.name,
@@ -149,7 +132,7 @@ export class LangChainTracer extends BaseTracer {
149
132
  end_time: run.end_time,
150
133
  run_type: run.run_type,
151
134
  reference_example_id: example_id,
152
- extra: run.extra ?? {},
135
+ extra: runExtra,
153
136
  execution_order: run.execution_order,
154
137
  serialized: run.serialized,
155
138
  error: run.error,
@@ -169,7 +152,8 @@ export class LangChainTracer extends BaseTracer {
169
152
  body: JSON.stringify(persistedRun),
170
153
  });
171
154
  if (!response.ok) {
172
- console.error(`Failed to persist run: ${response.status} ${response.statusText}`);
155
+ const body = await response.text();
156
+ throw new Error(`Failed to persist run: ${response.status} ${response.statusText} ${body}`);
173
157
  }
174
158
  }
175
159
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LangChainTracerV1 = void 0;
4
+ const base_js_1 = require("../../memory/base.cjs");
4
5
  const tracer_js_1 = require("./tracer.cjs");
5
6
  class LangChainTracerV1 extends tracer_js_1.BaseTracer {
6
7
  constructor() {
@@ -62,6 +63,9 @@ class LangChainTracerV1 extends tracer_js_1.BaseTracer {
62
63
  const serialized = run.serialized;
63
64
  let runResult;
64
65
  if (run.run_type === "llm") {
66
+ const prompts = run.inputs.prompts
67
+ ? run.inputs.prompts
68
+ : run.inputs.messages.map((x) => (0, base_js_1.getBufferString)(x));
65
69
  const llmRun = {
66
70
  uuid: run.id,
67
71
  start_time: run.start_time,
@@ -71,7 +75,7 @@ class LangChainTracerV1 extends tracer_js_1.BaseTracer {
71
75
  serialized,
72
76
  type: run.run_type,
73
77
  session_id: session.id,
74
- prompts: run.inputs.prompts,
78
+ prompts,
75
79
  response: run.outputs,
76
80
  };
77
81
  runResult = llmRun;
@@ -1,3 +1,4 @@
1
+ import { getBufferString } from "../../memory/base.js";
1
2
  import { BaseTracer } from "./tracer.js";
2
3
  export class LangChainTracerV1 extends BaseTracer {
3
4
  constructor() {
@@ -59,6 +60,9 @@ export class LangChainTracerV1 extends BaseTracer {
59
60
  const serialized = run.serialized;
60
61
  let runResult;
61
62
  if (run.run_type === "llm") {
63
+ const prompts = run.inputs.prompts
64
+ ? run.inputs.prompts
65
+ : run.inputs.messages.map((x) => getBufferString(x));
62
66
  const llmRun = {
63
67
  uuid: run.id,
64
68
  start_time: run.start_time,
@@ -68,7 +72,7 @@ export class LangChainTracerV1 extends BaseTracer {
68
72
  serialized,
69
73
  type: run.run_type,
70
74
  session_id: session.id,
71
- prompts: run.inputs.prompts,
75
+ prompts,
72
76
  response: run.outputs,
73
77
  };
74
78
  runResult = llmRun;
@@ -210,11 +210,11 @@ class CallbackManager extends BaseCallbackManager {
210
210
  this.inheritableHandlers = [];
211
211
  this._parentRunId = parentRunId;
212
212
  }
213
- async handleLLMStart(llm, prompts, runId = (0, uuid_1.v4)()) {
213
+ async handleLLMStart(llm, prompts, runId = (0, uuid_1.v4)(), _parentRunId = undefined, extraParams = undefined) {
214
214
  await Promise.all(this.handlers.map(async (handler) => {
215
215
  if (!handler.ignoreLLM) {
216
216
  try {
217
- await handler.handleLLMStart?.(llm, prompts, runId, this._parentRunId);
217
+ await handler.handleLLMStart?.(llm, prompts, runId, this._parentRunId, extraParams);
218
218
  }
219
219
  catch (err) {
220
220
  console.error(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
@@ -223,16 +223,16 @@ class CallbackManager extends BaseCallbackManager {
223
223
  }));
224
224
  return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this._parentRunId);
225
225
  }
226
- async handleChatModelStart(llm, messages, runId = (0, uuid_1.v4)()) {
226
+ async handleChatModelStart(llm, messages, runId = (0, uuid_1.v4)(), _parentRunId = undefined, extraParams = undefined) {
227
227
  let messageStrings;
228
228
  await Promise.all(this.handlers.map(async (handler) => {
229
229
  if (!handler.ignoreLLM) {
230
230
  try {
231
231
  if (handler.handleChatModelStart)
232
- await handler.handleChatModelStart?.(llm, messages, runId, this._parentRunId);
232
+ await handler.handleChatModelStart?.(llm, messages, runId, this._parentRunId, extraParams);
233
233
  else if (handler.handleLLMStart) {
234
234
  messageStrings = messages.map((x) => (0, base_js_2.getBufferString)(x));
235
- await handler.handleLLMStart?.(llm, messageStrings, runId, this._parentRunId);
235
+ await handler.handleLLMStart?.(llm, messageStrings, runId, this._parentRunId, extraParams);
236
236
  }
237
237
  }
238
238
  catch (err) {
@@ -354,14 +354,14 @@ class CallbackManager extends BaseCallbackManager {
354
354
  }
355
355
  if (tracingEnabled &&
356
356
  !callbackManager.handlers.some((handler) => handler.name === "langchain_tracer")) {
357
- const session = typeof process !== "undefined"
358
- ? // eslint-disable-next-line no-process-env
359
- process.env?.LANGCHAIN_SESSION
360
- : undefined;
361
357
  if (tracingV2Enabled) {
362
- callbackManager.addHandler(await (0, initialize_js_1.getTracingV2CallbackHandler)(session), true);
358
+ callbackManager.addHandler(await (0, initialize_js_1.getTracingV2CallbackHandler)(), true);
363
359
  }
364
360
  else {
361
+ const session = typeof process !== "undefined"
362
+ ? // eslint-disable-next-line no-process-env
363
+ process.env?.LANGCHAIN_SESSION
364
+ : undefined;
365
365
  callbackManager.addHandler(await (0, initialize_js_1.getTracingCallbackHandler)(session), true);
366
366
  }
367
367
  }
@@ -47,10 +47,10 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
47
47
  constructor(parentRunId?: string);
48
48
  handleLLMStart(llm: {
49
49
  name: string;
50
- }, prompts: string[], runId?: string): Promise<CallbackManagerForLLMRun>;
50
+ }, prompts: string[], runId?: string, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): Promise<CallbackManagerForLLMRun>;
51
51
  handleChatModelStart(llm: {
52
52
  name: string;
53
- }, messages: BaseChatMessage[][], runId?: string): Promise<CallbackManagerForLLMRun>;
53
+ }, messages: BaseChatMessage[][], runId?: string, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): Promise<CallbackManagerForLLMRun>;
54
54
  handleChainStart(chain: {
55
55
  name: string;
56
56
  }, inputs: ChainValues, runId?: string): Promise<CallbackManagerForChainRun>;
@@ -203,11 +203,11 @@ export class CallbackManager extends BaseCallbackManager {
203
203
  this.inheritableHandlers = [];
204
204
  this._parentRunId = parentRunId;
205
205
  }
206
- async handleLLMStart(llm, prompts, runId = uuidv4()) {
206
+ async handleLLMStart(llm, prompts, runId = uuidv4(), _parentRunId = undefined, extraParams = undefined) {
207
207
  await Promise.all(this.handlers.map(async (handler) => {
208
208
  if (!handler.ignoreLLM) {
209
209
  try {
210
- await handler.handleLLMStart?.(llm, prompts, runId, this._parentRunId);
210
+ await handler.handleLLMStart?.(llm, prompts, runId, this._parentRunId, extraParams);
211
211
  }
212
212
  catch (err) {
213
213
  console.error(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
@@ -216,16 +216,16 @@ export class CallbackManager extends BaseCallbackManager {
216
216
  }));
217
217
  return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this._parentRunId);
218
218
  }
219
- async handleChatModelStart(llm, messages, runId = uuidv4()) {
219
+ async handleChatModelStart(llm, messages, runId = uuidv4(), _parentRunId = undefined, extraParams = undefined) {
220
220
  let messageStrings;
221
221
  await Promise.all(this.handlers.map(async (handler) => {
222
222
  if (!handler.ignoreLLM) {
223
223
  try {
224
224
  if (handler.handleChatModelStart)
225
- await handler.handleChatModelStart?.(llm, messages, runId, this._parentRunId);
225
+ await handler.handleChatModelStart?.(llm, messages, runId, this._parentRunId, extraParams);
226
226
  else if (handler.handleLLMStart) {
227
227
  messageStrings = messages.map((x) => getBufferString(x));
228
- await handler.handleLLMStart?.(llm, messageStrings, runId, this._parentRunId);
228
+ await handler.handleLLMStart?.(llm, messageStrings, runId, this._parentRunId, extraParams);
229
229
  }
230
230
  }
231
231
  catch (err) {
@@ -347,14 +347,14 @@ export class CallbackManager extends BaseCallbackManager {
347
347
  }
348
348
  if (tracingEnabled &&
349
349
  !callbackManager.handlers.some((handler) => handler.name === "langchain_tracer")) {
350
- const session = typeof process !== "undefined"
351
- ? // eslint-disable-next-line no-process-env
352
- process.env?.LANGCHAIN_SESSION
353
- : undefined;
354
350
  if (tracingV2Enabled) {
355
- callbackManager.addHandler(await getTracingV2CallbackHandler(session), true);
351
+ callbackManager.addHandler(await getTracingV2CallbackHandler(), true);
356
352
  }
357
353
  else {
354
+ const session = typeof process !== "undefined"
355
+ ? // eslint-disable-next-line no-process-env
356
+ process.env?.LANGCHAIN_SESSION
357
+ : undefined;
358
358
  callbackManager.addHandler(await getTracingCallbackHandler(session), true);
359
359
  }
360
360
  }
@@ -4,7 +4,7 @@ exports.COMBINE_PROMPT_SELECTOR = exports.COMBINE_PROMPT = exports.COMBINE_QA_PR
4
4
  /* eslint-disable spaced-comment */
5
5
  const prompt_js_1 = require("../../prompts/prompt.cjs");
6
6
  const chat_js_1 = require("../../prompts/chat.cjs");
7
- const prompt_selector_js_1 = require("../prompt_selector.cjs");
7
+ const conditional_js_1 = require("../../prompts/selectors/conditional.cjs");
8
8
  const qa_template = `Use the following portion of a long document to see if any of the text is relevant to answer the question.
9
9
  Return any relevant text verbatim.
10
10
  {context}
@@ -24,8 +24,8 @@ const messages = [
24
24
  const CHAT_QA_PROMPT =
25
25
  /*#__PURE__*/ chat_js_1.ChatPromptTemplate.fromPromptMessages(messages);
26
26
  exports.COMBINE_QA_PROMPT_SELECTOR =
27
- /*#__PURE__*/ new prompt_selector_js_1.ConditionalPromptSelector(exports.DEFAULT_COMBINE_QA_PROMPT, [
28
- [prompt_selector_js_1.isChatModel, CHAT_QA_PROMPT],
27
+ /*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.DEFAULT_COMBINE_QA_PROMPT, [
28
+ [conditional_js_1.isChatModel, CHAT_QA_PROMPT],
29
29
  ]);
30
30
  const combine_prompt = `Given the following extracted parts of a long document and a question, create a final answer.
31
31
  If you don't know the answer, just say that you don't know. Don't try to make up an answer.
@@ -70,6 +70,6 @@ const combine_messages = [
70
70
  const CHAT_COMBINE_PROMPT =
71
71
  /*#__PURE__*/ chat_js_1.ChatPromptTemplate.fromPromptMessages(combine_messages);
72
72
  exports.COMBINE_PROMPT_SELECTOR =
73
- /*#__PURE__*/ new prompt_selector_js_1.ConditionalPromptSelector(exports.COMBINE_PROMPT, [
74
- [prompt_selector_js_1.isChatModel, CHAT_COMBINE_PROMPT],
73
+ /*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.COMBINE_PROMPT, [
74
+ [conditional_js_1.isChatModel, CHAT_COMBINE_PROMPT],
75
75
  ]);
@@ -1,5 +1,5 @@
1
1
  import { PromptTemplate } from "../../prompts/prompt.js";
2
- import { ConditionalPromptSelector } from "../prompt_selector.js";
2
+ import { ConditionalPromptSelector } from "../../prompts/selectors/conditional.js";
3
3
  export declare const DEFAULT_COMBINE_QA_PROMPT: PromptTemplate;
4
4
  export declare const COMBINE_QA_PROMPT_SELECTOR: ConditionalPromptSelector;
5
5
  export declare const COMBINE_PROMPT: PromptTemplate;
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable spaced-comment */
2
2
  import { PromptTemplate } from "../../prompts/prompt.js";
3
3
  import { ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, } from "../../prompts/chat.js";
4
- import { ConditionalPromptSelector, isChatModel } from "../prompt_selector.js";
4
+ import { ConditionalPromptSelector, isChatModel, } from "../../prompts/selectors/conditional.js";
5
5
  const qa_template = `Use the following portion of a long document to see if any of the text is relevant to answer the question.
6
6
  Return any relevant text verbatim.
7
7
  {context}
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QUESTION_PROMPT_SELECTOR = exports.CHAT_QUESTION_PROMPT = exports.DEFAULT_TEXT_QA_PROMPT = exports.DEFAULT_TEXT_QA_PROMPT_TMPL = exports.REFINE_PROMPT_SELECTOR = exports.CHAT_REFINE_PROMPT = exports.DEFAULT_REFINE_PROMPT = exports.DEFAULT_REFINE_PROMPT_TMPL = void 0;
4
4
  /* eslint-disable spaced-comment */
5
5
  const index_js_1 = require("../../prompts/index.cjs");
6
- const prompt_selector_js_1 = require("../prompt_selector.cjs");
6
+ const conditional_js_1 = require("../../prompts/selectors/conditional.cjs");
7
7
  exports.DEFAULT_REFINE_PROMPT_TMPL = `The original question is as follows: {question}
8
8
  We have provided an existing answer: {existing_answer}
9
9
  We have the opportunity to refine the existing answer
@@ -34,8 +34,8 @@ const messages = [
34
34
  exports.CHAT_REFINE_PROMPT =
35
35
  /*#__PURE__*/ index_js_1.ChatPromptTemplate.fromPromptMessages(messages);
36
36
  exports.REFINE_PROMPT_SELECTOR =
37
- /*#__PURE__*/ new prompt_selector_js_1.ConditionalPromptSelector(exports.DEFAULT_REFINE_PROMPT, [
38
- [prompt_selector_js_1.isChatModel, exports.CHAT_REFINE_PROMPT],
37
+ /*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.DEFAULT_REFINE_PROMPT, [
38
+ [conditional_js_1.isChatModel, exports.CHAT_REFINE_PROMPT],
39
39
  ]);
40
40
  exports.DEFAULT_TEXT_QA_PROMPT_TMPL = `Context information is below.
41
41
  ---------------------
@@ -58,6 +58,6 @@ const chat_messages = [
58
58
  exports.CHAT_QUESTION_PROMPT =
59
59
  /*#__PURE__*/ index_js_1.ChatPromptTemplate.fromPromptMessages(chat_messages);
60
60
  exports.QUESTION_PROMPT_SELECTOR =
61
- /*#__PURE__*/ new prompt_selector_js_1.ConditionalPromptSelector(exports.DEFAULT_TEXT_QA_PROMPT, [
62
- [prompt_selector_js_1.isChatModel, exports.CHAT_QUESTION_PROMPT],
61
+ /*#__PURE__*/ new conditional_js_1.ConditionalPromptSelector(exports.DEFAULT_TEXT_QA_PROMPT, [
62
+ [conditional_js_1.isChatModel, exports.CHAT_QUESTION_PROMPT],
63
63
  ]);