Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Created October 31, 2022 06:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescalam/37d799cf824e16f0a6337b8a3e25fd34 to your computer and use it in GitHub Desktop.
Save jamescalam/37d799cf824e16f0a6337b8a3e25fd34 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment