Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e2ac0b7
uv
bowenliang123 Mar 20, 2025
27df775
nit
bowenliang123 Apr 9, 2025
6b4f534
nit
bowenliang123 Apr 9, 2025
189c09e
fix: Add missing dependencies
QuantumGhost Apr 14, 2025
8b4f5c2
chore: update lockfile for uv
QuantumGhost Apr 14, 2025
bc8247d
refactor(api): delete poetry related configuration in pyproject.toml
QuantumGhost Apr 14, 2025
49b4540
remove poetry.lock
bowenliang123 Apr 14, 2025
e193e08
rm dev/sync-poetry script
bowenliang123 Apr 14, 2025
aed4995
disable tests on poetry
bowenliang123 Apr 14, 2025
643f82c
disable tests on poetry
bowenliang123 Apr 14, 2025
3b0f826
nit
bowenliang123 Apr 14, 2025
84f9a85
feat(api): delete poetry related files
QuantumGhost Apr 14, 2025
bebd1d9
Merge remote-tracking branch 'bowenliang123/uv' into uv
QuantumGhost Apr 14, 2025
4aca94f
feat(api): replace poetry with uv in various scripts
QuantumGhost Apr 14, 2025
dd0388a
script(api): add a script for update dependencies in dify backend
QuantumGhost Apr 14, 2025
4415644
docs(api): Update Development docs for Dify API
QuantumGhost Apr 14, 2025
26a66bf
chore(api): remove poetry related configurations and scripts
QuantumGhost Apr 14, 2025
0b799b2
chore(api): Use uv when building docker image
QuantumGhost Apr 14, 2025
810d824
ci(api): Remove old VDB test pipeline
QuantumGhost Apr 14, 2025
c7590ed
ci(api): rename Workflow files
QuantumGhost Apr 14, 2025
5bb881f
update dev/reformat and dev/run-mypy with uv
bowenliang123 Apr 14, 2025
6e2baec
ci(api): add back missing VDB Test pipeline.
QuantumGhost Apr 14, 2025
1429638
replace api-tests.yaml
bowenliang123 Apr 14, 2025
f9c5a9f
update style jobs with uv
bowenliang123 Apr 14, 2025
529ad8c
Replace vdb-tests.yml
bowenliang123 Apr 14, 2025
6860ae9
replace db-migration-test.yml
bowenliang123 Apr 14, 2025
c971986
nit
bowenliang123 Apr 14, 2025
ea11d29
restore Cache MyPy
bowenliang123 Apr 14, 2025
3dc4962
rename to dev/mypy-check
bowenliang123 Apr 14, 2025
1b76bf7
Merge branch 'uv' of https://github.com/bowenliang123/dify into uv
QuantumGhost Apr 15, 2025
c71b488
chore(api): merge changes to dependencies from main
QuantumGhost Apr 15, 2025
cc8e5c1
chore(api): update uv.lock
QuantumGhost Apr 15, 2025
64a4b78
Merge remote-tracking branch 'upstream/main' into uv
QuantumGhost Apr 15, 2025
1b705d6
skip api/poetry.lock
bowenliang123 Apr 15, 2025
bddfdf6
add api/uv.lock
bowenliang123 Apr 15, 2025
04e3f19
simplify uv run commands of `uv run python -m`
bowenliang123 Apr 15, 2025
fe35bfa
update uv to 0.6.14
bowenliang123 Apr 15, 2025
2a4be8b
correct deprecation notice in api/README.md
bowenliang123 Apr 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .devcontainer/post_create_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

npm add -g [email protected]
cd web && pnpm install
pipx install poetry
pipx install uv

echo 'alias start-api="cd /workspaces/dify/api && poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug"' >> ~/.bashrc
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
echo 'alias start-api="cd /workspaces/dify/api && uv run python -m flask run --host 0.0.0.0 --port=5001 --debug"' >> ~/.bashrc
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
echo 'alias start-web="cd /workspaces/dify/web && pnpm dev"' >> ~/.bashrc
echo 'alias start-containers="cd /workspaces/dify/docker && docker-compose -f docker-compose.middleware.yaml -p dify --env-file middleware.env up -d"' >> ~/.bashrc
echo 'alias stop-containers="cd /workspaces/dify/docker && docker-compose -f docker-compose.middleware.yaml -p dify --env-file middleware.env down"' >> ~/.bashrc
Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/post_start_command.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

cd api && poetry install
cd api && uv sync
36 changes: 0 additions & 36 deletions .github/actions/setup-poetry/action.yml

This file was deleted.

34 changes: 34 additions & 0 deletions .github/actions/setup-uv/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Setup UV and Python

