fix: add HuggingFace mirror support for China network

- Add HF_ENDPOINT configuration option
- Set HuggingFace mirror to https://hf-mirror.com
- Fix 'cannot connect to huggingface.co' error
- Update .env.example with HF_ENDPOINT setting

Problem:
- RAG engine uses sentence-transformers model
- Model download requires connection to huggingface.co
- China network cannot access huggingface.co
- Error: 'We couldn't connect to https://huggingface.co'

Solution:
- Add HF_ENDPOINT environment variable support
- Use hf-mirror.com as HuggingFace mirror
- Set mirror before loading sentence-transformers
- Document in .env.example

Files:
- backend/app/config.py: add HF_ENDPOINT config
- backend/app/core/rag_engine.py: set HF_ENDPOINT before model load
- backend/setup_hf_mirror.sh: setup script
- backend/.env: configure mirror (not tracked)
This commit is contained in:
2026-03-22 03:33:17 +00:00
parent f68edf2696
commit 04f7d372ea
3 changed files with 33 additions and 0 deletions

View File

@@ -33,6 +33,9 @@ class Settings(BaseSettings):
CHUNK_SIZE: int = 500 CHUNK_SIZE: int = 500
CHUNK_OVERLAP: int = 50 CHUNK_OVERLAP: int = 50
# HuggingFace
HF_ENDPOINT: str | None = None # Optional: HuggingFace mirror endpoint
@property @property
def DATABASE_URL(self) -> str: def DATABASE_URL(self) -> str:
"""构建数据库连接 URL密码安全编码""" """构建数据库连接 URL密码安全编码"""

View File

@@ -4,6 +4,7 @@ This module provides the RAGEngine class that handles knowledge document
storage and retrieval using ChromaDB and sentence-transformers embeddings. storage and retrieval using ChromaDB and sentence-transformers embeddings.
""" """
import os
from typing import Optional from typing import Optional
import chromadb import chromadb
@@ -28,6 +29,11 @@ class RAGEngine:
"""Initialize RAG engine with ChromaDB and embedding model.""" """Initialize RAG engine with ChromaDB and embedding model."""
settings = get_settings() settings = get_settings()
# Set HuggingFace mirror if configured
if settings.HF_ENDPOINT:
os.environ['HF_ENDPOINT'] = settings.HF_ENDPOINT
logger.info(f"Using HuggingFace mirror: {settings.HF_ENDPOINT}")
# Initialize ChromaDB persistent client # Initialize ChromaDB persistent client
logger.info(f"Initializing ChromaDB at: {settings.CHROMA_DB_PATH}") logger.info(f"Initializing ChromaDB at: {settings.CHROMA_DB_PATH}")
self.chroma_client = chromadb.PersistentClient( self.chroma_client = chromadb.PersistentClient(

View File

@@ -0,0 +1,24 @@
#!/bin/bash
# 设置 HuggingFace 镜像环境变量
echo "配置 HuggingFace 镜像..."
# 添加到 .env 文件
if ! grep -q "HF_ENDPOINT" /data/erp-ass/backend/.env; then
echo "" >> /data/erp-ass/backend/.env
echo "# HuggingFace Mirror" >> /data/erp-ass/backend/.env
echo "HF_ENDPOINT=https://hf-mirror.com" >> /data/erp-ass/backend/.env
echo "✓ 已添加 HF_ENDPOINT 到 .env"
else
echo "✓ HF_ENDPOINT 已存在"
fi
# 设置当前会话环境变量
export HF_ENDPOINT=https://hf-mirror.com
echo ""
echo "环境变量已设置:"
echo " HF_ENDPOINT=https://hf-mirror.com"
echo ""
echo "请重启后端服务:"
echo " ./stop.sh && ./start.sh"