Skip to content

Commit

Permalink
offer to install aider[hf]
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-gauthier committed Jul 10, 2024
1 parent 4e07710 commit f688c18
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 14 deletions.
11 changes: 9 additions & 2 deletions aider/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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
Expand Down Expand Up @@ -654,7 +654,14 @@ def cmd_help(self, args):
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,
Expand Down
31 changes: 28 additions & 3 deletions aider/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

Expand Down Expand Up @@ -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")
Expand Down
17 changes: 17 additions & 0 deletions aider/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import subprocess
import sys
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -176,3 +178,18 @@ def append_msg(role, lines):
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}")
1 change: 1 addition & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions requirements-hf.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
#

llama-index-embeddings-huggingface

# To retain python 3.9 compatibility
scipy<1.14

2 changes: 1 addition & 1 deletion requirements-hf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

configargparse
GitPython
openai
tiktoken
jsonschema
rich
prompt_toolkit
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ numpy==1.26.4
# pandas
# pyarrow
# pydeck
# scipy
# streamlit
openai==1.35.10
# via
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f688c18

Please sign in to comment.