From 000031f7bb6905b655ebcb6a582738b4d19110f5 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Sun, 30 Jun 2024 20:35:47 -1000 Subject: [PATCH] Add example docker file for building agents (#401) * Add example docker file for building agents * fix ruff errors * use entrypoint * one more type * fix typecheck workflow * more types --- .github/workflows/check-types.yml | 85 +++++++++++-------- examples/Dockerfile-example | 49 +++++++++++ livekit-agents/livekit/agents/cli/log.py | 2 +- .../livekit/agents/llm/function_context.py | 2 +- 4 files changed, 100 insertions(+), 38 deletions(-) create mode 100644 examples/Dockerfile-example diff --git a/.github/workflows/check-types.yml b/.github/workflows/check-types.yml index d446a8756..409c5cc4b 100644 --- a/.github/workflows/check-types.yml +++ b/.github/workflows/check-types.yml @@ -1,4 +1,4 @@ -name: Check Types +name: Check Types on: push: @@ -16,39 +16,52 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - with: - submodules: recursive - - - uses: actions/setup-python@v5 - with: - python-version: '3.9' - cache: 'pip' - - - name: Install mypy - run: python -m pip install --upgrade mypy - - - name: Install all packages - run: | - pip install ./livekit-agents \ - ./livekit-plugins/livekit-plugins-openai \ - ./livekit-plugins/livekit-plugins-deepgram \ - ./livekit-plugins/livekit-plugins-google \ - ./livekit-plugins/livekit-plugins-nltk \ - ./livekit-plugins/livekit-plugins-silero \ - ./livekit-plugins/livekit-plugins-elevenlabs \ - ./livekit-plugins/livekit-plugins-cartesia \ - ./livekit-plugins/livekit-plugins-azure - - name: Check Types - run : | - mypy --install-types --non-interactive \ - -p livekit.agents \ - -p livekit.plugins.openai \ - -p livekit.plugins.deepgram \ - -p livekit.plugins.google \ - -p livekit.plugins.nltk \ - -p livekit.plugins.silero \ - -p livekit.plugins.elevenlabs \ - -p livekit.plugins.cartesia \ - -p livekit.plugins.azure + - uses: actions/checkout@v3 + with: + submodules: recursive + - uses: actions/setup-python@v5 + with: + python-version: "3.9" + cache: "pip" + + - name: Install mypy + run: python -m pip install --upgrade mypy + + - name: Install all packages + run: | + pip install ./livekit-agents \ + ./livekit-plugins/livekit-plugins-openai \ + ./livekit-plugins/livekit-plugins-deepgram \ + ./livekit-plugins/livekit-plugins-google \ + ./livekit-plugins/livekit-plugins-nltk \ + ./livekit-plugins/livekit-plugins-silero \ + ./livekit-plugins/livekit-plugins-elevenlabs \ + ./livekit-plugins/livekit-plugins-cartesia \ + ./livekit-plugins/livekit-plugins-azure + + - name: Install stub packages + run: | + pip install \ + pandas-stubs \ + types-Pygments \ + types-cachetools \ + types-cffi \ + types-psutil \ + types-pyOpenSSL \ + types-requests \ + types-openpyxl \ + types-requests + + - name: Check Types + run: | + mypy --install-types --non-interactive \ + -p livekit.agents \ + -p livekit.plugins.openai \ + -p livekit.plugins.deepgram \ + -p livekit.plugins.google \ + -p livekit.plugins.nltk \ + -p livekit.plugins.silero \ + -p livekit.plugins.elevenlabs \ + -p livekit.plugins.cartesia \ + -p livekit.plugins.azure diff --git a/examples/Dockerfile-example b/examples/Dockerfile-example new file mode 100644 index 000000000..994fa428e --- /dev/null +++ b/examples/Dockerfile-example @@ -0,0 +1,49 @@ +# This is an example Dockerfile that builds a minimal container for running LK Agents +# syntax=docker/dockerfile:1 +ARG PYTHON_VERSION=3.11.6 +FROM python:${PYTHON_VERSION}-slim + +# Prevents Python from writing pyc files. +ENV PYTHONDONTWRITEBYTECODE=1 + +# Keeps Python from buffering stdout and stderr to avoid situations where +# the application crashes without emitting any logs due to buffering. +ENV PYTHONUNBUFFERED=1 + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/home/appuser" \ + --shell "/sbin/nologin" \ + --uid "${UID}" \ + appuser + + +# Install gcc and other build dependencies. +RUN apt-get update && \ + apt-get install -y \ + gcc \ + python3-dev \ + && rm -rf /var/lib/apt/lists/* + +USER appuser + +RUN mkdir -p /home/appuser/.cache +RUN chown -R appuser /home/appuser/.cache + +WORKDIR /home/appuser + +COPY requirements.txt . +RUN python -m pip install --user --no-cache-dir -r requirements.txt + +COPY . . + +# ensure that any dependent models are downloaded at build-time +RUN python myagent.py download-files + +# Run the application. +ENTRYPOINT ["python", "myagent.py"] +CMD ["start"] diff --git a/livekit-agents/livekit/agents/cli/log.py b/livekit-agents/livekit/agents/cli/log.py index 356ab8809..bd7c2682c 100644 --- a/livekit-agents/livekit/agents/cli/log.py +++ b/livekit-agents/livekit/agents/cli/log.py @@ -70,7 +70,7 @@ def default(self, o: Any): return o.isoformat() elif istraceback(o): return "".join(traceback.format_tb(o)).strip() - elif type(o) == Exception or isinstance(o, Exception) or type(o) == type: + elif type(o) is Exception or isinstance(o, Exception) or type(o) is type: return str(o) # extra values are formatted as str() if the encoder raises TypeError diff --git a/livekit-agents/livekit/agents/llm/function_context.py b/livekit-agents/livekit/agents/llm/function_context.py index aaeafdbb1..a189275b4 100644 --- a/livekit-agents/livekit/agents/llm/function_context.py +++ b/livekit-agents/livekit/agents/llm/function_context.py @@ -219,7 +219,7 @@ def is_type_supported(t: type) -> bool: for e in t: if initial_type is None: initial_type = type(e.value) - if type(e.value) != initial_type: + if type(e.value) is not initial_type: return False return initial_type in (str, int)