-
Notifications
You must be signed in to change notification settings - Fork 1
/
hybrid_pinecone_client.py
90 lines (83 loc) · 3.2 KB
/
hybrid_pinecone_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import requests
class HybridPinecone:
# initializes the HybridPinecone object
def __init__(self, api_key, environment):
# make environment, headers and project_id available across all the function within the class
self.environment = environment
self.headers = {'Api-Key': api_key}
# get project_id
res = requests.get(
f"https://controller.{self.environment}.pinecone.io/actions/whoami",
headers=self.headers
)
self.project_id = res.json()['project_name']
self.host = None
# creates an index in pinecone vector database
def create_index(self, index_name, dimension, metric, pod_type):
# index specification
params = {
'name': index_name,
'dimension': dimension,
'metric': metric,
'pod_type': pod_type
}
# sent a post request with the headers and parameters to pinecone database
res = requests.post(
f"https://controller.{self.environment}.pinecone.io/databases",
headers=self.headers,
json=params
)
# return the creation status
return res
# get the project_id for the index and update self.host variable
def connect_index(self, index_name):
# set the self.host variable
self.host = f"{index_name}-{self.project_id}.svc.{self.environment}.pinecone.io"
res = self.describe_index_stats()
# return index related information as json
return res
def describe_index(self, index_name):
# send a get request to pinecone database to get index description
res = requests.get(
f"https://controller.{self.environment}.pinecone.io/databases/{index_name}",
headers=self.headers
)
return res.json()
# returns description of the index
def describe_index_stats(self):
# send a get request to pinecone database to get index description
res = requests.get(
f"https://{self.host}/describe_index_stats",
headers=self.headers
)
# return the index description as json
return res.json()
# uploads the documents to pinecone database
def upsert(self, vectors):
# send a post request with vectors to pinecone database
res = requests.post(
f"https://{self.host}/hybrid/vectors/upsert",
headers=self.headers,
json={'vectors': vectors}
)
# return the http response status
return res
# searches pinecone database with the query
def query(self, query):
# sends a post request to hybrib vector index with the query dict
res = requests.post(
f"https://{self.host}/hybrid/query",
headers=self.headers,
json=query
)
# returns the result as json
return res.json()
# deletes an index in pinecone database
def delete_index(self, index_name):
# sends a delete request
res = requests.delete(
f"https://controller.{self.environment}.pinecone.io/databases/{index_name}",
headers=self.headers
)
# returns the http response status
return res