langchain 0.0.134 → 0.0.135

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 (42) hide show
  1. package/dist/agents/chat_convo/outputParser.cjs +13 -10
  2. package/dist/agents/chat_convo/outputParser.js +13 -10
  3. package/dist/callbacks/base.d.ts +6 -3
  4. package/dist/callbacks/handlers/tracer.cjs +2 -2
  5. package/dist/callbacks/handlers/tracer.d.ts +2 -2
  6. package/dist/callbacks/handlers/tracer.js +2 -2
  7. package/dist/callbacks/manager.cjs +2 -2
  8. package/dist/callbacks/manager.d.ts +2 -2
  9. package/dist/callbacks/manager.js +2 -2
  10. package/dist/chat_models/openai.cjs +10 -5
  11. package/dist/chat_models/openai.js +10 -5
  12. package/dist/llms/writer.cjs +167 -0
  13. package/dist/llms/writer.d.ts +60 -0
  14. package/dist/llms/writer.js +163 -0
  15. package/dist/load/import_constants.cjs +2 -0
  16. package/dist/load/import_constants.js +2 -0
  17. package/dist/load/import_map.cjs +2 -1
  18. package/dist/load/import_map.d.ts +1 -0
  19. package/dist/load/import_map.js +1 -0
  20. package/dist/memory/summary_buffer.d.ts +1 -1
  21. package/dist/retrievers/score_threshold.cjs +45 -0
  22. package/dist/retrievers/score_threshold.d.ts +15 -0
  23. package/dist/retrievers/score_threshold.js +41 -0
  24. package/dist/sql_db.cjs +8 -1
  25. package/dist/sql_db.d.ts +1 -0
  26. package/dist/sql_db.js +8 -1
  27. package/dist/stores/message/mongodb.cjs +48 -0
  28. package/dist/stores/message/mongodb.d.ts +15 -0
  29. package/dist/stores/message/mongodb.js +44 -0
  30. package/dist/util/sql_utils.cjs +8 -2
  31. package/dist/util/sql_utils.d.ts +2 -1
  32. package/dist/util/sql_utils.js +8 -2
  33. package/llms/writer.cjs +1 -0
  34. package/llms/writer.d.ts +1 -0
  35. package/llms/writer.js +1 -0
  36. package/package.json +30 -1
  37. package/retrievers/score_threshold.cjs +1 -0
  38. package/retrievers/score_threshold.d.ts +1 -0
  39. package/retrievers/score_threshold.js +1 -0
  40. package/stores/message/mongodb.cjs +1 -0
  41. package/stores/message/mongodb.d.ts +1 -0
  42. package/stores/message/mongodb.js +1 -0
@@ -36,16 +36,19 @@ class ChatConversationalAgentOutputParser extends types_js_1.AgentActionOutputPa
36
36
  */
