Skip to content

weaviatedb

langroid/vector_store/weaviatedb.py

VectorDistances

Fallback class when weaviate is not installed, to avoid import errors.

WeaviateDB(config=WeaviateDBConfig())

Bases: VectorStore

Source code in langroid/vector_store/weaviatedb.py
def __init__(self, config: WeaviateDBConfig = WeaviateDBConfig()):
    super().__init__(config)
    try:
        import weaviate
        from weaviate.classes.init import Auth
    except ImportError:
        raise LangroidImportError("weaviate", "weaviate")

    self.config: WeaviateDBConfig = config
    load_dotenv()
    if self.config.docker:
        self.client = weaviate.connect_to_local(
            host=self.config.host,
            port=self.config.port,
        )
        self.config.cloud = False
    elif self.config.cloud:
        key = os.getenv("WEAVIATE_API_KEY")
        url = os.getenv("WEAVIATE_API_URL")
        if url is None or key is None:
            raise ValueError(
                """WEAVIATE_API_KEY, WEAVIATE_API_URL env variables must be set to 
                use WeaviateDB in cloud mode. Please set these values
                in your .env file.
                """
            )
        self.client = weaviate.connect_to_weaviate_cloud(
            cluster_url=url,
            auth_credentials=Auth.api_key(key),
        )
    else:
        self.client = weaviate.connect_to_embedded(
            version="latest", persistence_data_path=self.config.storage_path
        )

    if config.collection_name is not None:
        WeaviateDB.validate_and_format_collection_name(config.collection_name)

validate_and_format_collection_name(name) staticmethod

Formats the collection name to comply with Weaviate's naming rules: - Name must start with a capital letter. - Name can only contain letters, numbers, and underscores. - Replaces invalid characters with underscores.

Source code in langroid/vector_store/weaviatedb.py
@staticmethod
def validate_and_format_collection_name(name: str) -> str:
    """
    Formats the collection name to comply with Weaviate's naming rules:
    - Name must start with a capital letter.
    - Name can only contain letters, numbers, and underscores.
    - Replaces invalid characters with underscores.
    """
    if not name:
        raise ValueError("Collection name cannot be empty.")

    formatted_name = re.sub(r"[^a-zA-Z0-9_]", "_", name)

    # Ensure the first letter is capitalized
    if not formatted_name[0].isupper():
        formatted_name = formatted_name.capitalize()

    # Check if the name now meets the criteria
    if not re.match(r"^[A-Z][A-Za-z0-9_]*$", formatted_name):
        raise ValueError(
            f"Invalid collection name '{name}'."
            " Names must start with a capital letter "
            "and contain only letters, numbers, and underscores."
        )

    if formatted_name != name:
        logger.warning(
            f"Collection name '{name}' was reformatted to '{formatted_name}' "
            "to comply with Weaviate's rules."
        )

    return formatted_name