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 2.5KB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. cp middleware.env.example middleware.env
  10. # change the profile to other vector database if you are not using weaviate
  11. docker compose -f docker-compose.middleware.yaml --profile weaviate -p dify up -d
  12. cd ../api
  13. ```
  14. 2. Copy `.env.example` to `.env`
  15. ```cli
  16. cp .env.example .env
  17. ```
  18. 3. Generate a `SECRET_KEY` in the `.env` file.
  19. bash for Linux
  20. ```bash for Linux
  21. sed -i "/^SECRET_KEY=/c\SECRET_KEY=$(openssl rand -base64 42)" .env
  22. ```
  23. bash for Mac
  24. ```bash for Mac
  25. secret_key=$(openssl rand -base64 42)
  26. sed -i '' "/^SECRET_KEY=/c\\
  27. SECRET_KEY=${secret_key}" .env
  28. ```
  29. 4. Create environment.
  30. 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]
  31. ```bash
  32. poetry self add poetry-plugin-shell
  33. ```
  34. Then, You can execute `poetry shell` to activate the environment.
  35. 5. Install dependencies
  36. ```bash
  37. poetry env use 3.12
  38. poetry install
  39. ```
  40. 6. Run migrate
  41. Before the first launch, migrate the database to the latest version.
  42. ```bash
  43. poetry run python -m flask db upgrade
  44. ```
  45. 7. Start backend
  46. ```bash
  47. poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
  48. ```
  49. 8. Start Dify [web](../web) service.
  50. 9. Setup your application by visiting `http://localhost:3000`...
  51. 10. If you need to handle and debug the async tasks (e.g. dataset importing and documents indexing), please start the worker service.
  52. ```bash
  53. poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
  54. ```
  55. ## Testing
  56. 1. Install dependencies for both the backend and the test environment
  57. ```bash
  58. poetry install -C api --with dev
  59. ```
  60. 2. Run the tests locally with mocked system environment variables in `tool.pytest_env` section in `pyproject.toml`
  61. ```bash
  62. poetry run -P api bash dev/pytest/pytest_all_tests.sh
  63. ```