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: WeaviateDB Support #61

Merged
merged 1 commit into from
Jul 12, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/beyondllm/vectordb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .chroma import ChromaVectorDb
from .pinecone import PineconeVectorDb
from .pinecone import PineconeVectorDb
from .weaviate import WeaviateVectorDb
93 changes: 93 additions & 0 deletions src/beyondllm/vectordb/weaviate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from beyondllm.vectordb.base import VectorDb, VectorDbConfig
from dataclasses import dataclass
import warnings
import subprocess
import sys

warnings.filterwarnings("ignore")

@dataclass
class WeaviateVectorDb:
"""
from beyondllm.vectordb import WeaviateVectorDb
vectordb = WeaviateVectorDb(
url="<cluster_url>",
index_name="MyClass",
api_key"<your api key>",
additional_headers=None
)
"""
url: str
index_name: str
api_key: str = None # Optional, for authenticated access
additional_headers: dict = None # Optional, for custom headers

def __post_init__(self):
try:
from llama_index.vector_stores.weaviate import WeaviateVectorStore
import weaviate
except ImportError:
user_agree = input("The feature you're trying to use requires additional libraries: llama_index.vector_stores.weaviate and weaviate-client. Would you like to install them now? [y/N]: ")
if user_agree.lower() == 'y':
subprocess.check_call([sys.executable, "-m", "pip", "install", "llama_index.vector_stores.weaviate"])
subprocess.check_call([sys.executable, "-m", "pip", "install", "weaviate-client"])
from llama_index.vector_stores.weaviate import WeaviateVectorStore
import weaviate
else:
raise ImportError("The required 'llama_index.vector_stores.weaviate' and 'weaviate-client' are not installed.")

auth_config = weaviate.auth.AuthApiKey(api_key=self.api_key) if self.api_key else None
self.client = weaviate.connect_to_wcs(
cluster_url=self.url,
auth_credentials=auth_config,
headers=self.additional_headers
)
self.load()

def load(self):
try:
from llama_index.vector_stores.weaviate import WeaviateVectorStore
except ImportError:
raise ImportError("WeaviateVectorStore library is not installed. Please install it with `pip install llama_index.vector_stores.weaviate`.")

try:
if not self.client.collections.exists(self.index_name):
self.client.collections.create(
self.index_name,
)

vector_store = WeaviateVectorStore(
weaviate_client=self.client,
index_name=self.index_name,
)
self.client = vector_store
except Exception as e:
raise Exception(f"Failed to load the Weaviate Vectorstore: {e}")

return self.client

def add(self, *args, **kwargs):
return self.client.add(*args, **kwargs)

def stores_text(self, *args, **kwargs):
return self.client.stores_text(*args, **kwargs)

def is_embedding_query(self, *args, **kwargs):
return self.client.is_embedding_query(*args, **kwargs)

def query(self, *args, **kwargs):
return self.client.query(*args, **kwargs)

def insert_document(self, document):
self.client.insert(document)

def delete_index(self):
self.client.delete_index()

def close_connection(self):
self.client.close()

def load_from_kwargs(self, kwargs):
embed_config = VectorDbConfig(**kwargs)
self.config = embed_config
self.load()