Spaces:
Runtime error
Runtime error
pinecone integration to llamaindex
#2
by zhtet - opened
Files changed (2)
12345623 unmodified lines3031323334from pinecone import Pinecone, ServerlessSpecfrom llama_index.vector_stores.pinecone import PineconeVectorStorefrom dotenv import load_dotenvimport os23 unmodified lines# Initialize your indexpinecone_index = pc.Index(pc_index_name)# Define the vector storepinecone_vector_store = PineconeVectorStore(pinecone_index=pinecone_index)123456789101112131423 unmodified lines383940414243444546474849505152535455565758596061626364656667686970717273from 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 HfFileSystemimport os23 unmodified lines# Initialize your indexpinecone_index = pc.Index(pc_index_name)# 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 namespacesdef 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 = Noneif _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 index
4 unmodified lines567891011121314156 unmodified lines22232425262765 unmodified lines9394959697989910010110210310410510610710810911011111211311411511688 unmodified lines2052062072082092102112124 unmodified lines# local importsfrom models.llms import load_llm, integrated_llmsfrom models.embeddings import hf_embed_model, openai_embed_modelfrom models.llamaCustom import LlamaCustomfrom models.llamaCustomV2 import LlamaCustomV2# from models.vector_database import pinecone_vector_storefrom utils.chatbox import show_previous_messages, show_chat_inputfrom utils.util import validate_openai_api_key6 unmodified linesSettings,load_index_from_storage,)from llama_index.core.memory import ChatMemoryBufferfrom llama_index.core.base.llms.types import ChatMessage65 unmodified linesraise ereturn 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 indexdef 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."""passdef check_api_key(model_name: str, source: str):if source.startswith("openai"):if not st.session_state.openai_api_key:88 unmodified linesSettings.llm = llama_llmst.write("Processing Data ...")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)4 unmodified lines567891011121314156 unmodified lines2223242526272865 unmodified lines94959697989988 unmodified lines1881891901911921931941954 unmodified lines# local importsfrom models.llms import load_llm, integrated_llmsfrom models.embeddings import openai_embed_modelfrom models.llamaCustom import LlamaCustom# from models.llamaCustomV2 import LlamaCustomV2from models.vector_database import get_pinecone_indexfrom utils.chatbox import show_previous_messages, show_chat_inputfrom utils.util import validate_openai_api_key6 unmodified linesSettings,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 ChatMessage65 unmodified linesraise ereturn indexdef check_api_key(model_name: str, source: str):if source.startswith("openai"):if not st.session_state.openai_api_key:88 unmodified linesSettings.llm = llama_llmst.write("Processing Data ...")# 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