From f688c18b65a48bf9f1a6ba79d2e45abacc3ef377 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 Jul 2024 16:20:06 +0100 Subject: [PATCH] offer to install aider[hf] --- aider/commands.py | 11 +++++++++-- aider/help.py | 31 ++++++++++++++++++++++++++++--- aider/utils.py | 17 +++++++++++++++++ docker/Dockerfile | 1 + requirements-hf.in | 4 ++++ requirements-hf.txt | 2 +- requirements.in | 5 ----- requirements.txt | 3 --- 8 files changed, 60 insertions(+), 14 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 36cc020ca..36801f5f8 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -7,7 +7,7 @@ from pathlib import Path import git from aider import models, prompts, voice -from aider.help import Help +from aider.help import Help, PipInstallHF from aider.llm import litellm from aider.scrape import Scraper from aider.utils import is_image_file @@ -654,7 +654,14 @@ class Commands: from aider.coders import Coder if not self.help: - self.help = Help() + try: + self.help = Help() + except PipInstallHF as err: + self.io.tool_error(str(err)) + if self.io.confirm_ask("Run pip install?", default="y"): + self.help = Help(pip_install=True) + else: + return coder = Coder.create( main_model=self.coder.main_model, diff --git a/aider/help.py b/aider/help.py index 693ee87c5..5596aca6e 100755 --- a/aider/help.py +++ b/aider/help.py @@ -6,7 +6,7 @@ from pathlib import Path import importlib_resources -from aider import __version__ +from aider import __version__, utils from aider.dump import dump # noqa: F401 from aider.help_pats import exclude_website_pats @@ -87,10 +87,35 @@ def get_index(): return index +class PipInstallHF(Exception): + pass + + +pip_install_cmd = [ + "aider[hf]", + "--extra-index-url", + "https://download.pytorch.org/whl/cpu", +] + +pip_install_error = f""" +To use interactive /help you need to install HuggingFace embeddings: + +pip install {' '.join(pip_install_cmd)} + +""" + + class Help: - def __init__(self): + def __init__(self, pip_install=False): + if pip_install: + utils.pip_install(pip_install_cmd) + from llama_index.core import Settings - from llama_index.embeddings.huggingface import HuggingFaceEmbedding + + try: + from llama_index.embeddings.huggingface import HuggingFaceEmbedding + except ImportError: + raise PipInstallHF(pip_install_error) os.environ["TOKENIZERS_PARALLELISM"] = "true" Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5") diff --git a/aider/utils.py b/aider/utils.py index 6d097fbe7..115d47f14 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -1,4 +1,6 @@ import os +import subprocess +import sys import tempfile from pathlib import Path @@ -176,3 +178,18 @@ def split_chat_history_markdown(text, include_tool=False): messages = [m for m in messages if m["role"] != "tool"] return messages + + +def pip_install(args): + cmd = [ + sys.executable, + "-m", + "pip", + "install", + ] + cmd += args + + try: + subprocess.run(cmd) + except subprocess.CalledProcessError as e: + print(f"Error running pip download: {e}") diff --git a/docker/Dockerfile b/docker/Dockerfile index 5b16484c5..17c977961 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -7,6 +7,7 @@ RUN apt-get update && \ COPY . /aider RUN pip install --no-cache-dir /aider +RUN pip install --no-cache-dir /aider[hf] --extra-index-url https://download.pytorch.org/whl/cpu # Final stage FROM python:3.10-slim diff --git a/requirements-hf.in b/requirements-hf.in index 51edb71d4..15ae10a73 100644 --- a/requirements-hf.in +++ b/requirements-hf.in @@ -4,3 +4,7 @@ # llama-index-embeddings-huggingface + +# To retain python 3.9 compatibility +scipy<1.14 + diff --git a/requirements-hf.txt b/requirements-hf.txt index 24c16124d..c3d824033 100644 --- a/requirements-hf.txt +++ b/requirements-hf.txt @@ -228,7 +228,7 @@ scikit-learn==1.5.1 # via sentence-transformers scipy==1.13.1 # via - # -c requirements.txt + # -r requirements-hf.in # scikit-learn # sentence-transformers sentence-transformers==3.0.1 diff --git a/requirements.in b/requirements.in index 169eeb698..1ba1f37c9 100644 --- a/requirements.in +++ b/requirements.in @@ -4,8 +4,6 @@ configargparse GitPython -openai -tiktoken jsonschema rich prompt_toolkit @@ -37,9 +35,6 @@ networkx<3.3 # v0.22.2 seems to break tree-sitter-languages? tree-sitter==0.21.3 -# To retain python 3.9 compatibility -scipy<1.14 - # GitHub Release action failing on "KeyError: 'home-page'" # https://github.com/pypa/twine/blob/6fbf880ee60915cf1666348c4bdd78a10415f2ac/twine/__init__.py#L40 # Uses importlib-metadata diff --git a/requirements.txt b/requirements.txt index 18ec70d3b..d674612cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -196,7 +196,6 @@ numpy==1.26.4 # pandas # pyarrow # pydeck - # scipy # streamlit openai==1.35.10 # via @@ -310,8 +309,6 @@ rpds-py==0.18.1 # referencing rsa==4.9 # via google-auth -scipy==1.13.1 - # via -r requirements.in six==1.16.0 # via python-dateutil smmap==5.0.1