@tanstack/ai-mistral 0.2.3 → 0.3.0
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/esm/adapters/embedding.d.ts +76 -0
- package/dist/esm/adapters/embedding.js +122 -0
- package/dist/esm/adapters/embedding.js.map +1 -0
- package/dist/esm/adapters/text.js +702 -731
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/embedding/embedding-provider-options.d.ts +29 -0
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.js +3 -8
- package/dist/esm/model-meta.d.ts +31 -4
- package/dist/esm/model-meta.js +253 -49
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/tools/function-tool.js +33 -23
- package/dist/esm/tools/function-tool.js.map +1 -1
- package/dist/esm/tools/tool-converter.js +11 -7
- package/dist/esm/tools/tool-converter.js.map +1 -1
- package/dist/esm/utils/client.js +36 -37
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/schema-converter.js +73 -89
- package/dist/esm/utils/schema-converter.js.map +1 -1
- package/package.json +10 -6
- package/src/adapters/embedding.ts +177 -0
- package/src/adapters/text.ts +11 -2
- package/src/embedding/embedding-provider-options.ts +32 -0
- package/src/index.ts +17 -1
- package/src/model-meta.ts +42 -5
- package/src/utils/schema-converter.ts +2 -2
- package/dist/esm/index.js.map +0 -1
|
@@ -1,93 +1,77 @@
|
|
|
1
|
+
//#region src/utils/schema-converter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Recursively transform null values to undefined in an object.
|
|
4
|
+
*
|
|
5
|
+
* This is needed because Mistral's structured output may require optional
|
|
6
|
+
* fields to be declared nullable. When Mistral returns null for optional
|
|
7
|
+
* fields, we convert them back to undefined to match the original Zod schema.
|
|
8
|
+
*/
|
|
1
9
|
function transformNullsToUndefined(obj) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
11
|
-
result[key] = transformNullsToUndefined(value);
|
|
12
|
-
}
|
|
13
|
-
return result;
|
|
14
|
-
}
|
|
15
|
-
return obj;
|
|
10
|
+
if (obj === null) return;
|
|
11
|
+
if (Array.isArray(obj)) return obj.map((item) => transformNullsToUndefined(item));
|
|
12
|
+
if (typeof obj === "object" && Object.getPrototypeOf(obj) === Object.prototype) {
|
|
13
|
+
const result = {};
|
|
14
|
+
for (const [key, value] of Object.entries(obj)) result[key] = transformNullsToUndefined(value);
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
return obj;
|
|
16
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Transform a JSON schema to be compatible with Mistral's structured output
|
|
21
|
+
* requirements when `strict: true` is used.
|
|
22
|
+
*
|
|
23
|
+
* Mistral (in strict mode) requires:
|
|
24
|
+
* - All properties must be in the `required` array
|
|
25
|
+
* - Optional fields should have null added to their type union
|
|
26
|
+
* - additionalProperties must be false for objects
|
|
27
|
+
*/
|
|
17
28
|
function makeMistralStructuredOutputCompatible(schema, originalRequired = []) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
};
|
|
63
|
-
} else if (Array.isArray(prop.type) && !prop.type.includes("null")) {
|
|
64
|
-
properties[propName] = {
|
|
65
|
-
...prop,
|
|
66
|
-
type: [...prop.type, "null"]
|
|
67
|
-
};
|
|
68
|
-
} else if (!prop.type) {
|
|
69
|
-
properties[propName] = { anyOf: [prop, { type: "null" }] };
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
result.properties = properties;
|
|
74
|
-
if (allPropertyNames.length > 0) {
|
|
75
|
-
result.required = allPropertyNames;
|
|
76
|
-
} else {
|
|
77
|
-
delete result.required;
|
|
78
|
-
}
|
|
79
|
-
result.additionalProperties = false;
|
|
80
|
-
}
|
|
81
|
-
if (result.type === "array" && result.items) {
|
|
82
|
-
result.items = makeMistralStructuredOutputCompatible(
|
|
83
|
-
result.items,
|
|
84
|
-
result.items.required || []
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return result;
|
|
29
|
+
const result = { ...schema };
|
|
30
|
+
if (result.type === "object") {
|
|
31
|
+
if (!result.properties) result.properties = {};
|
|
32
|
+
const properties = { ...result.properties };
|
|
33
|
+
const allPropertyNames = Object.keys(properties);
|
|
34
|
+
for (const propName of allPropertyNames) {
|
|
35
|
+
const prop = properties[propName];
|
|
36
|
+
const wasOptional = !originalRequired.includes(propName);
|
|
37
|
+
if (prop.type === "object" && prop.properties) {
|
|
38
|
+
const converted = makeMistralStructuredOutputCompatible(prop, prop.required || []);
|
|
39
|
+
if (wasOptional) properties[propName] = {
|
|
40
|
+
...converted,
|
|
41
|
+
type: Array.isArray(converted.type) ? converted.type.includes("null") ? converted.type : [...converted.type, "null"] : [converted.type, "null"]
|
|
42
|
+
};
|
|
43
|
+
else properties[propName] = converted;
|
|
44
|
+
} else if (prop.type === "array" && prop.items) {
|
|
45
|
+
const converted = {
|
|
46
|
+
...prop,
|
|
47
|
+
items: makeMistralStructuredOutputCompatible(prop.items, prop.items.required || [])
|
|
48
|
+
};
|
|
49
|
+
if (wasOptional) properties[propName] = {
|
|
50
|
+
...converted,
|
|
51
|
+
type: Array.isArray(converted.type) ? converted.type.includes("null") ? converted.type : [...converted.type, "null"] : [converted.type, "null"]
|
|
52
|
+
};
|
|
53
|
+
else properties[propName] = converted;
|
|
54
|
+
} else if (wasOptional) {
|
|
55
|
+
if (prop.type && !Array.isArray(prop.type)) properties[propName] = {
|
|
56
|
+
...prop,
|
|
57
|
+
type: [prop.type, "null"]
|
|
58
|
+
};
|
|
59
|
+
else if (Array.isArray(prop.type) && !prop.type.includes("null")) properties[propName] = {
|
|
60
|
+
...prop,
|
|
61
|
+
type: [...prop.type, "null"]
|
|
62
|
+
};
|
|
63
|
+
else if (!prop.type) properties[propName] = { anyOf: [prop, { type: "null" }] };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
result.properties = properties;
|
|
67
|
+
if (allPropertyNames.length > 0) result.required = allPropertyNames;
|
|
68
|
+
else delete result.required;
|
|
69
|
+
result.additionalProperties = false;
|
|
70
|
+
}
|
|
71
|
+
if (result.type === "array" && result.items) result.items = makeMistralStructuredOutputCompatible(result.items, result.items.required || []);
|
|
72
|
+
return result;
|
|
88
73
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
//# sourceMappingURL=schema-converter.js.map
|
|
74
|
+
//#endregion
|
|
75
|
+
export { makeMistralStructuredOutputCompatible, transformNullsToUndefined };
|
|
76
|
+
|
|
77
|
+
//# sourceMappingURL=schema-converter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-converter.js","sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["/**\n * Recursively transform null values to undefined in an object.\n *\n * This is needed because Mistral's structured output may require optional\n * fields to be declared nullable. When Mistral returns null for optional\n * fields, we convert them back to undefined to match the original Zod schema.\n */\nexport function transformNullsToUndefined<T>(obj: T): T {\n if (obj === null) {\n //
|
|
1
|
+
{"version":3,"file":"schema-converter.js","names":[],"sources":["../../../src/utils/schema-converter.ts"],"sourcesContent":["/**\n * Recursively transform null values to undefined in an object.\n *\n * This is needed because Mistral's structured output may require optional\n * fields to be declared nullable. When Mistral returns null for optional\n * fields, we convert them back to undefined to match the original Zod schema.\n */\nexport function transformNullsToUndefined<T>(obj: T): T {\n if (obj === null) {\n // oxlint-disable-next-line eslint-js/no-restricted-syntax -- generic T has no structural overlap with undefined; null→undefined conversion is this function's documented contract\n return undefined as unknown as T\n }\n\n if (Array.isArray(obj)) {\n // Preserve array length and indices — converting null elements to\n // undefined slots rather than dropping them. `Array<T | null>` schemas\n // depend on positional alignment.\n // oxlint-disable-next-line eslint-js/no-restricted-syntax -- the mapped array (unknown[]) has no structural overlap with generic T; positional null→undefined transform preserves the array shape T describes\n return obj.map((item) => transformNullsToUndefined(item)) as unknown as T\n }\n\n if (\n typeof obj === 'object' &&\n Object.getPrototypeOf(obj) === Object.prototype\n ) {\n // Preserve every key — `null` values become `undefined` values, but the\n // key itself is not removed. Schemas distinguishing absent vs explicit\n // null rely on this.\n const result: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(obj as Record<string, unknown>)) {\n result[key] = transformNullsToUndefined(value)\n }\n return result as T\n }\n\n return obj\n}\n\n/**\n * Transform a JSON schema to be compatible with Mistral's structured output\n * requirements when `strict: true` is used.\n *\n * Mistral (in strict mode) requires:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for objects\n */\nexport function makeMistralStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired: Array<string> = [],\n): Record<string, any> {\n const result = { ...schema }\n\n if (result.type === 'object') {\n if (!result.properties) {\n result.properties = {}\n }\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n for (const propName of allPropertyNames) {\n const prop = properties[propName]\n const wasOptional = !originalRequired.includes(propName)\n\n if (prop.type === 'object' && prop.properties) {\n const converted = makeMistralStructuredOutputCompatible(\n prop,\n prop.required || [],\n )\n if (wasOptional) {\n properties[propName] = {\n ...converted,\n type: Array.isArray(converted.type)\n ? converted.type.includes('null')\n ? converted.type\n : [...converted.type, 'null']\n : [converted.type, 'null'],\n }\n } else {\n properties[propName] = converted\n }\n } else if (prop.type === 'array' && prop.items) {\n const converted = {\n ...prop,\n items: makeMistralStructuredOutputCompatible(\n prop.items,\n prop.items.required || [],\n ),\n }\n if (wasOptional) {\n properties[propName] = {\n ...converted,\n type: Array.isArray(converted.type)\n ? converted.type.includes('null')\n ? converted.type\n : [...converted.type, 'null']\n : [converted.type, 'null'],\n }\n } else {\n properties[propName] = converted\n }\n } else if (wasOptional) {\n if (prop.type && !Array.isArray(prop.type)) {\n properties[propName] = {\n ...prop,\n type: [prop.type, 'null'],\n }\n } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {\n properties[propName] = {\n ...prop,\n type: [...prop.type, 'null'],\n }\n } else if (!prop.type) {\n properties[propName] = { anyOf: [prop, { type: 'null' }] }\n }\n }\n }\n\n result.properties = properties\n if (allPropertyNames.length > 0) {\n result.required = allPropertyNames\n } else {\n delete result.required\n }\n result.additionalProperties = false\n }\n\n if (result.type === 'array' && result.items) {\n result.items = makeMistralStructuredOutputCompatible(\n result.items,\n result.items.required || [],\n )\n }\n\n return result\n}\n"],"mappings":";;;;;;;;AAOA,SAAgB,0BAA6B,KAAW;CACtD,IAAI,QAAQ,MAEV;CAGF,IAAI,MAAM,QAAQ,GAAG,GAKnB,OAAO,IAAI,KAAK,SAAS,0BAA0B,IAAI,CAAC;CAG1D,IACE,OAAO,QAAQ,YACf,OAAO,eAAe,GAAG,MAAM,OAAO,WACtC;EAIA,MAAM,SAAkC,CAAC;EACzC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,GAA8B,GACtE,OAAO,OAAO,0BAA0B,KAAK;EAE/C,OAAO;CACT;CAEA,OAAO;AACT;;;;;;;;;;AAWA,SAAgB,sCACd,QACA,mBAAkC,CAAC,GACd;CACrB,MAAM,SAAS,EAAE,GAAG,OAAO;CAE3B,IAAI,OAAO,SAAS,UAAU;EAC5B,IAAI,CAAC,OAAO,YACV,OAAO,aAAa,CAAC;EAEvB,MAAM,aAAa,EAAE,GAAG,OAAO,WAAW;EAC1C,MAAM,mBAAmB,OAAO,KAAK,UAAU;EAE/C,KAAK,MAAM,YAAY,kBAAkB;GACvC,MAAM,OAAO,WAAW;GACxB,MAAM,cAAc,CAAC,iBAAiB,SAAS,QAAQ;GAEvD,IAAI,KAAK,SAAS,YAAY,KAAK,YAAY;IAC7C,MAAM,YAAY,sCAChB,MACA,KAAK,YAAY,CAAC,CACpB;IACA,IAAI,aACF,WAAW,YAAY;KACrB,GAAG;KACH,MAAM,MAAM,QAAQ,UAAU,IAAI,IAC9B,UAAU,KAAK,SAAS,MAAM,IAC5B,UAAU,OACV,CAAC,GAAG,UAAU,MAAM,MAAM,IAC5B,CAAC,UAAU,MAAM,MAAM;IAC7B;SAEA,WAAW,YAAY;GAE3B,OAAO,IAAI,KAAK,SAAS,WAAW,KAAK,OAAO;IAC9C,MAAM,YAAY;KAChB,GAAG;KACH,OAAO,sCACL,KAAK,OACL,KAAK,MAAM,YAAY,CAAC,CAC1B;IACF;IACA,IAAI,aACF,WAAW,YAAY;KACrB,GAAG;KACH,MAAM,MAAM,QAAQ,UAAU,IAAI,IAC9B,UAAU,KAAK,SAAS,MAAM,IAC5B,UAAU,OACV,CAAC,GAAG,UAAU,MAAM,MAAM,IAC5B,CAAC,UAAU,MAAM,MAAM;IAC7B;SAEA,WAAW,YAAY;GAE3B,OAAO,IAAI;QACL,KAAK,QAAQ,CAAC,MAAM,QAAQ,KAAK,IAAI,GACvC,WAAW,YAAY;KACrB,GAAG;KACH,MAAM,CAAC,KAAK,MAAM,MAAM;IAC1B;SACK,IAAI,MAAM,QAAQ,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,MAAM,GAC/D,WAAW,YAAY;KACrB,GAAG;KACH,MAAM,CAAC,GAAG,KAAK,MAAM,MAAM;IAC7B;SACK,IAAI,CAAC,KAAK,MACf,WAAW,YAAY,EAAE,OAAO,CAAC,MAAM,EAAE,MAAM,OAAO,CAAC,EAAE;GAAA;EAG/D;EAEA,OAAO,aAAa;EACpB,IAAI,iBAAiB,SAAS,GAC5B,OAAO,WAAW;OAElB,OAAO,OAAO;EAEhB,OAAO,uBAAuB;CAChC;CAEA,IAAI,OAAO,SAAS,WAAW,OAAO,OACpC,OAAO,QAAQ,sCACb,OAAO,OACP,OAAO,MAAM,YAAY,CAAC,CAC5B;CAGF,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-mistral",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Mistral adapter for TanStack AI",
|
|
6
6
|
"author": "",
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
"./adapters/text": {
|
|
21
21
|
"types": "./dist/esm/adapters/text.d.ts",
|
|
22
22
|
"import": "./dist/esm/adapters/text.js"
|
|
23
|
+
},
|
|
24
|
+
"./adapters/embedding": {
|
|
25
|
+
"types": "./dist/esm/adapters/embedding.d.ts",
|
|
26
|
+
"import": "./dist/esm/adapters/embedding.js"
|
|
23
27
|
}
|
|
24
28
|
},
|
|
25
29
|
"files": [
|
|
@@ -34,12 +38,12 @@
|
|
|
34
38
|
],
|
|
35
39
|
"devDependencies": {
|
|
36
40
|
"@vitest/coverage-v8": "4.0.14",
|
|
37
|
-
"vite": "^
|
|
38
|
-
"@tanstack/ai": "0.
|
|
41
|
+
"vite": "^8.1.4",
|
|
42
|
+
"@tanstack/ai": "0.44.0"
|
|
39
43
|
},
|
|
40
44
|
"peerDependencies": {
|
|
41
45
|
"zod": "^4.0.0",
|
|
42
|
-
"@tanstack/ai": "^0.
|
|
46
|
+
"@tanstack/ai": "^0.44.0"
|
|
43
47
|
},
|
|
44
48
|
"dependencies": {
|
|
45
49
|
"@mistralai/mistralai": "2.2.0"
|
|
@@ -47,9 +51,9 @@
|
|
|
47
51
|
"scripts": {
|
|
48
52
|
"build": "vite build",
|
|
49
53
|
"clean": "premove ./build ./dist",
|
|
50
|
-
"lint:fix": "
|
|
54
|
+
"lint:fix": "oxlint src --type-aware --fix",
|
|
51
55
|
"test:build": "publint --strict",
|
|
52
|
-
"test:
|
|
56
|
+
"test:oxlint": "oxlint src --type-aware",
|
|
53
57
|
"test:lib": "vitest run",
|
|
54
58
|
"test:lib:dev": "pnpm test:lib --watch",
|
|
55
59
|
"test:types": "tsc"
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { BaseEmbeddingAdapter } from '@tanstack/ai/adapters'
|
|
2
|
+
import { toRunErrorPayload } from '@tanstack/ai/adapter-internals'
|
|
3
|
+
import { requireTextOnlyEmbeddingInput } from '@tanstack/ai'
|
|
4
|
+
import {
|
|
5
|
+
createMistralClient,
|
|
6
|
+
generateId,
|
|
7
|
+
getMistralApiKeyFromEnv,
|
|
8
|
+
} from '../utils/client'
|
|
9
|
+
import type {
|
|
10
|
+
EmbeddingOptions,
|
|
11
|
+
EmbeddingResult,
|
|
12
|
+
TokenUsage,
|
|
13
|
+
} from '@tanstack/ai'
|
|
14
|
+
import type { Mistral } from '@mistralai/mistralai'
|
|
15
|
+
import type { EmbeddingRequest } from '@mistralai/mistralai/models/components'
|
|
16
|
+
import type {
|
|
17
|
+
MistralEmbeddingModel,
|
|
18
|
+
MistralEmbeddingModelInputModalitiesByName,
|
|
19
|
+
MistralEmbeddingModelProviderOptionsByName,
|
|
20
|
+
} from '../model-meta'
|
|
21
|
+
import type { MistralEmbeddingProviderOptions } from '../embedding/embedding-provider-options'
|
|
22
|
+
import type { MistralClientConfig } from '../utils/client'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Configuration for Mistral embedding adapter.
|
|
26
|
+
*/
|
|
27
|
+
export type MistralEmbeddingConfig = MistralClientConfig
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Mistral Embedding Adapter
|
|
31
|
+
*
|
|
32
|
+
* Tree-shakeable adapter for Mistral text embeddings.
|
|
33
|
+
* Supports mistral-embed and codestral-embed.
|
|
34
|
+
*
|
|
35
|
+
* Features:
|
|
36
|
+
* - Batch embedding (one request for the whole input array)
|
|
37
|
+
* - Dimension reduction for codestral-embed via the top-level `dimensions`
|
|
38
|
+
* option (mapped to Mistral's `outputDimension`); mistral-embed has a fixed
|
|
39
|
+
* 1024-dimension output and rejects `dimensions`.
|
|
40
|
+
*/
|
|
41
|
+
export class MistralEmbeddingAdapter<
|
|
42
|
+
TModel extends MistralEmbeddingModel,
|
|
43
|
+
> extends BaseEmbeddingAdapter<
|
|
44
|
+
TModel,
|
|
45
|
+
MistralEmbeddingProviderOptions,
|
|
46
|
+
MistralEmbeddingModelProviderOptionsByName,
|
|
47
|
+
MistralEmbeddingModelInputModalitiesByName
|
|
48
|
+
> {
|
|
49
|
+
readonly name = 'mistral' as const
|
|
50
|
+
|
|
51
|
+
protected client: Mistral
|
|
52
|
+
|
|
53
|
+
constructor(config: MistralEmbeddingConfig, model: TModel) {
|
|
54
|
+
super(model, {})
|
|
55
|
+
this.client = createMistralClient(config)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async createEmbeddings(
|
|
59
|
+
options: EmbeddingOptions<MistralEmbeddingProviderOptions>,
|
|
60
|
+
): Promise<EmbeddingResult> {
|
|
61
|
+
const { model, logger, modelOptions } = options
|
|
62
|
+
const texts = requireTextOnlyEmbeddingInput(options.input, this.name, model)
|
|
63
|
+
|
|
64
|
+
if (options.dimensions !== undefined && model === 'mistral-embed') {
|
|
65
|
+
throw new Error(
|
|
66
|
+
'mistral-embed does not support requesting dimensions (output is a fixed 1024-dimension vector). Use codestral-embed for dimension reduction, or omit `dimensions`.',
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
// Spread modelOptions first so it can never override the validated
|
|
72
|
+
// fields (server routes often pass modelOptions through from untyped
|
|
73
|
+
// client input).
|
|
74
|
+
const request: EmbeddingRequest = {
|
|
75
|
+
...modelOptions,
|
|
76
|
+
model,
|
|
77
|
+
inputs: texts,
|
|
78
|
+
}
|
|
79
|
+
if (options.dimensions !== undefined) {
|
|
80
|
+
request.outputDimension = options.dimensions
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
logger.request(
|
|
84
|
+
`activity=embed provider=${this.name} model=${model} inputs=${texts.length}`,
|
|
85
|
+
{ provider: this.name, model },
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
const response = await this.client.embeddings.create(request)
|
|
89
|
+
|
|
90
|
+
const usage: TokenUsage = {
|
|
91
|
+
promptTokens: response.usage.promptTokens ?? 0,
|
|
92
|
+
completionTokens: 0,
|
|
93
|
+
totalTokens: response.usage.totalTokens ?? 0,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
id: generateId(this.name),
|
|
98
|
+
model,
|
|
99
|
+
// Mistral returns one entry per input; `index` is optional in the SDK
|
|
100
|
+
// types, so fall back to array order when it's absent.
|
|
101
|
+
embeddings: response.data.map((item, arrayIndex) => ({
|
|
102
|
+
vector: item.embedding ?? [],
|
|
103
|
+
index: item.index ?? arrayIndex,
|
|
104
|
+
})),
|
|
105
|
+
usage,
|
|
106
|
+
}
|
|
107
|
+
} catch (error: unknown) {
|
|
108
|
+
logger.errors(`${this.name}.createEmbeddings fatal`, {
|
|
109
|
+
error: toRunErrorPayload(error, `${this.name}.createEmbeddings failed`),
|
|
110
|
+
source: `${this.name}.createEmbeddings`,
|
|
111
|
+
})
|
|
112
|
+
throw error
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Creates a Mistral embedding adapter with explicit API key.
|
|
119
|
+
* Type resolution happens here at the call site.
|
|
120
|
+
*
|
|
121
|
+
* @param model - The model name (e.g., 'mistral-embed')
|
|
122
|
+
* @param apiKey - Your Mistral API key
|
|
123
|
+
* @param config - Optional additional configuration
|
|
124
|
+
* @returns Configured Mistral embedding adapter instance with resolved types
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const adapter = createMistralEmbedding('mistral-embed', 'api_key');
|
|
129
|
+
*
|
|
130
|
+
* const result = await embed({
|
|
131
|
+
* adapter,
|
|
132
|
+
* input: 'a red guitar'
|
|
133
|
+
* });
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export function createMistralEmbedding<TModel extends MistralEmbeddingModel>(
|
|
137
|
+
model: TModel,
|
|
138
|
+
apiKey: string,
|
|
139
|
+
config?: Omit<MistralEmbeddingConfig, 'apiKey'>,
|
|
140
|
+
): MistralEmbeddingAdapter<TModel> {
|
|
141
|
+
return new MistralEmbeddingAdapter({ apiKey, ...config }, model)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Creates a Mistral embedding adapter using the `MISTRAL_API_KEY` environment variable.
|
|
146
|
+
* Type resolution happens here at the call site.
|
|
147
|
+
*
|
|
148
|
+
* Looks for `MISTRAL_API_KEY` in:
|
|
149
|
+
* - `process.env` (Node.js)
|
|
150
|
+
* - `window.env` (Browser with injected env)
|
|
151
|
+
*
|
|
152
|
+
* @param model - The model name (e.g., 'codestral-embed')
|
|
153
|
+
* @param config - Optional configuration (excluding apiKey which is auto-detected)
|
|
154
|
+
* @returns Configured Mistral embedding adapter instance with resolved types
|
|
155
|
+
* @throws Error if MISTRAL_API_KEY is not found in environment
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* // Automatically uses MISTRAL_API_KEY from environment
|
|
160
|
+
* const adapter = mistralEmbedding('codestral-embed');
|
|
161
|
+
*
|
|
162
|
+
* const result = await embed({
|
|
163
|
+
* adapter,
|
|
164
|
+
* input: ['a red guitar', 'a blue drum kit'],
|
|
165
|
+
* dimensions: 256
|
|
166
|
+
* });
|
|
167
|
+
*
|
|
168
|
+
* console.log(result.embeddings[0].vector)
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export function mistralEmbedding<TModel extends MistralEmbeddingModel>(
|
|
172
|
+
model: TModel,
|
|
173
|
+
config?: Omit<MistralEmbeddingConfig, 'apiKey'>,
|
|
174
|
+
): MistralEmbeddingAdapter<TModel> {
|
|
175
|
+
const apiKey = getMistralApiKeyFromEnv()
|
|
176
|
+
return createMistralEmbedding(model, apiKey, config)
|
|
177
|
+
}
|
package/src/adapters/text.ts
CHANGED
|
@@ -42,7 +42,7 @@ import type { MistralClientConfig } from '../utils/client'
|
|
|
42
42
|
/** Cast an event object to StreamChunk. Adapters construct events with string
|
|
43
43
|
* literal types which are structurally compatible with the EventType enum. */
|
|
44
44
|
const asChunk = (chunk: Record<string, unknown>) =>
|
|
45
|
-
//
|
|
45
|
+
// oxlint-disable-next-line eslint-js/no-restricted-syntax -- Record<string, unknown> doesn't structurally overlap the StreamChunk discriminated union; events are built with literal `type` fields the union accepts at runtime
|
|
46
46
|
chunk as unknown as StreamChunk
|
|
47
47
|
|
|
48
48
|
/**
|
|
@@ -1021,8 +1021,17 @@ export class MistralTextAdapter<
|
|
|
1021
1021
|
}
|
|
1022
1022
|
}
|
|
1023
1023
|
|
|
1024
|
+
if (part.type === 'document') {
|
|
1025
|
+
const documentValue = part.source.value
|
|
1026
|
+
const documentUrl =
|
|
1027
|
+
part.source.type === 'data' && !documentValue.startsWith('data:')
|
|
1028
|
+
? `data:${part.source.mimeType};base64,${documentValue}`
|
|
1029
|
+
: documentValue
|
|
1030
|
+
return { type: 'document_url', documentUrl }
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1024
1033
|
throw new Error(
|
|
1025
|
-
`Mistral text adapter does not support content part of type '${(part as ContentPart).type}'. Supported types: text, image. Use a vision-capable model (
|
|
1034
|
+
`Mistral text adapter does not support content part of type '${(part as ContentPart).type}'. Supported types: text, image, document. Use a vision-capable model (mistral-medium-latest or mistral-small-latest) for images and documents.`,
|
|
1026
1035
|
)
|
|
1027
1036
|
}
|
|
1028
1037
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider options for Mistral embedding models.
|
|
3
|
+
*
|
|
4
|
+
* `dimensions` is deliberately absent: it's a first-class top-level option on
|
|
5
|
+
* `embed()` and is mapped to Mistral's `outputDimension` request field by the
|
|
6
|
+
* adapter (codestral-embed only — mistral-embed has a fixed 1024-dim output).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Provider options for `mistral-embed`.
|
|
11
|
+
*
|
|
12
|
+
* mistral-embed accepts no provider-specific options: its output is a fixed
|
|
13
|
+
* 1024-dimension float vector.
|
|
14
|
+
*/
|
|
15
|
+
export type MistralEmbedProviderOptions = Record<string, never>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Provider options for `codestral-embed`.
|
|
19
|
+
*/
|
|
20
|
+
export interface CodestralEmbedProviderOptions {
|
|
21
|
+
/**
|
|
22
|
+
* The data type of the output embedding values. Mirrors the Mistral SDK's
|
|
23
|
+
* `EmbeddingDtype` enum. Defaults to `float`.
|
|
24
|
+
*/
|
|
25
|
+
outputDtype?: 'float' | 'int8' | 'uint8' | 'binary' | 'ubinary'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Widest provider options shape accepted by the Mistral embedding adapter.
|
|
30
|
+
* Per-model narrowing happens via `MistralEmbeddingModelProviderOptionsByName`.
|
|
31
|
+
*/
|
|
32
|
+
export type MistralEmbeddingProviderOptions = CodestralEmbedProviderOptions
|
package/src/index.ts
CHANGED
|
@@ -14,6 +14,19 @@ export {
|
|
|
14
14
|
type MistralTextProviderOptions,
|
|
15
15
|
} from './adapters/text'
|
|
16
16
|
|
|
17
|
+
// Embedding adapter - for embedding vectors
|
|
18
|
+
export {
|
|
19
|
+
MistralEmbeddingAdapter,
|
|
20
|
+
createMistralEmbedding,
|
|
21
|
+
mistralEmbedding,
|
|
22
|
+
type MistralEmbeddingConfig,
|
|
23
|
+
} from './adapters/embedding'
|
|
24
|
+
export type {
|
|
25
|
+
MistralEmbeddingProviderOptions,
|
|
26
|
+
MistralEmbedProviderOptions,
|
|
27
|
+
CodestralEmbedProviderOptions,
|
|
28
|
+
} from './embedding/embedding-provider-options'
|
|
29
|
+
|
|
17
30
|
// Types
|
|
18
31
|
export type {
|
|
19
32
|
MistralChatModelProviderOptionsByName,
|
|
@@ -21,8 +34,11 @@ export type {
|
|
|
21
34
|
ResolveProviderOptions,
|
|
22
35
|
ResolveInputModalities,
|
|
23
36
|
MistralChatModels,
|
|
37
|
+
MistralEmbeddingModel,
|
|
38
|
+
MistralEmbeddingModelProviderOptionsByName,
|
|
39
|
+
MistralEmbeddingModelInputModalitiesByName,
|
|
24
40
|
} from './model-meta'
|
|
25
|
-
export { MISTRAL_CHAT_MODELS } from './model-meta'
|
|
41
|
+
export { MISTRAL_CHAT_MODELS, MISTRAL_EMBEDDING_MODELS } from './model-meta'
|
|
26
42
|
export type {
|
|
27
43
|
MistralTextMetadata,
|
|
28
44
|
MistralImageMetadata,
|
package/src/model-meta.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import type { MistralTextProviderOptions } from './text/text-provider-options'
|
|
2
|
+
import type {
|
|
3
|
+
CodestralEmbedProviderOptions,
|
|
4
|
+
MistralEmbedProviderOptions,
|
|
5
|
+
} from './embedding/embedding-provider-options'
|
|
2
6
|
|
|
3
7
|
/** Provider options for vision-capable Mistral models (pixtral-*). */
|
|
4
8
|
export type MistralVisionProviderOptions = MistralTextProviderOptions
|
|
@@ -19,7 +23,7 @@ interface ModelMeta<TProviderOptions = unknown> {
|
|
|
19
23
|
output?: { normal: number }
|
|
20
24
|
}
|
|
21
25
|
supports: {
|
|
22
|
-
input: Array<'text' | 'image' | 'audio'>
|
|
26
|
+
input: Array<'text' | 'image' | 'audio' | 'document'>
|
|
23
27
|
output: Array<'text'>
|
|
24
28
|
endpoints: Array<'chat' | 'embeddings'>
|
|
25
29
|
|
|
@@ -61,7 +65,7 @@ const MISTRAL_MEDIUM_LATEST = {
|
|
|
61
65
|
output: { normal: 2 },
|
|
62
66
|
},
|
|
63
67
|
supports: {
|
|
64
|
-
input: ['text', 'image'],
|
|
68
|
+
input: ['text', 'image', 'document'],
|
|
65
69
|
output: ['text'],
|
|
66
70
|
endpoints: ['chat'],
|
|
67
71
|
features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
|
|
@@ -77,7 +81,7 @@ const MISTRAL_SMALL_LATEST = {
|
|
|
77
81
|
output: { normal: 0.3 },
|
|
78
82
|
},
|
|
79
83
|
supports: {
|
|
80
|
-
input: ['text', 'image'],
|
|
84
|
+
input: ['text', 'image', 'document'],
|
|
81
85
|
output: ['text'],
|
|
82
86
|
endpoints: ['chat'],
|
|
83
87
|
features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
|
|
@@ -141,7 +145,7 @@ const PIXTRAL_LARGE_LATEST = {
|
|
|
141
145
|
output: { normal: 6 },
|
|
142
146
|
},
|
|
143
147
|
supports: {
|
|
144
|
-
input: ['text', 'image'],
|
|
148
|
+
input: ['text', 'image', 'document'],
|
|
145
149
|
output: ['text'],
|
|
146
150
|
endpoints: ['chat'],
|
|
147
151
|
features: ['streaming', 'tools', 'json_object', 'json_schema', 'vision'],
|
|
@@ -157,7 +161,7 @@ const PIXTRAL_12B_2409 = {
|
|
|
157
161
|
output: { normal: 0.15 },
|
|
158
162
|
},
|
|
159
163
|
supports: {
|
|
160
|
-
input: ['text', 'image'],
|
|
164
|
+
input: ['text', 'image', 'document'],
|
|
161
165
|
output: ['text'],
|
|
162
166
|
endpoints: ['chat'],
|
|
163
167
|
features: ['streaming', 'tools', 'json_object', 'vision'],
|
|
@@ -268,6 +272,39 @@ export type MistralChatModelProviderOptionsByName = {
|
|
|
268
272
|
[OPEN_MISTRAL_NEMO.name]: MistralTextProviderOptions
|
|
269
273
|
}
|
|
270
274
|
|
|
275
|
+
/**
|
|
276
|
+
* Embedding models (based on endpoints: "embeddings")
|
|
277
|
+
*/
|
|
278
|
+
export const MISTRAL_EMBEDDING_MODELS = [
|
|
279
|
+
'mistral-embed',
|
|
280
|
+
'codestral-embed',
|
|
281
|
+
] as const
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Union type of all supported Mistral embedding model names.
|
|
285
|
+
*/
|
|
286
|
+
export type MistralEmbeddingModel = (typeof MISTRAL_EMBEDDING_MODELS)[number]
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Type-only map from embedding model name to its provider options type.
|
|
290
|
+
*
|
|
291
|
+
* `mistral-embed` accepts no provider options (fixed 1024-dim output);
|
|
292
|
+
* `codestral-embed` additionally supports `outputDtype`.
|
|
293
|
+
*/
|
|
294
|
+
export type MistralEmbeddingModelProviderOptionsByName = {
|
|
295
|
+
'mistral-embed': MistralEmbedProviderOptions
|
|
296
|
+
'codestral-embed': CodestralEmbedProviderOptions
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Per-model input modalities for embedding models. Mistral embedding models
|
|
301
|
+
* are text-only, so image inputs fail at compile time.
|
|
302
|
+
*/
|
|
303
|
+
export type MistralEmbeddingModelInputModalitiesByName = {
|
|
304
|
+
'mistral-embed': readonly ['text']
|
|
305
|
+
'codestral-embed': readonly ['text']
|
|
306
|
+
}
|
|
307
|
+
|
|
271
308
|
/**
|
|
272
309
|
* Resolves the provider options type for a specific Mistral model.
|
|
273
310
|
*/
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export function transformNullsToUndefined<T>(obj: T): T {
|
|
9
9
|
if (obj === null) {
|
|
10
|
-
//
|
|
10
|
+
// oxlint-disable-next-line eslint-js/no-restricted-syntax -- generic T has no structural overlap with undefined; null→undefined conversion is this function's documented contract
|
|
11
11
|
return undefined as unknown as T
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -15,7 +15,7 @@ export function transformNullsToUndefined<T>(obj: T): T {
|
|
|
15
15
|
// Preserve array length and indices — converting null elements to
|
|
16
16
|
// undefined slots rather than dropping them. `Array<T | null>` schemas
|
|
17
17
|
// depend on positional alignment.
|
|
18
|
-
//
|
|
18
|
+
// oxlint-disable-next-line eslint-js/no-restricted-syntax -- the mapped array (unknown[]) has no structural overlap with generic T; positional null→undefined transform preserves the array shape T describes
|
|
19
19
|
return obj.map((item) => transformNullsToUndefined(item)) as unknown as T
|
|
20
20
|
}
|
|
21
21
|
|
package/dist/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|