langchain 0.0.94 → 0.0.95

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StructuredChatOutputParserWithRetries = exports.StructuredChatOutputParser = exports.StructuredChatAgent = exports.AgentActionOutputParser = exports.ZeroShotAgentOutputParser = exports.ZeroShotAgent = exports.initializeAgentExecutorWithOptions = exports.initializeAgentExecutor = exports.AgentExecutor = exports.ChatConversationalAgentOutputParserWithRetries = exports.ChatConversationalAgentOutputParser = exports.ChatConversationalAgent = exports.ChatAgentOutputParser = exports.ChatAgent = exports.Toolkit = exports.createVectorStoreRouterAgent = exports.createVectorStoreAgent = exports.createSqlAgent = exports.createOpenApiAgent = exports.createJsonAgent = exports.ZapierToolKit = exports.VectorStoreToolkit = exports.VectorStoreRouterToolkit = exports.SqlToolkit = exports.RequestsToolkit = exports.OpenApiToolkit = exports.JsonToolkit = exports.LLMSingleActionAgent = exports.BaseSingleActionAgent = exports.Agent = void 0;
3
+ exports.OpenAIAgent = exports.StructuredChatOutputParserWithRetries = exports.StructuredChatOutputParser = exports.StructuredChatAgent = exports.AgentActionOutputParser = exports.ZeroShotAgentOutputParser = exports.ZeroShotAgent = exports.initializeAgentExecutorWithOptions = exports.initializeAgentExecutor = exports.AgentExecutor = exports.ChatConversationalAgentOutputParserWithRetries = exports.ChatConversationalAgentOutputParser = exports.ChatConversationalAgent = exports.ChatAgentOutputParser = exports.ChatAgent = exports.Toolkit = exports.createVectorStoreRouterAgent = exports.createVectorStoreAgent = exports.createSqlAgent = exports.createOpenApiAgent = exports.createJsonAgent = exports.ZapierToolKit = exports.VectorStoreToolkit = exports.VectorStoreRouterToolkit = exports.SqlToolkit = exports.RequestsToolkit = exports.OpenApiToolkit = exports.JsonToolkit = exports.LLMSingleActionAgent = exports.BaseSingleActionAgent = exports.Agent = void 0;
4
4
  var agent_js_1 = require("./agent.cjs");
5
5
  Object.defineProperty(exports, "Agent", { enumerable: true, get: function () { return agent_js_1.Agent; } });
6
6
  Object.defineProperty(exports, "BaseSingleActionAgent", { enumerable: true, get: function () { return agent_js_1.BaseSingleActionAgent; } });
@@ -45,3 +45,5 @@ Object.defineProperty(exports, "StructuredChatAgent", { enumerable: true, get: f
45
45
  var outputParser_js_4 = require("./structured_chat/outputParser.cjs");
46
46
  Object.defineProperty(exports, "StructuredChatOutputParser", { enumerable: true, get: function () { return outputParser_js_4.StructuredChatOutputParser; } });
47
47
  Object.defineProperty(exports, "StructuredChatOutputParserWithRetries", { enumerable: true, get: function () { return outputParser_js_4.StructuredChatOutputParserWithRetries; } });
48
+ var index_js_6 = require("./openai/index.cjs");
49
+ Object.defineProperty(exports, "OpenAIAgent", { enumerable: true, get: function () { return index_js_6.OpenAIAgent; } });
@@ -12,3 +12,4 @@ export { ZeroShotAgentOutputParser } from "./mrkl/outputParser.js";
12
12
  export { AgentActionOutputParser, AgentInput, SerializedAgent, SerializedAgentT, SerializedZeroShotAgent, StoppingMethod, } from "./types.js";
13
13
  export { StructuredChatAgent, StructuredChatAgentInput, StructuredChatCreatePromptArgs, } from "./structured_chat/index.js";
14
14
  export { StructuredChatOutputParser, StructuredChatOutputParserArgs, StructuredChatOutputParserWithRetries, } from "./structured_chat/outputParser.js";
15
+ export { OpenAIAgent, OpenAIAgentInput, OpenAIAgentCreatePromptArgs, } from "./openai/index.js";
@@ -12,3 +12,4 @@ export { ZeroShotAgentOutputParser } from "./mrkl/outputParser.js";
12
12
  export { AgentActionOutputParser, } from "./types.js";
13
13
  export { StructuredChatAgent, } from "./structured_chat/index.js";
14
14
  export { StructuredChatOutputParser, StructuredChatOutputParserWithRetries, } from "./structured_chat/outputParser.js";
15
+ export { OpenAIAgent, } from "./openai/index.js";
@@ -79,6 +79,7 @@ async function initializeAgentExecutorWithOptions(tools, llm, options = {
79
79
  returnMessages: true,
80
80
  memoryKey: "chat_history",
81
81
  inputKey: "input",
82
+ outputKey: "output",
82
83
  }),
83
84
  ...rest,
84
85
  });
