From 8bb61a3297231d1b353045d013b2d9de9b7c3b69 Mon Sep 17 00:00:00 2001 From: hailin Date: Sun, 11 May 2025 13:11:23 +0800 Subject: [PATCH] . --- llama_index/indices/loading.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/llama_index/indices/loading.py b/llama_index/indices/loading.py index 4c65946..70e43d1 100644 --- a/llama_index/indices/loading.py +++ b/llama_index/indices/loading.py @@ -6,12 +6,15 @@ from llama_index.indices.composability.graph import ComposableGraph from llama_index.indices.registry import INDEX_STRUCT_TYPE_TO_INDEX_CLASS from llama_index.storage.storage_context import StorageContext +from llama_index import ServiceContext + logger = logging.getLogger(__name__) def load_index_from_storage( storage_context: StorageContext, index_id: Optional[str] = None, + service_context: Optional[ServiceContext] = None, # 添加service_context参数 **kwargs: Any, ) -> BaseIndex: """Load index from storage context. @@ -30,7 +33,7 @@ def load_index_from_storage( else: index_ids = [index_id] - indices = load_indices_from_storage(storage_context, index_ids=index_ids, **kwargs) + indices = load_indices_from_storage(storage_context, index_ids=index_ids, service_context=service_context, **kwargs) if len(indices) == 0: raise ValueError( @@ -48,6 +51,7 @@ def load_index_from_storage( def load_indices_from_storage( storage_context: StorageContext, index_ids: Optional[Sequence[str]] = None, + service_context: Optional[ServiceContext] = None, # 添加service_context参数 **kwargs: Any, ) -> List[BaseIndex]: """Load multiple indices from storage context. @@ -75,9 +79,18 @@ def load_indices_from_storage( for index_struct in index_structs: 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, **kwargs - ) + index_struct=index_struct, + storage_context=storage_context, + service_context=service_context, # 确保每个索引使用正确的service_context + **kwargs + ) indices.append(index) return indices