-
Notifications
You must be signed in to change notification settings - Fork 84
/
create-index-legacy.py
61 lines (43 loc) · 1.75 KB
/
create-index-legacy.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
import random
import string
import os
import pinecone
import time
def read_env_var(name):
value = os.environ.get(name)
if value is None:
raise Exception("Environment variable {} is not set".format(name))
return value
def random_embedding_values(dimension=2):
return [random.random() for _ in range(dimension)]
def random_string(length):
return "".join(random.choice(string.ascii_lowercase) for i in range(length))
def main():
api_key = read_env_var("PINECONE_API_KEY")
environment = read_env_var("PINECONE_ENVIRONMENT")
index_name = read_env_var("INDEX_NAME")
dimension = int(read_env_var("DIMENSION"))
metric = read_env_var("METRIC")
print(f"Beginning test with environment {environment} and index {index_name}")
pinecone.init(api_key=api_key, environment=environment)
if index_name in pinecone.list_indexes():
print(f"Index {index_name} already exists")
pinecone.delete_index(index_name)
pinecone.create_index(name=index_name, dimension=dimension, metric=metric)
print(f"Waiting for index {index_name} to be ready...")
time.sleep(60)
print("Done waiting.")
description = pinecone.describe_index(index_name)
print(f"Index description: {description}")
print(f"Beginning upsert of 1000 vectors to index {index_name}...")
index = pinecone.Index(name=index_name)
for _ in range(100):
vector = random_embedding_values(dimension)
vecs = [{"id": random_string(10), "values": vector} for i in range(10)]
index.upsert(vectors=[vecs])
print("Done upserting.")
print(f"Beginning query of index {index_name}...")
index.query(vector=random_embedding_values(dimension))
print("Done querying.")
if __name__ == "__main__":
main()