Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for user-defined storage_context in llamaindex_knowledge … #468

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
}
},
"store_and_index": {
"data_parse": {
"transformations": [
{
"create_object": true,
Expand All @@ -34,7 +34,22 @@
]
}
}
]
],
"store_and_index": {
"storage_context": {
"vector_store": {
"create_object": true,
"module": "llama_index.vector_stores.elasticsearch",
"class": "ElasticsearchStore",
"init_args": {
"index_name": "agentscope_code_rag",
"es_url": "http://localhost:9200",
"es_user": "elastic",
"es_password": ""
}
}
}
}
},
{
"knowledge_id": "agentscope_api_rag",
Expand All @@ -57,7 +72,22 @@
}
}
}
]
],
"store_and_index": {
"storage_context": {
"vector_store": {
"create_object": true,
"module": "llama_index.vector_stores.elasticsearch",
"class": "ElasticsearchStore",
"init_args": {
"index_name": "agentscope_api_rag",
"es_url": "http://localhost:9200",
"es_user": "elastic",
"es_password": ""
}
}
}
}
},
{
"knowledge_id": "agentscope_global_rag",
Expand Down Expand Up @@ -95,7 +125,7 @@
}
}
},
"store_and_index": {
"data_parse": {
"transformations": [
{
"create_object": true,
Expand All @@ -109,6 +139,21 @@
]
}
}
]
],
"store_and_index": {
"storage_context": {
"vector_store": {
"create_object": true,
"module": "llama_index.vector_stores.elasticsearch",
"class": "ElasticsearchStore",
"init_args": {
"index_name": "agentscope_global_rag",
"es_url": "http://localhost:9200",
"es_user": "elastic",
"es_password": ""
}
}
}
}
}
]
51 changes: 48 additions & 3 deletions src/agentscope/rag/llama_index_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,21 @@ def _load_index(self) -> None:
Load the persisted index from persist_dir.
"""
# load the storage_context
storage_context = StorageContext.from_defaults(
persist_dir=self.persist_dir,
# Inject the persist_dir setting
self.knowledge_config.get("store_and_index", {}).get(
"storage_context",
{},
).update(
{
"persist_dir": self.persist_dir,
},
)
storage_context = self._set_store(self.knowledge_config)
if not storage_context:
# if storage_context is not set, use the default
storage_context = StorageContext.from_defaults(
persist_dir=self.persist_dir,
)
# construct index from
self.index = load_index_from_storage(
storage_context=storage_context,
Expand Down Expand Up @@ -287,10 +299,14 @@ def _data_to_index(self) -> None:
transformations=transformations,
)
nodes = nodes + nodes_docs
# set store
storage_context = self._set_store(self.knowledge_config)
# convert nodes to index
# if storage_context is None, use the default
self.index = VectorStoreIndex(
nodes=nodes,
embed_model=self.emb_model,
storage_context=storage_context,
)
logger.info("index calculation completed.")
# persist the calculated index
Expand Down Expand Up @@ -401,7 +417,16 @@ def _set_transformations(self, config: dict) -> Any:
Args:
config (dict): a dictionary containing configurations.
"""
if "store_and_index" in config:
if "data_parse" in config:
temp = self._prepare_args_from_config(
config=config.get("data_parse", {}),
)
transformations = temp.get("transformations")
elif "store_and_index" in config:
logger.warning(
"The old configuration structure is deprecated, "
"please use data_parse instead of store_and_index.",
)
temp = self._prepare_args_from_config(
config=config.get("store_and_index", {}),
)
Expand All @@ -427,6 +452,26 @@ def _set_transformations(self, config: dict) -> Any:
transformations = {"transformations": transformations}
return transformations

def _set_store(self, config: dict) -> Any:
"""
Set the store as needed, or just use the default setting.

Args:
config (dict): a dictionary containing configurations.
"""
if "store_and_index" in config:
temp = self._prepare_args_from_config(
config=config.get("store_and_index", {}),
)
context_config = temp.get("storage_context")
else:
return None

# Create the storage context
storage_context = StorageContext.from_defaults(**context_config)
logger.info("storage_context is ready.")
return storage_context

def _get_retriever(
self,
similarity_top_k: int = None,
Expand Down