Skip to content

Commit

Permalink
Add unit test script for chat app
Browse files Browse the repository at this point in the history
  • Loading branch information
sd109 committed Nov 13, 2024
1 parent 3a00c21 commit f520480
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 60 deletions.
15 changes: 0 additions & 15 deletions web-apps/build.sh

This file was deleted.

10 changes: 7 additions & 3 deletions web-apps/chat/defaults.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@

model_name:
model_instruction: "You are a helpful and cheerful AI assistant. Please respond appropriately."
backend_url:
# Default target is a local ollama instance
# running inside the same docker network
model_name: smollm2:135m
backend_url: http://ollama:11434

host_address: 0.0.0.0

model_instruction: "You are a helpful and cheerful AI assistant. Please respond appropriately."

page_title: Large Language Model

# LLM request parameters
Expand Down
23 changes: 23 additions & 0 deletions web-apps/chat/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
import unittest

# from unittest import mock
from gradio_client import Client

url = os.environ.get("GRADIO_URL", "http://localhost:7860")
client = Client(url)

class TestSuite(unittest.TestCase):

def test_gradio_api(self):
result = client.predict("Hi", api_name="/chat")
self.assertGreater(len(result), 0)

# def test_mock_response(self):
# with mock.patch('app.client.stream_response', return_value=(char for char in "Mocked")) as mock_response:
# result = client.predict("Hi", api_name="/chat")
# # mock_response.assert_called_once_with("Hi", [])
# self.assertEqual(result, "Mocked")

if __name__ == "__main__":
unittest.main()
20 changes: 0 additions & 20 deletions web-apps/functions.sh

This file was deleted.

10 changes: 9 additions & 1 deletion web-apps/kind-images.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
set -e

source functions.sh
find_images() {
images=""
for dir in $(ls $1); do
if [[ -f $1/$dir/Dockerfile ]]; then
images+="$dir "
fi
done
echo $images
}

if [[ -z $1 ]]; then
echo "Published image tag must be provided as sole command line arg"
Expand Down
21 changes: 0 additions & 21 deletions web-apps/run.sh

This file was deleted.

93 changes: 93 additions & 0 deletions web-apps/test-images.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash
set -e

IMAGE_TAG="${1:-latest}"
echo Testing image tag $IMAGE_TAG

find_images() {
images=""
for dir in $(ls $1); do
if [[ -f $1/$dir/Dockerfile ]]; then
images+="$dir "
fi
done
echo $images
}

image_name() {
echo ghcr.io/stackhpc/azimuth-llm-$1-ui
}

build() {
if [[ -f $1/Dockerfile ]]; then
echo Building $1 docker image
docker build . -t $(image_name $1) -f $1/Dockerfile
else
echo No Dockerfile found for $1
exit 1
fi
}

log () {
echo
echo $@
}

test() {

echo
echo "----- Starting test process for $1 app -----"
echo

DOCKER_NET_NAME=azimuth-llm-shared
if [[ ! $(docker network ls | grep $DOCKER_NET_NAME) ]]; then
docker network create $DOCKER_NET_NAME
fi

if [[ -f $1/test.py ]]; then

# Ensure app image is available
IMAGE_NAME=$(image_name $1)
if [[ $IMAGE_TAG == "latest" ]]; then
build $1
else
log "Pulling image $IMAGE_NAME:$IMAGE_TAG"
docker pull $IMAGE_NAME:$IMAGE_TAG
fi

# Ensure Ollama instance is available
if [[ $(curl -s localhost:11434) == "Ollama is running" ]]; then
log "Using existing ollama process running on localhost:11434"
else
log "Ollama process not running - starting containerised server"
docker run --rm --network $DOCKER_NET_NAME -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
sleep 3
docker exec -it ollama ollama pull smollm2:135m
fi

log "Starting Gradio app container"
docker run --network $DOCKER_NET_NAME -d --name $1-app $IMAGE_NAME

# Give the app time to start
sleep 3

log "Running tests"
docker run --network $DOCKER_NET_NAME --rm --name $1-test-suite -e GRADIO_URL=http://$1-app:7860 --entrypoint python $IMAGE_NAME test.py

log "Removing containers:"
docker rm -f ollama $1-app

log "Removing docker network:"
docker network rm $DOCKER_NET_NAME

echo
echo "----- Tests succeed -----"
echo
else
echo No test.py file found for $1 app
fi
}

for image in $(find_images .); do
test $image
done

0 comments on commit f520480

Please sign in to comment.