Files
mchat/faiss/config.py
2026-04-10 12:47:39 +00:00

39 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# config.py
from pydantic_settings import BaseSettings
from functools import lru_cache
class Settings(BaseSettings):
# FAISS 配置(已优化为你的 LLM 相似问题缓存场景)
FAISS_DIM: int = 1024 # 根据你的 embedding 模型修改e.g. bge-large=1024, text-embedding-3-large=3072
FAISS_INDEX_DIR: str = "indexes"
FAISS_GLOBAL_DIR: str = "global"
FAISS_CONVERSATION_DIR: str = "conversations"
FAISS_GLOBAL_INDEX_NAME: str = "global_qa.index"
FAISS_INDEX_PATH: str = "faiss_index.bin"
FAISS_INDEX_TYPE: str = "HNSW" # 默认改为 HNSW最推荐
# HNSW 专用参数(速度 + 精度平衡)
HNSW_M: int = 32 # 每层连接数16-64越大精度越高但内存稍多
HNSW_EF_CONSTRUCTION: int = 200 # 构建质量100-400
HNSW_EF_SEARCH: int = 64 # 查询精度32-128越大越准但稍慢
# 是否使用余弦相似度(强烈推荐用于文本 embedding
USE_COSINE_SIMILARITY: bool = True # True = 自动归一化 + Index*IP
ENABLE_REQUEST_LOGS: bool = True # 是否打印插入/搜索业务日志
# FastAPI 配置
APP_HOST: str = "0.0.0.0"
APP_PORT: int = 8000
APP_TITLE: str = "FAISS 相似问题缓存服务"
APP_DESCRIPTION: str = "LLM 对话语义缓存 - 减少 token 消耗"
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
extra = "ignore"
@lru_cache()
def get_settings() -> Settings:
return Settings()