This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
75 lines (55 loc) · 1.65 KB
/
client.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
"""
Weaviate client module
"""
import weaviate
from txtai.pipeline import Pipeline
class Weaviate(Pipeline):
"""
Weaviate pipeline client. Supports indexing and searching content with Weaviate.
"""
def __init__(self, url="http://localhost:8080"):
"""
Create a new client.
Args:
url: Weaviate service url
"""
self.client = weaviate.Client(url)
# Delete autogenerated schema
self.client.schema.delete_all()
def __call__(self, inputs, action="index"):
"""
Executes an action with Weaviate.
Args:
inputs: data inputs
action: action to perform - index or search
Returns:
results
"""
if action == "index":
return [self.index(data, vector) for data, vector in inputs]
# Default to search action
return [self.search(vector) for vector in inputs]
def index(self, data, vector):
"""
Indexes data-vector pair in Weaviate.
Args:
data: record metadata
vector: record embeddings
Returns:
uuid from Weaviate
"""
return self.client.data_object.create(
{"content": data},
"Post",
vector = vector,
)
def search(self, vector):
"""
Runs a search using input vector.
Args:
vector: input vector
Returns:
search results
"""
nearvector = {"vector": vector}
return self.client.query.get("Post", ["content", "_additional {certainty}"]).with_near_vector(nearvector).with_limit(1).do()