您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Dify Backend API
  2. ## Usage
  3. > [!IMPORTANT]
  4. > In the v0.6.12 release, we deprecated `pip` as the package management tool for Dify API Backend service and replaced it with `poetry`.
  5. 1. Start the docker-compose stack
  6. The backend require some middleware, including PostgreSQL, Redis, and Weaviate, which can be started together using `docker-compose`.
  7. ```bash
  8. cd ../docker
  9. docker compose -f docker-compose.middleware.yaml -p dify up -d
  10. cd ../api
  11. ```
  12. 2. Copy `.env.example` to `.env`
  13. 3. Generate a `SECRET_KEY` in the `.env` file.
  14. ```bash for Linux
  15. sed -i "/^SECRET_KEY=/c\SECRET_KEY=$(openssl rand -base64 42)" .env
  16. ```
  17. ```bash for Mac
  18. secret_key=$(openssl rand -base64 42)
  19. sed -i '' "/^SECRET_KEY=/c\\
  20. SECRET_KEY=${secret_key}" .env
  21. ```
  22. 4. Create environment.
  23. Dify API service uses [Poetry](https://python-poetry.org/docs/) to manage dependencies. You can execute `poetry shell` to activate the environment.
  24. 5. Install dependencies
  25. ```bash
  26. poetry env use 3.10
  27. poetry install
  28. ```
  29. In case of contributors missing to update dependencies for `pyproject.toml`, you can perform the following shell instead.
  30. ```bash
  31. poetry shell # activate current environment
  32. poetry add $(cat requirements.txt) # install dependencies of production and update pyproject.toml
  33. poetry add $(cat requirements-dev.txt) --group dev # install dependencies of development and update pyproject.toml
  34. ```
  35. 6. Run migrate
  36. Before the first launch, migrate the database to the latest version.
  37. ```bash
  38. poetry run python -m flask db upgrade
  39. ```
  40. 7. Start backend
  41. ```bash
  42. poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
  43. ```
  44. 8. Start Dify [web](../web) service.
  45. 9. Setup your application by visiting `http://localhost:3000`...
  46. 10. If you need to debug local async processing, please start the worker service.
  47. ```bash
  48. poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
  49. ```
  50. The started celery app handles the async tasks, e.g. dataset importing and documents indexing.
  51. ## Testing
  52. 1. Install dependencies for both the backend and the test environment
  53. ```bash
  54. poetry install --with dev
  55. ```
  56. 2. Run the tests locally with mocked system environment variables in `tool.pytest_env` section in `pyproject.toml`
  57. ```bash
  58. cd ../
  59. poetry run -C api bash dev/pytest/pytest_all_tests.sh
  60. ```