Source code for tests.system.weaviate.example_weaviate_using_hook
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsimportpendulumfromweaviate.classes.configimportDataType,Propertyfromweaviate.collections.classes.configimportConfigurefromairflow.decoratorsimportdag,task,teardown
[docs]defexample_weaviate_dag_using_hook():"""Example Weaviate DAG demonstrating usage of the hook."""@task()defcreate_collection_with_vectorizer():""" Example task to create collection with OpenAI Vectorizer responsible for vectorining data using Weaviate cluster. """fromairflow.providers.weaviate.hooks.weaviateimportWeaviateHookweaviate_hook=WeaviateHook()weaviate_hook.create_collection(COLLECTION_NAME,description="Information from a Jeopardy! question",properties=[Property(name="question",description="The question",data_type=DataType.TEXT),Property(name="answer",description="The answer",data_type=DataType.TEXT),Property(name="category",description="The category",data_type=DataType.TEXT),],vectorizer_config=Configure.Vectorizer.text2vec_openai(),)@task()defcreate_collection_without_vectorizer():""" Example task to create collection without any Vectorizer. You're expected to provide custom vectors for your data. """fromairflow.providers.weaviate.hooks.weaviateimportWeaviateHookweaviate_hook=WeaviateHook()# collection definition object. Weaviate's autoschema feature will infer properties when importing.weaviate_hook.create_collection("QuestionWithoutVectorizerUsingHook",vectorizer_config=None,)@task(trigger_rule="all_done")defstore_data_without_vectors_in_xcom():importjsonfrompathlibimportPathdata=json.load(Path("jeopardy_data_without_vectors.json").open())returndata@task(trigger_rule="all_done")defstore_data_with_vectors_in_xcom():importjsonfrompathlibimportPathdata=json.load(Path("jeopardy_data_with_vectors.json").open())returndata@task(trigger_rule="all_done")defbatch_data_without_vectors(data:list):fromairflow.providers.weaviate.hooks.weaviateimportWeaviateHookweaviate_hook=WeaviateHook()weaviate_hook.batch_data(COLLECTION_NAME,data)@task(trigger_rule="all_done")defbatch_data_with_vectors(data:list):fromairflow.providers.weaviate.hooks.weaviateimportWeaviateHookweaviate_hook=WeaviateHook()weaviate_hook.batch_data("QuestionWithoutVectorizerUsingHook",data)@teardown@taskdefdelete_weaviate_collection_vector():""" Example task to delete a weaviate collection """fromairflow.providers.weaviate.hooks.weaviateimportWeaviateHookweaviate_hook=WeaviateHook()# collection definition object. Weaviate's autoschema feature will infer properties when importing.weaviate_hook.delete_collections([COLLECTION_NAME])@teardown@taskdefdelete_weaviate_collection_without_vector():""" Example task to delete a weaviate collection """fromairflow.providers.weaviate.hooks.weaviateimportWeaviateHookweaviate_hook=WeaviateHook()# collection definition object. Weaviate's autoschema feature will infer properties when importing.weaviate_hook.delete_collections(["QuestionWithoutVectorizerUsingHook"])data_with_vectors=store_data_with_vectors_in_xcom()(create_collection_without_vectorizer()>>batch_data_with_vectors(data_with_vectors["return_value"])>>delete_weaviate_collection_vector())data_without_vectors=store_data_without_vectors_in_xcom()(create_collection_with_vectorizer()>>batch_data_without_vectors(data_without_vectors["return_value"])>>delete_weaviate_collection_without_vector())
example_weaviate_dag_using_hook()fromtests_common.test_utils.system_testsimportget_test_run# noqa: E402# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)