feat: huggingface_hub.set_endpoint() mirror + API base_url support

This commit is contained in:
Research Assistant 2026-05-13 21:03:24 +08:00
parent 8bf9476a76
commit beda4119e8
2 changed files with 22 additions and 3 deletions

View file

@ -76,7 +76,14 @@ def get_embedding_model(vault: Path):
model_name = settings.get("vector_db_model", "BAAI/bge-small-en-v1.5")
# HF_ENDPOINT is set by the JS plugin via environment variable — don't override
# Apply HF mirror endpoint via huggingface_hub API (works alongside env var)
hf_endpoint = settings.get("vector_db_hf_endpoint", "") or os.environ.get("HF_ENDPOINT", "")
if hf_endpoint:
try:
from huggingface_hub import set_endpoint
set_endpoint(hf_endpoint)
except Exception:
pass
if _cached_model is not None and _cached_model_name == model_name:
return _cached_model
@ -136,7 +143,7 @@ def _embed_paper_api(vault, zotero_key, chunks, collection) -> int:
raise ValueError("No API key configured for vector DB")
from openai import OpenAI
client = OpenAI(api_key=api_key)
client = OpenAI(api_key=api_key, base_url=settings.get("vector_db_api_base", None) or None)
texts = [c["text"] for c in chunks]
ids = [f"{zotero_key}_{c['chunk_index']}" for c in chunks]
@ -184,7 +191,7 @@ def retrieve_chunks(vault: Path, query: str, limit: int = 5, expand: bool = True
if not api_key:
raise ValueError("No API key configured for vector DB")
from openai import OpenAI
client = OpenAI(api_key=api_key)
client = OpenAI(api_key=api_key, base_url=settings.get("vector_db_api_base", None) or None)
response = client.embeddings.create(model="text-embedding-3-small", input=query)
query_embedding = response.data[0].embedding
else:

View file

@ -570,6 +570,7 @@ const DEFAULT_SETTINGS = {
vector_db_mode: 'local',
vector_db_model: 'BAAI/bge-small-en-v1.5',
vector_db_api_key: '',
vector_db_api_base: '',
vector_db_hf_endpoint: 'https://hf-mirror.com',
vector_db_last_model: '',
frozen_skills: {},
@ -3083,6 +3084,17 @@ class PaperForgeSettingTab extends PluginSettingTab {
button.setDisabled(false);
});
});
new Setting(containerEl)
.setName('API Base URL')
.setDesc('Custom OpenAI-compatible API endpoint (e.g., https://api.openai.com/v1). Leave empty for default.')
.addText(text => {
text.setPlaceholder('https://api.openai.com/v1')
.setValue(this.plugin.settings.vector_db_api_base || '')
.onChange(value => {
this.plugin.settings.vector_db_api_base = value;
this.plugin.saveSettings();
});
});
}
// Rebuild button with live terminal output