-
Notifications
You must be signed in to change notification settings - Fork 0
/
fave.py
79 lines (67 loc) · 2.45 KB
/
fave.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
from typing import List
import fave_api
from fave_api.rest import ApiException
from fave_api.models import Index, Document
class FaVe():
def __init__(
self,
url: str = "http://localhost:1234",
) -> None:
self._url = url
configuration = fave_api.Configuration()
configuration.host = url+"/v1"
self._client = fave_api.DefaultApi(fave_api.ApiClient(configuration))
def create_collection(
self, collection: str, indexes: list[Index]
) -> None:
colection = fave_api.Collection()
colection.name = collection
colection.indexes = indexes
try:
self._client.fave_create_collection(colection)
except ApiException as e:
raise Exception("%s\n" % e)
def add_documents(
self, collection: str, documents: list[Document], properties_to_vectorize: list[str]
) -> None:
rqst = fave_api.AddDocumentsRequest()
rqst.name = collection
rqst.documents = documents
rqst.properties_to_vectorize = properties_to_vectorize
try:
self._client.fave_add_documents(rqst)
except ApiException as e:
raise Exception("%s\n" % e)
def get_document(
self, collection, property, value: str
) -> Document:
try:
return self._client.fave_get_documents(property, value, collection)
except ApiException as e:
raise Exception("%s\n" % e)
def get_nearest_documents(
self, collection, query: str, limit: int, distance: float
) -> List[Document]:
rqst = fave_api.NearestDocumentsRequest()
rqst.name = collection
rqst.text = query
rqst.limit = limit
rqst.distance = distance
try:
resp = self._client.fave_get_nearest_documents(rqst)
except ApiException as e:
raise Exception("%s\n" % e)
return resp.documents
def get_nearest_documents_by_vector(
self, collection: str, vector: list[float],limit: int, distance: float
) -> List[Document]:
rqst = fave_api.NearestDocumentsByVectorRequest()
rqst.name = collection
rqst.vector = vector
rqst.limit = limit
rqst.distance = distance
try:
resp = self._client.fave_get_nearest_documents_by_vector(rqst)
except ApiException as e:
raise Exception("%s\n" % e)
return resp.documents