File size: 11,006 Bytes
7a8b33f |
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
import faiss
import shutil
from beartype import beartype
import numpy as np
import json
import argparse
from zsvision.zs_utils import BlockTimer
import tiktoken
from pathlib import Path
from langchain.text_splitter import RecursiveCharacterTextSplitter
from pipeline_paths import PIPELINE_PATHS
from llm_api_utils import (
init_openai_with_api_key,
EMBEDDING_DIMENSIONS,
PRICE_PER_1K_TOKENS,
)
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain.docstore.in_memory import InMemoryDocstore
class EmbedResults:
def __init__(
self,
embedding_model="ada",
limit=0,
refresh=False,
refresh_faiss_db=False,
text_embedding_chunk_size=500,
filter_str="",
):
self.embedding_model = embedding_model
self.limit = limit
self.refresh = refresh
self.refresh_faiss_db = refresh_faiss_db
self.text_embedding_chunk_size = text_embedding_chunk_size
self.filter_str = filter_str
@beartype
def compute_embeddings_from_chunks(
self, embedding_function: OpenAIEmbeddings, metadatas: list, faiss_db
):
doc_chunks = []
metadatas_without_chunks = []
for metadata in metadatas:
doc_chunk = metadata.pop("doc_chunk")
doc_chunks.append(doc_chunk)
metadatas_without_chunks.append(metadata)
with BlockTimer(f"Embedding {len(metadatas)} fragments"):
embeddings = embedding_function.embed_documents(doc_chunks)
# account for name mangling in Python
faiss_db._FAISS__add(doc_chunks, embeddings, metadatas_without_chunks)
return faiss_db
@beartype
def parse_date_of_fetching(self, data: dict) -> str:
evidence_keys = {
"search_results_fetched",
"results_fetched_from_wikipedia_1M_with_cohere-22-12",
}
for key in evidence_keys:
if key in data["dates"]:
evidence_fetched_date = data["dates"][key]
return evidence_fetched_date
raise ValueError(f"Could not find evidence fetched date in {data['dates']}")
def embed_for_uuid(self, srcs):
init_openai_with_api_key()
embedding_function = OpenAIEmbeddings()
index = faiss.IndexFlatL2(EMBEDDING_DIMENSIONS[self.embedding_model])
docstore = InMemoryDocstore({})
index_to_docstore_id = {}
faiss_db = FAISS(
embedding_function=embedding_function.embed_query,
index=index,
docstore=docstore,
index_to_docstore_id=index_to_docstore_id,
)
already_embedded_chunks = {
doc.metadata["chunk_tag"] for doc in faiss_db.docstore._dict.values()
}
splitter = RecursiveCharacterTextSplitter(
chunk_size=self.text_embedding_chunk_size,
chunk_overlap=0,
)
kwarg_list = []
seen_links = set()
metadatas = []
total_chunks = 0
chunks_to_embed = 0
chunks_to_skip = 0
for data in srcs:
evidence_fetched_date = self.parse_date_of_fetching(data)
for document in data["documents"]:
for search_result in document["search_results"]:
# Don't embed the same link twice
if search_result["link"] in seen_links:
continue
seen_links.add(search_result["link"])
doc_chunks = [
doc.page_content
for doc in splitter.create_documents([search_result["text"]])
]
chunk_tags = [
f"{search_result['link']}-chunk-{idx}-chunk_sz-{self.text_embedding_chunk_size}"
for idx in range(len(doc_chunks))
]
for doc_chunk, chunk_tag in zip(doc_chunks, chunk_tags):
if chunk_tag not in already_embedded_chunks:
metadatas.append(
{
"doc_chunk": doc_chunk,
"link": search_result["link"],
"chunk_tag": chunk_tag,
"date_accessed": evidence_fetched_date,
"query": document["claim"],
}
)
chunks_to_embed += 1
else:
chunks_to_skip += 1
total_chunks += len(doc_chunks)
encoding = tiktoken.encoding_for_model(self.embedding_model)
doc_chunks = [x["doc_chunk"] for x in metadatas]
num_words = len(" ".join(doc_chunks).split())
num_tokens = len(encoding.encode("".join(doc_chunks)))
print(
f"Created {total_chunks} chunks of text to answer from {len(seen_links)} websites"
)
print(
f"Embedding {chunks_to_embed} (skipping {chunks_to_skip}) chunks of text from {len(seen_links)} websites)"
)
print(
f"Embedding {num_tokens} tokens ({num_words} words) from {len(doc_chunks)} chunks"
)
print(
f"Step5: Estimated cost: {num_tokens * PRICE_PER_1K_TOKENS[self.embedding_model]['embed'] / 1000:.2f} USD"
)
if metadatas:
self.compute_embeddings_from_chunks(
embedding_function=embedding_function,
faiss_db=faiss_db,
metadatas=metadatas,
)
return faiss_db
return None
def embed(self):
init_openai_with_api_key()
src_paths = []
for evidence_key in (
"google_search_results_evidence",
"cohere_wikipedia_evidence",
):
evidence_paths = list(PIPELINE_PATHS[evidence_key].glob("**/*.json"))
src_paths.extend(evidence_paths)
if self.filter_str:
num_paths = len(src_paths)
src_paths = [
src_path for src_path in src_paths if self.filter_str in src_path.name
]
print(
f"Filtering for {self.filter_str} (from {num_paths} to {len(src_paths)})"
)
print(f"Found {len(src_paths)} collections of evidence")
src_paths = sorted(src_paths)
embedding_function = OpenAIEmbeddings()
faiss_persist_dir = (
PIPELINE_PATHS["faiss_db_embeddings_for_evidence"]
/ f"{self.embedding_model}_chunk_size_{self.text_embedding_chunk_size}"
)
if faiss_persist_dir.exists():
if self.refresh_faiss_db:
print(f"Deleting existing database at {faiss_persist_dir}")
shutil.rmtree(faiss_persist_dir)
# check which chunks we've already embedded to avoid duplication
if faiss_persist_dir.exists() and not self.refresh_faiss_db:
faiss_db = FAISS.load_local(
folder_path=str(faiss_persist_dir), embeddings=embedding_function
)
print(f"Found existing database at {faiss_persist_dir}, using... ")
else:
index = faiss.IndexFlatL2(EMBEDDING_DIMENSIONS[self.embedding_model])
docstore = InMemoryDocstore({})
index_to_docstore_id = {}
faiss_db = FAISS(
embedding_function=embedding_function.embed_query,
index=index,
docstore=docstore,
index_to_docstore_id=index_to_docstore_id,
)
print(f"Persisting intialised database to {faiss_persist_dir}")
faiss_db.save_local(folder_path=str(faiss_persist_dir))
already_embedded_chunks = {
doc.metadata["chunk_tag"] for doc in faiss_db.docstore._dict.values()
}
splitter = RecursiveCharacterTextSplitter(
chunk_size=self.text_embedding_chunk_size,
chunk_overlap=0,
)
kwarg_list = []
seen_links = set()
metadatas = []
total_chunks = 0
chunks_to_embed = 0
chunks_to_skip = 0
for src_path in src_paths:
with open(src_path, "r") as f:
data = json.load(f)
evidence_fetched_date = self.parse_date_of_fetching(data)
for document in data["documents"]:
for search_result in document["search_results"]:
# Don't embed the same link twice
if search_result["link"] in seen_links:
continue
seen_links.add(search_result["link"])
doc_chunks = [
doc.page_content
for doc in splitter.create_documents([search_result["text"]])
]
chunk_tags = [
f"{search_result['link']}-chunk-{idx}-chunk_sz-{self.text_embedding_chunk_size}"
for idx in range(len(doc_chunks))
]
for doc_chunk, chunk_tag in zip(doc_chunks, chunk_tags):
if chunk_tag not in already_embedded_chunks:
metadatas.append(
{
"doc_chunk": doc_chunk,
"link": search_result["link"],
"chunk_tag": chunk_tag,
"date_accessed": evidence_fetched_date,
"query": document["claim"],
}
)
chunks_to_embed += 1
else:
chunks_to_skip += 1
total_chunks += len(doc_chunks)
encoding = tiktoken.encoding_for_model(self.embedding_model)
doc_chunks = [x["doc_chunk"] for x in metadatas]
num_words = len(" ".join(doc_chunks).split())
num_tokens = len(encoding.encode("".join(doc_chunks)))
print(
f"Created {total_chunks} chunks of text to answer from {len(seen_links)} websites"
)
print(
f"Embedding {chunks_to_embed} (skipping {chunks_to_skip}) chunks of text from {len(seen_links)} websites)"
)
print(
f"Embedding {num_tokens} tokens ({num_words} words) from {len(doc_chunks)} chunks"
)
print(
f"Estimated cost: {num_tokens * PRICE_PER_1K_TOKENS[self.embedding_model]['embed'] / 1000:.2f} USD"
)
if metadatas:
self.compute_embeddings_from_chunks(
embedding_function=embedding_function,
faiss_persist_dir=faiss_persist_dir,
metadatas=metadatas,
)
|