Source code for tests.system.weaviate.example_weaviate_cohere
# 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__importannotationsimportpendulumfromairflow.decoratorsimportdag,setup,task,teardownfromairflow.providers.cohere.operators.embeddingimportCohereEmbeddingOperatorfromairflow.providers.weaviate.operators.weaviateimportWeaviateIngestOperator
[docs]defexample_weaviate_cohere():""" Example DAG which creates embeddings using CohereEmbeddingOperator and the uses WeaviateIngestOperator to insert embeddings to Weaviate . """@setup@taskdefcreate_weaviate_collection():""" 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(name=COLLECTION_NAME,vectorizer_config=None)@setup@taskdefget_data_to_embed():importjsonfrompathlibimportPathdata=json.load(Path("jeopardy_data_without_vectors.json").open())return[[item["Question"]]foritemindata]data_to_embed=get_data_to_embed()embed_data=CohereEmbeddingOperator.partial(task_id="embedding_using_xcom_data",).expand(input_text=data_to_embed["return_value"])@taskdefupdate_vector_data_in_json(**kwargs):importjsonfrompathlibimportPathti=kwargs["ti"]data=json.load(Path("jeopardy_data_without_vectors.json").open())embedded_data=ti.xcom_pull(task_ids="embedding_using_xcom_data",key="return_value")fori,vectorinenumerate(embedded_data):data[i]["Vector"]=vector[0]returndataupdate_vector_data_in_json=update_vector_data_in_json()perform_ingestion=WeaviateIngestOperator(task_id="perform_ingestion",conn_id="weaviate_default",collection_name=COLLECTION_NAME,input_data=update_vector_data_in_json["return_value"],)embed_query=CohereEmbeddingOperator(task_id="embed_query",input_text=["biology"],)@teardown@taskdefdelete_weaviate_collections():""" 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])(create_weaviate_collection()>>embed_data>>update_vector_data_in_json>>perform_ingestion>>embed_query>>delete_weaviate_collections())
example_weaviate_cohere()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)