-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
197 lines (152 loc) · 5.97 KB
/
utils.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# pylint: disable=locally-disabled, multiple-statements, fixme, line-too-long, missing-module-docstring, missing-function-docstring
import json
import os
import yaml
import argparse
import getpass
from llama_index.core.node_parser import SentenceSplitter
from llama_index.readers.string_iterable import StringIterableReader
# from llama_index.node_parser import SimpleNodeParser
from llama_index.llms.openai import OpenAI
# from llama_index.llms import OpenAI
from llama_index.core import (
StorageContext,
ServiceContext,
)
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Document
from llama_index.vector_stores.deeplake import DeepLakeVectorStore
from global_variable import (
PILLS_JSON_FILE_CLEANED,
PILLS_JSON_FILE_CLEANED_2,
VECTOR_STORE_PATH_DESCRIPTION,
)
parser = argparse.ArgumentParser()
parser.add_argument("--credentials", action="store_true")
args = parser.parse_args()
if args.credentials:
os.environ["ACTIVELOOP_TOKEN"] = getpass.getpass(
"Copy and paste your ActiveLoop token: "
)
os.environ["OPENAI_API_KEY"] = getpass.getpass(
"Copy and paste your OpenAI API key: "
)
else:
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env.
os.environ["ACTIVELOOP_TOKEN"] = os.getenv("ACTIVELOOP_TOKEN")
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
print("credentials entered")
def create_storage_and_service_contexts(
vector_store_path: str,
):
vector_store = load_vector_store(
vector_store_path, overwrite=True, token=os.environ["ACTIVELOOP_TOKEN"]
)
loader = StringIterableReader()
chunks = create_chunks(get_pills_info())
documents = loader.load_data(texts=chunks)
node_parser = SentenceSplitter.from_defaults(separator="\n")
nodes = node_parser.get_nodes_from_documents(documents)
print(f"Number of nodes: {len(nodes)}")
print(f"Number of docs: {len(documents)}")
# To ensure same id's per run, we manually set them.
for idx, node in enumerate(nodes):
node.id_ = f"node_{idx}"
llm = OpenAI(model="gpt-4")
# text_splitter = SentenceSplitter(separator="\n", chunk_size=1024, chunk_overlap=20)
service_context = ServiceContext.from_defaults(llm=llm)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex(
nodes=nodes,
storage_context=storage_context,
service_context=service_context,
)
return service_context, storage_context, nodes, llm, index
def create_chunks(pills_info: dict):
chunks = [
pills_info[el]["description"] if pills_info[el]["description"] else "."
for el in pills_info
]
return chunks
def load_vector_store(
vector_store_path: str,
overwrite: bool = False,
token: str = None,
runtime: dict = {"tensor_db": True},
):
vector_store = DeepLakeVectorStore(
dataset_path=vector_store_path,
overwrite=overwrite,
runtime=runtime,
read_only=True,
token=token,
)
return vector_store
def get_pills_info():
with open(PILLS_JSON_FILE_CLEANED, "r", encoding="utf-8") as file:
pills_info = json.load(file)
return pills_info
def get_pills_info2():
with open(PILLS_JSON_FILE_CLEANED_2, "r", encoding="utf-8") as file:
pills_info = json.load(file)
return pills_info
def get_index_and_nodes_from_activeloop(vector_store_path: str):
vector_store = load_vector_store(
vector_store_path=vector_store_path, token=os.environ["ACTIVELOOP_TOKEN"]
)
chunks = []
for el in vector_store.client:
chunks.append(str(el.text.data()["value"]))
loader = StringIterableReader()
documents = loader.load_data(texts=chunks)
node_parser = SentenceSplitter.from_defaults(separator="\n")
nodes = node_parser.get_nodes_from_documents(documents)
# To ensure same id's per run, we manually set them.
for idx, node in enumerate(nodes):
node.id_ = f"node_{idx}"
llm = OpenAI(model="gpt-4")
service_context = ServiceContext.from_defaults(llm=llm)
index = VectorStoreIndex(nodes=nodes)
return index, nodes, service_context
def get_index_and_nodes_after_visual_similarity(filenames: list):
"""
Takes the filenames of the similar images and after having the id of the similar images return the index and nodes (based on description similarity)
"""
vector_store = load_vector_store(
vector_store_path=VECTOR_STORE_PATH_DESCRIPTION,
token=os.environ["ACTIVELOOP_TOKEN"],
)
conditions = " or ".join(f"filename == '{name}'" for name in filenames)
tql_query = f"select * where {conditions}"
# filtered_elements = vector_store.vectorstore.search(query=tql_query)
filtered_elements = vector_store._vectorstore.search(query=tql_query)
chunks = []
for el in filtered_elements["text"]:
chunks.append(el)
loader = StringIterableReader()
documents = loader.load_data(texts=chunks)
node_parser = SentenceSplitter.from_defaults(separator="\n")
nodes = node_parser.get_nodes_from_documents(documents)
# To ensure same id's per run, we manually set them.
for idx, node in enumerate(nodes):
node.id_ = f"node_{idx}"
llm = OpenAI(model="gpt-4")
service_context = ServiceContext.from_defaults(llm=llm)
index = VectorStoreIndex(nodes=nodes)
return index, nodes, service_context, filtered_elements
def keep_best_k_unique_nodes(reranked_nodes_bm25, reranked_nodes_vector, k=4):
"""
Keeps the best k unique nodes from the two lists of nodes
"""
all_nodes = []
node_ids = set()
for n in reranked_nodes_bm25 + reranked_nodes_vector:
if n.node.node_id not in node_ids:
all_nodes.append(n)
node_ids.add(n.node.node_id)
if len(all_nodes) > k:
return all_nodes[:k]
return all_nodes