@@ -95,10 +96,17 @@ async function initializeAgentExecutorWithOptions(tools, llm, options = {
95
96
  return executor;
96
97
  }
97
98
  case "openai-functions": {
98
- const { agentArgs, ...rest } = options;
99
+ const { agentArgs, memory, ...rest } = options;
99
100
  const executor = executor_js_1.AgentExecutor.fromAgentAndTools({
100
101
  agent: index_js_5.OpenAIAgent.fromLLMAndTools(llm, tools, agentArgs),
101
102
  tools,
103
+ memory: memory ??
104
+ new buffer_memory_js_1.BufferMemory({
105
+ returnMessages: true,
106
+ memoryKey: "chat_history",
107
+ inputKey: "input",
108
+ outputKey: "output",
109
+ }),
102
110
  ...rest,
103
111
  });
104
112
  return executor;
@@ -36,7 +36,6 @@ export type InitializeAgentExecutorOptionsStructured = ({
36
36
  } & Omit<AgentExecutorInput, "agent" | "tools">) | ({
37
37
  agentType: "openai-functions";
38
38
  agentArgs?: Parameters<typeof OpenAIAgent.fromLLMAndTools>[2];
39
- memory?: never;
40
39
  } & Omit<AgentExecutorInput, "agent" | "tools">);
41
40
  /**
42
41
  * Initialize an agent executor with options
@@ -75,6 +75,7 @@ export async function initializeAgentExecutorWithOptions(tools, llm, options = {
75
75
  returnMessages: true,
76
76
  memoryKey: "chat_history",
77
77
  inputKey: "input",
78
+ outputKey: "output",
78
79
  }),
79
80
  ...rest,
80
81
  });
@@ -91,10 +92,17 @@ export async function initializeAgentExecutorWithOptions(tools, llm, options = {
91
92
  return executor;
92
93
  }
93
94
  case "openai-functions": {
94
- const { agentArgs, ...rest } = options;
95
+ const { agentArgs, memory, ...rest } = options;
95
96
  const executor = AgentExecutor.fromAgentAndTools({
96
97
  agent: OpenAIAgent.fromLLMAndTools(llm, tools, agentArgs),
97
98
  tools,
99
+ memory: memory ??
100
+ new BufferMemory({
101
+ returnMessages: true,
102
+ memoryKey: "chat_history",
103
+ inputKey: "input",
104
+ outputKey: "output",
105
+ }),
98
106
  ...rest,
99
107
  });
100
108
  return executor;
@@ -55,6 +55,7 @@ class OpenAIAgent extends agent_js_1.Agent {
55
55
  const { prefix = prompt_js_1.PREFIX } = fields || {};
56
56
  return chat_js_1.ChatPromptTemplate.fromPromptMessages([
57
57
  chat_js_1.SystemMessagePromptTemplate.fromTemplate(prefix),
58
+ new chat_js_1.MessagesPlaceholder("chat_history"),
58
59
  chat_js_1.HumanMessagePromptTemplate.fromTemplate("{input}"),
59
60
  new chat_js_1.MessagesPlaceholder("agent_scratchpad"),
60
61
  ]);
@@ -52,6 +52,7 @@ export class OpenAIAgent extends Agent {
52
52
  const { prefix = PREFIX } = fields || {};
53
53
  return ChatPromptTemplate.fromPromptMessages([
54
54
  SystemMessagePromptTemplate.fromTemplate(prefix),
55
+ new MessagesPlaceholder("chat_history"),
55
56
  HumanMessagePromptTemplate.fromTemplate("{input}"),
56
57
  new MessagesPlaceholder("agent_scratchpad"),
57
58
  ]);
@@ -39,11 +39,12 @@ class BaseChain extends index_js_2.BaseLangChain {
39
39
  async run(
40
40
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
41
  input, callbacks) {
42
- const isKeylessInput = this.inputKeys.length <= 1;
42
+ const inputKeys = this.inputKeys.filter((k) => !this.memory?.memoryKeys.includes(k) ?? true);
43
+ const isKeylessInput = inputKeys.length <= 1;
43
44
  if (!isKeylessInput) {
44
45
  throw new Error(`Chain ${this._chainType()} expects multiple inputs, cannot use 'run' `);
45
46
  }
46
- const values = this.inputKeys.length ? { [this.inputKeys[0]]: input } : {};
47
+ const values = inputKeys.length ? { [inputKeys[0]]: input } : {};
47
48
  const returnValues = await this.call(values, callbacks);
48
49
  const keys = Object.keys(returnValues);
49
50
  if (keys.length === 1) {
@@ -36,11 +36,12 @@ export class BaseChain extends BaseLangChain {
36
36
  async run(
37
37
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
38
  input, callbacks) {
39
- const isKeylessInput = this.inputKeys.length <= 1;
39
+ const inputKeys = this.inputKeys.filter((k) => !this.memory?.memoryKeys.includes(k) ?? true);
40
+ const isKeylessInput = inputKeys.length <= 1;
40
41
  if (!isKeylessInput) {
41
42
  throw new Error(`Chain ${this._chainType()} expects multiple inputs, cannot use 'run' `);
42
43
  }
43
- const values = this.inputKeys.length ? { [this.inputKeys[0]]: input } : {};
44
+ const values = inputKeys.length ? { [inputKeys[0]]: input } : {};
44
45
  const returnValues = await this.call(values, callbacks);
45
46
  const keys = Object.keys(returnValues);
46
47
  if (keys.length === 1) {
@@ -42,17 +42,21 @@ class StuffDocumentsChain extends base_js_1.BaseChain {
42
42
  this.inputKey = fields.inputKey ?? this.inputKey;
43
43
  }
44
44
  /** @ignore */
45
- async _call(values, runManager) {
45
+ _prepInputs(values) {
46
46
  if (!(this.inputKey in values)) {
47
47
  throw new Error(`Document key ${this.inputKey} not found.`);
48
48
  }
49
49
  const { [this.inputKey]: docs, ...rest } = values;
50
50
  const texts = docs.map(({ pageContent }) => pageContent);
51
51
  const text = texts.join("\n\n");
52
- const result = await this.llmChain.call({
52
+ return {
53
53
  ...rest,
54
54
  [this.documentVariableName]: text,
55
- }, runManager?.getChild("combine_documents"));
55
+ };
56
+ }
57
+ /** @ignore */
58
+ async _call(values, runManager) {
59
+ const result = await this.llmChain.call(this._prepInputs(values), runManager?.getChild("combine_documents"));
56
60
  return result;
57
61
  }
58
62
  _chainType() {
@@ -163,11 +167,11 @@ class MapReduceDocumentsChain extends base_js_1.BaseChain {
163
167
  const canSkipMapStep = i !== 0 || !this.ensureMapStep;
164
168
  if (canSkipMapStep) {
165
169
  // Calculate the total tokens required in the input
166
- const promises = inputs.map(async (i) => {
167
- const prompt = await this.llmChain.prompt.format(i);
168
- return this.llmChain.llm.getNumTokens(prompt);
169
- });
170
- const length = await Promise.all(promises).then((results) => results.reduce((a, b) => a + b, 0));
170
+ const formatted = await this.combineDocumentChain.llmChain.prompt.format(this.combineDocumentChain._prepInputs({
171
+ [this.combineDocumentChain.inputKey]: currentDocs,
172
+ ...rest,
173
+ }));
174
+ const length = await this.combineDocumentChain.llmChain.llm.getNumTokens(formatted);
171
175
  const withinTokenLimit = length < this.maxTokens;
172
176
  // If we can skip the map step, and we're within the token limit, we don't
173
177
  // need to run the map step, so just break out of the loop.
@@ -193,7 +197,10 @@ class MapReduceDocumentsChain extends base_js_1.BaseChain {
193
197
  }
194
198
  // Now, with the final result of all the inputs from the `llmChain`, we can
195
199
  // run the `combineDocumentChain` over them.
196
- const newInputs = { input_documents: currentDocs, ...rest };
200
+ const newInputs = {
201
+ [this.combineDocumentChain.inputKey]: currentDocs,
202
+ ...rest,
203
+ };
197
204
  const result = await this.combineDocumentChain.call(newInputs, runManager?.getChild("combine_documents"));
198
205
  // Return the intermediate steps results if the flag is set
199
206
  if (this.returnIntermediateSteps) {
@@ -213,7 +220,7 @@ class MapReduceDocumentsChain extends base_js_1.BaseChain {
213
220
  }
214
221
  return new MapReduceDocumentsChain({
215
222
  llmChain: await llm_chain_js_1.LLMChain.deserialize(data.llm_chain),
216
- combineDocumentChain: await base_js_1.BaseChain.deserialize(data.combine_document_chain),
223
+ combineDocumentChain: await StuffDocumentsChain.deserialize(data.combine_document_chain),
217
224
  });
218
225
  }
219
226
  serialize() {
@@ -25,6 +25,8 @@ export declare class StuffDocumentsChain extends BaseChain implements StuffDocum
25
25
  get outputKeys(): string[];
26
26
  constructor(fields: StuffDocumentsChainInput);
27
27
  /** @ignore */
28
+ _prepInputs(values: ChainValues): ChainValues;
29
+ /** @ignore */
28
30
  _call(values: ChainValues, runManager?: CallbackManagerForChainRun): Promise<ChainValues>;
29
31
  _chainType(): "stuff_documents_chain";
30
32
  static deserialize(data: SerializedStuffDocumentsChain): Promise<StuffDocumentsChain>;
@@ -38,7 +40,7 @@ export interface MapReduceDocumentsChainInput extends StuffDocumentsChainInput {
38
40
  /** Ensures that the map step is taken regardless of max tokens */
39
41
  ensureMapStep?: boolean;
40
42
  /** Chain to use to combine results of applying llm_chain to documents. */
41
- combineDocumentChain: BaseChain;
43
+ combineDocumentChain: StuffDocumentsChain;
42
44
  /** Return the results of the map steps in the output. */
43
45
  returnIntermediateSteps?: boolean;
44
46
  }
@@ -57,7 +59,7 @@ export declare class MapReduceDocumentsChain extends BaseChain implements MapRed
57
59
  maxTokens: number;
58
60
  maxIterations: number;
59
61
  ensureMapStep: boolean;
60
- combineDocumentChain: BaseChain;
62
+ combineDocumentChain: StuffDocumentsChain;
61
63
  constructor(fields: MapReduceDocumentsChainInput);
62
64
  /** @ignore */
63
65
  _call(values: ChainValues, runManager?: CallbackManagerForChainRun): Promise<ChainValues>;
@@ -39,17 +39,21 @@ export class StuffDocumentsChain extends BaseChain {
39
39
  this.inputKey = fields.inputKey ?? this.inputKey;
40
40
  }
41
41
  /** @ignore */
42
- async _call(values, runManager) {
42
+ _prepInputs(values) {
43
43
  if (!(this.inputKey in values)) {
44
44
  throw new Error(`Document key ${this.inputKey} not found.`);
45
45
  }
46
46
  const { [this.inputKey]: docs, ...rest } = values;
47
47
  const texts = docs.map(({ pageContent }) => pageContent);
48
48
  const text = texts.join("\n\n");
49
- const result = await this.llmChain.call({
49
+ return {
50
50
  ...rest,
51
51
  [this.documentVariableName]: text,
52
- }, runManager?.getChild("combine_documents"));
52
+ };
53
+ }
54
+ /** @ignore */
55
+ async _call(values, runManager) {
56
+ const result = await this.llmChain.call(this._prepInputs(values), runManager?.getChild("combine_documents"));
53
57
  return result;
54
58
  }
55
59
  _chainType() {
@@ -159,11 +163,11 @@ export class MapReduceDocumentsChain extends BaseChain {
159
163
  const canSkipMapStep = i !== 0 || !this.ensureMapStep;
160
164
  if (canSkipMapStep) {
161
165
  // Calculate the total tokens required in the input
162
- const promises = inputs.map(async (i) => {
163
- const prompt = await this.llmChain.prompt.format(i);
164
- return this.llmChain.llm.getNumTokens(prompt);
165
- });
166
- const length = await Promise.all(promises).then((results) => results.reduce((a, b) => a + b, 0));
166
+ const formatted = await this.combineDocumentChain.llmChain.prompt.format(this.combineDocumentChain._prepInputs({
167
+ [this.combineDocumentChain.inputKey]: currentDocs,
168
+ ...rest,
169
+ }));
170
+ const length = await this.combineDocumentChain.llmChain.llm.getNumTokens(formatted);
167
171
  const withinTokenLimit = length < this.maxTokens;
168
172
  // If we can skip the map step, and we're within the token limit, we don't
169
173
  // need to run the map step, so just break out of the loop.
@@ -189,7 +193,10 @@ export class MapReduceDocumentsChain extends BaseChain {
189
193
  }
190
194
  // Now, with the final result of all the inputs from the `llmChain`, we can
191
195
  // run the `combineDocumentChain` over them.
192
- const newInputs = { input_documents: currentDocs, ...rest };
196
+ const newInputs = {
197
+ [this.combineDocumentChain.inputKey]: currentDocs,
198
+ ...rest,
199
+ };
193
200
  const result = await this.combineDocumentChain.call(newInputs, runManager?.getChild("combine_documents"));
194
201
  // Return the intermediate steps results if the flag is set
195
202
  if (this.returnIntermediateSteps) {
@@ -209,7 +216,7 @@ export class MapReduceDocumentsChain extends BaseChain {
209
216
  }
210
217
  return new MapReduceDocumentsChain({
211
218
  llmChain: await LLMChain.deserialize(data.llm_chain),
212
- combineDocumentChain: await BaseChain.deserialize(data.combine_document_chain),
219
+ combineDocumentChain: await StuffDocumentsChain.deserialize(data.combine_document_chain),
213
220
  });
214
221
  }
215
222
  serialize() {
@@ -45,7 +45,7 @@ export type SerializedChatVectorDBQAChain = {
45
45
  export type SerializedMapReduceDocumentsChain = {
46
46
  _type: "map_reduce_documents_chain";
47
47
  llm_chain?: SerializedLLMChain;
48
- combine_document_chain?: SerializedBaseChain;
48
+ combine_document_chain?: SerializedStuffDocumentsChain;
49
49
  };
50
50
  export type SerializedRefineDocumentsChain = {
51
51
  _type: "refine_documents_chain";
@@ -35,6 +35,7 @@ exports.optionalImportEntrypoints = [
35
35
  "langchain/vectorstores/typeorm",
36
36
  "langchain/vectorstores/myscale",
37
37
  "langchain/vectorstores/redis",
38
+ "langchain/vectorstores/typesense",
38
39
  "langchain/vectorstores/singlestore",
39
40
  "langchain/vectorstores/tigris",
40
41
  "langchain/memory/zep",
@@ -32,6 +32,7 @@ export const optionalImportEntrypoints = [
32
32
  "langchain/vectorstores/typeorm",
33
33
  "langchain/vectorstores/myscale",
34
34
  "langchain/vectorstores/redis",
35
+ "langchain/vectorstores/typesense",
35
36
  "langchain/vectorstores/singlestore",
36
37
  "langchain/vectorstores/tigris",
37
38
  "langchain/memory/zep",
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Typesense = void 0;
4
+ const base_js_1 = require("./base.cjs");
5
+ const async_caller_js_1 = require("../util/async_caller.cjs");
6
+ /**
7
+ * Typesense vector store.
8
+ */
9
+ class Typesense extends base_js_1.VectorStore {
10
+ constructor(embeddings, config) {
11
+ super(embeddings, config);
12
+ Object.defineProperty(this, "client", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: void 0
17
+ });
18
+ Object.defineProperty(this, "schemaName", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "searchParams", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ Object.defineProperty(this, "vectorColumnName", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "pageContentColumnName", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "metadataColumnNames", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: void 0
47
+ });
48
+ Object.defineProperty(this, "caller", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ Object.defineProperty(this, "import", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: void 0
59
+ });
60
+ // Assign config values to class properties.
61
+ this.client = config.typesenseClient;
62
+ this.schemaName = config.schemaName;
63
+ this.searchParams = config.searchParams || {
64
+ q: "*",
65
+ per_page: 5,
66
+ query_by: "",
67
+ };
68
+ this.vectorColumnName = config.columnNames?.vector || "vec";
69
+ this.pageContentColumnName = config.columnNames?.pageContent || "text";
70
+ this.metadataColumnNames = config.columnNames?.metadataColumnNames || [];
71
+ // Assign import function.
72
+ this.import = config.import || this.importToTypesense.bind(this);
73
+ this.caller = new async_caller_js_1.AsyncCaller(config);
74
+ }
75
+ /**
76
+ * Default function to import data to typesense
77
+ * @param data
78
+ * @param collectionName
79
+ */
80
+ async importToTypesense(data, collectionName) {
81
+ const chunkSize = 2000;
82
+ for (let i = 0; i < data.length; i += chunkSize) {
83
+ const chunk = data.slice(i, i + chunkSize);
84
+ await this.caller.call(async () => {
85
+ await this.client
86
+ .collections(collectionName)
87
+ .documents()
88
+ .import(chunk, { action: "emplace", dirty_values: "drop" });
89
+ });
90
+ }
91
+ }
92
+ /**
93
+ * Transform documents to Typesense records.
94
+ * @param documents
95
+ * @returns Typesense records.
96
+ */
97
+ _documentsToTypesenseRecords(documents, vectors) {
98
+ const metadatas = documents.map((doc) => doc.metadata);
99
+ const typesenseDocuments = documents.map((doc, index) => {
100
+ const metadata = metadatas[index];
101
+ const objectWithMetadatas = {};
102
+ this.metadataColumnNames.forEach((metadataColumnName) => {
103
+ objectWithMetadatas[metadataColumnName] = metadata[metadataColumnName];
104
+ });
105
+ return {
106
+ [this.pageContentColumnName]: doc.pageContent,
107
+ [this.vectorColumnName]: vectors[index],
108
+ ...objectWithMetadatas,
109
+ };
110
+ });
111
+ return typesenseDocuments;
112
+ }
113
+ /**
114
+ * Transform the Typesense records to documents.
115
+ * @param typesenseRecords
116
+ * @returns documents
117
+ */
118
+ _typesenseRecordsToDocuments(typesenseRecords) {
119
+ const documents = typesenseRecords?.map((hit) => {
120
+ const objectWithMetadatas = {};
121
+ this.metadataColumnNames.forEach((metadataColumnName) => {
122
+ objectWithMetadatas[metadataColumnName] = hit[metadataColumnName];
123
+ });
124
+ const document = {
125
+ pageContent: hit[this.pageContentColumnName] || "",
126
+ metadata: objectWithMetadatas,
127
+ };
128
+ return document;
129
+ }) || [];
130
+ return documents;
131
+ }
132
+ /**
133
+ * Add documents to the vector store.
134
+ * Will be updated if in the metadata there is a document with the same id if is using the default import function.
135
+ * Metadata will be added in the columns of the schema based on metadataColumnNames.
136
+ * @param documents Documents to add.
137
+ */
138
+ async addDocuments(documents) {
139
+ const typesenseDocuments = this._documentsToTypesenseRecords(documents, await this.embeddings.embedDocuments(documents.map((doc) => doc.pageContent)));
140
+ await this.import(typesenseDocuments, this.schemaName);
141
+ }
142
+ async addVectors(vectors, documents) {
143
+ const typesenseDocuments = this._documentsToTypesenseRecords(documents, vectors);
144
+ await this.import(typesenseDocuments, this.schemaName);
145
+ }
146
+ /**
147
+ * Search for similar documents with their similarity score.
148
+ * All the documents have 0 as similarity score because Typesense API
149
+ * does not return the similarity score.
150
+ * @param vectorPrompt vector to search for
151
+ * @param k amount of results to return
152
+ * @returns similar documents with their similarity score
153
+ */
154
+ async similaritySearchVectorWithScore(vectorPrompt, k, filter = {}) {
155
+ const amount = k || this.searchParams.per_page || 5;
156
+ const vector_query = `${this.vectorColumnName}:([${vectorPrompt}], k:${amount})`;
157
+ const typesenseResponse = await this.client.multiSearch.perform({
158
+ searches: [
159
+ {
160
+ ...this.searchParams,
161
+ ...filter,
162
+ per_page: amount,
163
+ vector_query,
164
+ collection: this.schemaName,
165
+ },
166
+ ],
167
+ }, {});
168
+ const results = typesenseResponse.results[0].hits;
169
+ const hits = results?.map((hit) => hit.document);
170
+ const documents = this._typesenseRecordsToDocuments(hits).map((doc) => [doc, 0]);
171
+ return documents;
172
+ }
173
+ /**
174
+ * Delete documents from the vector store.
175
+ * @param documentIds ids of the documents to delete
176
+ */
177
+ async deleteDocuments(documentIds) {
178
+ await this.client
179
+ .collections(this.schemaName)
180
+ .documents()
181
+ .delete({
182
+ filter_by: `id:=${documentIds.join(",")}`,
183
+ });
184
+ }
185
+ /**
186
+ * Create a vector store from documents.
187
+ * @param docs documents
188
+ * @param embeddings embeddings
189
+ * @param config Typesense configuration
190
+ * @returns Typesense vector store
191
+ * @warning You can omit this method, and only use the constructor and addDocuments.
192
+ */
193
+ static async fromDocuments(docs, embeddings, config) {
194
+ const instance = new Typesense(embeddings, config);
195
+ await instance.addDocuments(docs);
196
+ return instance;
197
+ }
198
+ /**
199
+ * Create a vector store from texts.
200
+ * @param texts
201
+ * @param metadatas
202
+ * @param embeddings
203
+ * @param config
204
+ * @returns Typesense vector store
205
+ */
206
+ static async fromTexts(texts, metadatas, embeddings, config) {
207
+ const instance = new Typesense(embeddings, config);
208
+ const documents = texts.map((text, i) => ({
209
+ pageContent: text,
210
+ metadata: metadatas[i] || {},
211
+ }));
212
+ await instance.addDocuments(documents);
213
+ return instance;
214
+ }
215
+ }
216
+ exports.Typesense = Typesense;
@@ -0,0 +1,124 @@
1
+ import type { Client } from "typesense";
2
+ import type { MultiSearchRequestSchema } from "typesense/lib/Typesense/MultiSearch.js";
3
+ import type { Document } from "../document.js";
4
+ import { Embeddings } from "../embeddings/base.js";
5
+ import { VectorStore } from "./base.js";
6
+ import { AsyncCallerParams } from "../util/async_caller.js";
7
+ /**
8
+ * Typesense vector store configuration.
9
+ */
10
+ export interface TypesenseConfig extends AsyncCallerParams {
11
+ /**
12
+ * Typesense client.
13
+ */
14
+ typesenseClient: Client;
15
+ /**
16
+ * Typesense schema name in which documents will be stored and searched.
17
+ */
18
+ schemaName: string;
19
+ /**
20
+ * Typesense search parameters.
21
+ * @default { q: '*', per_page: 5, query_by: '' }
22
+ */
23
+ searchParams?: MultiSearchRequestSchema;
24
+ /**
25
+ * Column names.
26
+ */
27
+ columnNames?: {
28
+ /**
29
+ * Vector column name.
30
+ * @default 'vec'
31
+ */
32
+ vector?: string;
33
+ /**
34
+ * Page content column name.
35
+ * @default 'text'
36
+ */
37
+ pageContent?: string;
38
+ /**
39
+ * Metadata column names.
40
+ * @default []
41
+ */
42
+ metadataColumnNames?: string[];
43
+ };
44
+ /**
45
+ * Replace default import function.
46
+ * Default import function will update documents if there is a document with the same id.
47
+ * @param data
48
+ * @param collectionName
49
+ */
50
+ import?<T extends Record<string, unknown> = Record<string, unknown>>(data: T[], collectionName: string): Promise<void>;
51
+ }
52
+ /**
53
+ * Typesense vector store.
54
+ */
55
+ export declare class Typesense extends VectorStore {
56
+ FilterType: Partial<MultiSearchRequestSchema>;
57
+ private client;
58
+ private schemaName;
59
+ private searchParams;
60
+ private vectorColumnName;
61
+ private pageContentColumnName;
62
+ private metadataColumnNames;
63
+ private caller;
64
+ private import;
65
+ constructor(embeddings: Embeddings, config: TypesenseConfig);
66
+ /**
67
+ * Default function to import data to typesense
68
+ * @param data
69
+ * @param collectionName
70
+ */
71
+ private importToTypesense;
72
+ /**
73
+ * Transform documents to Typesense records.
74
+ * @param documents
75
+ * @returns Typesense records.
76
+ */
77
+ _documentsToTypesenseRecords(documents: Document[], vectors: number[][]): Record<string, unknown>[];
78
+ /**
79
+ * Transform the Typesense records to documents.
80
+ * @param typesenseRecords
81
+ * @returns documents
82
+ */
83
+ _typesenseRecordsToDocuments(typesenseRecords: Record<string, unknown>[] | undefined): Document[];
84
+ /**
85
+ * Add documents to the vector store.
86
+ * Will be updated if in the metadata there is a document with the same id if is using the default import function.
87
+ * Metadata will be added in the columns of the schema based on metadataColumnNames.
88
+ * @param documents Documents to add.
89
+ */
90
+ addDocuments(documents: Document[]): Promise<void>;
91
+ addVectors(vectors: number[][], documents: Document[]): Promise<void>;
92
+ /**
93
+ * Search for similar documents with their similarity score.
94
+ * All the documents have 0 as similarity score because Typesense API
95
+ * does not return the similarity score.
96
+ * @param vectorPrompt vector to search for
97
+ * @param k amount of results to return
98
+ * @returns similar documents with their similarity score
99
+ */
100
+ similaritySearchVectorWithScore(vectorPrompt: number[], k?: number, filter?: this["FilterType"]): Promise<[Document<Record<string, unknown>>, number][]>;
101
+ /**
102
+ * Delete documents from the vector store.
103
+ * @param documentIds ids of the documents to delete
104
+ */
105
+ deleteDocuments(documentIds: string[]): Promise<void>;
106
+ /**
107
+ * Create a vector store from documents.
108
+ * @param docs documents
109
+ * @param embeddings embeddings
110
+ * @param config Typesense configuration
111
+ * @returns Typesense vector store
112
+ * @warning You can omit this method, and only use the constructor and addDocuments.
113
+ */
114
+ static fromDocuments(docs: Document[], embeddings: Embeddings, config: TypesenseConfig): Promise<Typesense>;
115
+ /**
116
+ * Create a vector store from texts.
117
+ * @param texts
118
+ * @param metadatas
119
+ * @param embeddings
120
+ * @param config
121
+ * @returns Typesense vector store
122
+ */
123
+ static fromTexts(texts: string[], metadatas: object[], embeddings: Embeddings, config: TypesenseConfig): Promise<Typesense>;
124
+ }
@@ -0,0 +1,212 @@
1
+ import { VectorStore } from "./base.js";
2
+ import { AsyncCaller } from "../util/async_caller.js";
3
+ /**
4
+ * Typesense vector store.
5
+ */
6
+ export class Typesense extends VectorStore {
7
+ constructor(embeddings, config) {
8
+ super(embeddings, config);
9
+ Object.defineProperty(this, "client", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: void 0
14
+ });
15
+ Object.defineProperty(this, "schemaName", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: void 0
20
+ });
21
+ Object.defineProperty(this, "searchParams", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: void 0
26
+ });
27
+ Object.defineProperty(this, "vectorColumnName", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "pageContentColumnName", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "metadataColumnNames", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "caller", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ Object.defineProperty(this, "import", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ // Assign config values to class properties.
58
+ this.client = config.typesenseClient;
59
+ this.schemaName = config.schemaName;
60
+ this.searchParams = config.searchParams || {
61
+ q: "*",
62
+ per_page: 5,
63
+ query_by: "",
64
+ };
65
+ this.vectorColumnName = config.columnNames?.vector || "vec";
66
+ this.pageContentColumnName = config.columnNames?.pageContent || "text";
67
+ this.metadataColumnNames = config.columnNames?.metadataColumnNames || [];
68
+ // Assign import function.
69
+ this.import = config.import || this.importToTypesense.bind(this);
70
+ this.caller = new AsyncCaller(config);
71
+ }
72
+ /**
73
+ * Default function to import data to typesense
74
+ * @param data
75
+ * @param collectionName
76
+ */
77
+ async importToTypesense(data, collectionName) {
78
+ const chunkSize = 2000;
79
+ for (let i = 0; i < data.length; i += chunkSize) {
80
+ const chunk = data.slice(i, i + chunkSize);
81
+ await this.caller.call(async () => {
82
+ await this.client
83
+ .collections(collectionName)
84
+ .documents()
85
+ .import(chunk, { action: "emplace", dirty_values: "drop" });
86
+ });
87
+ }
88
+ }
89
+ /**
90
+ * Transform documents to Typesense records.
91
+ * @param documents
92
+ * @returns Typesense records.
93
+ */
94
+ _documentsToTypesenseRecords(documents, vectors) {
95
+ const metadatas = documents.map((doc) => doc.metadata);
96
+ const typesenseDocuments = documents.map((doc, index) => {
97
+ const metadata = metadatas[index];
98
+ const objectWithMetadatas = {};
99
+ this.metadataColumnNames.forEach((metadataColumnName) => {
100
+ objectWithMetadatas[metadataColumnName] = metadata[metadataColumnName];
101
+ });
102
+ return {
103
+ [this.pageContentColumnName]: doc.pageContent,
104
+ [this.vectorColumnName]: vectors[index],
105
+ ...objectWithMetadatas,
106
+ };
107
+ });
108
+ return typesenseDocuments;
109
+ }
110
+ /**
111
+ * Transform the Typesense records to documents.
112
+ * @param typesenseRecords
113
+ * @returns documents
114
+ */
115
+ _typesenseRecordsToDocuments(typesenseRecords) {
116
+ const documents = typesenseRecords?.map((hit) => {
117
+ const objectWithMetadatas = {};
118
+ this.metadataColumnNames.forEach((metadataColumnName) => {
119
+ objectWithMetadatas[metadataColumnName] = hit[metadataColumnName];
120
+ });
121
+ const document = {
122
+ pageContent: hit[this.pageContentColumnName] || "",
123
+ metadata: objectWithMetadatas,
124
+ };
125
+ return document;
126
+ }) || [];
127
+ return documents;
128
+ }
129
+ /**
130
+ * Add documents to the vector store.
131
+ * Will be updated if in the metadata there is a document with the same id if is using the default import function.
132
+ * Metadata will be added in the columns of the schema based on metadataColumnNames.
133
+ * @param documents Documents to add.
134
+ */
135
+ async addDocuments(documents) {
136
+ const typesenseDocuments = this._documentsToTypesenseRecords(documents, await this.embeddings.embedDocuments(documents.map((doc) => doc.pageContent)));
137
+ await this.import(typesenseDocuments, this.schemaName);
138
+ }
139
+ async addVectors(vectors, documents) {
140
+ const typesenseDocuments = this._documentsToTypesenseRecords(documents, vectors);
141
+ await this.import(typesenseDocuments, this.schemaName);
142
+ }
143
+ /**
144
+ * Search for similar documents with their similarity score.
145
+ * All the documents have 0 as similarity score because Typesense API
146
+ * does not return the similarity score.
147
+ * @param vectorPrompt vector to search for
148
+ * @param k amount of results to return
149
+ * @returns similar documents with their similarity score
150
+ */
151
+ async similaritySearchVectorWithScore(vectorPrompt, k, filter = {}) {
152
+ const amount = k || this.searchParams.per_page || 5;
153
+ const vector_query = `${this.vectorColumnName}:([${vectorPrompt}], k:${amount})`;
154
+ const typesenseResponse = await this.client.multiSearch.perform({
155
+ searches: [
156
+ {
157
+ ...this.searchParams,
158
+ ...filter,
159
+ per_page: amount,
160
+ vector_query,
161
+ collection: this.schemaName,
162
+ },
163
+ ],
164
+ }, {});
165
+ const results = typesenseResponse.results[0].hits;
166
+ const hits = results?.map((hit) => hit.document);
167
+ const documents = this._typesenseRecordsToDocuments(hits).map((doc) => [doc, 0]);
168
+ return documents;
169
+ }
170
+ /**
171
+ * Delete documents from the vector store.
172
+ * @param documentIds ids of the documents to delete
173
+ */
174
+ async deleteDocuments(documentIds) {
175
+ await this.client
176
+ .collections(this.schemaName)
177
+ .documents()
178
+ .delete({
179
+ filter_by: `id:=${documentIds.join(",")}`,
180
+ });
181
+ }
182
+ /**
183
+ * Create a vector store from documents.
184
+ * @param docs documents
185
+ * @param embeddings embeddings
186
+ * @param config Typesense configuration
187
+ * @returns Typesense vector store
188
+ * @warning You can omit this method, and only use the constructor and addDocuments.
189
+ */
190
+ static async fromDocuments(docs, embeddings, config) {
191
+ const instance = new Typesense(embeddings, config);
192
+ await instance.addDocuments(docs);
193
+ return instance;
194
+ }
195
+ /**
196
+ * Create a vector store from texts.
197
+ * @param texts
198
+ * @param metadatas
199
+ * @param embeddings
200
+ * @param config
201
+ * @returns Typesense vector store
202
+ */
203
+ static async fromTexts(texts, metadatas, embeddings, config) {
204
+ const instance = new Typesense(embeddings, config);
205
+ const documents = texts.map((text, i) => ({
206
+ pageContent: text,
207
+ metadata: metadatas[i] || {},
208
+ }));
209
+ await instance.addDocuments(documents);
210
+ return instance;
211
+ }
212
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langchain",
3
- "version": "0.0.94",
3
+ "version": "0.0.95",
4
4
  "description": "Typescript bindings for langchain",
5
5
  "type": "module",
6
6
  "engines": {
@@ -166,6 +166,9 @@
166
166
  "vectorstores/redis.cjs",
167
167
  "vectorstores/redis.js",
168
168
  "vectorstores/redis.d.ts",
169
+ "vectorstores/typesense.cjs",
170
+ "vectorstores/typesense.js",
171
+ "vectorstores/typesense.d.ts",
169
172
  "vectorstores/singlestore.cjs",
170
173
  "vectorstores/singlestore.js",
171
174
  "vectorstores/singlestore.d.ts",
@@ -508,6 +511,7 @@
508
511
  "ts-jest": "^29.1.0",
509
512
  "typeorm": "^0.3.12",
510
513
  "typescript": "^5.0.0",
514
+ "typesense": "^1.5.3",
511
515
  "weaviate-ts-client": "^1.0.0"
512
516
  },
513
517
  "peerDependencies": {
@@ -557,6 +561,7 @@
557
561
  "replicate": "^0.9.0",
558
562
  "srt-parser-2": "^1.2.2",
559
563
  "typeorm": "^0.3.12",
564
+ "typesense": "^1.5.3",
560
565
  "weaviate-ts-client": "^1.0.0"
561
566
  },
562
567
  "peerDependenciesMeta": {
@@ -698,6 +703,9 @@
698
703
  "typeorm": {
699
704
  "optional": true
700
705
  },
706
+ "typesense": {
707
+ "optional": true
708
+ },
701
709
  "weaviate-ts-client": {
702
710
  "optional": true
703
711
  }
@@ -1012,6 +1020,11 @@
1012
1020
  "import": "./vectorstores/redis.js",
1013
1021
  "require": "./vectorstores/redis.cjs"
1014
1022
  },
1023
+ "./vectorstores/typesense": {
1024
+ "types": "./vectorstores/typesense.d.ts",
1025
+ "import": "./vectorstores/typesense.js",
1026
+ "require": "./vectorstores/typesense.cjs"
1027
+ },
1015
1028
  "./vectorstores/singlestore": {
1016
1029
  "types": "./vectorstores/singlestore.d.ts",
1017
1030
  "import": "./vectorstores/singlestore.js",
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/vectorstores/typesense.cjs');
@@ -0,0 +1 @@
1
+ export * from '../dist/vectorstores/typesense.js'
@@ -0,0 +1 @@
1
+ export * from '../dist/vectorstores/typesense.js'