carolinedfrasca/llamaindex-chat-with-streamlit-docs

Import Error with llama-index

Closed this issue · 6 comments

Hi,

I was trying to fork this project from the original interface posted and before editing it in codespaces, I get:

ImportError: cannot import name 'VectorStoreIndex' from 'llama_index' (unknown location)

I was able to get it to work yesterday morning, but have been unable to find a bug in my code to account for it. Please let me know if you can help!
Screenshot 2024-02-13 at 12 40 38

llama_index v0.10 adjusts the package structure, and you can make the following modifications to the code:

import streamlit as st
from llama_index.core import VectorStoreIndex, ServiceContext, Document
from llama_index.llms.openai import OpenAI
import openai
from llama_index.core import SimpleDirectoryReader

In fact, the differences between versions are not limited to changes in the package structure. For example, ServiceContext has also been deprecated in the new version.
Therefore, you may need to lower the version of llama_index to run this code. or modify the code as bellows:

from llama_index.core import Settings
...
Settings.llm = OpenAI(model="gpt-3.5-turbo", temperature=0.5, system_prompt="You are an expert on the Streamlit Python library and your job is to answer technical questions. Assume that all questions are related to the Streamlit Python library. Keep your answers technical and based on facts – do not hallucinate features.")
index = VectorStoreIndex.from_documents(docs)

Thanks for the help! Which version of llama-index would be best to maintain the old structure?

You can auto update the existing code to the new structure. Advice below is from LlamaIndex :

To help assist with migrating, pip install llama-index and pip install llama-index-core both come with a command-line tool to update existing code and notebooks.

NOTE: The CLI tool updates files in place. Please ensure you have your data backed up to undo any changes as needed.

After installing v0.10.0, you can upgrade your existing imports automatically:

llamaindex-cli upgrade-file <file_path>
OR
llamaindex-cli upgrade <folder_path>
For notebooks, new pip install statements are inserted and imports are updated.

For .py and .md files, import statements are also updated, and new requirements are printed to the terminal.

Hello, same Issue for me! I tried now version 9 of llamaindex, which also did not work.

Sadly I was to stupid to change the code as suggested by @raoqu.

Unfortunately I am also not able to run the migration tool, since I am getting the following issue:
ImportError: cannot import name 'SimpleDirectoryReader' from 'llama_index.core' (unknown location)

Any help is appreciated. Have a good day.

Hello, same Issue for me! I tried now version 9 of llamaindex, which also did not work.

Sadly I was to stupid to change the code as suggested by @raoqu.

Unfortunately I am also not able to run the migration tool, since I am getting the following issue: ImportError: cannot import name 'SimpleDirectoryReader' from 'llama_index.core' (unknown location)

Any help is appreciated. Have a good day.

Solution:
There are some recent change in llama index like llama_index is llama_index.core , Here you can try these ways:

there may be installation issue, so you can install again, [pip install llama-index]. if the problem still persist then try to create a new virtual environments and install requirements.txt by this command pip install -r requirements.txt
Then try this code, Most probably your problem will be solved.

from llama_index.core import SimpleDirectoryReader

reader = SimpleDirectoryReader(input_dir="path/to/directory")
documents = reader.load_data()

Have a great day.

Apologies for the delay! I just updated the repo to account for the 0.10.0 update of LlamaIndex. Note that you might need to start with a fresh environment if you're running the app locally.