@nocobase/plugin-ai 2.2.0-alpha.3 → 2.2.0-alpha.4
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.
- package/dist/ai/docs/nocobase/ops-management/version-control/index.md +1 -0
- package/dist/client/559.9f97ebcdb0bd6231.js +10 -0
- package/dist/client/index.js +3 -3
- package/dist/client/llm-providers/mistral/ModelSettings.d.ts +10 -0
- package/dist/client/llm-providers/mistral/index.d.ts +10 -0
- package/dist/client/llm-services/utils.d.ts +5 -0
- package/dist/externalVersion.js +16 -16
- package/dist/locale/en-US.json +1 -0
- package/dist/locale/zh-CN.json +1 -0
- package/dist/node_modules/@langchain/mistralai/LICENSE +21 -0
- package/dist/node_modules/@langchain/mistralai/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/node_modules/@langchain/mistralai/dist/chat_models.cjs +859 -0
- package/dist/node_modules/@langchain/mistralai/dist/chat_models.d.cts +541 -0
- package/dist/node_modules/@langchain/mistralai/dist/chat_models.d.ts +541 -0
- package/dist/node_modules/@langchain/mistralai/dist/chat_models.js +857 -0
- package/dist/node_modules/@langchain/mistralai/dist/embeddings.cjs +140 -0
- package/dist/node_modules/@langchain/mistralai/dist/embeddings.d.cts +116 -0
- package/dist/node_modules/@langchain/mistralai/dist/embeddings.d.ts +116 -0
- package/dist/node_modules/@langchain/mistralai/dist/embeddings.js +139 -0
- package/dist/node_modules/@langchain/mistralai/dist/index.cjs +21 -0
- package/dist/node_modules/@langchain/mistralai/dist/index.d.cts +4 -0
- package/dist/node_modules/@langchain/mistralai/dist/index.d.ts +4 -0
- package/dist/node_modules/@langchain/mistralai/dist/index.js +5 -0
- package/dist/node_modules/@langchain/mistralai/dist/llms.cjs +275 -0
- package/dist/node_modules/@langchain/mistralai/dist/llms.d.cts +147 -0
- package/dist/node_modules/@langchain/mistralai/dist/llms.d.ts +147 -0
- package/dist/node_modules/@langchain/mistralai/dist/llms.js +274 -0
- package/dist/node_modules/@langchain/mistralai/dist/utils.cjs +61 -0
- package/dist/node_modules/@langchain/mistralai/dist/utils.js +59 -0
- package/dist/node_modules/@langchain/mistralai/package.json +1 -0
- package/dist/node_modules/@langchain/xai/dist/index.cjs +2 -2
- package/dist/node_modules/@langchain/xai/package.json +1 -1
- package/dist/node_modules/fs-extra/package.json +1 -1
- package/dist/node_modules/jsonrepair/package.json +1 -1
- package/dist/node_modules/just-bash/package.json +1 -1
- package/dist/node_modules/nodejs-snowflake/package.json +1 -1
- package/dist/node_modules/openai/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/ai-employees/ai-employee.js +7 -5
- package/dist/server/ai-employees/middleware/conversation.d.ts +2 -0
- package/dist/server/ai-employees/middleware/conversation.js +2 -1
- package/dist/server/ai-employees/utils.d.ts +3 -1
- package/dist/server/ai-employees/utils.js +2 -0
- package/dist/server/llm-providers/mistral.d.ts +62 -0
- package/dist/server/llm-providers/mistral.js +265 -0
- package/dist/server/llm-providers/provider.d.ts +6 -2
- package/dist/server/llm-providers/provider.js +27 -14
- package/dist/server/plugin.js +2 -0
- package/dist/server/workflow/nodes/employee/index.js +91 -45
- package/package.json +3 -2
- package/dist/client/559.a0f2f1cc2be3c039.js +0 -10
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const __mistralai_mistralai_lib_http_js = require_rolldown_runtime.__toESM(require("@mistralai/mistralai/lib/http.js"));
|
|
3
|
+
const __langchain_core_utils_env = require_rolldown_runtime.__toESM(require("@langchain/core/utils/env"));
|
|
4
|
+
const __langchain_core_embeddings = require_rolldown_runtime.__toESM(require("@langchain/core/embeddings"));
|
|
5
|
+
const __langchain_core_utils_chunk_array = require_rolldown_runtime.__toESM(require("@langchain/core/utils/chunk_array"));
|
|
6
|
+
|
|
7
|
+
//#region src/embeddings.ts
|
|
8
|
+
/**
|
|
9
|
+
* Class for generating embeddings using the MistralAI API.
|
|
10
|
+
*/
|
|
11
|
+
var MistralAIEmbeddings = class extends __langchain_core_embeddings.Embeddings {
|
|
12
|
+
modelName = "mistral-embed";
|
|
13
|
+
model = "mistral-embed";
|
|
14
|
+
encodingFormat = "float";
|
|
15
|
+
batchSize = 512;
|
|
16
|
+
stripNewLines = true;
|
|
17
|
+
apiKey;
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated use serverURL instead
|
|
20
|
+
*/
|
|
21
|
+
endpoint;
|
|
22
|
+
serverURL;
|
|
23
|
+
beforeRequestHooks;
|
|
24
|
+
requestErrorHooks;
|
|
25
|
+
responseHooks;
|
|
26
|
+
httpClient;
|
|
27
|
+
constructor(fields) {
|
|
28
|
+
super(fields ?? {});
|
|
29
|
+
const apiKey = fields?.apiKey ?? (0, __langchain_core_utils_env.getEnvironmentVariable)("MISTRAL_API_KEY");
|
|
30
|
+
if (!apiKey) throw new Error("API key missing for MistralAI, but it is required.");
|
|
31
|
+
this.apiKey = apiKey;
|
|
32
|
+
this.serverURL = fields?.serverURL ?? this.serverURL;
|
|
33
|
+
this.modelName = fields?.model ?? fields?.modelName ?? this.model;
|
|
34
|
+
this.model = this.modelName;
|
|
35
|
+
this.encodingFormat = fields?.encodingFormat ?? this.encodingFormat;
|
|
36
|
+
this.batchSize = fields?.batchSize ?? this.batchSize;
|
|
37
|
+
this.stripNewLines = fields?.stripNewLines ?? this.stripNewLines;
|
|
38
|
+
this.beforeRequestHooks = fields?.beforeRequestHooks ?? this.beforeRequestHooks;
|
|
39
|
+
this.requestErrorHooks = fields?.requestErrorHooks ?? this.requestErrorHooks;
|
|
40
|
+
this.responseHooks = fields?.responseHooks ?? this.responseHooks;
|
|
41
|
+
this.httpClient = fields?.httpClient ?? this.httpClient;
|
|
42
|
+
this.addAllHooksToHttpClient();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Method to generate embeddings for an array of documents. Splits the
|
|
46
|
+
* documents into batches and makes requests to the MistralAI API to generate
|
|
47
|
+
* embeddings.
|
|
48
|
+
* @param {Array<string>} texts Array of documents to generate embeddings for.
|
|
49
|
+
* @returns {Promise<number[][]>} Promise that resolves to a 2D array of embeddings for each document.
|
|
50
|
+
*/
|
|
51
|
+
async embedDocuments(texts) {
|
|
52
|
+
const batches = (0, __langchain_core_utils_chunk_array.chunkArray)(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
|
|
53
|
+
const batchRequests = batches.map((batch) => this.embeddingWithRetry(batch));
|
|
54
|
+
const batchResponses = await Promise.all(batchRequests);
|
|
55
|
+
const embeddings = [];
|
|
56
|
+
for (let i = 0; i < batchResponses.length; i += 1) {
|
|
57
|
+
const batch = batches[i];
|
|
58
|
+
const { data: batchResponse } = batchResponses[i];
|
|
59
|
+
for (let j = 0; j < batch.length; j += 1) embeddings.push(batchResponse[j].embedding ?? []);
|
|
60
|
+
}
|
|
61
|
+
return embeddings;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Method to generate an embedding for a single document. Calls the
|
|
65
|
+
* embeddingWithRetry method with the document as the input.
|
|
66
|
+
* @param {string} text Document to generate an embedding for.
|
|
67
|
+
* @returns {Promise<number[]>} Promise that resolves to an embedding for the document.
|
|
68
|
+
*/
|
|
69
|
+
async embedQuery(text) {
|
|
70
|
+
const { data } = await this.embeddingWithRetry(this.stripNewLines ? text.replace(/\n/g, " ") : text);
|
|
71
|
+
return data[0].embedding ?? [];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Private method to make a request to the MistralAI API to generate
|
|
75
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
76
|
+
* API.
|
|
77
|
+
* @param {string | Array<string>} inputs Text to send to the MistralAI API.
|
|
78
|
+
* @returns {Promise<MistralAIEmbeddingsResponse>} Promise that resolves to the response from the API.
|
|
79
|
+
*/
|
|
80
|
+
async embeddingWithRetry(inputs) {
|
|
81
|
+
const { Mistral } = await this.imports();
|
|
82
|
+
const client = new Mistral({
|
|
83
|
+
apiKey: this.apiKey,
|
|
84
|
+
serverURL: this.serverURL,
|
|
85
|
+
...this.httpClient ? { httpClient: this.httpClient } : {}
|
|
86
|
+
});
|
|
87
|
+
const embeddingsRequest = {
|
|
88
|
+
model: this.model,
|
|
89
|
+
inputs,
|
|
90
|
+
encodingFormat: this.encodingFormat
|
|
91
|
+
};
|
|
92
|
+
return this.caller.call(async () => {
|
|
93
|
+
const res = await client.embeddings.create(embeddingsRequest);
|
|
94
|
+
return res;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
addAllHooksToHttpClient() {
|
|
98
|
+
try {
|
|
99
|
+
this.removeAllHooksFromHttpClient();
|
|
100
|
+
const hasHooks = [
|
|
101
|
+
this.beforeRequestHooks,
|
|
102
|
+
this.requestErrorHooks,
|
|
103
|
+
this.responseHooks
|
|
104
|
+
].some((hook) => hook && hook.length > 0);
|
|
105
|
+
if (hasHooks && !this.httpClient) this.httpClient = new __mistralai_mistralai_lib_http_js.HTTPClient();
|
|
106
|
+
if (this.beforeRequestHooks) for (const hook of this.beforeRequestHooks) this.httpClient?.addHook("beforeRequest", hook);
|
|
107
|
+
if (this.requestErrorHooks) for (const hook of this.requestErrorHooks) this.httpClient?.addHook("requestError", hook);
|
|
108
|
+
if (this.responseHooks) for (const hook of this.responseHooks) this.httpClient?.addHook("response", hook);
|
|
109
|
+
} catch {
|
|
110
|
+
throw new Error("Error in adding all hooks");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
removeAllHooksFromHttpClient() {
|
|
114
|
+
try {
|
|
115
|
+
if (this.beforeRequestHooks) for (const hook of this.beforeRequestHooks) this.httpClient?.removeHook("beforeRequest", hook);
|
|
116
|
+
if (this.requestErrorHooks) for (const hook of this.requestErrorHooks) this.httpClient?.removeHook("requestError", hook);
|
|
117
|
+
if (this.responseHooks) for (const hook of this.responseHooks) this.httpClient?.removeHook("response", hook);
|
|
118
|
+
} catch {
|
|
119
|
+
throw new Error("Error in removing hooks");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
removeHookFromHttpClient(hook) {
|
|
123
|
+
try {
|
|
124
|
+
this.httpClient?.removeHook("beforeRequest", hook);
|
|
125
|
+
this.httpClient?.removeHook("requestError", hook);
|
|
126
|
+
this.httpClient?.removeHook("response", hook);
|
|
127
|
+
} catch {
|
|
128
|
+
throw new Error("Error in removing hook");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/** @ignore */
|
|
132
|
+
async imports() {
|
|
133
|
+
const { Mistral } = await import("@mistralai/mistralai");
|
|
134
|
+
return { Mistral };
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
//#endregion
|
|
139
|
+
exports.MistralAIEmbeddings = MistralAIEmbeddings;
|
|
140
|
+
//# sourceMappingURL=embeddings.cjs.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { BeforeRequestHook, HTTPClient, RequestErrorHook, ResponseHook } from "@mistralai/mistralai/lib/http.js";
|
|
2
|
+
import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings";
|
|
3
|
+
|
|
4
|
+
//#region src/embeddings.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Interface for MistralAIEmbeddings parameters. Extends EmbeddingsParams and
|
|
8
|
+
* defines additional parameters specific to the MistralAIEmbeddings class.
|
|
9
|
+
*/
|
|
10
|
+
interface MistralAIEmbeddingsParams extends EmbeddingsParams {
|
|
11
|
+
/**
|
|
12
|
+
* The API key to use.
|
|
13
|
+
* @default {process.env.MISTRAL_API_KEY}
|
|
14
|
+
*/
|
|
15
|
+
apiKey?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The name of the model to use.
|
|
18
|
+
* Alias for `model`.
|
|
19
|
+
* @default {"mistral-embed"}
|
|
20
|
+
*/
|
|
21
|
+
modelName?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The name of the model to use.
|
|
24
|
+
* @default {"mistral-embed"}
|
|
25
|
+
*/
|
|
26
|
+
model?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The format of the output data.
|
|
29
|
+
* @default {"float"}
|
|
30
|
+
*/
|
|
31
|
+
encodingFormat?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Override the default server URL used by the Mistral SDK.
|
|
34
|
+
* @deprecated use serverURL instead
|
|
35
|
+
*/
|
|
36
|
+
endpoint?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Override the default server URL used by the Mistral SDK.
|
|
39
|
+
*/
|
|
40
|
+
serverURL?: string;
|
|
41
|
+
/**
|
|
42
|
+
* The maximum number of documents to embed in a single request.
|
|
43
|
+
* @default {512}
|
|
44
|
+
*/
|
|
45
|
+
batchSize?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Whether to strip new lines from the input text. This is recommended,
|
|
48
|
+
* but may not be suitable for all use cases.
|
|
49
|
+
* @default {true}
|
|
50
|
+
*/
|
|
51
|
+
stripNewLines?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* A list of custom hooks that must follow (req: Request) => Awaitable<Request | void>
|
|
54
|
+
* They are automatically added when a ChatMistralAI instance is created
|
|
55
|
+
*/
|
|
56
|
+
beforeRequestHooks?: BeforeRequestHook[];
|
|
57
|
+
/**
|
|
58
|
+
* A list of custom hooks that must follow (err: unknown, req: Request) => Awaitable<void>
|
|
59
|
+
* They are automatically added when a ChatMistralAI instance is created
|
|
60
|
+
*/
|
|
61
|
+
requestErrorHooks?: RequestErrorHook[];
|
|
62
|
+
/**
|
|
63
|
+
* A list of custom hooks that must follow (res: Response, req: Request) => Awaitable<void>
|
|
64
|
+
* They are automatically added when a ChatMistralAI instance is created
|
|
65
|
+
*/
|
|
66
|
+
responseHooks?: ResponseHook[];
|
|
67
|
+
/**
|
|
68
|
+
* Optional custom HTTP client to manage API requests
|
|
69
|
+
* Allows users to add custom fetch implementations, hooks, as well as error and response processing.
|
|
70
|
+
*/
|
|
71
|
+
httpClient?: HTTPClient;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Class for generating embeddings using the MistralAI API.
|
|
75
|
+
*/
|
|
76
|
+
declare class MistralAIEmbeddings extends Embeddings implements MistralAIEmbeddingsParams {
|
|
77
|
+
modelName: string;
|
|
78
|
+
model: string;
|
|
79
|
+
encodingFormat: string;
|
|
80
|
+
batchSize: number;
|
|
81
|
+
stripNewLines: boolean;
|
|
82
|
+
apiKey: string;
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated use serverURL instead
|
|
85
|
+
*/
|
|
86
|
+
endpoint: string;
|
|
87
|
+
serverURL?: string;
|
|
88
|
+
beforeRequestHooks?: Array<BeforeRequestHook>;
|
|
89
|
+
requestErrorHooks?: Array<RequestErrorHook>;
|
|
90
|
+
responseHooks?: Array<ResponseHook>;
|
|
91
|
+
httpClient?: HTTPClient;
|
|
92
|
+
constructor(fields?: Partial<MistralAIEmbeddingsParams>);
|
|
93
|
+
/**
|
|
94
|
+
* Method to generate embeddings for an array of documents. Splits the
|
|
95
|
+
* documents into batches and makes requests to the MistralAI API to generate
|
|
96
|
+
* embeddings.
|
|
97
|
+
* @param {Array<string>} texts Array of documents to generate embeddings for.
|
|
98
|
+
* @returns {Promise<number[][]>} Promise that resolves to a 2D array of embeddings for each document.
|
|
99
|
+
*/
|
|
100
|
+
embedDocuments(texts: string[]): Promise<number[][]>;
|
|
101
|
+
/**
|
|
102
|
+
* Method to generate an embedding for a single document. Calls the
|
|
103
|
+
* embeddingWithRetry method with the document as the input.
|
|
104
|
+
* @param {string} text Document to generate an embedding for.
|
|
105
|
+
* @returns {Promise<number[]>} Promise that resolves to an embedding for the document.
|
|
106
|
+
*/
|
|
107
|
+
embedQuery(text: string): Promise<number[]>;
|
|
108
|
+
private embeddingWithRetry;
|
|
109
|
+
addAllHooksToHttpClient(): void;
|
|
110
|
+
removeAllHooksFromHttpClient(): void;
|
|
111
|
+
removeHookFromHttpClient(hook: BeforeRequestHook | RequestErrorHook | ResponseHook): void;
|
|
112
|
+
private imports;
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
export { MistralAIEmbeddings, MistralAIEmbeddingsParams };
|
|
116
|
+
//# sourceMappingURL=embeddings.d.cts.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { BeforeRequestHook, HTTPClient, RequestErrorHook, ResponseHook } from "@mistralai/mistralai/lib/http.js";
|
|
2
|
+
import { Embeddings, EmbeddingsParams } from "@langchain/core/embeddings";
|
|
3
|
+
|
|
4
|
+
//#region src/embeddings.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Interface for MistralAIEmbeddings parameters. Extends EmbeddingsParams and
|
|
8
|
+
* defines additional parameters specific to the MistralAIEmbeddings class.
|
|
9
|
+
*/
|
|
10
|
+
interface MistralAIEmbeddingsParams extends EmbeddingsParams {
|
|
11
|
+
/**
|
|
12
|
+
* The API key to use.
|
|
13
|
+
* @default {process.env.MISTRAL_API_KEY}
|
|
14
|
+
*/
|
|
15
|
+
apiKey?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The name of the model to use.
|
|
18
|
+
* Alias for `model`.
|
|
19
|
+
* @default {"mistral-embed"}
|
|
20
|
+
*/
|
|
21
|
+
modelName?: string;
|
|
22
|
+
/**
|
|
23
|
+
* The name of the model to use.
|
|
24
|
+
* @default {"mistral-embed"}
|
|
25
|
+
*/
|
|
26
|
+
model?: string;
|
|
27
|
+
/**
|
|
28
|
+
* The format of the output data.
|
|
29
|
+
* @default {"float"}
|
|
30
|
+
*/
|
|
31
|
+
encodingFormat?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Override the default server URL used by the Mistral SDK.
|
|
34
|
+
* @deprecated use serverURL instead
|
|
35
|
+
*/
|
|
36
|
+
endpoint?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Override the default server URL used by the Mistral SDK.
|
|
39
|
+
*/
|
|
40
|
+
serverURL?: string;
|
|
41
|
+
/**
|
|
42
|
+
* The maximum number of documents to embed in a single request.
|
|
43
|
+
* @default {512}
|
|
44
|
+
*/
|
|
45
|
+
batchSize?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Whether to strip new lines from the input text. This is recommended,
|
|
48
|
+
* but may not be suitable for all use cases.
|
|
49
|
+
* @default {true}
|
|
50
|
+
*/
|
|
51
|
+
stripNewLines?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* A list of custom hooks that must follow (req: Request) => Awaitable<Request | void>
|
|
54
|
+
* They are automatically added when a ChatMistralAI instance is created
|
|
55
|
+
*/
|
|
56
|
+
beforeRequestHooks?: BeforeRequestHook[];
|
|
57
|
+
/**
|
|
58
|
+
* A list of custom hooks that must follow (err: unknown, req: Request) => Awaitable<void>
|
|
59
|
+
* They are automatically added when a ChatMistralAI instance is created
|
|
60
|
+
*/
|
|
61
|
+
requestErrorHooks?: RequestErrorHook[];
|
|
62
|
+
/**
|
|
63
|
+
* A list of custom hooks that must follow (res: Response, req: Request) => Awaitable<void>
|
|
64
|
+
* They are automatically added when a ChatMistralAI instance is created
|
|
65
|
+
*/
|
|
66
|
+
responseHooks?: ResponseHook[];
|
|
67
|
+
/**
|
|
68
|
+
* Optional custom HTTP client to manage API requests
|
|
69
|
+
* Allows users to add custom fetch implementations, hooks, as well as error and response processing.
|
|
70
|
+
*/
|
|
71
|
+
httpClient?: HTTPClient;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Class for generating embeddings using the MistralAI API.
|
|
75
|
+
*/
|
|
76
|
+
declare class MistralAIEmbeddings extends Embeddings implements MistralAIEmbeddingsParams {
|
|
77
|
+
modelName: string;
|
|
78
|
+
model: string;
|
|
79
|
+
encodingFormat: string;
|
|
80
|
+
batchSize: number;
|
|
81
|
+
stripNewLines: boolean;
|
|
82
|
+
apiKey: string;
|
|
83
|
+
/**
|
|
84
|
+
* @deprecated use serverURL instead
|
|
85
|
+
*/
|
|
86
|
+
endpoint: string;
|
|
87
|
+
serverURL?: string;
|
|
88
|
+
beforeRequestHooks?: Array<BeforeRequestHook>;
|
|
89
|
+
requestErrorHooks?: Array<RequestErrorHook>;
|
|
90
|
+
responseHooks?: Array<ResponseHook>;
|
|
91
|
+
httpClient?: HTTPClient;
|
|
92
|
+
constructor(fields?: Partial<MistralAIEmbeddingsParams>);
|
|
93
|
+
/**
|
|
94
|
+
* Method to generate embeddings for an array of documents. Splits the
|
|
95
|
+
* documents into batches and makes requests to the MistralAI API to generate
|
|
96
|
+
* embeddings.
|
|
97
|
+
* @param {Array<string>} texts Array of documents to generate embeddings for.
|
|
98
|
+
* @returns {Promise<number[][]>} Promise that resolves to a 2D array of embeddings for each document.
|
|
99
|
+
*/
|
|
100
|
+
embedDocuments(texts: string[]): Promise<number[][]>;
|
|
101
|
+
/**
|
|
102
|
+
* Method to generate an embedding for a single document. Calls the
|
|
103
|
+
* embeddingWithRetry method with the document as the input.
|
|
104
|
+
* @param {string} text Document to generate an embedding for.
|
|
105
|
+
* @returns {Promise<number[]>} Promise that resolves to an embedding for the document.
|
|
106
|
+
*/
|
|
107
|
+
embedQuery(text: string): Promise<number[]>;
|
|
108
|
+
private embeddingWithRetry;
|
|
109
|
+
addAllHooksToHttpClient(): void;
|
|
110
|
+
removeAllHooksFromHttpClient(): void;
|
|
111
|
+
removeHookFromHttpClient(hook: BeforeRequestHook | RequestErrorHook | ResponseHook): void;
|
|
112
|
+
private imports;
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
export { MistralAIEmbeddings, MistralAIEmbeddingsParams };
|
|
116
|
+
//# sourceMappingURL=embeddings.d.ts.map
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { HTTPClient } from "@mistralai/mistralai/lib/http.js";
|
|
2
|
+
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
3
|
+
import { Embeddings } from "@langchain/core/embeddings";
|
|
4
|
+
import { chunkArray } from "@langchain/core/utils/chunk_array";
|
|
5
|
+
|
|
6
|
+
//#region src/embeddings.ts
|
|
7
|
+
/**
|
|
8
|
+
* Class for generating embeddings using the MistralAI API.
|
|
9
|
+
*/
|
|
10
|
+
var MistralAIEmbeddings = class extends Embeddings {
|
|
11
|
+
modelName = "mistral-embed";
|
|
12
|
+
model = "mistral-embed";
|
|
13
|
+
encodingFormat = "float";
|
|
14
|
+
batchSize = 512;
|
|
15
|
+
stripNewLines = true;
|
|
16
|
+
apiKey;
|
|
17
|
+
/**
|
|
18
|
+
* @deprecated use serverURL instead
|
|
19
|
+
*/
|
|
20
|
+
endpoint;
|
|
21
|
+
serverURL;
|
|
22
|
+
beforeRequestHooks;
|
|
23
|
+
requestErrorHooks;
|
|
24
|
+
responseHooks;
|
|
25
|
+
httpClient;
|
|
26
|
+
constructor(fields) {
|
|
27
|
+
super(fields ?? {});
|
|
28
|
+
const apiKey = fields?.apiKey ?? getEnvironmentVariable("MISTRAL_API_KEY");
|
|
29
|
+
if (!apiKey) throw new Error("API key missing for MistralAI, but it is required.");
|
|
30
|
+
this.apiKey = apiKey;
|
|
31
|
+
this.serverURL = fields?.serverURL ?? this.serverURL;
|
|
32
|
+
this.modelName = fields?.model ?? fields?.modelName ?? this.model;
|
|
33
|
+
this.model = this.modelName;
|
|
34
|
+
this.encodingFormat = fields?.encodingFormat ?? this.encodingFormat;
|
|
35
|
+
this.batchSize = fields?.batchSize ?? this.batchSize;
|
|
36
|
+
this.stripNewLines = fields?.stripNewLines ?? this.stripNewLines;
|
|
37
|
+
this.beforeRequestHooks = fields?.beforeRequestHooks ?? this.beforeRequestHooks;
|
|
38
|
+
this.requestErrorHooks = fields?.requestErrorHooks ?? this.requestErrorHooks;
|
|
39
|
+
this.responseHooks = fields?.responseHooks ?? this.responseHooks;
|
|
40
|
+
this.httpClient = fields?.httpClient ?? this.httpClient;
|
|
41
|
+
this.addAllHooksToHttpClient();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Method to generate embeddings for an array of documents. Splits the
|
|
45
|
+
* documents into batches and makes requests to the MistralAI API to generate
|
|
46
|
+
* embeddings.
|
|
47
|
+
* @param {Array<string>} texts Array of documents to generate embeddings for.
|
|
48
|
+
* @returns {Promise<number[][]>} Promise that resolves to a 2D array of embeddings for each document.
|
|
49
|
+
*/
|
|
50
|
+
async embedDocuments(texts) {
|
|
51
|
+
const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
|
|
52
|
+
const batchRequests = batches.map((batch) => this.embeddingWithRetry(batch));
|
|
53
|
+
const batchResponses = await Promise.all(batchRequests);
|
|
54
|
+
const embeddings = [];
|
|
55
|
+
for (let i = 0; i < batchResponses.length; i += 1) {
|
|
56
|
+
const batch = batches[i];
|
|
57
|
+
const { data: batchResponse } = batchResponses[i];
|
|
58
|
+
for (let j = 0; j < batch.length; j += 1) embeddings.push(batchResponse[j].embedding ?? []);
|
|
59
|
+
}
|
|
60
|
+
return embeddings;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Method to generate an embedding for a single document. Calls the
|
|
64
|
+
* embeddingWithRetry method with the document as the input.
|
|
65
|
+
* @param {string} text Document to generate an embedding for.
|
|
66
|
+
* @returns {Promise<number[]>} Promise that resolves to an embedding for the document.
|
|
67
|
+
*/
|
|
68
|
+
async embedQuery(text) {
|
|
69
|
+
const { data } = await this.embeddingWithRetry(this.stripNewLines ? text.replace(/\n/g, " ") : text);
|
|
70
|
+
return data[0].embedding ?? [];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Private method to make a request to the MistralAI API to generate
|
|
74
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
75
|
+
* API.
|
|
76
|
+
* @param {string | Array<string>} inputs Text to send to the MistralAI API.
|
|
77
|
+
* @returns {Promise<MistralAIEmbeddingsResponse>} Promise that resolves to the response from the API.
|
|
78
|
+
*/
|
|
79
|
+
async embeddingWithRetry(inputs) {
|
|
80
|
+
const { Mistral } = await this.imports();
|
|
81
|
+
const client = new Mistral({
|
|
82
|
+
apiKey: this.apiKey,
|
|
83
|
+
serverURL: this.serverURL,
|
|
84
|
+
...this.httpClient ? { httpClient: this.httpClient } : {}
|
|
85
|
+
});
|
|
86
|
+
const embeddingsRequest = {
|
|
87
|
+
model: this.model,
|
|
88
|
+
inputs,
|
|
89
|
+
encodingFormat: this.encodingFormat
|
|
90
|
+
};
|
|
91
|
+
return this.caller.call(async () => {
|
|
92
|
+
const res = await client.embeddings.create(embeddingsRequest);
|
|
93
|
+
return res;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
addAllHooksToHttpClient() {
|
|
97
|
+
try {
|
|
98
|
+
this.removeAllHooksFromHttpClient();
|
|
99
|
+
const hasHooks = [
|
|
100
|
+
this.beforeRequestHooks,
|
|
101
|
+
this.requestErrorHooks,
|
|
102
|
+
this.responseHooks
|
|
103
|
+
].some((hook) => hook && hook.length > 0);
|
|
104
|
+
if (hasHooks && !this.httpClient) this.httpClient = new HTTPClient();
|
|
105
|
+
if (this.beforeRequestHooks) for (const hook of this.beforeRequestHooks) this.httpClient?.addHook("beforeRequest", hook);
|
|
106
|
+
if (this.requestErrorHooks) for (const hook of this.requestErrorHooks) this.httpClient?.addHook("requestError", hook);
|
|
107
|
+
if (this.responseHooks) for (const hook of this.responseHooks) this.httpClient?.addHook("response", hook);
|
|
108
|
+
} catch {
|
|
109
|
+
throw new Error("Error in adding all hooks");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
removeAllHooksFromHttpClient() {
|
|
113
|
+
try {
|
|
114
|
+
if (this.beforeRequestHooks) for (const hook of this.beforeRequestHooks) this.httpClient?.removeHook("beforeRequest", hook);
|
|
115
|
+
if (this.requestErrorHooks) for (const hook of this.requestErrorHooks) this.httpClient?.removeHook("requestError", hook);
|
|
116
|
+
if (this.responseHooks) for (const hook of this.responseHooks) this.httpClient?.removeHook("response", hook);
|
|
117
|
+
} catch {
|
|
118
|
+
throw new Error("Error in removing hooks");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
removeHookFromHttpClient(hook) {
|
|
122
|
+
try {
|
|
123
|
+
this.httpClient?.removeHook("beforeRequest", hook);
|
|
124
|
+
this.httpClient?.removeHook("requestError", hook);
|
|
125
|
+
this.httpClient?.removeHook("response", hook);
|
|
126
|
+
} catch {
|
|
127
|
+
throw new Error("Error in removing hook");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** @ignore */
|
|
131
|
+
async imports() {
|
|
132
|
+
const { Mistral } = await import("@mistralai/mistralai");
|
|
133
|
+
return { Mistral };
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
export { MistralAIEmbeddings };
|
|
139
|
+
//# sourceMappingURL=embeddings.js.map
|