Spaces:
Sleeping

smartgit / ingest_pinecone.py
yamlspace's picture
testing ingest scripts
85ecfcf
Raw History Blame Contribute Delete
1.79 kB
import os
import getpass
import shutil
import pinecone
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import DeepLake
from langchain.text_splitter import CharacterTextSplitter
from langchain.document_loaders import TextLoader
from dotenv import load_dotenv
load_dotenv()
embeddings = OpenAIEmbeddings(disallowed_special=())
index_name = "./index/" + 'finecast'
index_name = 'finecast'
pinecone.init(
api_key=os.environ.get('PINECONE_API_KEY'),
environment="northamerica-northeast1-gcp"
)
# check if index already exists (it shouldn't if this is first time)
if index_name not in pinecone.list_indexes():
# if does not exist, create index
pinecone.create_index(
index_name,
dimension=len(res['data'][0]['embedding']),
metric='cosine',
metadata_config={'indexed': ['channel_id', 'published']}
)
# connect to index
index = pinecone.Index(index_name)
if os.path.exists(index_name):
shutil.rmtree(index_name)
db = DeepLake(dataset_path=index_name, embedding_function=embeddings) #dataset would be publicly available
root_dirs = ['../../elsewhen/finecast-infrastructure','../../elsewhen/finecast/infra2','../../elsewhen/finecast/infrastructure','../../elsewhen/finecast/ci']
docs = []
for root_dir in root_dirs:
docs = []
for dirpath, dirnames, filenames in os.walk(root_dir):
for file in filenames:
try:
loader = TextLoader(os.path.join(dirpath, file), encoding='utf-8')
docs.extend(loader.load_and_split())
except Exception as e:
pass
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(docs)
db.add_documents(texts)