2023-02-14 18:30:39 +00:00
|
|
|
import os
|
2023-05-13 08:36:17 +00:00
|
|
|
|
2023-02-10 15:44:42 +00:00
|
|
|
import tiktoken
|
|
|
|
from langchain.embeddings import OpenAIEmbeddings
|
2023-05-13 08:36:17 +00:00
|
|
|
from langchain.vectorstores import FAISS
|
|
|
|
from retry import retry
|
|
|
|
|
2023-02-15 14:42:57 +00:00
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
# from langchain.embeddings import HuggingFaceEmbeddings
|
|
|
|
# from langchain.embeddings import HuggingFaceInstructEmbeddings
|
|
|
|
# from langchain.embeddings import CohereEmbeddings
|
2023-02-10 15:44:42 +00:00
|
|
|
|
|
|
|
|
2023-06-03 09:01:50 +00:00
|
|
|
def num_tokens_from_string(string: str, encoding_name: str) -> tuple[int, float]:
|
2023-05-12 10:02:25 +00:00
|
|
|
# Function to convert string to tokens and estimate user cost.
|
2023-02-10 15:44:42 +00:00
|
|
|
encoding = tiktoken.get_encoding(encoding_name)
|
|
|
|
num_tokens = len(encoding.encode(string))
|
2023-06-02 20:27:55 +00:00
|
|
|
total_price = (num_tokens / 1000) * 0.0004
|
2023-02-10 15:44:42 +00:00
|
|
|
return num_tokens, total_price
|
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-15 03:04:31 +00:00
|
|
|
@retry(tries=10, delay=60)
|
|
|
|
def store_add_texts_with_retry(store, i):
|
|
|
|
store.add_texts([i.page_content], metadatas=[i.metadata])
|
2023-05-12 10:02:25 +00:00
|
|
|
# store_pine.add_texts([i.page_content], metadatas=[i.metadata])
|
|
|
|
|
2023-02-15 03:04:31 +00:00
|
|
|
|
2023-02-14 18:30:39 +00:00
|
|
|
def call_openai_api(docs, folder_name):
|
2023-05-12 10:02:25 +00:00
|
|
|
# Function to create a vector store from the documents and save it to disk.
|
2023-02-14 18:30:39 +00:00
|
|
|
|
|
|
|
# create output folder if it doesn't exist
|
|
|
|
if not os.path.exists(f"outputs/{folder_name}"):
|
|
|
|
os.makedirs(f"outputs/{folder_name}")
|
|
|
|
|
2023-02-12 16:25:01 +00:00
|
|
|
from tqdm import tqdm
|
2023-06-02 20:27:55 +00:00
|
|
|
|
2023-02-12 16:25:01 +00:00
|
|
|
docs_test = [docs[0]]
|
|
|
|
# remove the first element from docs
|
|
|
|
docs.pop(0)
|
|
|
|
# cut first n docs if you want to restart
|
2023-05-12 10:02:25 +00:00
|
|
|
# docs = docs[:n]
|
2023-02-12 16:25:01 +00:00
|
|
|
c1 = 0
|
2023-03-13 14:20:03 +00:00
|
|
|
# pinecone.init(
|
|
|
|
# api_key="", # find at app.pinecone.io
|
|
|
|
# environment="us-east1-gcp" # next to api key in console
|
|
|
|
# )
|
2023-05-12 10:02:25 +00:00
|
|
|
# index_name = "pandas"
|
2023-06-03 09:01:50 +00:00
|
|
|
if ( # azure
|
2023-08-05 12:08:51 +00:00
|
|
|
os.environ.get("OPENAI_API_BASE")
|
|
|
|
and os.environ.get("OPENAI_API_VERSION")
|
2023-06-03 09:01:50 +00:00
|
|
|
and os.environ.get("AZURE_DEPLOYMENT_NAME")
|
2023-06-17 14:09:22 +00:00
|
|
|
and os.environ.get("AZURE_EMBEDDINGS_DEPLOYMENT_NAME")
|
2023-06-03 09:01:50 +00:00
|
|
|
):
|
|
|
|
os.environ["OPENAI_API_TYPE"] = "azure"
|
|
|
|
openai_embeddings = OpenAIEmbeddings(model=os.environ.get("AZURE_EMBEDDINGS_DEPLOYMENT_NAME"))
|
|
|
|
else:
|
|
|
|
openai_embeddings = OpenAIEmbeddings()
|
|
|
|
store = FAISS.from_documents(docs_test, openai_embeddings)
|
2023-05-12 10:02:25 +00:00
|
|
|
# store_pine = Pinecone.from_documents(docs_test, OpenAIEmbeddings(), index_name=index_name)
|
2023-02-14 13:06:28 +00:00
|
|
|
|
|
|
|
# Uncomment for MPNet embeddings
|
|
|
|
# model_name = "sentence-transformers/all-mpnet-base-v2"
|
|
|
|
# hf = HuggingFaceEmbeddings(model_name=model_name)
|
|
|
|
# store = FAISS.from_documents(docs_test, hf)
|
2023-06-02 20:27:55 +00:00
|
|
|
for i in tqdm(
|
|
|
|
docs, desc="Embedding 🦖", unit="docs", total=len(docs), bar_format="{l_bar}{bar}| Time Left: {remaining}"
|
|
|
|
):
|
2023-02-12 16:25:01 +00:00
|
|
|
try:
|
2023-02-15 03:04:31 +00:00
|
|
|
store_add_texts_with_retry(store, i)
|
2023-02-12 16:25:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
print("Error on ", i)
|
|
|
|
print("Saving progress")
|
|
|
|
print(f"stopped at {c1} out of {len(docs)}")
|
2023-02-15 18:40:23 +00:00
|
|
|
store.save_local(f"outputs/{folder_name}")
|
|
|
|
break
|
2023-02-12 16:25:01 +00:00
|
|
|
c1 += 1
|
2023-02-15 18:40:23 +00:00
|
|
|
store.save_local(f"outputs/{folder_name}")
|
2023-02-10 15:44:42 +00:00
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-14 18:30:39 +00:00
|
|
|
def get_user_permission(docs, folder_name):
|
2023-05-12 10:02:25 +00:00
|
|
|
# Function to ask user permission to call the OpenAI api and spend their OpenAI funds.
|
2023-02-10 15:44:42 +00:00
|
|
|
# Here we convert the docs list to a string and calculate the number of OpenAI tokens the string represents.
|
2023-05-12 10:02:25 +00:00
|
|
|
# docs_content = (" ".join(docs))
|
2023-02-10 15:44:42 +00:00
|
|
|
docs_content = ""
|
|
|
|
for doc in docs:
|
|
|
|
docs_content += doc.page_content
|
|
|
|
|
|
|
|
tokens, total_price = num_tokens_from_string(string=docs_content, encoding_name="cl100k_base")
|
|
|
|
# Here we print the number of tokens and the approx user cost with some visually appealing formatting.
|
|
|
|
print(f"Number of Tokens = {format(tokens, ',d')}")
|
|
|
|
print(f"Approx Cost = ${format(total_price, ',.2f')}")
|
2023-05-12 10:02:25 +00:00
|
|
|
# Here we check for user permission before calling the API.
|
2023-02-10 15:44:42 +00:00
|
|
|
user_input = input("Price Okay? (Y/N) \n").lower()
|
|
|
|
if user_input == "y":
|
2023-02-14 18:30:39 +00:00
|
|
|
call_openai_api(docs, folder_name)
|
2023-02-10 15:44:42 +00:00
|
|
|
elif user_input == "":
|
2023-02-14 18:30:39 +00:00
|
|
|
call_openai_api(docs, folder_name)
|
2023-02-10 15:44:42 +00:00
|
|
|
else:
|
2023-02-14 14:05:16 +00:00
|
|
|
print("The API was not called. No money was spent.")
|