-
Notifications
You must be signed in to change notification settings - Fork 2
/
better_vectara.py
133 lines (108 loc) · 3.9 KB
/
better_vectara.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
123
124
125
126
127
128
129
130
131
132
133
# Features that Forrest's vectara Python SDK does not provide
# TO be added to the Vectara Python SDK shortly
import json
from typing import Any
import requests
from vectara import Vectara
class BetterVectara(Vectara):
def read_corpus(
self,
corpusIds: list[int],
read_basic_info: bool = True,
read_size: bool = False,
read_api_keys: bool = False,
read_custom_dimensions: bool = False,
read_filter_attributes: bool = False,
):
url = f"{self.base_url}/v1/read-corpus"
headers = {"customer-id": self.customer_id}
if self.api_key:
headers["x-api-key"] = self.api_key
else:
headers["Authorization"] = f"Bearer {self.jwt_token}"
payload = {
"corpusId": corpusIds,
"readBasicInfo": read_basic_info,
"readSize": read_size,
"readApiKeys": read_api_keys,
"readCustomDimensions": read_custom_dimensions,
"readFilterAttributes": read_filter_attributes,
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
return response.json()
def create_corpus_with_metadata_filters(
self,
corpus_name: str,
corpus_description: str = "",
metadata_filters: list[dict] = [],
):
url = f"{self.base_url}/v1/create-corpus"
filter_attributes = []
for metadata_filter in metadata_filters:
filter_attributes.append(
{
"name": metadata_filter["name"],
"description": "",
"indexed": metadata_filter["indexed"],
"type": metadata_filter["type"].value,
"level": metadata_filter["level"].value,
}
)
payload = json.dumps(
{
"corpus": {
"name": corpus_name,
"description": corpus_description,
"filterAttributes": filter_attributes,
}
}
)
headers = {"customer-id": self.customer_id}
if self.api_key:
headers["x-api-key"] = self.api_key
else:
headers["Authorization"] = f"Bearer {self.jwt_token}"
response = requests.post(url, headers=headers, data=payload)
if response.status_code == 200:
return response.json()["corpusId"]
else:
raise Exception(f"Failed to create corpus: {response.text}")
def list_documents_with_filter(
self,
corpus_id: int,
numResults: int = 10,
pageKey: str | None = None,
metadataFilter: str | None = None,
) -> dict:
url = f"{self.base_url}/v1/list-documents"
headers = {}
if self.api_key:
headers["x-api-key"] = self.api_key
else:
headers["Authorization"] = f"Bearer {self.jwt_token}"
payload: dict[str, Any] = {"corpusId": corpus_id, "numResults": numResults}
if pageKey:
payload["pageKey"] = pageKey
if metadataFilter:
payload["metadataFilter"] = metadataFilter
response = requests.post(url, headers=headers, data=json.dumps(payload))
return response.json()
def list_all_documents(self, corpus_id: int, metadataFilter: str | None = None):
pageKey: str | None = None
while True:
page = self.list_documents_with_filter(
corpus_id,
numResults=1000,
pageKey=pageKey,
metadataFilter=metadataFilter,
)
if page["document"] == []:
yield from page["document"]
break
else:
yield from page["document"]
pageKey = page["nextPageKey"]
if pageKey:
continue
else:
break