Spaces:
Runtime error
Runtime error
pinecone integration to llamaindex
#2
by zhtet - opened
Files changed (2)
123456789101112131423 unmodified lines3839403334414243444546474849505152535455565758596061626364656667686970717273from typing import Listfrom pinecone import Pinecone, ServerlessSpecfrom llama_index.vector_stores.pinecone import PineconeVectorStorefrom dotenv import load_dotenvfrom llama_index.core import ( SimpleDirectoryReader, Document, VectorStoreIndex, StorageContext,)from huggingface_hub import HfFileSystem
import os
23 unmodified lines# Initialize your indexpinecone_index = pc.Index(pc_index_name)
# Define the vector storepinecone_vector_store = PineconeVectorStore(pinecone_index=pinecone_index)# print("Deleting all vectors in the pinecone index: ", pinecone_index.delete(delete_all=True))# print("Deleting all vectors with the namespace 'calregs_pdf': ", pinecone_index.delete(namespace="calregs_pdf"))
SAVE_DIR = "uploaded_files"
def _namespace_exists(namespace: str): namespaces = pinecone_index.describe_index_stats()["namespaces"] return namespace in namespaces
def get_pinecone_index(filename: str) -> VectorStoreIndex: """This function loads the index from Pinecone if it exists, otherwise it creates a new index from the document.""" namespace = filename.replace(".", "_").replace(" ", "_") pinecone_vector_store = PineconeVectorStore( pinecone_index=pinecone_index, namespace=namespace, ) index = None if _namespace_exists(namespace=namespace): print(f"Namespace {namespace} exists.") index = VectorStoreIndex.from_vector_store(vector_store=pinecone_vector_store) else: reader = SimpleDirectoryReader(input_files=[f"{SAVE_DIR}/{filename}"]) docs = reader.load_data(show_progress=True) storage_context = StorageContext.from_defaults( vector_store=pinecone_vector_store ) index = VectorStoreIndex.from_documents( documents=docs, show_progress=True, storage_context=storage_context )
return index4 unmodified lines56788910101112121314156 unmodified lines2223242526272865 unmodified lines9495969697989910010110210310410510610710810911011111211397989988 unmodified lines1881891902082091911921931941954 unmodified lines
# local importsfrom models.llms import load_llm, integrated_llmsfrom models.embeddings import hf_embed_model, openai_embed_modelfrom models.embeddings import openai_embed_modelfrom models.llamaCustom import LlamaCustomfrom models.llamaCustomV2 import LlamaCustomV2# from models.llamaCustomV2 import LlamaCustomV2
# from models.vector_database import pinecone_vector_storefrom models.vector_database import get_pinecone_indexfrom utils.chatbox import show_previous_messages, show_chat_inputfrom utils.util import validate_openai_api_key
6 unmodified lines Settings, load_index_from_storage,)from llama_index.vector_stores.pinecone import PineconeVectorStorefrom llama_index.core.memory import ChatMemoryBufferfrom llama_index.core.base.llms.types import ChatMessage
65 unmodified lines raise e return index
# def get_pinecone_index(filename: str) -> VectorStoreIndex:# """Thie function loads the index from Pinecone if it exists, otherwise it creates a new index from the document."""# reader = SimpleDirectoryReader(input_files=[f"{SAVE_DIR}/{filename}"])# docs = reader.load_data(show_progress=True)# storage_context = StorageContext.from_defaults(vector_store=pinecone_vector_store)# index = VectorStoreIndex.from_documents(# documents=docs, show_progress=True, storage_context=storage_context# )
# return index
def get_chroma_index(filename: str) -> VectorStoreIndex: """This function loads the index from Chroma if it exists, otherwise it creates a new index from the document.""" pass
def check_api_key(model_name: str, source: str): if source.startswith("openai"): if not st.session_state.openai_api_key:88 unmodified lines Settings.llm = llama_llm
st.write("Processing Data ...") index = get_index(selected_file) # index = get_pinecone_index(selected_file) # index = get_index(selected_file) index = get_pinecone_index(selected_file)
st.write("Finishing Up ...") llama_custom = LlamaCustom(model_name=selected_llm_name, index=index)models/vector_database.py+41-2
pages/llama_custom_demo.py+6-23