This commit is contained in:
hailin 2025-05-11 13:53:09 +08:00
parent 507a87b3d0
commit 5f97f88311
2 changed files with 15 additions and 19 deletions

View File

@ -4,6 +4,10 @@ from app.core.embedding import embedder # 使用已加载的本地嵌入模型
from app.core.config import settings from app.core.config import settings
from llama_index.vector_stores.faiss import FaissVectorStore from llama_index.vector_stores.faiss import FaissVectorStore
from llama_index import VectorStoreIndex, ServiceContext, StorageContext, load_index_from_storage from llama_index import VectorStoreIndex, ServiceContext, StorageContext, load_index_from_storage
from llama_index import set_global_service_context, ServiceContext
import os import os
import logging import logging
import faiss # 引入faiss import faiss # 引入faiss
@ -44,16 +48,18 @@ def search_docs(request: QueryRequest, user_id: str = Query(..., description="
vector_store = FaissVectorStore(faiss_index=faiss_index) vector_store = FaissVectorStore(faiss_index=faiss_index)
logger.info("FaissVectorStore created successfully.") logger.info("FaissVectorStore created successfully.")
# 创建 StorageContext 实例(确保同时加载文本和向量)
storage_context = StorageContext.from_defaults(persist_dir=index_path, vector_store=vector_store)
logger.info("Storage context created successfully.")
# 创建 ServiceContext 实例,确保使用本地嵌入模型,且明确禁用 LLM # 创建 ServiceContext 实例,确保使用本地嵌入模型,且明确禁用 LLM
service_context = ServiceContext.from_defaults(embed_model=embedder, llm=None, llm_predictor=None) service_context = ServiceContext.from_defaults(embed_model=embedder, llm=None, llm_predictor=None)
logger.info("Service context created successfully.") logger.info("Service context created successfully.")
set_global_service_context(service_context)
# 创建 StorageContext 实例(确保同时加载文本和向量)
storage_context = StorageContext.from_defaults(persist_dir=index_path, vector_store=vector_store)
logger.info("Storage context created successfully.")
# 使用 load_index_from_storage 加载索引 # 使用 load_index_from_storage 加载索引
index = load_index_from_storage(storage_context, service_context) index = load_index_from_storage(storage_context)
logger.info("VectorStoreIndex loaded successfully.") logger.info("VectorStoreIndex loaded successfully.")
# 设置索引的 ServiceContext # 设置索引的 ServiceContext

View File

@ -62,10 +62,6 @@ def load_indices_from_storage(
**kwargs: Additional keyword args to pass to the index constructors. **kwargs: Additional keyword args to pass to the index constructors.
""" """
# 使用 kwargs 获取 service_context
service_context = kwargs.get('service_context', None)
if index_ids is None: if index_ids is None:
logger.info("Loading all indices.") logger.info("Loading all indices.")
index_structs = storage_context.index_store.index_structs() index_structs = storage_context.index_store.index_structs()
@ -83,17 +79,11 @@ def load_indices_from_storage(
type_ = index_struct.get_type() type_ = index_struct.get_type()
index_cls = INDEX_STRUCT_TYPE_TO_INDEX_CLASS[type_] index_cls = INDEX_STRUCT_TYPE_TO_INDEX_CLASS[type_]
# index = index_cls(
# index_struct=index_struct, storage_context=storage_context, **kwargs
# )
# 显式传递service_context确保禁用OpenAI
index = index_cls( index = index_cls(
index_struct=index_struct, index_struct=index_struct, storage_context=storage_context, **kwargs
storage_context=storage_context, )
service_context=service_context, # 确保每个索引使用正确的service_context
**kwargs
)
indices.append(index) indices.append(index)
return indices return indices