Skip to main content
Table of contents

While the default version on Ubuntu 20.04 for Python is 3.8, I've added Python 3.11.5 (latest version).

Errors

GPTSimpleVectorIndex is deprecated

Attempting to run python3.11 model-ai.py and I'm seeing the following response

/home/{user}/.local/lib/python3.11/site-packages/langchain/__init__.py:40: UserWarning: Importing BasePromptTemplate from langchain root module is no longer supported.
 warnings.warn(
/home/{user}/.local/lib/python3.11/site-packages/langchain/__init__.py:40: UserWarning: Importing PromptTemplate from langchain root module is no longer supported.
 warnings.warn(
Traceback (most recent call last):
 File "/{path}/model-ai.py", line 5, in <module>
   from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, ServiceContext, PromptHelper
ImportError: cannot import name 'GPTSimpleVectorIndex' from 'llama_index' (/home/{user}/.local/lib/python3.11/site-packages/llama_index/__init__.py)

 

The initial lines of code for the model-ai.py file are

import os

os.environ["OPENAI_API_KEY"] = '{secret-key}'

from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, ServiceContext, PromptHelper
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys

As per llama_index 0.8.40, GPTSimpleVectorIndex is deprecated and replaced by VectorStoreIndex, therefore change accordingly.

Before

from llama_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, ServiceContext, PromptHelper

After

from llama_index import SimpleDirectoryReader, GPTVectorStoreIndex, LLMPredictor, ServiceContext, PromptHelper

 

PromptHelper - chunk_overlap_ratio

Traceback (most recent call last):
  File "/{path}/model-ai.py", line 50, in <module>
    init_index("docs")
  File "/{path}/model-ai.py", line 24, in init_index
    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/indices/prompt_helper.py", line 89, in __init__
    raise ValueError("chunk_overlap_ratio must be a float between 0. and 1.")
ValueError: chunk_overlap_ratio must be a float between 0. and 1.

Before

prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

After

prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, embedding_limit, chunk_size_limit=chunk_size_limit)

However, this might be correct for LlamaIndex 0.6.6, reading through https://docs.llamaindex.ai/en/stable/api_reference/service_context/prompt_helper.html the parameters have changed.  The parameters are now:

  • context_window (int) – Context window for the LLM.
  • num_output (int) – Number of outputs for the LLM.
  • chunk_overlap_ratio (float) – Chunk overlap as a ratio of chunk size
  • chunk_size_limit (Optional[int]) – Maximum chunk size to use.
  • tokenizer (Optional[Callable[[str], List]]) – Tokenizer to use.
  • separator (str) – Separator for text splitter

Updated PromptHelper to work with LlamaIndex 0.8.41

prompt_helper = PromptHelper(context_window, num_outputs, chunk_overlap_ratio, chunk_size_limit)

 

pypdf module missing

Traceback (most recent call last):
 File "/{path}/model-ai.py", line 54, in <module>
   init_index("docs")
 File "/{path}/model-ai.py", line 32, in init_index
   documents = SimpleDirectoryReader(directory_path).load_data()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/readers/file/base.py", line 203, in load_data
   docs = reader.load_data(input_file, extra_info=metadata)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/readers/file/docs_reader.py", line 23, in load_data
   raise ImportError(
ImportError: pypdf is required to read PDF files: `pip install pypdf`

As I'm PIP from Python 3.11.x, the command to install will be

pip3.11 install pypdf

 

ModuleNotFoundError: No module named '_sqlite3'

Traceback (most recent call last):
  File "/{path}/model-ai.py", line 54, in <module>
    init_index("docs")
  File "/{path}/model-ai.py", line 36, in init_index
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/indices/service_context.py", line 175, in from_defaults
    node_parser = node_parser or _get_default_node_parser(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/indices/service_context.py", line 34, in _get_default_node_parser
    return SimpleNodeParser.from_defaults(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/node_parser/simple.py", line 60, in from_defaults
    text_splitter = text_splitter or get_default_text_splitter(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/text_splitter/__init__.py", line 22, in get_default_text_splitter
    return SentenceSplitter(
           ^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/text_splitter/sentence_splitter.py", line 95, in __init__
    chunking_tokenizer_fn = chunking_tokenizer_fn or split_by_sentence_tokenizer()
                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/text_splitter/utils.py", line 34, in split_by_sentence_tokenizer
    import nltk
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/__init__.py", line 153, in <module>
    from nltk.translate import *
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/translate/__init__.py", line 24, in <module>
    from nltk.translate.meteor_score import meteor_score as meteor
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/translate/meteor_score.py", line 13, in <module>
    from nltk.corpus import WordNetCorpusReader, wordnet
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/corpus/__init__.py", line 64, in <module>
    from nltk.corpus.reader import *
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/corpus/reader/__init__.py", line 106, in <module>
    from nltk.corpus.reader.panlex_lite import *
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/corpus/reader/panlex_lite.py", line 15, in <module>
    import sqlite3
  File "/usr/local/lib/python3.11/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.11/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

If the Python interpreter cannot find the _sqlite3 module, which is part of the standard library, you can install it with the following command:

pip3.11 install db-sqlite3

SQLite3 is a flexible database engine that is accessible in the Python standard library.

Post running the above command, then re-running python3.11 model-ai.py

Traceback (most recent call last):
  File "/{path}/model-ai.py", line 54, in <module>
    init_index("docs")
  File "/{path}/model-ai.py", line 36, in init_index
    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/indices/service_context.py", line 175, in from_defaults
    node_parser = node_parser or _get_default_node_parser(
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/indices/service_context.py", line 34, in _get_default_node_parser
    return SimpleNodeParser.from_defaults(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/node_parser/simple.py", line 60, in from_defaults
    text_splitter = text_splitter or get_default_text_splitter(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/text_splitter/__init__.py", line 22, in get_default_text_splitter
    return SentenceSplitter(
           ^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/text_splitter/sentence_splitter.py", line 95, in __init__
    chunking_tokenizer_fn = chunking_tokenizer_fn or split_by_sentence_tokenizer()
                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/{user}/.local/lib/python3.11/site-packages/llama_index/text_splitter/utils.py", line 34, in split_by_sentence_tokenizer
    import nltk
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/__init__.py", line 153, in <module>
    from nltk.translate import *
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/translate/__init__.py", line 24, in <module>
    from nltk.translate.meteor_score import meteor_score as meteor
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/translate/meteor_score.py", line 13, in <module>
    from nltk.corpus import WordNetCorpusReader, wordnet
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/corpus/__init__.py", line 64, in <module>
    from nltk.corpus.reader import *
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/corpus/reader/__init__.py", line 106, in <module>
    from nltk.corpus.reader.panlex_lite import *
  File "/home/{user}/.local/lib/python3.11/site-packages/nltk/corpus/reader/panlex_lite.py", line 15, in <module>
    import sqlite3
  File "/usr/local/lib/python3.11/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.11/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

Same error.... a little more digging.  The error message "ModuleNotFoundError: No module named '_sqlite3'" indicates that Python cannot find the _sqlite3 module, which is essential for working with SQLite databases. This error typically occurs when the SQLite module is not installed or configured correctly.

To resolve this issue, you can try the following steps:

Install the SQLite Library:

You can install the SQLite library using pip, the Python package manager. Open your command prompt or terminal and run the following command:
bash

pip install pysqlite3

This will install the pysqlite3 package, which is a Python interface to the SQLite database.

Check for Python Version Compatibility:

Ensure that you are using a Python version that is compatible with the SQLite module. In most cases, SQLite should be compatible with Python 3.x.

Check Your Environment:

If you are working within a virtual environment, make sure it is activated. The installed packages should be accessible from within the active virtual environment.

Verify Installation:

After installing pysqlite3, you can verify that it's installed correctly by opening a Python interactive shell and importing it:
python

import sqlite3

If there are no errors, it means the SQLite library is installed and functioning correctly.

Rebuild Python (Advanced):
If you continue to encounter issues, especially on platforms where Python is built from source (e.g., some Linux distributions), you may need to rebuild Python with SQLite support. Ensure that you have the necessary development libraries installed on your system before rebuilding Python.

Consider Using a Virtual Environment:

If you're using virtual environments, create a new virtual environment and ensure that it's activated before installing any packages.  If you've followed these steps and are still encountering the "_sqlite3" module not found error, please provide more details about your Python environment and the steps you've taken so far, and I can provide more specific guidance.

 

Running pip3.11 install --user {package}

pip3.11 install --user pysqlite3

Response

ERROR: Could not build wheels for pysqlite3, which is required to install pyproject.toml-based projects

The error message you're encountering, "Could not build wheels for pysqlite3," indicates that there was a problem building the Python wheels for the pysqlite3 package during installation. This error can occur due to various reasons, but it's often related to missing build dependencies or system-specific issues.

To resolve this issue, you can try the following steps:

Install Build Dependencies:

Make sure you have the necessary build dependencies installed on your system. The required dependencies may vary depending on your operating system. For example, on Debian/Ubuntu-based systems, you can use the following command to install the required packages:

sudo apt-get install python3-dev libsqlite3-dev

Response

Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-dev is already the newest version (3.8.2-0ubuntu2).
Suggested packages:
  sqlite3-doc
The following NEW packages will be installed:
  libsqlite3-dev
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 697 kB of archives.
After this operation, 2372 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 libsqlite3-dev amd64 3.31.1-4ubuntu0.5 [697 kB]
Fetched 697 kB in 2s (401 kB/s)
Selecting previously unselected package libsqlite3-dev:amd64.
(Reading database ... 146994 files and directories currently installed.)
Preparing to unpack .../libsqlite3-dev_3.31.1-4ubuntu0.5_amd64.deb ...
Unpacking libsqlite3-dev:amd64 (3.31.1-4ubuntu0.5) ...
Setting up libsqlite3-dev:amd64 (3.31.1-4ubuntu0.5) ...

OK, let's try and re-run the package install command

pip3.11 install pysqlite3

Response

Defaulting to user installation because normal site-packages is not writeable
Collecting pysqlite3
  Using cached pysqlite3-0.5.2.tar.gz (40 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: pysqlite3
  Building wheel for pysqlite3 (setup.py) ... done
  Created wheel for pysqlite3: filename=pysqlite3-0.5.2-cp311-cp311-linux_x86_64.whl size=181871 sha256=0cd681ce78efc9944d1b37987d5a9038b3e071c62aab8ccd8c211f8a8355d30e
  Stored in directory: /home/{user}/.cache/pip/wheels/9c/c7/8d/ab361fea28bd859525da6175948ed260dcf63fef2518b61abd
Successfully built pysqlite3
Installing collected packages: pysqlite3
Successfully installed pysqlite3-0.5.2

Installed!

 

Back to testing the Python3 command

No success - the response was

../
ModuleNotFoundError: No module named '_sqlite3'

 

Check sqlite3

Create a Python file with one line.  I named the file 'sql-ai.py', with the content

import sqlite3

Tested by running the following

python3.11 sql-ai.py

Response

Traceback (most recent call last):
  File "/{path}/sql-ai.py", line 1, in <module>
    import sqlite3
  File "/usr/local/lib/python3.11/sqlite3/__init__.py", line 57, in <module>
    from sqlite3.dbapi2 import *
  File "/usr/local/lib/python3.11/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

 

From /usr/src run the following

sudo apt-get install sqlite3 libsqlite3-dev

Response

Reading package lists... Done
Building dependency tree
Reading state information... Done
libsqlite3-dev is already the newest version (3.31.1-4ubuntu0.5).
Suggested packages:
  sqlite3-doc
The following NEW packages will be installed:
  sqlite3
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 860 kB of archives.
After this operation, 2803 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://azure.archive.ubuntu.com/ubuntu focal-updates/main amd64 sqlite3 amd64 3.31.1-4ubuntu0.5 [860 kB]
Fetched 860 kB in 2s (565 kB/s)
Selecting previously unselected package sqlite3.
(Reading database ... 147003 files and directories currently installed.)
Preparing to unpack .../sqlite3_3.31.1-4ubuntu0.5_amd64.deb ...
Unpacking sqlite3 (3.31.1-4ubuntu0.5) ...
Setting up sqlite3 (3.31.1-4ubuntu0.5) ...
Processing triggers for man-db (2.9.1-1) ...

Post this install, I retested

 

Upgrade pip and Setuptools

Ensure that your pip and setuptools are up-to-date by running

python3.11 -m pip install --upgrade pip setuptools

 

Related articles

Andrew Fletcher18 Mar 2024
Resolving CVE-2022-48624 less issue
To resolve the CVE-2022-48624 vulnerability on Ubuntu using Nginx, it's crucial to understand that the issue lies within the "less" package, not Nginx itself. The vulnerability affects "less" before version 606, where close_altfile in filename.c in less omits shell_quote calls for LESSCLOSE,...
Andrew Fletcher06 Mar 2024
Terminal command to find and replace
In many terminal text editors, you use find command as reference in Terminal commands - find. &nbsp;How about find and replace. &nbsp;This action depends on the specific text editor you're using in the terminal. &nbsp;Here are a few common terminal text editors and how you can find and replace...