langchain 0.0.137 → 0.0.138

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.
@@ -0,0 +1,104 @@
1
+ import { Embeddings, EmbeddingsParams } from "./base.js";
2
+ import { ConfigurationParameters } from "../chat_models/minimax.js";
3
+ /**
4
+ * Interface for MinimaxEmbeddings parameters. Extends EmbeddingsParams and
5
+ * defines additional parameters specific to the MinimaxEmbeddings class.
6
+ */
7
+ export interface MinimaxEmbeddingsParams extends EmbeddingsParams {
8
+ /** Model name to use */
9
+ modelName: string;
10
+ /**
11
+ * API key to use when making requests. Defaults to the value of
12
+ * `MINIMAX_GROUP_ID` environment variable.
13
+ */
14
+ minimaxGroupId?: string;
15
+ /**
16
+ * Secret key to use when making requests. Defaults to the value of
17
+ * `MINIMAX_API_KEY` environment variable.
18
+ */
19
+ minimaxApiKey?: string;
20
+ /**
21
+ * The maximum number of documents to embed in a single request. This is
22
+ * limited by the Minimax API to a maximum of 4096.
23
+ */
24
+ batchSize?: number;
25
+ /**
26
+ * Whether to strip new lines from the input text. This is recommended by
27
+ * Minimax, but may not be suitable for all use cases.
28
+ */
29
+ stripNewLines?: boolean;
30
+ /**
31
+ * The target use-case after generating the vector.
32
+ * When using embeddings, the vector of the target content is first generated through the db and stored in the vector database,
33
+ * and then the vector of the retrieval text is generated through the query.
34
+ * Note: For the parameters of the partial algorithm, we adopted a separate algorithm plan for query and db.
35
+ * Therefore, for a paragraph of text, if it is to be used as a retrieval text, it should use the db,
36
+ * and if it is used as a retrieval text, it should use the query.
37
+ */
38
+ type?: "db" | "query";
39
+ }
40
+ export interface CreateMinimaxEmbeddingRequest {
41
+ /**
42
+ * @type {string}
43
+ * @memberof CreateMinimaxEmbeddingRequest
44
+ */
45
+ model: string;
46
+ /**
47
+ * Text to generate vector expectation
48
+ * @type {CreateEmbeddingRequestInput}
49
+ * @memberof CreateMinimaxEmbeddingRequest
50
+ */
51
+ texts: string[];
52
+ /**
53
+ * The target use-case after generating the vector. When using embeddings,
54
+ * first generate the vector of the target content through the db and store it in the vector database,
55
+ * and then generate the vector of the retrieval text through the query.
56
+ * Note: For the parameter of the algorithm, we use the algorithm scheme of query and db separation,
57
+ * so a text, if it is to be retrieved as a text, should use the db,
58
+ * if it is used as a retrieval text, should use the query.
59
+ * @type {string}
60
+ * @memberof CreateMinimaxEmbeddingRequest
61
+ */
62
+ type: "db" | "query";
63
+ }
64
+ /**
65
+ * Class for generating embeddings using the Minimax API. Extends the
66
+ * Embeddings class and implements MinimaxEmbeddingsParams
67
+ */
68
+ export declare class MinimaxEmbeddings extends Embeddings implements MinimaxEmbeddingsParams {
69
+ modelName: string;
70
+ batchSize: number;
71
+ stripNewLines: boolean;
72
+ minimaxGroupId?: string;
73
+ minimaxApiKey?: string;
74
+ type: "db" | "query";
75
+ apiUrl: string;
76
+ basePath?: string;
77
+ headers?: Record<string, string>;
78
+ constructor(fields?: Partial<MinimaxEmbeddingsParams> & {
79
+ configuration?: ConfigurationParameters;
80
+ });
81
+ /**
82
+ * Method to generate embeddings for an array of documents. Splits the
83
+ * documents into batches and makes requests to the Minimax API to generate
84
+ * embeddings.
85
+ * @param texts Array of documents to generate embeddings for.
86
+ * @returns Promise that resolves to a 2D array of embeddings for each document.
87
+ */
88
+ embedDocuments(texts: string[]): Promise<number[][]>;
89
+ /**
90
+ * Method to generate an embedding for a single document. Calls the
91
+ * embeddingWithRetry method with the document as the input.
92
+ * @param text Document to generate an embedding for.
93
+ * @returns Promise that resolves to an embedding for the document.
94
+ */
95
+ embedQuery(text: string): Promise<number[]>;
96
+ /**
97
+ * Private method to make a request to the Minimax API to generate
98
+ * embeddings. Handles the retry logic and returns the response from the
99
+ * API.
100
+ * @param request Request to send to the Minimax API.
101
+ * @returns Promise that resolves to the response from the API.
102
+ */
103
+ private embeddingWithRetry;
104
+ }
@@ -0,0 +1,148 @@
1
+ import { getEnvironmentVariable } from "../util/env.js";
2
+ import { chunkArray } from "../util/chunk.js";
3
+ import { Embeddings } from "./base.js";
4
+ /**
5
+ * Class for generating embeddings using the Minimax API. Extends the
6
+ * Embeddings class and implements MinimaxEmbeddingsParams
7
+ */
8
+ export class MinimaxEmbeddings extends Embeddings {
9
+ constructor(fields) {
10
+ const fieldsWithDefaults = { maxConcurrency: 2, ...fields };
11
+ super(fieldsWithDefaults);
12
+ Object.defineProperty(this, "modelName", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: "embo-01"
17
+ });
18
+ Object.defineProperty(this, "batchSize", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: 512
23
+ });
24
+ Object.defineProperty(this, "stripNewLines", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: true
29
+ });
30
+ Object.defineProperty(this, "minimaxGroupId", {
31
+ enumerable: true,
32
+ configurable: true,
33
+ writable: true,
34
+ value: void 0
35
+ });
36
+ Object.defineProperty(this, "minimaxApiKey", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "type", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: "db"
47
+ });
48
+ Object.defineProperty(this, "apiUrl", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: void 0
53
+ });
54
+ Object.defineProperty(this, "basePath", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: "https://api.minimax.chat/v1"
59
+ });
60
+ Object.defineProperty(this, "headers", {
61
+ enumerable: true,
62
+ configurable: true,
63
+ writable: true,
64
+ value: void 0
65
+ });
66
+ this.minimaxGroupId =
67
+ fields?.minimaxGroupId ?? getEnvironmentVariable("MINIMAX_GROUP_ID");
68
+ if (!this.minimaxGroupId) {
69
+ throw new Error("Minimax GroupID not found");
70
+ }
71
+ this.minimaxApiKey =
72
+ fields?.minimaxApiKey ?? getEnvironmentVariable("MINIMAX_API_KEY");
73
+ if (!this.minimaxApiKey) {
74
+ throw new Error("Minimax ApiKey not found");
75
+ }
76
+ this.modelName = fieldsWithDefaults?.modelName ?? this.modelName;
77
+ this.batchSize = fieldsWithDefaults?.batchSize ?? this.batchSize;
78
+ this.type = fieldsWithDefaults?.type ?? this.type;
79
+ this.stripNewLines =
80
+ fieldsWithDefaults?.stripNewLines ?? this.stripNewLines;
81
+ this.apiUrl = `${this.basePath}/embeddings`;
82
+ this.basePath = fields?.configuration?.basePath ?? this.basePath;
83
+ this.headers = fields?.configuration?.headers ?? this.headers;
84
+ }
85
+ /**
86
+ * Method to generate embeddings for an array of documents. Splits the
87
+ * documents into batches and makes requests to the Minimax API to generate
88
+ * embeddings.
89
+ * @param texts Array of documents to generate embeddings for.
90
+ * @returns Promise that resolves to a 2D array of embeddings for each document.
91
+ */
92
+ async embedDocuments(texts) {
93
+ const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
94
+ const batchRequests = batches.map((batch) => this.embeddingWithRetry({
95
+ model: this.modelName,
96
+ texts: batch,
97
+ type: this.type,
98
+ }));
99
+ const batchResponses = await Promise.all(batchRequests);
100
+ const embeddings = [];
101
+ for (let i = 0; i < batchResponses.length; i += 1) {
102
+ const batch = batches[i];
103
+ const { vectors: batchResponse } = batchResponses[i];
104
+ for (let j = 0; j < batch.length; j += 1) {
105
+ embeddings.push(batchResponse[j]);
106
+ }
107
+ }
108
+ return embeddings;
109
+ }
110
+ /**
111
+ * Method to generate an embedding for a single document. Calls the
112
+ * embeddingWithRetry method with the document as the input.
113
+ * @param text Document to generate an embedding for.
114
+ * @returns Promise that resolves to an embedding for the document.
115
+ */
116
+ async embedQuery(text) {
117
+ const { vectors } = await this.embeddingWithRetry({
118
+ model: this.modelName,
119
+ texts: [this.stripNewLines ? text.replace(/\n/g, " ") : text],
120
+ type: this.type,
121
+ });
122
+ return vectors[0];
123
+ }
124
+ /**
125
+ * Private method to make a request to the Minimax API to generate
126
+ * embeddings. Handles the retry logic and returns the response from the
127
+ * API.
128
+ * @param request Request to send to the Minimax API.
129
+ * @returns Promise that resolves to the response from the API.
130
+ */
131
+ async embeddingWithRetry(request) {
132
+ const makeCompletionRequest = async () => {
133
+ const url = `${this.apiUrl}?GroupId=${this.minimaxGroupId}`;
134
+ const response = await fetch(url, {
135
+ method: "POST",
136
+ headers: {
137
+ "Content-Type": "application/json",
138
+ Authorization: `Bearer ${this.minimaxApiKey}`,
139
+ ...this.headers,
140
+ },
141
+ body: JSON.stringify(request),
142
+ });
143
+ const json = await response.json();
144
+ return json;
145
+ };
146
+ return this.caller.call(makeCompletionRequest);
147
+ }
148
+ }
@@ -32,6 +32,42 @@ class Ollama extends base_js_1.LLM {
32
32
  writable: true,
33
33
  value: "http://localhost:11434"
34
34
  });
