"""Clear Weaviate index."""
import logging
import os

import weaviate
from langchain.embeddings import OpenAIEmbeddings
from langchain.indexes import SQLRecordManager, index
from langchain.vectorstores import Weaviate

logger = logging.getLogger(__name__)

WEAVIATE_URL = os.environ["WEAVIATE_URL"]
WEAVIATE_API_KEY = os.environ["WEAVIATE_API_KEY"]
RECORD_MANAGER_DB_URL = os.environ["RECORD_MANAGER_DB_URL"]
WEAVIATE_DOCS_INDEX_NAME = "LangChain_Combined_Docs_OpenAI_text_embedding_3_small"


def clear():
    client = weaviate.Client(
        url=WEAVIATE_URL,
        auth_client_secret=weaviate.AuthApiKey(api_key=WEAVIATE_API_KEY),
    )
    vectorstore = Weaviate(
        client=client,
        index_name=WEAVIATE_DOCS_INDEX_NAME,
        text_key="text",
        embedding=OpenAIEmbeddings(),
        by_text=False,
        attributes=["source", "title"],
    )

    record_manager = SQLRecordManager(
        f"weaviate/{WEAVIATE_DOCS_INDEX_NAME}", db_url=RECORD_MANAGER_DB_URL
    )
    record_manager.create_schema()

    indexing_stats = index(
        [],
        record_manager,
        vectorstore,
        cleanup="full",
        source_id_key="source",
    )

    logger.info("Indexing stats: ", indexing_stats)
    logger.info(
        "LangChain now has this many vectors: ",
        client.query.aggregate(WEAVIATE_DOCS_INDEX_NAME).with_meta_count().do(),
    )


if __name__ == "__main__":
    clear()

langchain.embeddings.OpenAIEmbeddings langchain.indexes.index langchain.indexes.SQLRecordManager
[((228, 255), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (245, 255), False, 'import logging\n'), ((893, 984), 'langchain.indexes.SQLRecordManager', 'SQLRecordManager', (['f"""weaviate/{WEAVIATE_DOCS_INDEX_NAME}"""'], {'db_url': 'RECORD_MANAGER_DB_URL'}), "(f'weaviate/{WEAVIATE_DOCS_INDEX_NAME}', db_url=\n    RECORD_MANAGER_DB_URL)\n", (909, 984), False, 'from langchain.indexes import SQLRecordManager, index\n'), ((1051, 1129), 'langchain.indexes.index', 'index', (['[]', 'record_manager', 'vectorstore'], {'cleanup': '"""full"""', 'source_id_key': '"""source"""'}), "([], record_manager, vectorstore, cleanup='full', source_id_key='source')\n", (1056, 1129), False, 'from langchain.indexes import SQLRecordManager, index\n'), ((590, 635), 'weaviate.AuthApiKey', 'weaviate.AuthApiKey', ([], {'api_key': 'WEAVIATE_API_KEY'}), '(api_key=WEAVIATE_API_KEY)\n', (609, 635), False, 'import weaviate\n'), ((782, 800), 'langchain.embeddings.OpenAIEmbeddings', 'OpenAIEmbeddings', ([], {}), '()\n', (798, 800), False, 'from langchain.embeddings import OpenAIEmbeddings\n')]