π OpenHands: AI-Driven Development
[Runtime] Reduce dependency to speed up CI and reduce image size (#3195)
Xingyao Wang1d49ef253ba0
diff --git a/.github/workflows/dummy-agent-test.yml b/.github/workflows/dummy-agent-test.ymlindex 8422f0c361..b13eaeb184 100644--- a/.github/workflows/dummy-agent-test.yml+++ b/.github/workflows/dummy-agent-test.yml@@ -25,7 +25,7 @@ jobs:- name: Set up environmentrun: |curl -sSL https://install.python-poetry.org | python3 -- poetry install --without evaluation+ poetry install --without evaluation,llama-indexpoetry run playwright install --with-deps chromiumwget https://huggingface.co/BAAI/bge-small-en-v1.5/raw/main/1_Pooling/config.json -P /tmp/llama_index/models--BAAI--bge-small-en-v1.5/snapshots/5c38ec7c405ec4b44b94cc5a9bb96e735b38267a/1_Pooling/- name: Run testsdiff --git a/.github/workflows/review-pr.yml b/.github/workflows/review-pr.ymlindex f38c65b832..8a6c295397 100644--- a/.github/workflows/review-pr.yml+++ b/.github/workflows/review-pr.yml@@ -50,7 +50,7 @@ jobs:run: |curl -sSL https://install.python-poetry.org | python3 -export PATH="/github/home/.local/bin:$PATH"- poetry install --without evaluation+ poetry install --without evaluation,llama-indexpoetry run playwright install --with-deps chromium- name: Run OpenDevindiff --git a/.github/workflows/run-unit-tests.yml b/.github/workflows/run-unit-tests.ymlindex 5fb79fc1f9..eb58070b29 100644--- a/.github/workflows/run-unit-tests.yml+++ b/.github/workflows/run-unit-tests.yml@@ -70,7 +70,7 @@ jobs:cache: "poetry"- name: Install Python dependencies using Poetry- run: poetry install+ run: poetry install --without evaluation,llama-index- name: Install & Start Dockerif: env.INSTALL_DOCKER == '1'@@ -153,7 +153,7 @@ jobs:cache: "poetry"- name: Install Python dependencies using Poetry- run: poetry install --without evaluation+ run: poetry install --without evaluation,llama-index- name: Build Environmentrun: make builddiff --git a/.github/workflows/solve-issue.yml b/.github/workflows/solve-issue.ymlindex df965b9554..5d86e52b5a 100644--- a/.github/workflows/solve-issue.yml+++ b/.github/workflows/solve-issue.yml@@ -40,7 +40,7 @@ jobs:run: |curl -sSL https://install.python-poetry.org | python3 -export PATH="/github/home/.local/bin:$PATH"- poetry install --without evaluation+ poetry install --without evaluation,llama-indexpoetry run playwright install --with-deps chromiumdiff --git a/Makefile b/Makefileindex 2e06fa01d2..8472d234a1 100644--- a/Makefile+++ b/Makefile@@ -141,7 +141,7 @@ install-python-dependencies:export HNSWLIB_NO_NATIVE=1; \poetry run pip install chroma-hnswlib; \fi- @poetry install+ @poetry install --without llama-index@if [ -f "/etc/manjaro-release" ]; then \echo "$(BLUE)Detected Manjaro Linux. Installing Playwright dependencies...$(RESET)"; \poetry run pip install playwright; \diff --git a/containers/app/Dockerfile b/containers/app/Dockerfileindex 198a30708d..d4aa2b38af 100644--- a/containers/app/Dockerfile+++ b/containers/app/Dockerfile@@ -26,7 +26,7 @@ RUN apt-get update -y \COPY ./pyproject.toml ./poetry.lock ./RUN touch README.md-RUN export POETRY_CACHE_DIR && poetry install --without evaluation --no-root && rm -rf $POETRY_CACHE_DIR+RUN export POETRY_CACHE_DIR && poetry install --without evaluation,llama-index --no-root && rm -rf $POETRY_CACHE_DIRFROM python:3.12.3-slim AS runtimediff --git a/opendevin/memory/memory.py b/opendevin/memory/memory.pyindex a175c36d3f..f000060cc0 100644--- a/opendevin/memory/memory.py+++ b/opendevin/memory/memory.py@@ -1,10 +1,5 @@import threading-import chromadb-import llama_index.embeddings.openai.base as llama_openai-from llama_index.core import Document, VectorStoreIndex-from llama_index.core.retrievers import VectorIndexRetriever-from llama_index.vector_stores.chroma import ChromaVectorStorefrom openai._exceptions import APIConnectionError, InternalServerError, RateLimitErrorfrom tenacity import (retry,@@ -17,94 +12,102 @@ from opendevin.core.config import LLMConfigfrom opendevin.core.logger import opendevin_logger as loggerfrom opendevin.core.utils import json-# TODO: this could be made configurable-num_retries: int = 10-retry_min_wait: int = 3-retry_max_wait: int = 300--# llama-index includes a retry decorator around openai.get_embeddings() function-# it is initialized with hard-coded values and errors-# this non-customizable behavior is creating issues when it's retrying faster than providers' rate limits-# this block attempts to banish it and replace it with our decorator, to allow users to set their own limits--if hasattr(llama_openai.get_embeddings, '__wrapped__'):- original_get_embeddings = llama_openai.get_embeddings.__wrapped__-else:- logger.warning('Cannot set custom retry limits.')- num_retries = 1- original_get_embeddings = llama_openai.get_embeddings---def attempt_on_error(retry_state):- logger.error(- f'{retry_state.outcome.exception()}. Attempt #{retry_state.attempt_number} | You can customize these settings in the configuration.',- exc_info=False,+try:+ import chromadb+ import llama_index.embeddings.openai.base as llama_openai+ from llama_index.core import Document, VectorStoreIndex+ from llama_index.core.retrievers import VectorIndexRetriever+ from llama_index.vector_stores.chroma import ChromaVectorStore++ LLAMA_INDEX_AVAILABLE = True+except ImportError:+ LLAMA_INDEX_AVAILABLE = False++if LLAMA_INDEX_AVAILABLE:+ # TODO: this could be made configurable+ num_retries: int = 10+ retry_min_wait: int = 3+ retry_max_wait: int = 300++ # llama-index includes a retry decorator around openai.get_embeddings() function+ # it is initialized with hard-coded values and errors+ # this non-customizable behavior is creating issues when it's retrying faster than providers' rate limits+ # this block attempts to banish it and replace it with our decorator, to allow users to set their own limits++ if hasattr(llama_openai.get_embeddings, '__wrapped__'):+ original_get_embeddings = llama_openai.get_embeddings.__wrapped__+ else:+ logger.warning('Cannot set custom retry limits.')+ num_retries = 1+ original_get_embeddings = llama_openai.get_embeddings++ def attempt_on_error(retry_state):+ logger.error(+ f'{retry_state.outcome.exception()}. Attempt #{retry_state.attempt_number} | You can customize these settings in the configuration.',+ exc_info=False,+ )+ return None++ @retry(+ reraise=True,+ stop=stop_after_attempt(num_retries),+ wait=wait_random_exponential(min=retry_min_wait, max=retry_max_wait),+ retry=retry_if_exception_type(+ (RateLimitError, APIConnectionError, InternalServerError)+ ),+ after=attempt_on_error,)- return None---@retry(- reraise=True,- stop=stop_after_attempt(num_retries),- wait=wait_random_exponential(min=retry_min_wait, max=retry_max_wait),- retry=retry_if_exception_type(- (RateLimitError, APIConnectionError, InternalServerError)- ),- after=attempt_on_error,-)-def wrapper_get_embeddings(*args, **kwargs):- return original_get_embeddings(*args, **kwargs)---llama_openai.get_embeddings = wrapper_get_embeddings---class EmbeddingsLoader:- """Loader for embedding model initialization."""-- @staticmethod- def get_embedding_model(strategy: str, llm_config: LLMConfig):- supported_ollama_embed_models = [- 'llama2',- 'mxbai-embed-large',- 'nomic-embed-text',- 'all-minilm',- 'stable-code',- ]- if strategy in supported_ollama_embed_models:- from llama_index.embeddings.ollama import OllamaEmbedding-- return OllamaEmbedding(- model_name=strategy,- base_url=llm_config.embedding_base_url,- ollama_additional_kwargs={'mirostat': 0},- )- elif strategy == 'openai':- from llama_index.embeddings.openai import OpenAIEmbedding-- return OpenAIEmbedding(- model='text-embedding-ada-002',- api_key=llm_config.api_key,- )- elif strategy == 'azureopenai':- from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding-- return AzureOpenAIEmbedding(- model='text-embedding-ada-002',- deployment_name=llm_config.embedding_deployment_name,- api_key=llm_config.api_key,- azure_endpoint=llm_config.base_url,- api_version=llm_config.api_version,- )- elif (strategy is not None) and (strategy.lower() == 'none'):- # TODO: this works but is not elegant enough. The incentive is when- # an agent using embeddings is not used, there is no reason we need to- # initialize an embedding model- return None- else:- from llama_index.embeddings.huggingface import HuggingFaceEmbedding-- return HuggingFaceEmbedding(model_name='BAAI/bge-small-en-v1.5')+ def wrapper_get_embeddings(*args, **kwargs):+ return original_get_embeddings(*args, **kwargs)++ llama_openai.get_embeddings = wrapper_get_embeddings++ class EmbeddingsLoader:+ """Loader for embedding model initialization."""++ @staticmethod+ def get_embedding_model(strategy: str, llm_config: LLMConfig):+ supported_ollama_embed_models = [+ 'llama2',+ 'mxbai-embed-large',+ 'nomic-embed-text',+ 'all-minilm',+ 'stable-code',+ ]+ if strategy in supported_ollama_embed_models:+ from llama_index.embeddings.ollama import OllamaEmbedding++ return OllamaEmbedding(+ model_name=strategy,+ base_url=llm_config.embedding_base_url,+ ollama_additional_kwargs={'mirostat': 0},+ )+ elif strategy == 'openai':+ from llama_index.embeddings.openai import OpenAIEmbedding++ return OpenAIEmbedding(+ model='text-embedding-ada-002',+ api_key=llm_config.api_key,+ )+ elif strategy == 'azureopenai':+ from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding++ return AzureOpenAIEmbedding(+ model='text-embedding-ada-002',+ deployment_name=llm_config.embedding_deployment_name,+ api_key=llm_config.api_key,+ azure_endpoint=llm_config.base_url,+ api_version=llm_config.api_version,+ )+ elif (strategy is not None) and (strategy.lower() == 'none'):+ # TODO: this works but is not elegant enough. The incentive is when+ # an agent using embeddings is not used, there is no reason we need to+ # initialize an embedding model+ return None+ else:+ from llama_index.embeddings.huggingface import HuggingFaceEmbedding++ return HuggingFaceEmbedding(model_name='BAAI/bge-small-en-v1.5')class LongTermMemory:@@ -112,6 +115,12 @@ class LongTermMemory:def __init__(self, llm_config: LLMConfig, memory_max_threads: int = 1):"""Initialize the chromadb and set up ChromaVectorStore for later use."""+ if not LLAMA_INDEX_AVAILABLE:+ raise ImportError(+ 'llama_index and its dependencies are not installed. '+ 'To use LongTermMemory, please run: poetry install --with llama-index'+ )+db = chromadb.Client(chromadb.Settings(anonymized_telemetry=False))self.collection = db.get_or_create_collection(name='memories')vector_store = ChromaVectorStore(chroma_collection=self.collection)diff --git a/opendevin/runtime/utils/runtime_templates/Dockerfile.j2 b/opendevin/runtime/utils/runtime_templates/Dockerfile.j2index fc15bda732..9855dfdcce 100644--- a/opendevin/runtime/utils/runtime_templates/Dockerfile.j2+++ b/opendevin/runtime/utils/runtime_templates/Dockerfile.j2@@ -56,7 +56,7 @@ RUN mv /opendevin/{{ source_code_dirname }} /opendevin/code# 3. Clear poetry, apt, mamba cachesRUN cd /opendevin/code && \/opendevin/miniforge3/bin/mamba run -n base poetry env use python3.11 && \- /opendevin/miniforge3/bin/mamba run -n base poetry install --no-interaction --no-root && \+ /opendevin/miniforge3/bin/mamba run -n base poetry install --only main,runtime --no-interaction --no-root && \apt-get update && \/opendevin/miniforge3/bin/mamba run -n base poetry run pip install playwright && \/opendevin/miniforge3/bin/mamba run -n base poetry run playwright install --with-deps chromium && \diff --git a/pyproject.toml b/pyproject.tomlindex 565a84643d..1bc3ca36a0 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -85,6 +85,7 @@ python-pptx = "*"pylatexenc = "*"opencv-python = "*"+[build-system]build-backend = "poetry.core.masonry.api"requires = [