35
+ Object.defineProperty(this, "embeddingOnly", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ Object.defineProperty(this, "f16KV", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
47
+ Object.defineProperty(this, "frequencyPenalty", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, "logitsAll", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "lowVram", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ Object.defineProperty(this, "mainGpu", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
35
71
  Object.defineProperty(this, "mirostat", {
36
72
  enumerable: true,
37
73
  configurable: true,
@@ -50,6 +86,12 @@ class Ollama extends base_js_1.LLM {
50
86
  writable: true,
51
87
  value: void 0
52
88
  });
89
+ Object.defineProperty(this, "numBatch", {
90
+ enumerable: true,
91
+ configurable: true,
92
+ writable: true,
93
+ value: void 0
94
+ });
53
95
  Object.defineProperty(this, "numCtx", {
54
96
  enumerable: true,
55
97
  configurable: true,
@@ -62,12 +104,36 @@ class Ollama extends base_js_1.LLM {
62
104
  writable: true,
63
105
  value: void 0
64
106
  });
107
+ Object.defineProperty(this, "numGqa", {
108
+ enumerable: true,
109
+ configurable: true,
110
+ writable: true,
111
+ value: void 0
112
+ });
113
+ Object.defineProperty(this, "numKeep", {
114
+ enumerable: true,
115
+ configurable: true,
116
+ writable: true,
117
+ value: void 0
118
+ });
65
119
  Object.defineProperty(this, "numThread", {
66
120
  enumerable: true,
67
121
  configurable: true,
68
122
  writable: true,
69
123
  value: void 0
70
124
  });
125
+ Object.defineProperty(this, "penalizeNewline", {
126
+ enumerable: true,
127
+ configurable: true,
128
+ writable: true,
129
+ value: void 0
130
+ });
131
+ Object.defineProperty(this, "presencePenalty", {
132
+ enumerable: true,
133
+ configurable: true,
134
+ writable: true,
135
+ value: void 0
136
+ });
71
137
  Object.defineProperty(this, "repeatLastN", {
72
138
  enumerable: true,
73
139
  configurable: true,
@@ -80,6 +146,18 @@ class Ollama extends base_js_1.LLM {
80
146
  writable: true,
81
147
  value: void 0
82
148
  });
149
+ Object.defineProperty(this, "ropeFrequencyBase", {
150
+ enumerable: true,
151
+ configurable: true,
152
+ writable: true,
153
+ value: void 0
154
+ });
155
+ Object.defineProperty(this, "ropeFrequencyScale", {
156
+ enumerable: true,
157
+ configurable: true,
158
+ writable: true,
159
+ value: void 0
160
+ });
83
161
  Object.defineProperty(this, "temperature", {
84
162
  enumerable: true,
85
163
  configurable: true,
@@ -110,23 +188,64 @@ class Ollama extends base_js_1.LLM {
110
188
  writable: true,
111
189
  value: void 0
112
190
  });
191
+ Object.defineProperty(this, "typicalP", {
192
+ enumerable: true,
193
+ configurable: true,
194
+ writable: true,
195
+ value: void 0
196
+ });
197
+ Object.defineProperty(this, "useMLock", {
198
+ enumerable: true,
199
+ configurable: true,
200
+ writable: true,
201
+ value: void 0
202
+ });
203
+ Object.defineProperty(this, "useMMap", {
204
+ enumerable: true,
205
+ configurable: true,
206
+ writable: true,
207
+ value: void 0
208
+ });
209
+ Object.defineProperty(this, "vocabOnly", {
210
+ enumerable: true,
211
+ configurable: true,
212
+ writable: true,
213
+ value: void 0
214
+ });
113
215
  this.model = fields.model ?? this.model;
114
216
  this.baseUrl = fields.baseUrl?.endsWith("/")
115
217
  ? fields.baseUrl.slice(0, -1)
116
218
  : fields.baseUrl ?? this.baseUrl;
219
+ this.embeddingOnly = fields.embeddingOnly;
220
+ this.f16KV = fields.f16KV;
221
+ this.frequencyPenalty = fields.frequencyPenalty;
222
+ this.logitsAll = fields.logitsAll;
223
+ this.lowVram = fields.lowVram;
224
+ this.mainGpu = fields.mainGpu;
117
225
  this.mirostat = fields.mirostat;
118
226
  this.mirostatEta = fields.mirostatEta;
119
227
  this.mirostatTau = fields.mirostatTau;
228
+ this.numBatch = fields.numBatch;
120
229
  this.numCtx = fields.numCtx;
121
230
  this.numGpu = fields.numGpu;
231
+ this.numGqa = fields.numGqa;
232
+ this.numKeep = fields.numKeep;
122
233
  this.numThread = fields.numThread;
234
+ this.penalizeNewline = fields.penalizeNewline;
235
+ this.presencePenalty = fields.presencePenalty;
123
236
  this.repeatLastN = fields.repeatLastN;
124
237
  this.repeatPenalty = fields.repeatPenalty;
238
+ this.ropeFrequencyBase = fields.ropeFrequencyBase;
239
+ this.ropeFrequencyScale = fields.ropeFrequencyScale;
125
240
  this.temperature = fields.temperature;
126
241
  this.stop = fields.stop;
127
242
  this.tfsZ = fields.tfsZ;
128
243
  this.topK = fields.topK;
129
244
  this.topP = fields.topP;
245
+ this.typicalP = fields.typicalP;
246
+ this.useMLock = fields.useMLock;
247
+ this.useMMap = fields.useMMap;
248
+ this.vocabOnly = fields.vocabOnly;
130
249
  }
131
250
  _llmType() {
132
251
  return "ollama";
@@ -135,19 +254,36 @@ class Ollama extends base_js_1.LLM {
135
254
  return {
136
255
  model: this.model,
137
256
  options: {
257
+ embedding_only: this.embeddingOnly,
258
+ f16_kv: this.f16KV,
259
+ frequency_penalty: this.frequencyPenalty,
260
+ logits_all: this.logitsAll,
261
+ low_vram: this.lowVram,
262
+ main_gpu: this.mainGpu,
138
263
  mirostat: this.mirostat,
139
264
  mirostat_eta: this.mirostatEta,
140
265
  mirostat_tau: this.mirostatTau,
266
+ num_batch: this.numBatch,
141
267
  num_ctx: this.numCtx,
142
268
  num_gpu: this.numGpu,
269
+ num_gqa: this.numGqa,
270
+ num_keep: this.numKeep,
143
271
  num_thread: this.numThread,
272
+ penalize_newline: this.penalizeNewline,
273
+ presence_penalty: this.presencePenalty,
144
274
  repeat_last_n: this.repeatLastN,
145
275
  repeat_penalty: this.repeatPenalty,
276
+ rope_frequency_base: this.ropeFrequencyBase,
277
+ rope_frequency_scale: this.ropeFrequencyScale,
146
278
  temperature: this.temperature,
147
279
  stop: options?.stop ?? this.stop,
148
280
  tfs_z: this.tfsZ,
149
281
  top_k: this.topK,
150
282
  top_p: this.topP,
283
+ typical_p: this.typicalP,
284
+ use_mlock: this.useMLock,
285
+ use_mmap: this.useMMap,
286
+ vocab_only: this.vocabOnly,
151
287
  },
152
288
  };
153
289
  }
@@ -12,37 +12,71 @@ export declare class Ollama extends LLM implements OllamaInput {
12
12
  lc_serializable: boolean;
13
13
  model: string;
14
14
  baseUrl: string;
15
+ embeddingOnly?: boolean;
16
+ f16KV?: boolean;
17
+ frequencyPenalty?: number;
18
+ logitsAll?: boolean;
19
+ lowVram?: boolean;
20
+ mainGpu?: number;
15
21
  mirostat?: number;
16
22
  mirostatEta?: number;
17
23
  mirostatTau?: number;
24
+ numBatch?: number;
18
25
  numCtx?: number;
19
26
  numGpu?: number;
27
+ numGqa?: number;
28
+ numKeep?: number;
20
29
  numThread?: number;
30
+ penalizeNewline?: boolean;
31
+ presencePenalty?: number;
21
32
  repeatLastN?: number;
22
33
  repeatPenalty?: number;
34
+ ropeFrequencyBase?: number;
35
+ ropeFrequencyScale?: number;
23
36
  temperature?: number;
24
37
  stop?: string[];
25
38
  tfsZ?: number;
26
39
  topK?: number;
27
40
  topP?: number;
41
+ typicalP?: number;
42
+ useMLock?: boolean;
43
+ useMMap?: boolean;
44
+ vocabOnly?: boolean;
28
45
  constructor(fields: OllamaInput & BaseLLMParams);
29
46
  _llmType(): string;
30
47
  invocationParams(options?: this["ParsedCallOptions"]): {
31
48
  model: string;
32
49
  options: {
50
+ embedding_only: boolean | undefined;
51
+ f16_kv: boolean | undefined;
52
+ frequency_penalty: number | undefined;
53
+ logits_all: boolean | undefined;
54
+ low_vram: boolean | undefined;
55
+ main_gpu: number | undefined;
33
56
  mirostat: number | undefined;
34
57
  mirostat_eta: number | undefined;
35
58
  mirostat_tau: number | undefined;
59
+ num_batch: number | undefined;
36
60
  num_ctx: number | undefined;
37
61
  num_gpu: number | undefined;
62
+ num_gqa: number | undefined;
63
+ num_keep: number | undefined;
38
64
  num_thread: number | undefined;
65
+ penalize_newline: boolean | undefined;
66
+ presence_penalty: number | undefined;
39
67
  repeat_last_n: number | undefined;
40
68
  repeat_penalty: number | undefined;
69
+ rope_frequency_base: number | undefined;
70
+ rope_frequency_scale: number | undefined;
41
71
  temperature: number | undefined;
42
72
  stop: string[] | undefined;
43
73
  tfs_z: number | undefined;
44
74
  top_k: number | undefined;
45
75
  top_p: number | undefined;
76
+ typical_p: number | undefined;
77
+ use_mlock: boolean | undefined;
78
+ use_mmap: boolean | undefined;
79
+ vocab_only: boolean | undefined;
46
80
  };
47
81
  };
48
82
  _streamResponseChunks(input: string, options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<GenerationChunk>;