From 5f97f8831112bd6a59b7f5b20e64786ebfa7539d Mon Sep 17 00:00:00 2001 From: hailin Date: Sun, 11 May 2025 13:53:09 +0800 Subject: [PATCH] . --- app/api/search.py | 16 +++++++++++----- llama_index/indices/loading.py | 18 ++++-------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/app/api/search.py b/app/api/search.py index 91f31d7..deb6ba5 100644 --- a/app/api/search.py +++ b/app/api/search.py @@ -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 diff --git a/llama_index/indices/loading.py b/llama_index/indices/loading.py index d129191..bb59c44 100644 --- a/llama_index/indices/loading.py +++ b/llama_index/indices/loading.py @@ -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