inputs:
python-version:
description: Python version to use and the UV installed with
required: true
default: '3.12'
uv-version:
description: UV version to set up
required: true
default: '0.6.14'
uv-lockfile:
description: Path to the UV lockfile to restore cache from
required: true
default: ''
enable-cache:
required: true
default: true

runs:
using: composite
steps:
- name: Set up Python ${{ inputs.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}

- name: Install uv
uses: astral-sh/setup-uv@v5
with:
version: ${{ inputs.uv-version }}
python-version: ${{ inputs.python-version }}
enable-cache: ${{ inputs.enable-cache }}
cache-dependency-glob: ${{ inputs.uv-lockfile }}
34 changes: 16 additions & 18 deletions .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ jobs:
test:
name: API Tests
runs-on: ubuntu-latest
defaults:
run:
shell: bash
strategy:
matrix:
python-version:
Expand All @@ -30,37 +33,32 @@ jobs:
fetch-depth: 0
persist-credentials: false

- name: Setup Poetry and Python ${{ matrix.python-version }}
uses: ./.github/actions/setup-poetry
- name: Setup UV and Python
uses: ./.github/actions/setup-uv
with:
python-version: ${{ matrix.python-version }}
poetry-lockfile: api/poetry.lock
uv-lockfile: api/uv.lock

- name: Check Poetry lockfile
run: |
poetry check -C api --lock
poetry show -C api
- name: Check UV lockfile
run: uv lock --project api --check

- name: Install dependencies
run: poetry install -C api --with dev

- name: Check dependencies in pyproject.toml
run: poetry run -P api bash dev/pytest/pytest_artifacts.sh
run: uv sync --project api --group dev

- name: Run Unit tests
run: poetry run -P api bash dev/pytest/pytest_unit_tests.sh
run: uv run --project api bash dev/pytest/pytest_unit_tests.sh

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

- name: Cache MyPy
- name: MyPy Cache
uses: actions/cache@v4
with:
path: api/.mypy_cache
key: mypy-${{ matrix.python-version }}-${{ runner.os }}-${{ hashFiles('api/poetry.lock') }}
key: mypy-${{ matrix.python-version }}-${{ runner.os }}-${{ hashFiles('api/uv.lock') }}

- name: Run mypy
run: dev/run-mypy
- name: Run MyPy Checks
run: dev/mypy-check

- name: Set up dotenvs
run: |
Expand All @@ -80,4 +78,4 @@ jobs:
ssrf_proxy

- name: Run Workflow
run: poetry run -P api bash dev/pytest/pytest_workflow.sh
run: uv run --project api bash dev/pytest/pytest_workflow.sh
12 changes: 5 additions & 7 deletions .github/workflows/db-migration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
fetch-depth: 0
persist-credentials: false

- name: Setup Poetry and Python
uses: ./.github/actions/setup-poetry
- name: Setup UV and Python
uses: ./.github/actions/setup-uv
with:
poetry-lockfile: api/poetry.lock
uv-lockfile: api/uv.lock

- name: Install dependencies
run: poetry install -C api
run: uv sync --project api

- name: Prepare middleware env
run: |
Expand All @@ -54,6 +54,4 @@ jobs:
- name: Run DB Migration
env:
DEBUG: true
run: |
cd api
poetry run python -m flask upgrade-db
run: uv run --directory api flask upgrade-db
1 change: 1 addition & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
with:
push: false
context: "{{defaultContext}}:${{ matrix.context }}"
file: "${{ matrix.file }}"
platforms: ${{ matrix.platform }}
cache-from: type=gha
cache-to: type=gha,mode=max
17 changes: 10 additions & 7 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,27 @@ jobs:
api/**
.github/workflows/style.yml

- name: Setup Poetry and Python
- name: Setup UV and Python
if: steps.changed-files.outputs.any_changed == 'true'
uses: ./.github/actions/setup-poetry
uses: ./.github/actions/setup-uv
with:
uv-lockfile: api/uv.lock
enable-cache: false

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

- name: Ruff check
if: steps.changed-files.outputs.any_changed == 'true'
run: |
poetry run -C api ruff --version
poetry run -C api ruff check ./
poetry run -C api ruff format --check ./
uv run --directory api ruff --version
uv run --directory api ruff check ./
uv run --directory api ruff format --check ./

- name: Dotenv check
if: steps.changed-files.outputs.any_changed == 'true'
run: poetry run -P api dotenv-linter ./api/.env.example ./web/.env.example
run: uv run --project api dotenv-linter ./api/.env.example ./web/.env.example

- name: Lint hints
if: failure()
Expand Down
20 changes: 9 additions & 11 deletions .github/workflows/vdb-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- api/core/rag/datasource/**
- docker/**
- .github/workflows/vdb-tests.yml
- api/poetry.lock
- api/uv.lock
- api/pyproject.toml

concurrency:
Expand All @@ -32,19 +32,17 @@ jobs:
fetch-depth: 0
persist-credentials: false

- name: Setup Poetry and Python ${{ matrix.python-version }}
uses: ./.github/actions/setup-poetry
- name: Setup UV and Python
uses: ./.github/actions/setup-uv
with:
python-version: ${{ matrix.python-version }}
poetry-lockfile: api/poetry.lock
uv-lockfile: api/uv.lock

- name: Check Poetry lockfile
run: |
poetry check -C api --lock
poetry show -C api
- name: Check UV lockfile
run: uv lock --project api --check

- name: Install dependencies
run: poetry install -C api --with dev
run: uv sync --project api --group dev

- name: Set up dotenvs
run: |
Expand Down Expand Up @@ -80,7 +78,7 @@ jobs:
elasticsearch

- name: Check TiDB Ready
run: poetry run -P api python api/tests/integration_tests/vdb/tidb_vector/check_tiflash_ready.py
run: uv run --project api python api/tests/integration_tests/vdb/tidb_vector/check_tiflash_ready.py

- name: Test Vector Stores
run: poetry run -P api bash dev/pytest/pytest_vdb.sh
run: uv run --project api bash dev/pytest/pytest_vdb.sh
19 changes: 5 additions & 14 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@ FROM python:3.12-slim-bookworm AS base

WORKDIR /app/api

# Install Poetry
ENV POETRY_VERSION=2.0.1
# Install uv
ENV UV_VERSION=0.6.14

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

RUN pip install --no-cache-dir poetry==${POETRY_VERSION}
RUN pip install --no-cache-dir uv==${UV_VERSION}

# Configure Poetry
ENV POETRY_CACHE_DIR=/tmp/poetry_cache
ENV POETRY_NO_INTERACTION=1
ENV POETRY_VIRTUALENVS_IN_PROJECT=true
ENV POETRY_VIRTUALENVS_CREATE=true
ENV POETRY_REQUESTS_TIMEOUT=15

FROM base AS packages

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

# Install Python dependencies
COPY pyproject.toml poetry.lock ./
RUN poetry install --sync --no-cache --no-root
COPY pyproject.toml uv.lock ./
RUN uv sync --locked

# production stage
FROM base AS production
Expand Down
29 changes: 16 additions & 13 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
## Usage

> [!IMPORTANT]
> In the v0.6.12 release, we deprecated `pip` as the package management tool for Dify API Backend service and replaced it with `poetry`.
>
> In the v1.3.0 release, `poetry` has been replaced with
> [`uv`](https://docs.astral.sh/uv/) as the package manager
> for Dify API backend service.

1. Start the docker-compose stack

Expand Down Expand Up @@ -37,53 +40,53 @@

4. Create environment.

Dify API service uses [Poetry](https://python-poetry.org/docs/) to manage dependencies. First, you need to add the poetry shell plugin, if you don't have it already, in order to run in a virtual environment. [Note: Poetry shell is no longer a native command so you need to install the poetry plugin beforehand]
Dify API service uses [UV](https://docs.astral.sh/uv/) to manage dependencies.
First, you need to add the uv package manager, if you don't have it already.

```bash
poetry self add poetry-plugin-shell
pip install uv
# Or on macOS
brew install uv
```

Then, You can execute `poetry shell` to activate the environment.

5. Install dependencies

```bash
poetry env use 3.12
poetry install
uv sync --group lint --group dev
```

6. Run migrate

Before the first launch, migrate the database to the latest version.

```bash
poetry run python -m flask db upgrade
uv run flask db upgrade
```

7. Start backend

```bash
poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
uv run flask run --host 0.0.0.0 --port=5001 --debug
```

8. Start Dify [web](../web) service.
9. Setup your application by visiting `http://localhost:3000`...
9. Setup your application by visiting `http://localhost:3000`.
10. If you need to handle and debug the async tasks (e.g. dataset importing and documents indexing), please start the worker service.

```bash
poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
uv run celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
```

## Testing

1. Install dependencies for both the backend and the test environment

```bash
poetry install -C api --with dev
uv sync --group lint --group dev
```

2. Run the tests locally with mocked system environment variables in `tool.pytest_env` section in `pyproject.toml`

```bash
poetry run -P api bash dev/pytest/pytest_all_tests.sh
uv run -P api bash dev/pytest/pytest_all_tests.sh
```
Loading
Loading