fix: SSL certificate issue and Windows terminal encoding
This commit is contained in:
+21
-1
@@ -1,4 +1,11 @@
|
||||
"""嵌入模型抽象层."""
|
||||
import os
|
||||
|
||||
# 解决 Windows SSL 证书问题: 强制使用本地缓存模型, 避免联网验证
|
||||
# 模型首次下载需要先临时取消这些环境变量 (或手动运行一次下载脚本)
|
||||
os.environ.setdefault("HF_HUB_OFFLINE", "1")
|
||||
os.environ.setdefault("TRANSFORMERS_OFFLINE", "1")
|
||||
|
||||
from sentence_transformers import SentenceTransformer
|
||||
|
||||
from src.core.config import EmbedConfig
|
||||
@@ -10,7 +17,20 @@ class Embedder:
|
||||
def __init__(self, config: EmbedConfig):
|
||||
self._config = config
|
||||
if config.mode == "local":
|
||||
self._model = SentenceTransformer(config.local_model)
|
||||
# 优先从本地缓存加载, 若模型未下载则通过镜像下载
|
||||
try:
|
||||
self._model = SentenceTransformer(
|
||||
config.local_model, local_files_only=True
|
||||
)
|
||||
except Exception:
|
||||
# 模型未缓存: 临时开启网络, 使用国内镜像
|
||||
os.environ.pop("HF_HUB_OFFLINE", None)
|
||||
os.environ.pop("TRANSFORMERS_OFFLINE", None)
|
||||
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
||||
self._model = SentenceTransformer(config.local_model)
|
||||
# 恢复离线设置, 后续加载走缓存
|
||||
os.environ["HF_HUB_OFFLINE"] = "1"
|
||||
os.environ["TRANSFORMERS_OFFLINE"] = "1"
|
||||
elif config.mode == "api":
|
||||
self._model = None # 延迟初始化, 需要 openai 包
|
||||
self._api_base = config.api_base
|
||||
|
||||
Reference in New Issue
Block a user