This commit is contained in:
parent
507a87b3d0
commit
5f97f88311
|
|
@ -4,6 +4,10 @@ from app.core.embedding import embedder # 使用已加载的本地嵌入模型
|
|||
from app.core.config import settings
|
||||
from llama_index.vector_stores.faiss import FaissVectorStore
|
||||
from llama_index import VectorStoreIndex, ServiceContext, StorageContext, load_index_from_storage
|
||||
|
||||
from llama_index import set_global_service_context, ServiceContext
|
||||
|
||||
|
||||
import os
|
||||
import logging
|
||||
import faiss # 引入faiss
|
||||
|
|
@ -44,16 +48,18 @@ def search_docs(request: QueryRequest, user_id: str = Query(..., description="
|
|||
vector_store = FaissVectorStore(faiss_index=faiss_index)
|
||||
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
|
||||
service_context = ServiceContext.from_defaults(embed_model=embedder, llm=None, llm_predictor=None)
|
||||
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 加载索引
|
||||
index = load_index_from_storage(storage_context, service_context)
|
||||
index = load_index_from_storage(storage_context)
|
||||
logger.info("VectorStoreIndex loaded successfully.")
|
||||
|
||||
# 设置索引的 ServiceContext
|
||||
|
|
|
|||
|
|
@ -62,10 +62,6 @@ def load_indices_from_storage(
|
|||
**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:
|
||||
logger.info("Loading all indices.")
|
||||
index_structs = storage_context.index_store.index_structs()
|
||||
|
|
@ -83,17 +79,11 @@ def load_indices_from_storage(
|
|||
type_ = index_struct.get_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_struct=index_struct,
|
||||
storage_context=storage_context,
|
||||
service_context=service_context, # 确保每个索引使用正确的service_context
|
||||
**kwargs
|
||||
index_struct=index_struct, storage_context=storage_context, **kwargs
|
||||
)
|
||||
|
||||
|
||||
indices.append(index)
|
||||
return indices
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue