langchain 0.0.134 → 0.0.136

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 (65) 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/chains/question_answering/map_reduce_prompts.d.ts +2 -2
  11. package/dist/chains/retrieval_qa.cjs +1 -1
  12. package/dist/chains/retrieval_qa.js +1 -1
  13. package/dist/chat_models/openai.cjs +10 -5
  14. package/dist/chat_models/openai.js +10 -5
  15. package/dist/llms/writer.cjs +167 -0
  16. package/dist/llms/writer.d.ts +60 -0
  17. package/dist/llms/writer.js +163 -0
  18. package/dist/load/import_constants.cjs +2 -0
  19. package/dist/load/import_constants.js +2 -0
  20. package/dist/load/import_map.cjs +2 -1
  21. package/dist/load/import_map.d.ts +1 -0
  22. package/dist/load/import_map.js +1 -0
  23. package/dist/memory/summary_buffer.d.ts +1 -1
  24. package/dist/output_parsers/prompts.d.ts +1 -1
  25. package/dist/prompts/prompt.cjs +3 -1
  26. package/dist/prompts/prompt.d.ts +12 -1
  27. package/dist/prompts/prompt.js +3 -1
  28. package/dist/retrievers/parent_document.cjs +8 -1
  29. package/dist/retrievers/parent_document.d.ts +2 -0
  30. package/dist/retrievers/parent_document.js +8 -1
  31. package/dist/retrievers/score_threshold.cjs +45 -0
  32. package/dist/retrievers/score_threshold.d.ts +15 -0
  33. package/dist/retrievers/score_threshold.js +41 -0
  34. package/dist/sql_db.cjs +8 -1
  35. package/dist/sql_db.d.ts +1 -0
  36. package/dist/sql_db.js +8 -1
  37. package/dist/stores/message/mongodb.cjs +48 -0
  38. package/dist/stores/message/mongodb.d.ts +15 -0
  39. package/dist/stores/message/mongodb.js +44 -0
  40. package/dist/tools/index.cjs +3 -1
  41. package/dist/tools/index.d.ts +1 -0
  42. package/dist/tools/index.js +1 -0
  43. package/dist/tools/wolframalpha.cjs +40 -0
  44. package/dist/tools/wolframalpha.d.ts +12 -0
  45. package/dist/tools/wolframalpha.js +36 -0
  46. package/dist/util/sql_utils.cjs +8 -2
  47. package/dist/util/sql_utils.d.ts +2 -1
  48. package/dist/util/sql_utils.js +8 -2
  49. package/dist/vectorstores/chroma.cjs +8 -0
  50. package/dist/vectorstores/chroma.d.ts +4 -1
  51. package/dist/vectorstores/chroma.js +8 -0
  52. package/dist/vectorstores/redis.cjs +9 -1
  53. package/dist/vectorstores/redis.js +9 -1
  54. package/dist/vectorstores/vectara.cjs +1 -1
  55. package/dist/vectorstores/vectara.js +1 -1
  56. package/llms/writer.cjs +1 -0
  57. package/llms/writer.d.ts +1 -0
  58. package/llms/writer.js +1 -0
  59. package/package.json +30 -1
  60. package/retrievers/score_threshold.cjs +1 -0
  61. package/retrievers/score_threshold.d.ts +1 -0
  62. package/retrievers/score_threshold.js +1 -0
  63. package/stores/message/mongodb.cjs +1 -0
  64. package/stores/message/mongodb.d.ts +1 -0
  65. package/stores/message/mongodb.js +1 -0
@@ -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
@@ -1,3 +1,3 @@
1
1
  import { PromptTemplate } from "../prompts/prompt.js";
2
2
  export declare const NAIVE_FIX_TEMPLATE = "Instructions:\n--------------\n{instructions}\n--------------\nCompletion:\n--------------\n{completion}\n--------------\n\nAbove, the Completion did not satisfy the constraints given in the Instructions.\nError:\n--------------\n{error}\n--------------\n\nPlease try again. Please only respond with an answer that satisfies the constraints laid out in the Instructions:";
