Find answers from the community

Updated 3 months ago

how can I add additional pdf/document to existing VectorStoreIndex(). I could not find any

how can I add additional pdf/document to existing VectorStoreIndex(). I could not find anything like add_documents().
Below is my script :

input_dir = './data/cisco_pdf/'
some_docs = SimpleDirectoryReader(input_dir=input_dir).load_data()

build index and query engine

vector_query_engine = VectorStoreIndex.from_documents(
some_docs,
use_async=True,
).as_query_engine()

Now I need to add 1 more pdf and i want to update my index for this additional PDF. could you please help here ?
k
A
5 comments
Please stand by @Anuj as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Sure, I can help with that. In LlamaIndex, you can add new documents to an existing index using the insert method. Here's how you can do it:

Plain Text
from llama_index.core import VectorStoreIndex

# Assuming 'index' is your existing VectorStoreIndex
for doc in some_docs:
    vector_query_engine.index.insert(doc)


This will add each document in some_docs to your existing index.

Please note that the insert method is used to add individual documents to the index. If you have multiple documents, you would need to loop over them and insert them one by one as shown above.

You can find more details on managing documents in the LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai but I am asking particularly wrt tools created ?
Let me check the knowledge base for you. One moment! :books:
----------------------
I see, you want to update the documents used by a tool in your LlamaIndex setup. Unfortunately, based on the provided knowledge sources, there isn't a direct method to add documents to an existing VectorStoreIndex that's already been used to create a QueryEngineTool.

However, you can create a new VectorStoreIndex with the additional documents and then create a new QueryEngineTool with the updated index. Here's how you can do it:

Plain Text
from llama_index.core import VectorStoreIndex, ToolMetadata
from llama_index.agent import QueryEngineTool

# Add the new document to your existing documents
some_docs.append(new_doc)

# Create a new VectorStoreIndex with the updated documents
new_vector_index = VectorStoreIndex.from_documents(some_docs)

# Create a new QueryEngineTool with the updated index
new_query_engine_tool = QueryEngineTool(
    query_engine=new_vector_index.as_query_engine(),
    metadata=ToolMetadata(
        name="new_tool_name",
        description="New tool description"
    )
)


In this code, new_doc is your additional document, and new_tool_name and New tool description should be replaced with the name and description of your new tool.

Please note that this will create a new tool and won't update the existing one. If you want to replace the existing tool with the new one, you would need to remove the old tool from your toolset and add the new one.

For more information on creating tools, you can refer to the LlamaIndex documentation ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord