Merge pull request #73 from arc53/chunk-embedding

chunked embedding
This commit is contained in:
Pavel 2023-02-12 20:46:17 +04:00 committed by GitHub
commit 98588494d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,12 +14,30 @@ def num_tokens_from_string(string: str, encoding_name: str) -> int:
def call_openai_api(docs): def call_openai_api(docs):
# Function to create a vector store from the documents and save it to disk. # Function to create a vector store from the documents and save it to disk.
store = FAISS.from_documents(docs, OpenAIEmbeddings()) from tqdm import tqdm
faiss.write_index(store.index, "docs.index") docs_test = [docs[0]]
store.index = None # remove the first element from docs
docs.pop(0)
# cut first n docs if you want to restart
#docs = docs[:n]
c1 = 0
store = FAISS.from_documents(docs_test, OpenAIEmbeddings())
for i in tqdm(docs, desc="Embedding 🦖", unit="docs", total=len(docs), bar_format='{l_bar}{bar}| Time Left: {remaining}'):
try:
import time
store.add_texts([i.page_content], metadatas=[i.metadata])
except Exception as e:
print(e)
print("Error on ", i)
print("Saving progress")
print(f"stopped at {c1} out of {len(docs)}")
store.save_local("outputs")
print("Sleeping for 10 seconds and trying again")
time.sleep(10)
store.add_texts([i.page_content], metadatas=[i.metadata])
c1 += 1
with open("faiss_store.pkl", "wb") as f: store.save_local("outputs")
pickle.dump(store, f)
def get_user_permission(docs): def get_user_permission(docs):
# Function to ask user permission to call the OpenAI api and spend their OpenAI funds. # Function to ask user permission to call the OpenAI api and spend their OpenAI funds.