Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

修复了issue-254,在设置向量嵌入模型时,增加了一个选项:自定义请求头。便于调用使用网关的模型

shisonghonga61218dd1b83
diff --git a/src-tauri/src/commands/search.rs b/src-tauri/src/commands/search.rs
index 23b12bcd..ad18074b 100644
--- a/src-tauri/src/commands/search.rs
+++ b/src-tauri/src/commands/search.rs
@@ -61,6 +61,12 @@ pub struct SearchEmbeddingConfig {
pub api_key: String,
pub model: String,
pub output_dimensionality: Option<u32>,
+ /// Extra HTTP headers to send with every embedding request, e.g.
+ /// `X-Model-Provider-Id: siliconflow` for the mify gateway.
+ /// Reserved names (Authorization, Content-Type, Host,
+ /// Content-Length) are skipped — they're managed by the client.
+ #[serde(default)]
+ pub extra_headers: Option<BTreeMap<String, String>>,
}
#[tauri::command]
@@ -681,6 +687,22 @@ async fn fetch_embedding(text: &str, cfg: &SearchEmbeddingConfig) -> Result<Vec<
req = req.bearer_auth(cfg.api_key.trim());
}
}
+ if let Some(extra) = cfg.extra_headers.as_ref() {
+ for (name, value) in extra {
+ let trimmed = name.trim();
+ if trimmed.is_empty() {
+ continue;
+ }
+ let lower = trimmed.to_ascii_lowercase();
+ if matches!(
+ lower.as_str(),
+ "authorization" | "content-type" | "host" | "content-length"
+ ) {
+ continue;
+ }
+ req = req.header(trimmed, value.as_str());
+ }
+ }
let body = if is_google {
google_embedding_body(&cfg.model, text, cfg.output_dimensionality)
} else {
@@ -940,6 +962,7 @@ mod tests {
api_key: "HEADER_KEY".to_string(),
model: "gemini-embedding-001".to_string(),
output_dimensionality: Some(768),
+ extra_headers: None,
};
let endpoint = google_embedding_endpoint(&cfg);
diff --git a/src/components/settings/sections/embedding-section.tsx b/src/components/settings/sections/embedding-section.tsx
index 09d060e2..495ea96d 100644
--- a/src/components/settings/sections/embedding-section.tsx
+++ b/src/components/settings/sections/embedding-section.tsx
@@ -38,6 +38,29 @@ function parsePositiveInteger(value: string): number | undefined {
return Math.floor(n)
}
+const RESERVED_HEADER_NAMES = new Set(["authorization", "content-type", "host", "content-length"])
+
+function headersToText(headers: Record<string, string>): string {
+ return Object.entries(headers)
+ .map(([k, v]) => `${k}: ${v}`)
+ .join("\n")
+}
+
+function parseHeadersText(text: string): Record<string, string> {
+ const out: Record<string, string> = {}
+ for (const rawLine of text.split(/\r?\n/)) {
+ const line = rawLine.trim()
+ if (!line || line.startsWith("#")) continue
+ const idx = line.indexOf(":")
+ if (idx <= 0) continue
+ const name = line.slice(0, idx).trim()
+ const value = line.slice(idx + 1).trim()
+ if (!name || RESERVED_HEADER_NAMES.has(name.toLowerCase())) continue
+ out[name] = value
+ }
+ return out
+}
+
export function EmbeddingSection({ draft, setDraft }: Props) {
const { t } = useTranslation()
const project = useWikiStore((s) => s.project)
@@ -49,6 +72,7 @@ export function EmbeddingSection({ draft, setDraft }: Props) {
const [reindex, setReindex] = useState<ReindexState>({ kind: "idle" })
const [testState, setTestState] = useState<TestState>({ kind: "idle" })
const [legacyDropped, setLegacyDropped] = useState(false)
+ const [headersText, setHeadersText] = useState<string>(() => headersToText(draft.embeddingExtraHeaders ?? {}))
const refreshStats = useCallback(async () => {
if (!project) return
@@ -94,6 +118,7 @@ export function EmbeddingSection({ draft, setDraft }: Props) {
outputDimensionality: draft.embeddingOutputDimensionality,
maxChunkChars: draft.embeddingMaxChunkChars,
overlapChunkChars: draft.embeddingOverlapChunkChars,
+ extraHeaders: draft.embeddingExtraHeaders,
}
async function runEmbeddingTest(kind: "connection" | "function") {
@@ -194,6 +219,24 @@ export function EmbeddingSection({ draft, setDraft }: Props) {
</p>
</div>
+ <div className="space-y-2">
+ <Label>{t("settings.sections.embedding.extraHeaders")}</Label>
+ <textarea
+ value={headersText}
+ onChange={(e) => {
+ const text = e.target.value
+ setHeadersText(text)
+ setDraft("embeddingExtraHeaders", parseHeadersText(text))
+ }}
+ placeholder={"X-Model-Provider-Id: siliconflow\nX-Custom-Header: value"}
+ rows={3}
+ className="flex w-full rounded-md border border-input bg-transparent px-3 py-2 font-mono text-sm shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
+ />
+ <p className="text-xs text-muted-foreground">
+ {t("settings.sections.embedding.extraHeadersHint")}
+ </p>
+ </div>
+
<div className="space-y-3 rounded-md border p-3">
<div className="text-sm font-medium">
{t("settings.sections.embedding.chunking")}
diff --git a/src/components/settings/settings-types.ts b/src/components/settings/settings-types.ts
index 01397218..87a06fd7 100644
--- a/src/components/settings/settings-types.ts
+++ b/src/components/settings/settings-types.ts
@@ -31,6 +31,8 @@ export interface SettingsDraft {
embeddingMaxChunkChars: number | undefined
/** Overlap characters between adjacent chunks. Empty = default (200). */
embeddingOverlapChunkChars: number | undefined
+ /** Extra HTTP headers to send on every embedding request. Empty = none. */
+ embeddingExtraHeaders: Record<string, string>
// Multimodal (image captioning at ingest time)
multimodalEnabled: boolean
diff --git a/src/components/settings/settings-view.tsx b/src/components/settings/settings-view.tsx
index 5a8f13c0..88427b1b 100644
--- a/src/components/settings/settings-view.tsx
+++ b/src/components/settings/settings-view.tsx
@@ -120,6 +120,7 @@ function initialDraft(
embeddingOutputDimensionality: embed.outputDimensionality,
embeddingMaxChunkChars: embed.maxChunkChars,
embeddingOverlapChunkChars: embed.overlapChunkChars,
+ embeddingExtraHeaders: embed.extraHeaders ?? {},
multimodalEnabled: multimodal.enabled,
multimodalUseMainLlm: multimodal.useMainLlm,
multimodalProvider: multimodal.provider,
@@ -287,6 +288,7 @@ export function SettingsView() {
outputDimensionality: draft.embeddingOutputDimensionality,
maxChunkChars: draft.embeddingMaxChunkChars,
overlapChunkChars: draft.embeddingOverlapChunkChars,
+ extraHeaders: draft.embeddingExtraHeaders,
}
const newMultimodal = {
enabled: draft.multimodalEnabled,
diff --git a/src/i18n/en.json b/src/i18n/en.json
index 4cb3e028..34839d14 100644
--- a/src/i18n/en.json
+++ b/src/i18n/en.json
@@ -255,6 +255,8 @@
"model": "Model",
"outputDimensionality": "Output dimensionality (Gemini only)",
"outputDimensionalityHint": "Optional. Sent only for Gemini native embedContent endpoints as `output_dimensionality`; OpenAI-compatible endpoints ignore this field. Leave empty for the model default. Rebuild embeddings after changing model or dimension.",
+ "extraHeaders": "Custom request headers",
+ "extraHeadersHint": "Optional. One per line, formatted `Header-Name: value`. Used for gateways that route by request header — e.g. mify needs `X-Model-Provider-Id: siliconflow`. Authorization, Content-Type, Host, and Content-Length are managed by the client and will be ignored.",
"chunking": "Chunking",
"maxChunkChars": "Max chunk size (characters)",
"maxChunkCharsHint": "Target size of each chunk. Lower this (e.g. 500) when your endpoint's context is small (llama.cpp n_ctx=512, mxbai-embed-large, etc.).",
diff --git a/src/i18n/zh.json b/src/i18n/zh.json
index 39b2146e..4937625b 100644
--- a/src/i18n/zh.json
+++ b/src/i18n/zh.json
@@ -255,6 +255,8 @@
"model": "Model",
"outputDimensionality": "输出维度(仅 Gemini)",
"outputDimensionalityHint": "可选。仅对 Gemini 原生 embedContent endpoint 作为 `output_dimensionality` 发送;OpenAI 兼容 endpoint 会忽略此项。留空则使用模型默认值。修改模型或维度后需要重建嵌入。",
+ "extraHeaders": "自定义请求头",
+ "extraHeadersHint": "可选。每行一条,格式 `Header-Name: value`。用于需要按请求头路由的网关,例如 mify 需要 `X-Model-Provider-Id: siliconflow`。Authorization、Content-Type、Host、Content-Length 由系统管理,会被忽略。",
"chunking": "切分参数",
"maxChunkChars": "每块最大字符数",
"maxChunkCharsHint": "单个 chunk 的目标长度。endpoint 上下文较小时(llama.cpp n_ctx=512、mxbai-embed-large 等)调低(如 500)。",
diff --git a/src/lib/embedding.ts b/src/lib/embedding.ts
index b918008b..d79fb12c 100644
--- a/src/lib/embedding.ts
+++ b/src/lib/embedding.ts
@@ -94,6 +94,14 @@ export async function fetchEmbedding(
headers.Authorization = `Bearer ${cfg.apiKey}`
}
}
+ if (cfg.extraHeaders) {
+ const reserved = new Set(["authorization", "content-type", "host", "content-length"])
+ for (const [k, v] of Object.entries(cfg.extraHeaders)) {
+ const name = k.trim()
+ if (!name || reserved.has(name.toLowerCase())) continue
+ headers[name] = v
+ }
+ }
let current = text
let attempts = 0
diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.ts
index 4303a18c..04d59bfa 100644
--- a/src/stores/wiki-store.ts
+++ b/src/stores/wiki-store.ts
@@ -97,6 +97,14 @@ interface EmbeddingConfig {
*/
maxChunkChars?: number
overlapChunkChars?: number
+ /**
+ * Extra HTTP headers to send with every embedding request, e.g.
+ * { "X-Model-Provider-Id": "siliconflow" }
+ * for gateways like mify that route by header. Reserved names
+ * (Authorization, Content-Type, Host, Content-Length) are ignored
+ * — they're managed by the embedding client itself.
+ */
+ extraHeaders?: Record<string, string>
}
/**