Skip to content

Commit 12de1d1

Browse files
build: introduce uv as Python package manager (#16317)
Co-authored-by: QuantumGhost <[email protected]>
1 parent f27a956 commit 12de1d1

File tree

23 files changed

+6568
-10983
lines changed

23 files changed

+6568
-10983
lines changed

.devcontainer/post_create_command.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
44
cd web && pnpm install
5-
pipx install poetry
5+
pipx install uv
66

7-
echo 'alias start-api="cd /workspaces/dify/api && poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug"' >> ~/.bashrc
8-
echo 'alias start-worker="cd /workspaces/dify/api && poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion"' >> ~/.bashrc
7+
echo 'alias start-api="cd /workspaces/dify/api && uv run python -m flask run --host 0.0.0.0 --port=5001 --debug"' >> ~/.bashrc
8+
echo 'alias start-worker="cd /workspaces/dify/api && uv run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion"' >> ~/.bashrc
99
echo 'alias start-web="cd /workspaces/dify/web && pnpm dev"' >> ~/.bashrc
1010
echo 'alias start-containers="cd /workspaces/dify/docker && docker-compose -f docker-compose.middleware.yaml -p dify --env-file middleware.env up -d"' >> ~/.bashrc
1111
echo 'alias stop-containers="cd /workspaces/dify/docker && docker-compose -f docker-compose.middleware.yaml -p dify --env-file middleware.env down"' >> ~/.bashrc
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/bash
22

3-
cd api && poetry install
3+
cd api && uv sync

.github/actions/setup-poetry/action.yml

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Setup UV and Python
2+
3+
inputs:
4+
python-version:
5+
description: Python version to use and the UV installed with
6+
required: true
7+
default: '3.12'
8+
uv-version:
9+
description: UV version to set up
10+
required: true
11+
default: '0.6.14'
12+
uv-lockfile:
13+
description: Path to the UV lockfile to restore cache from
14+
required: true
15+
default: ''
16+
enable-cache:
17+
required: true
18+
default: true
19+
20+
runs:
21+
using: composite
22+
steps:
23+
- name: Set up Python ${{ inputs.python-version }}
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: ${{ inputs.python-version }}
27+
28+
- name: Install uv
29+
uses: astral-sh/setup-uv@v5
30+
with:
31+
version: ${{ inputs.uv-version }}
32+
python-version: ${{ inputs.python-version }}
33+
enable-cache: ${{ inputs.enable-cache }}
34+
cache-dependency-glob: ${{ inputs.uv-lockfile }}

.github/workflows/api-tests.yml

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
test:
1818
name: API Tests
1919
runs-on: ubuntu-latest
20+
defaults:
21+
run:
22+
shell: bash
2023
strategy:
2124
matrix:
2225
python-version:
@@ -30,37 +33,32 @@ jobs:
3033
fetch-depth: 0
3134
persist-credentials: false
3235

33-
- name: Setup Poetry and Python ${{ matrix.python-version }}
34-
uses: ./.github/actions/setup-poetry
36+
- name: Setup UV and Python
37+
uses: ./.github/actions/setup-uv
3538
with:
3639
python-version: ${{ matrix.python-version }}
37-
poetry-lockfile: api/poetry.lock
40+
uv-lockfile: api/uv.lock
3841

39-
- name: Check Poetry lockfile
40-
run: |
41-
poetry check -C api --lock
42-
poetry show -C api
42+
- name: Check UV lockfile
43+
run: uv lock --project api --check
4344

4445
- name: Install dependencies
45-
run: poetry install -C api --with dev
46-
47-
- name: Check dependencies in pyproject.toml
48-
run: poetry run -P api bash dev/pytest/pytest_artifacts.sh
46+
run: uv sync --project api --group dev
4947

5048
- name: Run Unit tests
51-
run: poetry run -P api bash dev/pytest/pytest_unit_tests.sh
49+
run: uv run --project api bash dev/pytest/pytest_unit_tests.sh
5250

5351
- name: Run dify config tests
54-
run: poetry run -P api python dev/pytest/pytest_config_tests.py
52+
run: uv run --project api dev/pytest/pytest_config_tests.py
5553

56-
- name: Cache MyPy
54+
- name: MyPy Cache
5755
uses: actions/cache@v4
5856
with:
5957
path: api/.mypy_cache
60-
key: mypy-${{ matrix.python-version }}-${{ runner.os }}-${{ hashFiles('api/poetry.lock') }}
58+
key: mypy-${{ matrix.python-version }}-${{ runner.os }}-${{ hashFiles('api/uv.lock') }}
6159

62-
- name: Run mypy
63-
run: dev/run-mypy
60+
- name: Run MyPy Checks
61+
run: dev/mypy-check
6462

6563
- name: Set up dotenvs
6664
run: |
@@ -80,4 +78,4 @@ jobs:
8078
ssrf_proxy
8179
8280
- name: Run Workflow
83-
run: poetry run -P api bash dev/pytest/pytest_workflow.sh
81+
run: uv run --project api bash dev/pytest/pytest_workflow.sh

.github/workflows/db-migration-test.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ jobs:
2424
fetch-depth: 0
2525
persist-credentials: false
2626

27-
- name: Setup Poetry and Python
28-
uses: ./.github/actions/setup-poetry
27+
- name: Setup UV and Python
28+
uses: ./.github/actions/setup-uv
2929
with:
30-
poetry-lockfile: api/poetry.lock
30+
uv-lockfile: api/uv.lock
3131

3232
- name: Install dependencies
33-
run: poetry install -C api
33+
run: uv sync --project api
3434

3535
- name: Prepare middleware env
3636
run: |
@@ -54,6 +54,4 @@ jobs:
5454
- name: Run DB Migration
5555
env:
5656
DEBUG: true
57-
run: |
58-
cd api
59-
poetry run python -m flask upgrade-db
57+
run: uv run --directory api flask upgrade-db

.github/workflows/docker-build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
with:
4343
push: false
4444
context: "{{defaultContext}}:${{ matrix.context }}"
45+
file: "${{ matrix.file }}"
4546
platforms: ${{ matrix.platform }}
4647
cache-from: type=gha
4748
cache-to: type=gha,mode=max

.github/workflows/style.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,27 @@ jobs:
2929
api/**
3030
.github/workflows/style.yml
3131
32-
- name: Setup Poetry and Python
32+
- name: Setup UV and Python
3333
if: steps.changed-files.outputs.any_changed == 'true'
34-
uses: ./.github/actions/setup-poetry
34+
uses: ./.github/actions/setup-uv
35+
with:
36+
uv-lockfile: api/uv.lock
37+
enable-cache: false
3538

3639
- name: Install dependencies
3740
if: steps.changed-files.outputs.any_changed == 'true'
38-
run: poetry install -C api --only lint
41+
run: uv sync --project api --only-group lint
3942

4043
- name: Ruff check
4144
if: steps.changed-files.outputs.any_changed == 'true'
4245
run: |
43-
poetry run -C api ruff --version
44-
poetry run -C api ruff check ./
45-
poetry run -C api ruff format --check ./
46+
uv run --directory api ruff --version
47+
uv run --directory api ruff check ./
48+
uv run --directory api ruff format --check ./
4649
4750
- name: Dotenv check
4851
if: steps.changed-files.outputs.any_changed == 'true'
49-
run: poetry run -P api dotenv-linter ./api/.env.example ./web/.env.example
52+
run: uv run --project api dotenv-linter ./api/.env.example ./web/.env.example
5053

5154
- name: Lint hints
5255
if: failure()

.github/workflows/vdb-tests.yml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
- api/core/rag/datasource/**
99
- docker/**
1010
- .github/workflows/vdb-tests.yml
11-
- api/poetry.lock
11+
- api/uv.lock
1212
- api/pyproject.toml
1313

1414
concurrency:
@@ -32,19 +32,17 @@ jobs:
3232
fetch-depth: 0
3333
persist-credentials: false
3434

35-
- name: Setup Poetry and Python ${{ matrix.python-version }}
36-
uses: ./.github/actions/setup-poetry
35+
- name: Setup UV and Python
36+
uses: ./.github/actions/setup-uv
3737
with:
3838
python-version: ${{ matrix.python-version }}
39-
poetry-lockfile: api/poetry.lock
39+
uv-lockfile: api/uv.lock
4040

41-
- name: Check Poetry lockfile
42-
run: |
43-
poetry check -C api --lock
44-
poetry show -C api
41+
- name: Check UV lockfile
42+
run: uv lock --project api --check
4543

4644
- name: Install dependencies
47-
run: poetry install -C api --with dev
45+
run: uv sync --project api --group dev
4846

4947
- name: Set up dotenvs
5048
run: |
@@ -80,7 +78,7 @@ jobs:
8078
elasticsearch
8179
8280
- name: Check TiDB Ready
83-
run: poetry run -P api python api/tests/integration_tests/vdb/tidb_vector/check_tiflash_ready.py
81+
run: uv run --project api python api/tests/integration_tests/vdb/tidb_vector/check_tiflash_ready.py
8482

8583
- name: Test Vector Stores
86-
run: poetry run -P api bash dev/pytest/pytest_vdb.sh
84+
run: uv run --project api bash dev/pytest/pytest_vdb.sh

api/Dockerfile

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,11 @@ FROM python:3.12-slim-bookworm AS base
33

44
WORKDIR /app/api
55

6-
# Install Poetry
7-
ENV POETRY_VERSION=2.0.1
6+
# Install uv
7+
ENV UV_VERSION=0.6.14
88

9-
# if you located in China, you can use aliyun mirror to speed up
10-
# RUN pip install --no-cache-dir poetry==${POETRY_VERSION} -i https://mirrors.aliyun.com/pypi/simple/
11-
12-
RUN pip install --no-cache-dir poetry==${POETRY_VERSION}
9+
RUN pip install --no-cache-dir uv==${UV_VERSION}
1310

14-
# Configure Poetry
15-
ENV POETRY_CACHE_DIR=/tmp/poetry_cache
16-
ENV POETRY_NO_INTERACTION=1
17-
ENV POETRY_VIRTUALENVS_IN_PROJECT=true
18-
ENV POETRY_VIRTUALENVS_CREATE=true
19-
ENV POETRY_REQUESTS_TIMEOUT=15
2011

2112
FROM base AS packages
2213

@@ -27,8 +18,8 @@ RUN apt-get update \
2718
&& apt-get install -y --no-install-recommends gcc g++ libc-dev libffi-dev libgmp-dev libmpfr-dev libmpc-dev
2819

2920
# Install Python dependencies
30-
COPY pyproject.toml poetry.lock ./
31-
RUN poetry install --sync --no-cache --no-root
21+
COPY pyproject.toml uv.lock ./
22+
RUN uv sync --locked
3223

3324
# production stage
3425
FROM base AS production

0 commit comments

Comments
 (0)