From 6e1904934bf87f4e0505b1db4db9cb0dcb13062e Mon Sep 17 00:00:00 2001 From: Liora Milbaum Date: Sun, 24 Mar 2024 23:16:09 +0200 Subject: [PATCH 1/3] playground tests Signed-off-by: Liora Milbaum --- .devcontainer/Containerfile | 9 ++++ .devcontainer/devcontainer.json | 12 +++++ .github/workflows/playground.yaml | 48 +++++++++++++++++++ .gitignore | 1 + model_servers/llamacpp_python/README.md | 27 +++++++++-- .../llamacpp_python/base/Containerfile | 9 ++-- model_servers/llamacpp_python/base/Makefile | 18 +++++++ .../llamacpp_python/base/tests/__init__.py | 0 .../llamacpp_python/base/tests/conftest.py | 32 +++++++++++++ .../base/tests/requirements.txt | 6 +++ .../llamacpp_python/base/tests/test_alive.py | 13 +++++ .../llamacpp_python/src/requirements.txt | 3 +- 12 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 .devcontainer/Containerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .github/workflows/playground.yaml create mode 100644 model_servers/llamacpp_python/base/Makefile create mode 100644 model_servers/llamacpp_python/base/tests/__init__.py create mode 100644 model_servers/llamacpp_python/base/tests/conftest.py create mode 100644 model_servers/llamacpp_python/base/tests/requirements.txt create mode 100644 model_servers/llamacpp_python/base/tests/test_alive.py diff --git a/.devcontainer/Containerfile b/.devcontainer/Containerfile new file mode 100644 index 00000000..4ef78ce5 --- /dev/null +++ b/.devcontainer/Containerfile @@ -0,0 +1,9 @@ +FROM quay.io/containers/podman:v4.9.3 + +USER root + +COPY playground/tests/requirements.txt . + +RUN dnf install -y python3.11 python3-pip buildah git && \ + dnf clean all && \ + pip3 install -r requirements.txt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000..d9945aa9 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,12 @@ +{ + "name": "recepies", + "build": { + "dockerfile": "Containerfile", + "context": ".." + }, + "privileged": true, + "containerEnv": { + "REGISTRY": "ghcr.io", + "IMAGE_NAME": "ai-lab-recipes/playground" + } +} diff --git a/.github/workflows/playground.yaml b/.github/workflows/playground.yaml new file mode 100644 index 00000000..c377ea64 --- /dev/null +++ b/.github/workflows/playground.yaml @@ -0,0 +1,48 @@ +name: playground + +on: + pull_request: + branches: + - main + push: + branches: + - main + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository_owner }}/playground + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + services: + registry: + image: registry:2.8.3 + ports: + - 5000:5000 + steps: + - uses: actions/checkout@v4.1.1 + + - name: Login to ghcr + uses: docker/login-action@v3.1.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Buildah Action + uses: redhat-actions/buildah-build@v2.13 + with: + image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: latest + containerfiles: ./playground/Containerfile + context: playground + + - name: Set up Python + uses: actions/setup-python@v5.0.0 + + - name: Run tests + run: make -f playground/Makefile test diff --git a/.gitignore b/.gitignore index 3c2129ef..50cf3e3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.gguf *.bin *_pycache_* +port_check.lock diff --git a/model_servers/llamacpp_python/README.md b/model_servers/llamacpp_python/README.md index cdc8caae..3a5ca62d 100644 --- a/model_servers/llamacpp_python/README.md +++ b/model_servers/llamacpp_python/README.md @@ -3,7 +3,11 @@ From this directory, ```bash -podman build -t playground:image . +podman build -t playground . +``` +or +```bash +make -f Containerfile build ``` ### Download Model @@ -20,6 +24,10 @@ cd ../models wget cd ../ ``` +or +```bash +make -f Containerfile download +``` ### Deploy Model Service @@ -34,7 +42,11 @@ podman run --rm -it -d \ -e MODEL_PATH=models/ \ -e HOST=0.0.0.0 \ -e PORT=8001 \ - playground:image` + playground` +``` +or +```bash +make -f Containerfile run ``` #### Multiple Model Service: @@ -68,5 +80,14 @@ podman run --rm -it -d \ -p 8001:8001 \ -v Local/path/to/locallm/models:/locallm/models:ro,Z \ -e CONFIG_PATH=models/ \ - playground:image + playground +``` + +### DEV environment + +The environment is implemented with devcontainer technology. + +Running tests +```bash +make -f Containerfile test ``` diff --git a/model_servers/llamacpp_python/base/Containerfile b/model_servers/llamacpp_python/base/Containerfile index de459fb9..60b76000 100644 --- a/model_servers/llamacpp_python/base/Containerfile +++ b/model_servers/llamacpp_python/base/Containerfile @@ -1,7 +1,8 @@ -FROM registry.access.redhat.com/ubi9/python-39:latest +FROM registry.access.redhat.com/ubi9/python-311:1-52 WORKDIR /locallm COPY src . -RUN pip install --upgrade pip -RUN pip install --no-cache-dir --upgrade -r /locallm/requirements.txt +COPY requirements.txt /locallm/requirements.txt +RUN pip install --no-cache-dir --verbose -r /locallm/requirements.txt +COPY run.sh run.sh EXPOSE 8001 -ENTRYPOINT [ "sh", "run.sh" ] \ No newline at end of file +ENTRYPOINT [ "sh", "run.sh" ] diff --git a/model_servers/llamacpp_python/base/Makefile b/model_servers/llamacpp_python/base/Makefile new file mode 100644 index 00000000..f50151b2 --- /dev/null +++ b/model_servers/llamacpp_python/base/Makefile @@ -0,0 +1,18 @@ +.PHONY: build +build: + podman build -f Containerfile -t ghcr.io/ai-lab-recipes/playground --format docker playground + +models/llama-2-7b-chat.Q5_K_S.gguf: + curl -s -S -L -f https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q5_K_S.gguf -z $@ -o $@.tmp && mv -f $@.tmp $@ 2>/dev/null || rm -f $@.tmp $@ + +.PHONY: download +download: + pip install -r tests/requirements.txt + +.PHONY: run +run: install models/llama-2-7b-chat.Q5_K_S.gguf + podman run -it -d -p 8001:8001 -v ./models:/locallm/models:ro,Z -e MODEL_PATH=models/llama-2-7b-chat.Q5_K_S.gguf -e HOST=0.0.0.0 -e PORT=8001 --net=host ghcr.io/redhat-et/playground + +.PHONY: test +test: models/llama-2-7b-chat.Q5_K_S.gguf download + pytest --log-cli-level NOTSET diff --git a/model_servers/llamacpp_python/base/tests/__init__.py b/model_servers/llamacpp_python/base/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/model_servers/llamacpp_python/base/tests/conftest.py b/model_servers/llamacpp_python/base/tests/conftest.py new file mode 100644 index 00000000..96deb7dd --- /dev/null +++ b/model_servers/llamacpp_python/base/tests/conftest.py @@ -0,0 +1,32 @@ +import pytest_container +import os + + +MS = pytest_container.Container( + url=f"containers-storage:{os.environ['REGISTRY']}/{os.environ['IMAGE_NAME']}", + volume_mounts=[ + pytest_container.container.BindMount( + container_path="/locallm/models", + host_path="./models", + flags=["ro"] + ) + ], + extra_environment_variables={ + "MODEL_PATH": "models/llama-2-7b-chat.Q5_K_S.gguf", + "HOST": "0.0.0.0", + "PORT": "8001" + }, + forwarded_ports=[ + pytest_container.PortForwarding( + container_port=8001, + host_port=8001 + ) + ], + extra_launch_args=["--net=host"] + ) + +def pytest_generate_tests(metafunc): + pytest_container.auto_container_parametrize(metafunc) + +def pytest_addoption(parser): + pytest_container.add_logging_level_options(parser) diff --git a/model_servers/llamacpp_python/base/tests/requirements.txt b/model_servers/llamacpp_python/base/tests/requirements.txt new file mode 100644 index 00000000..751d336d --- /dev/null +++ b/model_servers/llamacpp_python/base/tests/requirements.txt @@ -0,0 +1,6 @@ +pip==24.0 +pytest-container==0.4.0 +pytest-testinfra==10.1.0 +pytest==8.1.1 +requests==2.31.0 +tenacity==8.2.3 diff --git a/model_servers/llamacpp_python/base/tests/test_alive.py b/model_servers/llamacpp_python/base/tests/test_alive.py new file mode 100644 index 00000000..fcad510a --- /dev/null +++ b/model_servers/llamacpp_python/base/tests/test_alive.py @@ -0,0 +1,13 @@ +import pytest_container +from .conftest import MS +import tenacity + +CONTAINER_IMAGES = [MS] + + +def test_etc_os_release_present(auto_container: pytest_container.container.ContainerData): + assert auto_container.connection.file("/etc/os-release").exists + +@tenacity.retry(stop=tenacity.stop_after_attempt(5), wait=tenacity.wait_exponential()) +def test_alive(auto_container: pytest_container.container.ContainerData, host): + host.run_expect([0],f"curl http://localhost:{auto_container.forwarded_ports[0].host_port}",).stdout.strip() diff --git a/model_servers/llamacpp_python/src/requirements.txt b/model_servers/llamacpp_python/src/requirements.txt index bbea3dd8..a3ffc4c0 100644 --- a/model_servers/llamacpp_python/src/requirements.txt +++ b/model_servers/llamacpp_python/src/requirements.txt @@ -1 +1,2 @@ -llama-cpp-python[server] \ No newline at end of file +llama-cpp-python[server]==0.2.57 +pip==24.0 From a50c9dbdedfe113895b49b857e4fe076606c6e66 Mon Sep 17 00:00:00 2001 From: sallyom Date: Thu, 28 Mar 2024 10:52:21 -0400 Subject: [PATCH 2/3] update for restructure Signed-off-by: sallyom --- .devcontainer/Containerfile | 2 +- .devcontainer/devcontainer.json | 2 +- .github/workflows/build-images.yaml | 6 +++--- .github/workflows/playground.yaml | 6 +++--- model_servers/llamacpp_python/base/Makefile | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.devcontainer/Containerfile b/.devcontainer/Containerfile index 4ef78ce5..ccd4b0d6 100644 --- a/.devcontainer/Containerfile +++ b/.devcontainer/Containerfile @@ -2,7 +2,7 @@ FROM quay.io/containers/podman:v4.9.3 USER root -COPY playground/tests/requirements.txt . +COPY model_servers/llamacpp_python/base/tests/requirements.txt . RUN dnf install -y python3.11 python3-pip buildah git && \ dnf clean all && \ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d9945aa9..02bb0e80 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,5 +1,5 @@ { - "name": "recepies", + "name": "recipes", "build": { "dockerfile": "Containerfile", "context": ".." diff --git a/.github/workflows/build-images.yaml b/.github/workflows/build-images.yaml index f6e31081..7a8007ca 100644 --- a/.github/workflows/build-images.yaml +++ b/.github/workflows/build-images.yaml @@ -34,7 +34,7 @@ jobs: uses: tj-actions/changed-files@v42 with: files: | - playground/** + model_servers/llamacpp_python/base/** - name: Get changed rag files id: changed-files-rag @@ -96,8 +96,8 @@ jobs: image: ${{ env.MODEL_SERVICE_IMAGE }} tags: latest ${{ github.sha }} platforms: linux/amd64, linux/arm64 - context: playground - containerfiles: ./playground/Containerfile + context: model_servers/llamacpp_python/base + containerfiles: ./model_servers/llamacpp_python/base/Containerfile - name: Push model-service image id: push_model_service diff --git a/.github/workflows/playground.yaml b/.github/workflows/playground.yaml index c377ea64..58d77a9d 100644 --- a/.github/workflows/playground.yaml +++ b/.github/workflows/playground.yaml @@ -38,11 +38,11 @@ jobs: with: image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: latest - containerfiles: ./playground/Containerfile - context: playground + containerfiles: ./model_servers/llamacpp_python/base/Containerfile + context: model_servers/llamacpp_python/base - name: Set up Python uses: actions/setup-python@v5.0.0 - name: Run tests - run: make -f playground/Makefile test + run: make -f model_servers/llamacpp_python/base/Makefile test diff --git a/model_servers/llamacpp_python/base/Makefile b/model_servers/llamacpp_python/base/Makefile index f50151b2..a47bd328 100644 --- a/model_servers/llamacpp_python/base/Makefile +++ b/model_servers/llamacpp_python/base/Makefile @@ -1,6 +1,6 @@ .PHONY: build build: - podman build -f Containerfile -t ghcr.io/ai-lab-recipes/playground --format docker playground + podman build -f Containerfile -t ghcr.io/ai-lab-recipes/playground --format docker . models/llama-2-7b-chat.Q5_K_S.gguf: curl -s -S -L -f https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q5_K_S.gguf -z $@ -o $@.tmp && mv -f $@.tmp $@ 2>/dev/null || rm -f $@.tmp $@ From f31feca382ae8870958aface7f3f43ff97c60417 Mon Sep 17 00:00:00 2001 From: Liora Milbaum Date: Thu, 28 Mar 2024 18:07:33 +0200 Subject: [PATCH 3/3] s/Containerfile/Makefile/ typo fix Signed-off-by: Liora Milbaum --- .github/workflows/build-images.yaml | 2 +- .../{playground.yaml => llamacpp_python.yaml} | 4 ++-- model_servers/llamacpp_python/README.md | 8 ++++---- model_servers/llamacpp_python/base/Containerfile | 6 ++---- model_servers/llamacpp_python/base/Makefile | 10 +++++----- 5 files changed, 14 insertions(+), 16 deletions(-) rename .github/workflows/{playground.yaml => llamacpp_python.yaml} (93%) diff --git a/.github/workflows/build-images.yaml b/.github/workflows/build-images.yaml index 7a8007ca..3a29bf83 100644 --- a/.github/workflows/build-images.yaml +++ b/.github/workflows/build-images.yaml @@ -96,7 +96,7 @@ jobs: image: ${{ env.MODEL_SERVICE_IMAGE }} tags: latest ${{ github.sha }} platforms: linux/amd64, linux/arm64 - context: model_servers/llamacpp_python/base + context: model_servers/llamacpp_python containerfiles: ./model_servers/llamacpp_python/base/Containerfile - name: Push model-service image diff --git a/.github/workflows/playground.yaml b/.github/workflows/llamacpp_python.yaml similarity index 93% rename from .github/workflows/playground.yaml rename to .github/workflows/llamacpp_python.yaml index 58d77a9d..7a8dcd75 100644 --- a/.github/workflows/playground.yaml +++ b/.github/workflows/llamacpp_python.yaml @@ -1,4 +1,4 @@ -name: playground +name: llamacpp_python on: pull_request: @@ -39,7 +39,7 @@ jobs: image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: latest containerfiles: ./model_servers/llamacpp_python/base/Containerfile - context: model_servers/llamacpp_python/base + context: model_servers/llamacpp_python/ - name: Set up Python uses: actions/setup-python@v5.0.0 diff --git a/model_servers/llamacpp_python/README.md b/model_servers/llamacpp_python/README.md index 3a5ca62d..7925ec3a 100644 --- a/model_servers/llamacpp_python/README.md +++ b/model_servers/llamacpp_python/README.md @@ -7,7 +7,7 @@ podman build -t playground . ``` or ```bash -make -f Containerfile build +make -f Makefile build ``` ### Download Model @@ -26,7 +26,7 @@ cd ../ ``` or ```bash -make -f Containerfile download +make -f Makefile models/llama-2-7b-chat.Q5_K_S.gguf ``` ### Deploy Model Service @@ -46,7 +46,7 @@ podman run --rm -it -d \ ``` or ```bash -make -f Containerfile run +make -f Makefile run ``` #### Multiple Model Service: @@ -89,5 +89,5 @@ The environment is implemented with devcontainer technology. Running tests ```bash -make -f Containerfile test +make -f Makefile test ``` diff --git a/model_servers/llamacpp_python/base/Containerfile b/model_servers/llamacpp_python/base/Containerfile index 60b76000..e32290f2 100644 --- a/model_servers/llamacpp_python/base/Containerfile +++ b/model_servers/llamacpp_python/base/Containerfile @@ -1,8 +1,6 @@ FROM registry.access.redhat.com/ubi9/python-311:1-52 WORKDIR /locallm COPY src . -COPY requirements.txt /locallm/requirements.txt -RUN pip install --no-cache-dir --verbose -r /locallm/requirements.txt -COPY run.sh run.sh +RUN pip install --no-cache-dir --verbose -r ./requirements.txt EXPOSE 8001 -ENTRYPOINT [ "sh", "run.sh" ] +ENTRYPOINT [ "sh", "./run.sh" ] diff --git a/model_servers/llamacpp_python/base/Makefile b/model_servers/llamacpp_python/base/Makefile index a47bd328..dacbd569 100644 --- a/model_servers/llamacpp_python/base/Makefile +++ b/model_servers/llamacpp_python/base/Makefile @@ -5,14 +5,14 @@ build: models/llama-2-7b-chat.Q5_K_S.gguf: curl -s -S -L -f https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q5_K_S.gguf -z $@ -o $@.tmp && mv -f $@.tmp $@ 2>/dev/null || rm -f $@.tmp $@ -.PHONY: download -download: - pip install -r tests/requirements.txt +.PHONY: install +install: + pip install -r model_servers/llamacpp_python/base/tests/requirements.txt .PHONY: run -run: install models/llama-2-7b-chat.Q5_K_S.gguf +run: models/llama-2-7b-chat.Q5_K_S.gguf install podman run -it -d -p 8001:8001 -v ./models:/locallm/models:ro,Z -e MODEL_PATH=models/llama-2-7b-chat.Q5_K_S.gguf -e HOST=0.0.0.0 -e PORT=8001 --net=host ghcr.io/redhat-et/playground .PHONY: test -test: models/llama-2-7b-chat.Q5_K_S.gguf download +test: models/llama-2-7b-chat.Q5_K_S.gguf install pytest --log-cli-level NOTSET