faiss server

This commit is contained in:
2026-04-10 11:55:00 +00:00
parent bc82e3e708
commit 8e39e609cc
30 changed files with 1271 additions and 1048 deletions

34
faiss/config.py Normal file
View File

@@ -0,0 +1,34 @@
# 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_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()