37
37
  async parse(text) {
38
38
  let jsonOutput = text.trim();
39
- if (jsonOutput.includes("```json")) {
40
- jsonOutput = jsonOutput.split("```json")[1].trimStart();
41
- }
42
- else if (jsonOutput.includes("```")) {
43
- const firstIndex = jsonOutput.indexOf("```");
44
- jsonOutput = jsonOutput.slice(firstIndex + 3).trimStart();
45
- }
46
- const lastIndex = jsonOutput.lastIndexOf("```");
47
- if (lastIndex !== -1) {
48
- jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
39
+ if (jsonOutput.includes("```json") || jsonOutput.includes("```")) {
40
+ const testString = jsonOutput.includes("```json") ? "```json" : "```";
41
+ const firstIndex = jsonOutput.indexOf(testString);
42
+ const actionInputIndex = jsonOutput.indexOf("action_input");
43
+ if (actionInputIndex > firstIndex) {
44
+ jsonOutput = jsonOutput
45
+ .slice(firstIndex + testString.length)
46
+ .trimStart();
47
+ const lastIndex = jsonOutput.lastIndexOf("```");
48
+ if (lastIndex !== -1) {
49
+ jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
50
+ }
51
+ }
49
52
  }
50
53
  try {
51
54
  const response = JSON.parse(jsonOutput);
@@ -33,16 +33,19 @@ export class ChatConversationalAgentOutputParser extends AgentActionOutputParser
33
33
  */
34
34
  async parse(text) {
35
35
  let jsonOutput = text.trim();
36
- if (jsonOutput.includes("```json")) {
37
- jsonOutput = jsonOutput.split("```json")[1].trimStart();
38
- }
39
- else if (jsonOutput.includes("```")) {
40
- const firstIndex = jsonOutput.indexOf("```");
41
- jsonOutput = jsonOutput.slice(firstIndex + 3).trimStart();
42
- }
43
- const lastIndex = jsonOutput.lastIndexOf("```");
44
- if (lastIndex !== -1) {
45
- jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
36
+ if (jsonOutput.includes("```json") || jsonOutput.includes("```")) {
37
+ const testString = jsonOutput.includes("```json") ? "```json" : "```";
38
+ const firstIndex = jsonOutput.indexOf(testString);
39
+ const actionInputIndex = jsonOutput.indexOf("action_input");
40
+ if (actionInputIndex > firstIndex) {
41
+ jsonOutput = jsonOutput
42
+ .slice(firstIndex + testString.length)
43
+ .trimStart();
44
+ const lastIndex = jsonOutput.lastIndexOf("```");
45
+ if (lastIndex !== -1) {
46
+ jsonOutput = jsonOutput.slice(0, lastIndex).trimEnd();
47
+ }
48
+ }
46
49
  }
47
50
  try {
48
51
  const response = JSON.parse(jsonOutput);
@@ -1,4 +1,4 @@
1
- import { AgentAction, AgentFinish, BaseMessage, ChainValues, LLMResult } from "../schema/index.js";
1
+ import { AgentAction, AgentFinish, BaseMessage, ChainValues, ChatGenerationChunk, GenerationChunk, LLMResult } from "../schema/index.js";
2
2
  import { Serializable, Serialized, SerializedNotImplemented } from "../load/serializable.js";
3
3
  import { SerializedFields } from "../load/map_keys.js";
4
4
  import { Document } from "../document.js";
@@ -22,6 +22,9 @@ export interface NewTokenIndices {
22
22
  prompt: number;
23
23
  completion: number;
24
24
  }
25
+ export type HandleLLMNewTokenCallbackFields = {
26
+ chunk?: GenerationChunk | ChatGenerationChunk;
27
+ };
25
28
  /**
26
29
  * Abstract class that provides a set of optional methods that can be
27
30
  * overridden in derived classes to handle various events during the
@@ -43,7 +46,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
43
46
  * idx.completion is the index of the completion that produced the token
44
47
  * (if multiple completions per prompt are requested)
45
48
  */
46
- idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
49
+ idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void> | void;
47
50
  /**
48
51
  * Called if an LLM/ChatModel run encounters an error
49
52
  */
@@ -182,7 +185,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
182
185
  /**
183
186
  * Called when an LLM/ChatModel in `streaming` mode produces a new token
184
187
  */
185
- handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
188
+ handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, fields?: HandleLLMNewTokenCallbackFields | undefined): void | Promise<void>;
186
189
  /**
187
190
  * Called if an LLM/ChatModel run encounters an error
188
191
  */
@@ -332,7 +332,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
332
332
  });
333
333
  await this.onText?.(run);
334
334
  }
335
- async handleLLMNewToken(token, idx, runId) {
335
+ async handleLLMNewToken(token, idx, runId, _parentRunId, _tags, fields) {
336
336
  const run = this.runMap.get(runId);
337
337
  if (!run || run?.run_type !== "llm") {
338
338
  return;
@@ -340,7 +340,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
340
340
  run.events.push({
341
341
  name: "new_token",
342
342
  time: Date.now(),
343
- kwargs: { token, idx },
343
+ kwargs: { token, idx, chunk: fields?.chunk },
344
344
  });
345
345
  await this.onLLMNewToken?.(run);
346
346
  }
@@ -1,7 +1,7 @@
1
1
  import { KVMap, BaseRun } from "langsmith/schemas";
2
2
  import { AgentAction, AgentFinish, BaseMessage, ChainValues, LLMResult } from "../../schema/index.js";
3
3
  import { Serialized } from "../../load/serializable.js";
4
- import { BaseCallbackHandler, BaseCallbackHandlerInput, NewTokenIndices } from "../base.js";
4
+ import { BaseCallbackHandler, BaseCallbackHandlerInput, HandleLLMNewTokenCallbackFields, NewTokenIndices } from "../base.js";
5
5
  import { Document } from "../../document.js";
6
6
  export type RunType = string;
7
7
  export interface Run extends BaseRun {
@@ -44,7 +44,7 @@ export declare abstract class BaseTracer extends BaseCallbackHandler {
44
44
  handleRetrieverEnd(documents: Document<Record<string, unknown>>[], runId: string): Promise<void>;
45
45
  handleRetrieverError(error: Error, runId: string): Promise<void>;
46
46
  handleText(text: string, runId: string): Promise<void>;
47
- handleLLMNewToken(token: string, idx: NewTokenIndices, runId: string): Promise<void>;
47
+ handleLLMNewToken(token: string, idx: NewTokenIndices, runId: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void>;
48
48
  onLLMStart?(run: Run): void | Promise<void>;
49
49
  onLLMEnd?(run: Run): void | Promise<void>;
50
50
  onLLMError?(run: Run): void | Promise<void>;
@@ -329,7 +329,7 @@ export class BaseTracer extends BaseCallbackHandler {
329
329
  });
330
330
  await this.onText?.(run);
331
331
  }
332
- async handleLLMNewToken(token, idx, runId) {
332
+ async handleLLMNewToken(token, idx, runId, _parentRunId, _tags, fields) {
333
333
  const run = this.runMap.get(runId);
334
334
  if (!run || run?.run_type !== "llm") {
335
335
  return;
@@ -337,7 +337,7 @@ export class BaseTracer extends BaseCallbackHandler {
337
337
  run.events.push({
338
338
  name: "new_token",
339
339
  time: Date.now(),
340
- kwargs: { token, idx },
340
+ kwargs: { token, idx, chunk: fields?.chunk },
341
341
  });
342
342
  await this.onLLMNewToken?.(run);
343
343
  }
@@ -137,11 +137,11 @@ class CallbackManagerForRetrieverRun extends BaseRunManager {
137
137
  }
138
138
  exports.CallbackManagerForRetrieverRun = CallbackManagerForRetrieverRun;
139
139
  class CallbackManagerForLLMRun extends BaseRunManager {
140
- async handleLLMNewToken(token, idx = { prompt: 0, completion: 0 }) {
140
+ async handleLLMNewToken(token, idx, _runId, _parentRunId, _tags, fields) {
141
141
  await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
142
142
  if (!handler.ignoreLLM) {
143
143
  try {
144
- await handler.handleLLMNewToken?.(token, idx, this.runId, this._parentRunId, this.tags);
144
+ await handler.handleLLMNewToken?.(token, idx ?? { prompt: 0, completion: 0 }, this.runId, this._parentRunId, this.tags, fields);
145
145
  }
146
146
  catch (err) {
147
147
  console.error(`Error in handler ${handler.constructor.name}, handleLLMNewToken: ${err}`);
@@ -1,5 +1,5 @@
1
1
  import { AgentAction, AgentFinish, BaseMessage, ChainValues, LLMResult } from "../schema/index.js";
2
- import { BaseCallbackHandler, CallbackHandlerMethods, NewTokenIndices } from "./base.js";
2
+ import { BaseCallbackHandler, CallbackHandlerMethods, HandleLLMNewTokenCallbackFields, NewTokenIndices } from "./base.js";
3
3
  import { LangChainTracerFields } from "./handlers/tracer_langchain.js";
4
4
  import { Serialized } from "../load/serializable.js";
5
5
  import { Document } from "../document.js";
@@ -62,7 +62,7 @@ export declare class CallbackManagerForRetrieverRun extends BaseRunManager imple
62
62
  handleRetrieverError(err: Error | unknown): Promise<void>;
63
63
  }
64
64
  export declare class CallbackManagerForLLMRun extends BaseRunManager implements BaseCallbackManagerMethods {
65
- handleLLMNewToken(token: string, idx?: NewTokenIndices): Promise<void>;
65
+ handleLLMNewToken(token: string, idx?: NewTokenIndices, _runId?: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void>;
66
66
  handleLLMError(err: Error | unknown): Promise<void>;
67
67
  handleLLMEnd(output: LLMResult): Promise<void>;
68
68
  }
@@ -131,11 +131,11 @@ export class CallbackManagerForRetrieverRun extends BaseRunManager {
131
131
  }
132
132
  }
133
133
  export class CallbackManagerForLLMRun extends BaseRunManager {
134
- async handleLLMNewToken(token, idx = { prompt: 0, completion: 0 }) {
134
+ async handleLLMNewToken(token, idx, _runId, _parentRunId, _tags, fields) {
135
135
  await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
136
136
  if (!handler.ignoreLLM) {
137
137
  try {
138
- await handler.handleLLMNewToken?.(token, idx, this.runId, this._parentRunId, this.tags);
138
+ await handler.handleLLMNewToken?.(token, idx ?? { prompt: 0, completion: 0 }, this.runId, this._parentRunId, this.tags, fields);
139
139
  }
140
140
  catch (err) {
141
141
  console.error(`Error in handler ${handler.constructor.name}, handleLLMNewToken: ${err}`);
@@ -402,7 +402,10 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
402
402
  });
403
403
  yield generationChunk;
404
404
  // eslint-disable-next-line no-void
405
- void runManager?.handleLLMNewToken(generationChunk.text ?? "");
405
+ void runManager?.handleLLMNewToken(generationChunk.text ?? "", {
406
+ prompt: 0,
407
+ completion: choice.index,
408
+ }, undefined, undefined, undefined, { chunk: generationChunk });
406
409
  }
407
410
  }
408
411
  startStream(request, options) {
@@ -548,13 +551,15 @@ class ChatOpenAI extends base_js_1.BaseChatModel {
548
551
  choice.message.function_call.arguments +=
549
552
  part.delta?.function_call?.arguments ?? "";
550
553
  }
551
- // eslint-disable-next-line no-void
554
+ const chunk = _convertDeltaToMessageChunk(part.delta, "assistant");
555
+ const generationChunk = new index_js_1.ChatGenerationChunk({
556
+ message: chunk,
557
+ text: chunk.content,
558
+ });
552
559
  void runManager?.handleLLMNewToken(part.delta?.content ?? "", {
553
560
  prompt: options.promptIndex ?? 0,
554
561
  completion: part.index,
555
- });
556
- // TODO we don't currently have a callback method for
557
- // sending the function call arguments
562
+ }, undefined, undefined, undefined, { chunk: generationChunk });
558
563
  }
559
564
  }
560
565
  // when all messages are finished, resolve
@@ -396,7 +396,10 @@ export class ChatOpenAI extends BaseChatModel {
396
396
  });
397
397
  yield generationChunk;
398
398
  // eslint-disable-next-line no-void
399
- void runManager?.handleLLMNewToken(generationChunk.text ?? "");
399
+ void runManager?.handleLLMNewToken(generationChunk.text ?? "", {
400
+ prompt: 0,
401
+ completion: choice.index,
402
+ }, undefined, undefined, undefined, { chunk: generationChunk });
400
403
  }
401
404
  }
402
405
  startStream(request, options) {
@@ -542,13 +545,15 @@ export class ChatOpenAI extends BaseChatModel {
542
545
  choice.message.function_call.arguments +=
543
546
  part.delta?.function_call?.arguments ?? "";
544
547
  }
545
- // eslint-disable-next-line no-void
548
+ const chunk = _convertDeltaToMessageChunk(part.delta, "assistant");
549
+ const generationChunk = new ChatGenerationChunk({
550
+ message: chunk,
551
+ text: chunk.content,
552
+ });
546
553
  void runManager?.handleLLMNewToken(part.delta?.content ?? "", {
547
554
  prompt: options.promptIndex ?? 0,
548
555
  completion: part.index,
549
- });
550
- // TODO we don't currently have a callback method for
551
- // sending the function call arguments
556
+ }, undefined, undefined, undefined, { chunk: generationChunk });
552
557
  }
553
558
  }
554
559
  // when all messages are finished, resolve
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Writer = void 0;
4
+ const writer_sdk_1 = require("@writerai/writer-sdk");
5
+ const base_js_1 = require("./base.cjs");
6
+ const env_js_1 = require("../util/env.cjs");
7
+ /**
8
+ * Class representing a Writer Large Language Model (LLM). It interacts
9
+ * with the Writer API to generate text completions.
10
+ */
11
+ class Writer extends base_js_1.LLM {
12
+ static lc_name() {
13
+ return "Writer";
14
+ }
15
+ get lc_secrets() {
16
+ return {
17
+ apiKey: "WRITER_API_KEY",
18
+ orgId: "WRITER_ORG_ID",
19
+ };
20
+ }
21
+ get lc_aliases() {
22
+ return {
23
+ apiKey: "writer_api_key",
24
+ orgId: "writer_org_id",
25
+ };
26
+ }
27
+ constructor(fields) {
28
+ super(fields ?? {});
29
+ Object.defineProperty(this, "lc_serializable", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: true
34
+ });
35
+ Object.defineProperty(this, "apiKey", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ Object.defineProperty(this, "orgId", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
47
+ Object.defineProperty(this, "model", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: "palmyra-instruct"
52
+ });
53
+ Object.defineProperty(this, "temperature", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "minTokens", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ Object.defineProperty(this, "maxTokens", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ Object.defineProperty(this, "bestOf", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: void 0
76
+ });
77
+ Object.defineProperty(this, "frequencyPenalty", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "logprobs", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, "n", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
95
+ Object.defineProperty(this, "presencePenalty", {
96
+ enumerable: true,
97
+ configurable: true,
98
+ writable: true,
99
+ value: void 0
100
+ });
101
+ Object.defineProperty(this, "topP", {
102
+ enumerable: true,
103
+ configurable: true,
104
+ writable: true,
105
+ value: void 0
106
+ });
107
+ const apiKey = fields?.apiKey ?? (0, env_js_1.getEnvironmentVariable)("WRITER_API_KEY");
108
+ const orgId = fields?.orgId ?? (0, env_js_1.getEnvironmentVariable)("WRITER_ORG_ID");
109
+ if (!apiKey) {
110
+ throw new Error("Please set the WRITER_API_KEY environment variable or pass it to the constructor as the apiKey field.");
111
+ }
112
+ if (!orgId) {
113
+ throw new Error("Please set the WRITER_ORG_ID environment variable or pass it to the constructor as the orgId field.");
114
+ }
115
+ this.apiKey = apiKey;
116
+ this.orgId = typeof orgId === "string" ? parseInt(orgId, 10) : orgId;
117
+ this.model = fields?.model ?? this.model;
118
+ this.temperature = fields?.temperature ?? this.temperature;
119
+ this.minTokens = fields?.minTokens ?? this.minTokens;
120
+ this.maxTokens = fields?.maxTokens ?? this.maxTokens;
121
+ this.bestOf = fields?.bestOf ?? this.bestOf;
122
+ this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
123
+ this.logprobs = fields?.logprobs ?? this.logprobs;
124
+ this.n = fields?.n ?? this.n;
125
+ this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
126
+ this.topP = fields?.topP ?? this.topP;
127
+ }
128
+ _llmType() {
129
+ return "writer";
130
+ }
131
+ /** @ignore */
132
+ async _call(prompt, options) {
133
+ const sdk = new writer_sdk_1.Writer({
134
+ security: {
135
+ apiKey: this.apiKey,
136
+ },
137
+ organizationId: this.orgId,
138
+ });
139
+ return this.caller.callWithOptions({ signal: options.signal }, async () => {
140
+ try {
141
+ const res = await sdk.completions.create({
142
+ completionRequest: {
143
+ prompt,
144
+ stop: options.stop,
145
+ temperature: this.temperature,
146
+ minTokens: this.minTokens,
147
+ maxTokens: this.maxTokens,
148
+ bestOf: this.bestOf,
149
+ n: this.n,
150
+ frequencyPenalty: this.frequencyPenalty,
151
+ logprobs: this.logprobs,
152
+ presencePenalty: this.presencePenalty,
153
+ topP: this.topP,
154
+ },
155
+ modelId: this.model,
156
+ });
157
+ return (res.completionResponse?.choices?.[0].text ?? "No completion found.");
158
+ }
159
+ catch (e) {
160
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
161
+ e.response = e.rawResponse;
162
+ throw e;
163
+ }
164
+ });
165
+ }
166
+ }
167
+ exports.Writer = Writer;
@@ -0,0 +1,60 @@
1
+ import { BaseLLMParams, LLM } from "./base.js";
2
+ /**
3
+ * Interface for the input parameters specific to the Writer model.
4
+ */
5
+ export interface WriterInput extends BaseLLMParams {
6
+ /** Writer API key */
7
+ apiKey?: string;
8
+ /** Writer organization ID */
9
+ orgId?: string | number;
10
+ /** Model to use */
11
+ model?: string;
12
+ /** Sampling temperature to use */
13
+ temperature?: number;
14
+ /** Minimum number of tokens to generate. */
15
+ minTokens?: number;
16
+ /** Maximum number of tokens to generate in the completion. */
17
+ maxTokens?: number;
18
+ /** Generates this many completions server-side and returns the "best"." */
19
+ bestOf?: number;
20
+ /** Penalizes repeated tokens according to frequency. */
21
+ frequencyPenalty?: number;
22
+ /** Whether to return log probabilities. */
23
+ logprobs?: number;
24
+ /** Number of completions to generate. */
25
+ n?: number;
26
+ /** Penalizes repeated tokens regardless of frequency. */
27
+ presencePenalty?: number;
28
+ /** Total probability mass of tokens to consider at each step. */
29
+ topP?: number;
30
+ }
31
+ /**
32
+ * Class representing a Writer Large Language Model (LLM). It interacts
33
+ * with the Writer API to generate text completions.
34
+ */
35
+ export declare class Writer extends LLM implements WriterInput {
36
+ static lc_name(): string;
37
+ get lc_secrets(): {
38
+ [key: string]: string;
39
+ } | undefined;
40
+ get lc_aliases(): {
41
+ [key: string]: string;
42
+ } | undefined;
43
+ lc_serializable: boolean;
44
+ apiKey: string;
45
+ orgId: number;
46
+ model: string;
47
+ temperature?: number;
48
+ minTokens?: number;
49
+ maxTokens?: number;
50
+ bestOf?: number;
51
+ frequencyPenalty?: number;
52
+ logprobs?: number;
53
+ n?: number;
54
+ presencePenalty?: number;
55
+ topP?: number;
56
+ constructor(fields?: WriterInput);
57
+ _llmType(): string;
58
+ /** @ignore */
59
+ _call(prompt: string, options: this["ParsedCallOptions"]): Promise<string>;
60
+ }
@@ -0,0 +1,163 @@
1
+ import { Writer as WriterClient } from "@writerai/writer-sdk";
2
+ import { LLM } from "./base.js";
3
+ import { getEnvironmentVariable } from "../util/env.js";
4
+ /**
5
+ * Class representing a Writer Large Language Model (LLM). It interacts
6
+ * with the Writer API to generate text completions.
7
+ */
8
+ export class Writer extends LLM {
9
+ static lc_name() {
10
+ return "Writer";
11
+ }
12
+ get lc_secrets() {
13
+ return {
14
+ apiKey: "WRITER_API_KEY",
15
+ orgId: "WRITER_ORG_ID",
16
+ };
17
+ }
18
+ get lc_aliases() {
19
+ return {
20
+ apiKey: "writer_api_key",
21
+ orgId: "writer_org_id",
22
+ };
23
+ }
24
+ constructor(fields) {
25
+ super(fields ?? {});
26
+ Object.defineProperty(this, "lc_serializable", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: true
31
+ });
32
+ Object.defineProperty(this, "apiKey", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ Object.defineProperty(this, "orgId", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ Object.defineProperty(this, "model", {
45
+ enumerable: true,
46
+ configurable: true,
47
+ writable: true,
48
+ value: "palmyra-instruct"
49
+ });
50
+ Object.defineProperty(this, "temperature", {
51
+ enumerable: true,
52
+ configurable: true,
53
+ writable: true,
54
+ value: void 0
55
+ });
56
+ Object.defineProperty(this, "minTokens", {
57
+ enumerable: true,
58
+ configurable: true,
59
+ writable: true,
60
+ value: void 0
61
+ });
62
+ Object.defineProperty(this, "maxTokens", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
68
+ Object.defineProperty(this, "bestOf", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: void 0
73
+ });
74
+ Object.defineProperty(this, "frequencyPenalty", {
75
+ enumerable: true,
76
+ configurable: true,
77
+ writable: true,
78
+ value: void 0
79
+ });
80
+ Object.defineProperty(this, "logprobs", {
81
+ enumerable: true,
82
+ configurable: true,
83
+ writable: true,
84
+ value: void 0
85
+ });
86
+ Object.defineProperty(this, "n", {
87
+ enumerable: true,
88
+ configurable: true,
89
+ writable: true,
90
+ value: void 0
91
+ });
92
+ Object.defineProperty(this, "presencePenalty", {
93
+ enumerable: true,
94
+ configurable: true,
95
+ writable: true,
96
+ value: void 0
97
+ });
98
+ Object.defineProperty(this, "topP", {
99
+ enumerable: true,
100
+ configurable: true,
101
+ writable: true,
102
+ value: void 0
103
+ });
104
+ const apiKey = fields?.apiKey ?? getEnvironmentVariable("WRITER_API_KEY");
105
+ const orgId = fields?.orgId ?? getEnvironmentVariable("WRITER_ORG_ID");
106
+ if (!apiKey) {
107
+ throw new Error("Please set the WRITER_API_KEY environment variable or pass it to the constructor as the apiKey field.");
108
+ }
109
+ if (!orgId) {
110
+ throw new Error("Please set the WRITER_ORG_ID environment variable or pass it to the constructor as the orgId field.");
111
+ }
112
+ this.apiKey = apiKey;
113
+ this.orgId = typeof orgId === "string" ? parseInt(orgId, 10) : orgId;
114
+ this.model = fields?.model ?? this.model;
115
+ this.temperature = fields?.temperature ?? this.temperature;
116
+ this.minTokens = fields?.minTokens ?? this.minTokens;
117
+ this.maxTokens = fields?.maxTokens ?? this.maxTokens;
118
+ this.bestOf = fields?.bestOf ?? this.bestOf;
119
+ this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
120
+ this.logprobs = fields?.logprobs ?? this.logprobs;
121
+ this.n = fields?.n ?? this.n;
122
+ this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
123
+ this.topP = fields?.topP ?? this.topP;
124
+ }
125
+ _llmType() {
126
+ return "writer";
127
+ }
128
+ /** @ignore */
129
+ async _call(prompt, options) {
130
+ const sdk = new WriterClient({
131
+ security: {
132
+ apiKey: this.apiKey,
133
+ },
134
+ organizationId: this.orgId,
135
+ });
136
+ return this.caller.callWithOptions({ signal: options.signal }, async () => {
137
+ try {
138
+ const res = await sdk.completions.create({
139
+ completionRequest: {
140
+ prompt,
141
+ stop: options.stop,
142
+ temperature: this.temperature,
143
+ minTokens: this.minTokens,
144
+ maxTokens: this.maxTokens,
145
+ bestOf: this.bestOf,
146
+ n: this.n,
147
+ frequencyPenalty: this.frequencyPenalty,
148
+ logprobs: this.logprobs,
149
+ presencePenalty: this.presencePenalty,
150
+ topP: this.topP,
151
+ },
152
+ modelId: this.model,
153
+ });
154
+ return (res.completionResponse?.choices?.[0].text ?? "No completion found.");
155
+ }
156
+ catch (e) {
157
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
158
+ e.response = e.rawResponse;
159
+ throw e;
160
+ }
161
+ });
162
+ }
163
+ }
@@ -29,6 +29,7 @@ exports.optionalImportEntrypoints = [
29
29
  "langchain/llms/googlepalm",
30
30
  "langchain/llms/sagemaker_endpoint",
31
31
  "langchain/llms/bedrock",
32
+ "langchain/llms/writer",
32
33
  "langchain/prompts/load",
33
34
  "langchain/vectorstores/analyticdb",
34
35
  "langchain/vectorstores/elasticsearch",
@@ -110,6 +111,7 @@ exports.optionalImportEntrypoints = [
110
111
  "langchain/stores/message/dynamodb",
111
112
  "langchain/stores/message/firestore",
112
113
  "langchain/stores/message/momento",
114
+ "langchain/stores/message/mongodb",
113
115
  "langchain/stores/message/redis",
114
116
  "langchain/stores/message/ioredis",
115
117
  "langchain/stores/message/upstash_redis",
@@ -26,6 +26,7 @@ export const optionalImportEntrypoints = [
26
26
  "langchain/llms/googlepalm",
27
27
  "langchain/llms/sagemaker_endpoint",
28
28
  "langchain/llms/bedrock",
29
+ "langchain/llms/writer",
29
30
  "langchain/prompts/load",
30
31
  "langchain/vectorstores/analyticdb",
31
32
  "langchain/vectorstores/elasticsearch",
@@ -107,6 +108,7 @@ export const optionalImportEntrypoints = [
107
108
  "langchain/stores/message/dynamodb",
108
109
  "langchain/stores/message/firestore",
109
110
  "langchain/stores/message/momento",
111
+ "langchain/stores/message/mongodb",
110
112
  "langchain/stores/message/redis",
111
113
  "langchain/stores/message/ioredis",
112
114
  "langchain/stores/message/upstash_redis",
@@ -25,7 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
27
  exports.retrievers__hyde = exports.retrievers__document_compressors__chain_extract = exports.retrievers__time_weighted = exports.retrievers__parent_document = exports.retrievers__document_compressors = exports.retrievers__contextual_compression = exports.retrievers__databerry = exports.retrievers__remote = exports.output_parsers = exports.callbacks = exports.schema__storage = exports.schema__runnable = exports.schema__retriever = exports.schema__query_constructor = exports.schema__output_parser = exports.schema = exports.chat_models__ollama = exports.chat_models__baiduwenxin = exports.chat_models__anthropic = exports.chat_models__openai = exports.chat_models__base = exports.document_transformers__openai_functions = exports.document_loaders__web__sort_xyz_blockchain = exports.document_loaders__web__serpapi = exports.document_loaders__base = exports.document = exports.memory = exports.text_splitter = exports.vectorstores__xata = exports.vectorstores__vectara = exports.vectorstores__prisma = exports.vectorstores__memory = exports.vectorstores__base = exports.prompts = exports.llms__ollama = exports.llms__aleph_alpha = exports.llms__ai21 = exports.llms__openai = exports.llms__base = exports.embeddings__openai = exports.embeddings__fake = exports.embeddings__cache_backed = exports.embeddings__base = exports.chains__openai_functions = exports.chains = exports.tools = exports.base_language = exports.agents__toolkits = exports.agents = exports.load__serializable = void 0;
28
- exports.evaluation = exports.experimental__plan_and_execute = exports.experimental__generative_agents = exports.experimental__babyagi = exports.experimental__autogpt = exports.util__math = exports.storage__in_memory = exports.stores__message__in_memory = exports.stores__file__in_memory = exports.stores__doc__in_memory = exports.cache = exports.retrievers__vespa = void 0;
28
+ exports.evaluation = exports.experimental__plan_and_execute = exports.experimental__generative_agents = exports.experimental__babyagi = exports.experimental__autogpt = exports.util__math = exports.storage__in_memory = exports.stores__message__in_memory = exports.stores__file__in_memory = exports.stores__doc__in_memory = exports.cache = exports.retrievers__vespa = exports.retrievers__score_threshold = void 0;
29
29
  exports.load__serializable = __importStar(require("../load/serializable.cjs"));
30
30
  exports.agents = __importStar(require("../agents/index.cjs"));
31
31
  exports.agents__toolkits = __importStar(require("../agents/toolkits/index.cjs"));
@@ -76,6 +76,7 @@ exports.retrievers__parent_document = __importStar(require("../retrievers/parent
76
76
  exports.retrievers__time_weighted = __importStar(require("../retrievers/time_weighted.cjs"));
77
77
  exports.retrievers__document_compressors__chain_extract = __importStar(require("../retrievers/document_compressors/chain_extract.cjs"));
78
78
  exports.retrievers__hyde = __importStar(require("../retrievers/hyde.cjs"));
79
+ exports.retrievers__score_threshold = __importStar(require("../retrievers/score_threshold.cjs"));
79
80
  exports.retrievers__vespa = __importStar(require("../retrievers/vespa.cjs"));
80
81
  exports.cache = __importStar(require("../cache/index.cjs"));
81
82
  exports.stores__doc__in_memory = __importStar(require("../stores/doc/in_memory.cjs"));
@@ -48,6 +48,7 @@ export * as retrievers__parent_document from "../retrievers/parent_document.js";
48
48
  export * as retrievers__time_weighted from "../retrievers/time_weighted.js";
49
49
  export * as retrievers__document_compressors__chain_extract from "../retrievers/document_compressors/chain_extract.js";
50
50
  export * as retrievers__hyde from "../retrievers/hyde.js";
51
+ export * as retrievers__score_threshold from "../retrievers/score_threshold.js";
51
52
  export * as retrievers__vespa from "../retrievers/vespa.js";
52
53
  export * as cache from "../cache/index.js";
53
54
  export * as stores__doc__in_memory from "../stores/doc/in_memory.js";
@@ -49,6 +49,7 @@ export * as retrievers__parent_document from "../retrievers/parent_document.js";
49
49
  export * as retrievers__time_weighted from "../retrievers/time_weighted.js";
50
50
  export * as retrievers__document_compressors__chain_extract from "../retrievers/document_compressors/chain_extract.js";
51
51
  export * as retrievers__hyde from "../retrievers/hyde.js";
52
+ export * as retrievers__score_threshold from "../retrievers/score_threshold.js";
52
53
  export * as retrievers__vespa from "../retrievers/vespa.js";
53
54
  export * as cache from "../cache/index.js";
54
55
  export * as stores__doc__in_memory from "../stores/doc/in_memory.js";
@@ -26,7 +26,7 @@ export declare class ConversationSummaryBufferMemory extends BaseConversationSum
26
26
  * @param _ InputValues object, not used in this method.
27
27
  * @returns Promise that resolves with MemoryVariables object containing the loaded chat messages.
28
28
  */
29
- loadMemoryVariables(_: InputValues): Promise<MemoryVariables>;
29
+ loadMemoryVariables(_?: InputValues): Promise<MemoryVariables>;
30
30
  /**
31
31
  * Method that saves the context of the conversation, including the input
32
32
  * and output values, and prunes the memory if it exceeds the maximum
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScoreThresholdRetriever = void 0;
4
+ const base_js_1 = require("../vectorstores/base.cjs");
5
+ class ScoreThresholdRetriever extends base_js_1.VectorStoreRetriever {
6
+ constructor(input) {
7
+ super(input);
8
+ Object.defineProperty(this, "minSimilarityScore", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: void 0
13
+ });
14
+ Object.defineProperty(this, "kIncrement", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: 10
19
+ });
20
+ Object.defineProperty(this, "maxK", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: 100
25
+ });
26
+ this.maxK = input.maxK ?? this.maxK;
27
+ this.minSimilarityScore =
28
+ input.minSimilarityScore ?? this.minSimilarityScore;
29
+ this.kIncrement = input.kIncrement ?? this.kIncrement;
30
+ }
31
+ async getRelevantDocuments(query) {
32
+ let currentK = 0;
33
+ let filteredResults = [];
34
+ do {
35
+ currentK += this.kIncrement;
36
+ const results = await this.vectorStore.similaritySearchWithScore(query, currentK, this.filter);
37
+ filteredResults = results.filter(([, score]) => score >= this.minSimilarityScore);
38
+ } while (filteredResults.length >= currentK && currentK < this.maxK);
39
+ return filteredResults.map((documents) => documents[0]).slice(0, this.maxK);
40
+ }
41
+ static fromVectorStore(vectorStore, options) {
42
+ return new this({ ...options, vectorStore });
43
+ }
44
+ }
45
+ exports.ScoreThresholdRetriever = ScoreThresholdRetriever;
@@ -0,0 +1,15 @@
1
+ import { VectorStore, VectorStoreRetriever, VectorStoreRetrieverInput } from "../vectorstores/base.js";
2
+ import { Document } from "../document.js";
3
+ export type ScoreThresholdRetrieverInput<V extends VectorStore> = Omit<VectorStoreRetrieverInput<V>, "k"> & {
4
+ maxK?: number;
5
+ kIncrement?: number;
6
+ minSimilarityScore: number;
7
+ };
8
+ export declare class ScoreThresholdRetriever<V extends VectorStore> extends VectorStoreRetriever<V> {
9
+ minSimilarityScore: number;
10
+ kIncrement: number;
11
+ maxK: number;
12
+ constructor(input: ScoreThresholdRetrieverInput<V>);
13
+ getRelevantDocuments(query: string): Promise<Document[]>;
14
+ static fromVectorStore<V extends VectorStore>(vectorStore: V, options: Omit<ScoreThresholdRetrieverInput<V>, "vectorStore">): ScoreThresholdRetriever<V>;
15
+ }
@@ -0,0 +1,41 @@
1
+ import { VectorStoreRetriever, } from "../vectorstores/base.js";
2
+ export class ScoreThresholdRetriever extends VectorStoreRetriever {
3
+ constructor(input) {
4
+ super(input);
5
+ Object.defineProperty(this, "minSimilarityScore", {
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true,
9
+ value: void 0
10
+ });
11
+ Object.defineProperty(this, "kIncrement", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: 10
16
+ });
17
+ Object.defineProperty(this, "maxK", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: 100
22
+ });
23
+ this.maxK = input.maxK ?? this.maxK;
24
+ this.minSimilarityScore =
25
+ input.minSimilarityScore ?? this.minSimilarityScore;
26
+ this.kIncrement = input.kIncrement ?? this.kIncrement;
27
+ }
28
+ async getRelevantDocuments(query) {
29
+ let currentK = 0;
30
+ let filteredResults = [];
31
+ do {
32
+ currentK += this.kIncrement;
33
+ const results = await this.vectorStore.similaritySearchWithScore(query, currentK, this.filter);
34
+ filteredResults = results.filter(([, score]) => score >= this.minSimilarityScore);
35
+ } while (filteredResults.length >= currentK && currentK < this.maxK);
36
+ return filteredResults.map((documents) => documents[0]).slice(0, this.maxK);
37
+ }
38
+ static fromVectorStore(vectorStore, options) {
39
+ return new this({ ...options, vectorStore });
40
+ }
41
+ }
package/dist/sql_db.cjs CHANGED
@@ -51,6 +51,12 @@ class SqlDatabase extends serializable_js_1.Serializable {
51
51
  writable: true,
52
52
  value: 3
53
53
  });
54
+ Object.defineProperty(this, "customDescription", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
54
60
  this.appDataSource = fields.appDataSource;
55
61
  this.appDataSourceOptions = fields.appDataSource.options;
56
62
  if (fields?.includesTables && fields?.ignoreTables) {
@@ -60,6 +66,7 @@ class SqlDatabase extends serializable_js_1.Serializable {
60
66
  this.ignoreTables = fields?.ignoreTables ?? [];
61
67
  this.sampleRowsInTableInfo =
62
68
  fields?.sampleRowsInTableInfo ?? this.sampleRowsInTableInfo;
69
+ this.customDescription = Object.fromEntries(Object.entries(fields?.customDescription ?? {}).filter(([key, _]) => this.allTables.map((table) => table.tableName).includes(key)));
63
70
  }
64
71
  static async fromDataSourceParams(fields) {
65
72
  const sqlDatabase = new SqlDatabase(fields);
@@ -100,7 +107,7 @@ class SqlDatabase extends serializable_js_1.Serializable {
100
107
  (0, sql_utils_js_1.verifyListTablesExistInDatabase)(this.allTables, targetTables, "Wrong target table name:");
101
108
  selectedTables = this.allTables.filter((currentTable) => targetTables.includes(currentTable.tableName));
102
109
  }
103
- return (0, sql_utils_js_1.generateTableInfoFromTables)(selectedTables, this.appDataSource, this.sampleRowsInTableInfo);
110
+ return (0, sql_utils_js_1.generateTableInfoFromTables)(selectedTables, this.appDataSource, this.sampleRowsInTableInfo, this.customDescription);
104
111
  }
105
112
  /**
106
113
  * Execute a SQL command and return a string representing the results.
package/dist/sql_db.d.ts CHANGED
@@ -11,6 +11,7 @@ export declare class SqlDatabase extends Serializable implements SqlDatabaseOpti
11
11
  includesTables: Array<string>;
12
12
  ignoreTables: Array<string>;
13
13
  sampleRowsInTableInfo: number;
14
+ customDescription?: Record<string, string>;
14
15
  protected constructor(fields: SqlDatabaseDataSourceParams);
15
16
  static fromDataSourceParams(fields: SqlDatabaseDataSourceParams): Promise<SqlDatabase>;
16
17
  static fromOptionsParams(fields: SqlDatabaseOptionsParams): Promise<SqlDatabase>;
package/dist/sql_db.js CHANGED
@@ -48,6 +48,12 @@ export class SqlDatabase extends Serializable {
48
48
  writable: true,
49
49
  value: 3
50
50
  });
51
+ Object.defineProperty(this, "customDescription", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
51
57
  this.appDataSource = fields.appDataSource;
52
58
  this.appDataSourceOptions = fields.appDataSource.options;
53
59
  if (fields?.includesTables && fields?.ignoreTables) {
@@ -57,6 +63,7 @@ export class SqlDatabase extends Serializable {
57
63
  this.ignoreTables = fields?.ignoreTables ?? [];
58
64
  this.sampleRowsInTableInfo =
59
65
  fields?.sampleRowsInTableInfo ?? this.sampleRowsInTableInfo;
66
+ this.customDescription = Object.fromEntries(Object.entries(fields?.customDescription ?? {}).filter(([key, _]) => this.allTables.map((table) => table.tableName).includes(key)));
60
67
  }
61
68
  static async fromDataSourceParams(fields) {
62
69
  const sqlDatabase = new SqlDatabase(fields);
@@ -97,7 +104,7 @@ export class SqlDatabase extends Serializable {
97
104
  verifyListTablesExistInDatabase(this.allTables, targetTables, "Wrong target table name:");
98
105
  selectedTables = this.allTables.filter((currentTable) => targetTables.includes(currentTable.tableName));
99
106
  }
100
- return generateTableInfoFromTables(selectedTables, this.appDataSource, this.sampleRowsInTableInfo);
107
+ return generateTableInfoFromTables(selectedTables, this.appDataSource, this.sampleRowsInTableInfo, this.customDescription);
101
108
  }
102
109
  /**
103
110
  * Execute a SQL command and return a string representing the results.
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MongoDBChatMessageHistory = void 0;
4
+ const mongodb_1 = require("mongodb");
5
+ const index_js_1 = require("../../schema/index.cjs");
6
+ const utils_js_1 = require("./utils.cjs");
7
+ class MongoDBChatMessageHistory extends index_js_1.BaseListChatMessageHistory {
8
+ constructor({ collection, sessionId }) {
9
+ super();
10
+ Object.defineProperty(this, "lc_namespace", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: ["langchain", "stores", "message", "mongodb"]
15
+ });
16
+ Object.defineProperty(this, "collection", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "sessionId", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.collection = collection;
29
+ this.sessionId = sessionId;
30
+ }
31
+ async getMessages() {
32
+ const document = await this.collection.findOne({
33
+ _id: new mongodb_1.ObjectId(this.sessionId),
34
+ });
35
+ const messages = document?.messages || [];
36
+ return (0, utils_js_1.mapStoredMessagesToChatMessages)(messages);
37
+ }
38
+ async addMessage(message) {
39
+ const messages = (0, utils_js_1.mapChatMessagesToStoredMessages)([message]);
40
+ await this.collection.updateOne({ _id: new mongodb_1.ObjectId(this.sessionId) }, {
41
+ $push: { messages: { $each: messages } },
42
+ }, { upsert: true });
43
+ }
44
+ async clear() {
45
+ await this.collection.deleteOne({ _id: new mongodb_1.ObjectId(this.sessionId) });
46
+ }
47
+ }
48
+ exports.MongoDBChatMessageHistory = MongoDBChatMessageHistory;
@@ -0,0 +1,15 @@
1
+ import { Collection, Document as MongoDBDocument } from "mongodb";
2
+ import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js";
3
+ export interface MongoDBChatMessageHistoryInput {
4
+ collection: Collection<MongoDBDocument>;
5
+ sessionId: string;
6
+ }
7
+ export declare class MongoDBChatMessageHistory extends BaseListChatMessageHistory {
8
+ lc_namespace: string[];
9
+ private collection;
10
+ private sessionId;
11
+ constructor({ collection, sessionId }: MongoDBChatMessageHistoryInput);
12
+ getMessages(): Promise<BaseMessage[]>;
13
+ addMessage(message: BaseMessage): Promise<void>;
14
+ clear(): Promise<void>;
15
+ }
@@ -0,0 +1,44 @@
1
+ import { ObjectId } from "mongodb";
2
+ import { BaseListChatMessageHistory } from "../../schema/index.js";
3
+ import { mapChatMessagesToStoredMessages, mapStoredMessagesToChatMessages, } from "./utils.js";
4
+ export class MongoDBChatMessageHistory extends BaseListChatMessageHistory {
5
+ constructor({ collection, sessionId }) {
6
+ super();
7
+ Object.defineProperty(this, "lc_namespace", {
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true,
11
+ value: ["langchain", "stores", "message", "mongodb"]
12
+ });
13
+ Object.defineProperty(this, "collection", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "sessionId", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.collection = collection;
26
+ this.sessionId = sessionId;
27
+ }
28
+ async getMessages() {
29
+ const document = await this.collection.findOne({
30
+ _id: new ObjectId(this.sessionId),
31
+ });
32
+ const messages = document?.messages || [];
33
+ return mapStoredMessagesToChatMessages(messages);
34
+ }
35
+ async addMessage(message) {
36
+ const messages = mapChatMessagesToStoredMessages([message]);
37
+ await this.collection.updateOne({ _id: new ObjectId(this.sessionId) }, {
38
+ $push: { messages: { $each: messages } },
39
+ }, { upsert: true });
40
+ }
41
+ async clear() {
42
+ await this.collection.deleteOne({ _id: new ObjectId(this.sessionId) });
43
+ }
44
+ }
@@ -139,12 +139,17 @@ const formatSqlResponseToSimpleTableString = (rawResult) => {
139
139
  }
140
140
  return globalString;
141
141
  };
142
- const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow) => {
142
+ const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow, customDescription) => {
143
143
  if (!tables) {
144
144
  return "";
145
145
  }
146
146
  let globalString = "";
147
147
  for (const currentTable of tables) {
148
+ // Add the custom info of the table
149
+ const tableCustomDescription = customDescription &&
150
+ Object.keys(customDescription).includes(currentTable.tableName)
151
+ ? `${customDescription[currentTable.tableName]}\n`
152
+ : "";
148
153
  // Add the creation of the table in SQL
149
154
  let schema = null;
150
155
  if (appDataSource.options.type === "postgres") {
@@ -199,7 +204,8 @@ const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow) =
199
204
  // If the request fails we catch it and only display a log message
200
205
  console.log(error);
201
206
  }
202
- globalString = globalString.concat(sqlCreateTableQuery +
207
+ globalString = globalString.concat(tableCustomDescription +
208
+ sqlCreateTableQuery +
203
209
  sqlSelectInfoQuery +
204
210
  columnNamesConcatString +
205
211
  sample);
@@ -4,6 +4,7 @@ export interface SqlDatabaseParams {
4
4
  includesTables?: Array<string>;
5
5
  ignoreTables?: Array<string>;
6
6
  sampleRowsInTableInfo?: number;
7
+ customDescription?: Record<string, string>;
7
8
  }
8
9
  export interface SqlDatabaseOptionsParams extends SqlDatabaseParams {
9
10
  appDataSourceOptions: DataSourceOptions;
@@ -27,5 +28,5 @@ export declare const verifyListTablesExistInDatabase: (tablesFromDatabase: Array
27
28
  export declare const verifyIncludeTablesExistInDatabase: (tablesFromDatabase: Array<SqlTable>, includeTables: Array<string>) => void;
28
29
  export declare const verifyIgnoreTablesExistInDatabase: (tablesFromDatabase: Array<SqlTable>, ignoreTables: Array<string>) => void;
29
30
  export declare const getTableAndColumnsName: (appDataSource: DataSource) => Promise<Array<SqlTable>>;
30
- export declare const generateTableInfoFromTables: (tables: Array<SqlTable> | undefined, appDataSource: DataSource, nbSampleRow: number) => Promise<string>;
31
+ export declare const generateTableInfoFromTables: (tables: Array<SqlTable> | undefined, appDataSource: DataSource, nbSampleRow: number, customDescription?: Record<string, string>) => Promise<string>;
31
32
  export declare const getPromptTemplateFromDataSource: (appDataSource: DataSource) => PromptTemplate;
@@ -132,12 +132,17 @@ const formatSqlResponseToSimpleTableString = (rawResult) => {
132
132
  }
133
133
  return globalString;
134
134
  };
135
- export const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow) => {
135
+ export const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow, customDescription) => {
136
136
  if (!tables) {
137
137
  return "";
138
138
  }
139
139
  let globalString = "";
140
140
  for (const currentTable of tables) {
141
+ // Add the custom info of the table
142
+ const tableCustomDescription = customDescription &&
143
+ Object.keys(customDescription).includes(currentTable.tableName)
144
+ ? `${customDescription[currentTable.tableName]}\n`
145
+ : "";
141
146
  // Add the creation of the table in SQL
142
147
  let schema = null;
143
148
  if (appDataSource.options.type === "postgres") {
@@ -192,7 +197,8 @@ export const generateTableInfoFromTables = async (tables, appDataSource, nbSampl
192
197
  // If the request fails we catch it and only display a log message
193
198
  console.log(error);
194
199
  }
195
- globalString = globalString.concat(sqlCreateTableQuery +
200
+ globalString = globalString.concat(tableCustomDescription +
201
+ sqlCreateTableQuery +
196
202
  sqlSelectInfoQuery +
197
203
  columnNamesConcatString +
198
204
  sample);
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/llms/writer.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/llms/writer.js'
package/llms/writer.js ADDED
@@ -0,0 +1 @@
1
+ export * from '../dist/llms/writer.js'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.134",
3
+ "version": "0.0.135",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -145,6 +145,9 @@
145
145
  "llms/bedrock.cjs",
146
146
  "llms/bedrock.js",
147
147
  "llms/bedrock.d.ts",
148
+ "llms/writer.cjs",
149
+ "llms/writer.js",
150
+ "llms/writer.d.ts",
148
151
  "prompts.cjs",
149
152
  "prompts.js",
150
153
  "prompts.d.ts",
@@ -457,6 +460,9 @@
457
460
  "retrievers/hyde.cjs",
458
461
  "retrievers/hyde.js",
459
462
  "retrievers/hyde.d.ts",
463
+ "retrievers/score_threshold.cjs",
464
+ "retrievers/score_threshold.js",
465
+ "retrievers/score_threshold.d.ts",
460
466
  "retrievers/self_query.cjs",
461
467
  "retrievers/self_query.js",
462
468
  "retrievers/self_query.d.ts",
@@ -517,6 +523,9 @@
517
523
  "stores/message/momento.cjs",
518
524
  "stores/message/momento.js",
519
525
  "stores/message/momento.d.ts",
526
+ "stores/message/mongodb.cjs",
527
+ "stores/message/mongodb.js",
528
+ "stores/message/mongodb.d.ts",
520
529
  "stores/message/redis.cjs",
521
530
  "stores/message/redis.js",
522
531
  "stores/message/redis.d.ts",
@@ -645,6 +654,7 @@
645
654
  "@typescript-eslint/eslint-plugin": "^5.58.0",
646
655
  "@typescript-eslint/parser": "^5.58.0",
647
656
  "@upstash/redis": "^1.20.6",
657
+ "@writerai/writer-sdk": "^0.40.2",
648
658
  "@xata.io/client": "^0.25.1",
649
659
  "@zilliz/milvus2-sdk-node": ">=2.2.11",
650
660
  "apify-client": "^2.7.1",
@@ -740,6 +750,7 @@
740
750
  "@tensorflow/tfjs-core": "*",
741
751
  "@tigrisdata/vector": "^1.1.0",
742
752
  "@upstash/redis": "^1.20.6",
753
+ "@writerai/writer-sdk": "^0.40.2",
743
754
  "@xata.io/client": "^0.25.1",
744
755
  "@zilliz/milvus2-sdk-node": ">=2.2.7",
745
756
  "apify-client": "^2.7.1",
@@ -887,6 +898,9 @@
887
898
  "@upstash/redis": {
888
899
  "optional": true
889
900
  },
901
+ "@writerai/writer-sdk": {
902
+ "optional": true
903
+ },
890
904
  "@xata.io/client": {
891
905
  "optional": true
892
906
  },
@@ -1283,6 +1297,11 @@
1283
1297
  "import": "./llms/bedrock.js",
1284
1298
  "require": "./llms/bedrock.cjs"
1285
1299
  },
1300
+ "./llms/writer": {
1301
+ "types": "./llms/writer.d.ts",
1302
+ "import": "./llms/writer.js",
1303
+ "require": "./llms/writer.cjs"
1304
+ },
1286
1305
  "./prompts": {
1287
1306
  "types": "./prompts.d.ts",
1288
1307
  "import": "./prompts.js",
@@ -1811,6 +1830,11 @@
1811
1830
  "import": "./retrievers/hyde.js",
1812
1831
  "require": "./retrievers/hyde.cjs"
1813
1832
  },
1833
+ "./retrievers/score_threshold": {
1834
+ "types": "./retrievers/score_threshold.d.ts",
1835
+ "import": "./retrievers/score_threshold.js",
1836
+ "require": "./retrievers/score_threshold.cjs"
1837
+ },
1814
1838
  "./retrievers/self_query": {
1815
1839
  "types": "./retrievers/self_query.d.ts",
1816
1840
  "import": "./retrievers/self_query.js",
@@ -1911,6 +1935,11 @@
1911
1935
  "import": "./stores/message/momento.js",
1912
1936
  "require": "./stores/message/momento.cjs"
1913
1937
  },
1938
+ "./stores/message/mongodb": {
1939
+ "types": "./stores/message/mongodb.d.ts",
1940
+ "import": "./stores/message/mongodb.js",
1941
+ "require": "./stores/message/mongodb.cjs"
1942
+ },
1914
1943
  "./stores/message/redis": {
1915
1944
  "types": "./stores/message/redis.d.ts",
1916
1945
  "import": "./stores/message/redis.js",
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/retrievers/score_threshold.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/retrievers/score_threshold.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/retrievers/score_threshold.js'
@@ -0,0 +1 @@
1
+ module.exports = require('../../dist/stores/message/mongodb.cjs');
@@ -0,0 +1 @@
1
+ export * from '../../dist/stores/message/mongodb.js'
@@ -0,0 +1 @@
1
+ export * from '../../dist/stores/message/mongodb.js'