3
- export declare const NAIVE_FIX_PROMPT: PromptTemplate<any, any>;
3
+ export declare const NAIVE_FIX_PROMPT: PromptTemplate<import("../prompts/prompt.js").ParamsFromFString<"Instructions:\n--------------\n{instructions}\n--------------\nCompletion:\n--------------\n{completion}\n--------------\n\nAbove, the Completion did not satisfy the constraints given in the Instructions.\nError:\n--------------\n{error}\n--------------\n\nPlease try again. Please only respond with an answer that satisfies the constraints laid out in the Instructions:">, any>;
@@ -88,8 +88,10 @@ class PromptTemplate extends base_js_1.BaseStringPromptTemplate {
88
88
  /**
89
89
  * Load prompt template from a template f-string
90
90
  */
91
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
91
  static fromTemplate(template, { templateFormat = "f-string", ...rest } = {}) {
92
+ if (templateFormat === "jinja2") {
93
+ throw new Error("jinja2 templates are not currently supported.");
94
+ }
93
95
  const names = new Set();
94
96
  (0, template_js_1.parseTemplate)(template, templateFormat).forEach((node) => {
95
97
  if (node.type === "variable") {
@@ -24,6 +24,16 @@ export interface PromptTemplateInput<RunInput extends InputValues = any, Partial
24
24
  */
25
25
  validateTemplate?: boolean;
26
26
  }
27
+ type NonAlphanumeric = " " | "\t" | "\n" | "\r" | '"' | "'" | "{" | "[" | "(" | "`" | ":" | ";";
28
+ /**
29
+ * Recursive type to extract template parameters from a string.
30
+ * @template T - The input string.
31
+ * @template Result - The resulting array of extracted template parameters.
32
+ */
33
+ type ExtractTemplateParamsRecursive<T extends string, Result extends string[] = []> = T extends `${string}{${infer Param}}${infer Rest}` ? Param extends `${NonAlphanumeric}${string}` ? ExtractTemplateParamsRecursive<Rest, Result> : ExtractTemplateParamsRecursive<Rest, [...Result, Param]> : Result;
34
+ export type ParamsFromFString<T extends string> = {
35
+ [Key in ExtractTemplateParamsRecursive<T>[number]]: string;
36
+ };
27
37
  /**
28
38
  * Schema to represent a basic prompt for an LLM.
29
39
  * @augments BasePromptTemplate
@@ -69,7 +79,7 @@ export declare class PromptTemplate<RunInput extends InputValues = any, PartialV
69
79
  /**
70
80
  * Load prompt template from a template f-string
71
81
  */
72
- static fromTemplate<RunInput extends InputValues = any>(template: string, { templateFormat, ...rest }?: Omit<PromptTemplateInput, "template" | "inputVariables">): PromptTemplate<RunInput, any>;
82
+ static fromTemplate<RunInput extends InputValues = Symbol, T extends string = string>(template: T, { templateFormat, ...rest }?: Omit<PromptTemplateInput<RunInput, string>, "template" | "inputVariables">): PromptTemplate<RunInput extends Symbol ? ParamsFromFString<T> : RunInput, any>;
73
83
  /**
74
84
  * Partially applies values to the prompt template.
75
85
  * @param values The values to be partially applied to the prompt template.
@@ -79,3 +89,4 @@ export declare class PromptTemplate<RunInput extends InputValues = any, PartialV
79
89
  serialize(): SerializedPromptTemplate;
80
90
  static deserialize(data: SerializedPromptTemplate): Promise<PromptTemplate>;
81
91
  }
92
+ export {};
@@ -85,8 +85,10 @@ export class PromptTemplate extends BaseStringPromptTemplate {
85
85
  /**
86
86
  * Load prompt template from a template f-string
87
87
  */
88
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
89
88
  static fromTemplate(template, { templateFormat = "f-string", ...rest } = {}) {
89
+ if (templateFormat === "jinja2") {
90
+ throw new Error("jinja2 templates are not currently supported.");
91
+ }
90
92
  const names = new Set();
91
93
  parseTemplate(template, templateFormat).forEach((node) => {
92
94
  if (node.type === "variable") {
@@ -84,12 +84,19 @@ class ParentDocumentRetriever extends retriever_js_1.BaseRetriever {
84
84
  writable: true,
85
85
  value: void 0
86
86
  });
87
+ Object.defineProperty(this, "parentK", {
88
+ enumerable: true,
89
+ configurable: true,
90
+ writable: true,
91
+ value: void 0
92
+ });
87
93
  this.vectorstore = fields.vectorstore;
88
94
  this.docstore = fields.docstore;
89
95
  this.childSplitter = fields.childSplitter;
90
96
  this.parentSplitter = fields.parentSplitter;
91
97
  this.idKey = fields.idKey ?? this.idKey;
92
98
  this.childK = fields.childK;
99
+ this.parentK = fields.parentK;
93
100
  }
94
101
  async _getRelevantDocuments(query) {
95
102
  const subDocs = await this.vectorstore.similaritySearch(query, this.childK);
@@ -107,7 +114,7 @@ class ParentDocumentRetriever extends retriever_js_1.BaseRetriever {
107
114
  parentDocs.push(parentDoc);
108
115
  }
109
116
  }
110
- return parentDocs;
117
+ return parentDocs.slice(0, this.parentK);
111
118
  }
112
119
  /**
113
120
  * Adds documents to the docstore and vectorstores.
@@ -14,6 +14,7 @@ export interface ParentDocumentRetrieverFields extends BaseRetrieverInput {
14
14
  parentSplitter?: TextSplitter;
15
15
  idKey?: string;
16
16
  childK?: number;
17
+ parentK?: number;
17
18
  }
18
19
  /**
19
20
  * A type of document retriever that splits input documents into smaller chunks
@@ -33,6 +34,7 @@ export declare class ParentDocumentRetriever extends BaseRetriever {
33
34
  protected parentSplitter?: TextSplitter;
34
35
  protected idKey: string;
35
36
  protected childK?: number;
37
+ protected parentK?: number;
36
38
  constructor(fields: ParentDocumentRetrieverFields);
37
39
  _getRelevantDocuments(query: string): Promise<Document[]>;
38
40
  /**
@@ -58,12 +58,19 @@ export class ParentDocumentRetriever extends BaseRetriever {
58
58
  writable: true,
59
59
  value: void 0
60
60
  });
61
+ Object.defineProperty(this, "parentK", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: void 0
66
+ });
61
67
  this.vectorstore = fields.vectorstore;
62
68
  this.docstore = fields.docstore;
63
69
  this.childSplitter = fields.childSplitter;
64
70
  this.parentSplitter = fields.parentSplitter;
65
71
  this.idKey = fields.idKey ?? this.idKey;
66
72
  this.childK = fields.childK;
73
+ this.parentK = fields.parentK;
67
74
  }
68
75
  async _getRelevantDocuments(query) {
69
76
  const subDocs = await this.vectorstore.similaritySearch(query, this.childK);
@@ -81,7 +88,7 @@ export class ParentDocumentRetriever extends BaseRetriever {
81
88
  parentDocs.push(parentDoc);
82
89
  }
83
90
  }
84
- return parentDocs;
91
+ return parentDocs.slice(0, this.parentK);
85
92
  }
86
93
  /**
87
94
  * Adds documents to the docstore and vectorstores.
@@ -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
+ }