-
Notifications
You must be signed in to change notification settings - Fork 14.3k
/
example_weaviate_cohere.py
122 lines (99 loc) · 3.95 KB
/
example_weaviate_cohere.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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__ import annotations
import pendulum
from airflow.decorators import dag, setup, task, teardown
from airflow.providers.cohere.operators.embedding import CohereEmbeddingOperator
from airflow.providers.weaviate.operators.weaviate import WeaviateIngestOperator
@dag(
schedule=None,
start_date=pendulum.datetime(2021, 1, 1, tz="UTC"),
catchup=False,
tags=["example", "weaviate", "cohere"],
)
def example_weaviate_cohere():
"""
Example DAG which creates embeddings using CohereEmbeddingOperator and the uses WeaviateIngestOperator to insert embeddings to Weaviate .
"""
@setup
@task
def create_weaviate_class():
"""
Example task to create class without any Vectorizer. You're expected to provide custom vectors for your data.
"""
from airflow.providers.weaviate.hooks.weaviate import WeaviateHook
weaviate_hook = WeaviateHook()
# Class definition object. Weaviate's autoschema feature will infer properties when importing.
class_obj = {
"class": "Weaviate_example_class",
"vectorizer": "none",
}
weaviate_hook.create_class(class_obj)
@setup
@task
def get_data_to_embed():
import json
from pathlib import Path
data = json.load(Path("jeopardy_data_without_vectors.json").open())
return [[item["Question"]] for item in data]
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"])
@task
def update_vector_data_in_json(**kwargs):
import json
from pathlib import Path
ti = 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")
for i, vector in enumerate(embedded_data):
data[i]["Vector"] = vector[0]
return data
update_vector_data_in_json = update_vector_data_in_json()
perform_ingestion = WeaviateIngestOperator(
task_id="perform_ingestion",
conn_id="weaviate_default",
class_name="Weaviate_example_class",
input_json=update_vector_data_in_json["return_value"],
)
embed_query = CohereEmbeddingOperator(
task_id="embed_query",
input_text=["biology"],
)
@teardown
@task
def delete_weaviate_class():
"""
Example task to delete a weaviate class
"""
from airflow.providers.weaviate.hooks.weaviate import WeaviateHook
weaviate_hook = WeaviateHook()
# Class definition object. Weaviate's autoschema feature will infer properties when importing.
weaviate_hook.delete_classes(["Weaviate_example_class"])
(
create_weaviate_class()
>> embed_data
>> update_vector_data_in_json
>> perform_ingestion
>> embed_query
>> delete_weaviate_class()
)
example_weaviate_cohere()
from tests.system.utils import get_test_run # noqa: E402
# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)
test_run = get_test_run(dag)