Repository navigation
Expand file tree
/
Copy pathembeddings.ts
More file actions
61 lines (58 loc) · 2.2 KB
/
Copy pathembeddings.ts
File metadata and controls
61 lines (58 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
import { createHash } from "node:crypto";
import type { AIConfig, AIModel } from "./config.js";
import { embeddingSource, embeddingApi } from "./providers.js";
import { fail } from "../../shared/errors.js";
export function embeddingModelIssue(config: AIConfig, model: AIModel) {
if (!model.embedding) return "请选择向量模型";
if (!model.enabled) return "模型已停用";
if (!config.vendors.some((v) => v.id === model.vendorId && v.enabled))
return "模型厂商已停用或不存在";
if (!embeddingSource(model.provider)) return "暂不支持该厂商的向量接口";
if (!model.apiKey && model.provider !== "ollama")
return "模型厂商尚未配置密钥";
if (
(embeddingSource(model.provider) === "rest" ||
embeddingApi(model) === "doubao-multimodal") &&
!model.embeddingDimensions
)
return "请在 AI 模型管理中填写向量维度";
return "";
}
export function embeddingSettings(model: AIModel) {
const source = embeddingSource(model.provider);
if (!model.embedding || !source) fail(400, "请选择受支持的向量模型");
return {
source: embeddingApi(model) === "doubao-multimodal" ? "rest" : source,
url:
model.baseUrl.replace(/\/+$/, "") +
(embeddingApi(model) === "doubao-multimodal"
? "/embeddings/multimodal"
: "/embeddings"),
// Always send the current vendor credential, including explicit removal.
apiKey: model.apiKey || null,
dimensions: model.embeddingDimensions ?? null,
...(embeddingApi(model) === "doubao-multimodal"
? {
request: {
model: model.model,
input: [{ type: "text", text: "{{text}}" }],
},
response: { data: { embedding: "{{embedding}}" } },
}
: source === "openAi"
? { model: model.model }
: {
request: {
model: model.model,
input: ["{{text}}", "{{..}}"],
encoding_format: "float",
},
response: { data: [{ embedding: "{{embedding}}" }, "{{..}}"] },
}),
};
}
export function embeddingFingerprint(model: AIModel) {
return createHash("sha256")
.update(JSON.stringify(embeddingSettings(model)))
.digest("hex");
}