You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.0KB

hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
hace 2 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Dify Backend API
  2. ## Usage
  3. 1. Start the docker-compose stack
  4. The backend require some middleware, including PostgreSQL, Redis, and Weaviate, which can be started together using `docker-compose`.
  5. ```bash
  6. cd ../docker
  7. docker-compose -f docker-compose.middleware.yaml -p dify up -d
  8. cd ../api
  9. ```
  10. 2. Copy `.env.example` to `.env`
  11. 3. Generate a `SECRET_KEY` in the `.env` file.
  12. ```bash
  13. sed -i "/^SECRET_KEY=/c\SECRET_KEY=$(openssl rand -base64 42)" .env
  14. ```
  15. 4. Create environment.
  16. Dify API service uses [Poetry](https://python-poetry.org/docs/) to manage dependencies. You can execute `poetry shell` to activate the environment.
  17. > Using pip can be found [below](#usage-with-pip).
  18. 6. Install dependencies
  19. ```bash
  20. poetry install
  21. ```
  22. In case of contributors missing to update dependencies for `pyproject.toml`, you can perform the following shell instead.
  23. ```bash
  24. poetry shell # activate current environment
  25. poetry add $(cat requirements.txt) # install dependencies of production and update pyproject.toml
  26. poetry add $(cat requirements-dev.txt) --group dev # install dependencies of development and update pyproject.toml
  27. ```
  28. 7. Run migrate
  29. Before the first launch, migrate the database to the latest version.
  30. ```bash
  31. poetry run python -m flask db upgrade
  32. ```
  33. 8. Start backend
  34. ```bash
  35. poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
  36. ```
  37. 9. Start Dify [web](../web) service.
  38. 10. Setup your application by visiting `http://localhost:3000`...
  39. 11. If you need to debug local async processing, please start the worker service.
  40. ```bash
  41. poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail
  42. ```
  43. The started celery app handles the async tasks, e.g. dataset importing and documents indexing.
  44. ## Testing
  45. 1. Install dependencies for both the backend and the test environment
  46. ```bash
  47. poetry install --with dev
  48. ```
  49. 2. Run the tests locally with mocked system environment variables in `tool.pytest_env` section in `pyproject.toml`
  50. ```bash
  51. cd ../
  52. poetry run -C api bash dev/pytest/pytest_all_tests.sh
  53. ```
  54. ## Usage with pip
  55. > [!NOTE]
  56. > In the next version, we will deprecate pip as the primary package management tool for dify api service, currently Poetry and pip coexist.
  57. 1. Start the docker-compose stack
  58. The backend require some middleware, including PostgreSQL, Redis, and Weaviate, which can be started together using `docker-compose`.
  59. ```bash
  60. cd ../docker
  61. docker-compose -f docker-compose.middleware.yaml -p dify up -d
  62. cd ../api
  63. ```
  64. 2. Copy `.env.example` to `.env`
  65. 3. Generate a `SECRET_KEY` in the `.env` file.
  66. ```bash
  67. sed -i "/^SECRET_KEY=/c\SECRET_KEY=$(openssl rand -base64 42)" .env
  68. ```
  69. 4. Create environment.
  70. If you use Anaconda, create a new environment and activate it
  71. ```bash
  72. conda create --name dify python=3.10
  73. conda activate dify
  74. ```
  75. 6. Install dependencies
  76. ```bash
  77. pip install -r requirements.txt
  78. ```
  79. 7. Run migrate
  80. Before the first launch, migrate the database to the latest version.
  81. ```bash
  82. flask db upgrade
  83. ```
  84. 8. Start backend:
  85. ```bash
  86. flask run --host 0.0.0.0 --port=5001 --debug
  87. ```
  88. 9. Setup your application by visiting http://localhost:5001/console/api/setup or other apis...
  89. 10. If you need to debug local async processing, please start the worker service.
  90. ```bash
  91. celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail
  92. ```
  93. The started celery app handles the async tasks, e.g. dataset importing and documents indexing.
  94. ## Testing
  95. 1. Install dependencies for both the backend and the test environment
  96. ```bash
  97. pip install -r requirements.txt -r requirements-dev.txt
  98. ```
  99. 2. Run the tests locally with mocked system environment variables in `tool.pytest_env` section in `pyproject.toml`
  100. ```bash
  101. dev/pytest/pytest_all_tests.sh
  102. ```