This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor AutoVectorStore and moved some methods to db_utils
- Loading branch information
1 parent
e731320
commit 79a4d59
Showing
2 changed files
with
73 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,39 @@ | ||
from typing import Any | ||
from llama_index.vector_stores.types import BasePydanticVectorStore | ||
|
||
from autollm.vectorstores.base import BaseVS | ||
|
||
# Mapping of vector store types to their respective class names. | ||
# This is used for dynamically importing the correct VectorStore class based on the type. | ||
VECTOR_STORE_TYPE_TO_VECTOR_CLASS_NAME = { | ||
'pinecone': 'PineconeVS', | ||
'qdrant': 'QdrantVS', | ||
'in_memory': 'InMemoryVS' | ||
} | ||
|
||
|
||
def import_vector_store_class(vector_store_type: str, class_name: str): | ||
def import_vector_store_class(vector_store_class_name: str): | ||
""" | ||
Imports a predefined vector store class by class name. | ||
Args: | ||
vector_store_type: str | ||
Type of vector store ("pinecone", "qdrant", "inmemory") | ||
class_name: str | ||
Name of the vector store class (example: "PineconeVS") | ||
Returns: | ||
The imported VectorStore class. | ||
""" | ||
module = __import__(f'autollm.vectorstores.{vector_store_type}', fromlist=[class_name]) | ||
class_ = getattr(module, class_name) | ||
module = __import__("llama_index.vector_stores", fromlist=[vector_store_class_name]) | ||
class_ = getattr(module, vector_store_class_name) | ||
return class_ | ||
|
||
|
||
class AutoVectorStore: | ||
"""A class for dynamically initializing a Vector Store based on the type and additional parameters.""" | ||
"""A class for dynamically initializing a Vector Store based on its class name and additional | ||
parameters. | ||
""" | ||
|
||
@staticmethod | ||
def from_defaults(vector_store_type: str, **kwargs: Any) -> BaseVS: | ||
def from_defaults(vector_store_class_name: str, *args, **kwargs) -> BasePydanticVectorStore: | ||
""" | ||
Initializes a Vector Store based on the type and additional parameters. | ||
Initializes a Vector Store based on its class name and additional parameters. | ||
Args: | ||
vector_store_type: str | ||
Type of vector store ("pinecone", "qdrant") | ||
vector_store_class_name: str | ||
The class name of the vector store (e.g., 'PineconeVectorStore') | ||
*args: Additional positional arguments for initializing the vector store | ||
**kwargs: Additional parameters for initializing the vector store | ||
Returns: | ||
An instance of the appropriate vector store class. | ||
Raises: | ||
ValueError: If the given {vector_store_type} is not supported | ||
""" | ||
try: | ||
vector_store_class_name = VECTOR_STORE_TYPE_TO_VECTOR_CLASS_NAME[vector_store_type] | ||
except KeyError: | ||
raise ValueError( | ||
f'Invalid store_type: {vector_store_type}. Supported types are {list(VECTOR_STORE_TYPE_TO_VECTOR_CLASS_NAME.keys())}' | ||
) | ||
|
||
VectorStore = import_vector_store_class(vector_store_type, vector_store_class_name) | ||
VectorStoreClass = import_vector_store_class(vector_store_class_name) | ||
|
||
return VectorStore(**kwargs) | ||
return VectorStoreClass(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters