Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. 3. Generate a `SECRET_KEY` in the `.env` file.
  16. ```bash for Linux
  17. sed -i "/^SECRET_KEY=/c\SECRET_KEY=$(openssl rand -base64 42)" .env
  18. ```
  19. ```bash for Mac
  20. secret_key=$(openssl rand -base64 42)
  21. sed -i '' "/^SECRET_KEY=/c\\
  22. SECRET_KEY=${secret_key}" .env
  23. ```
  24. 4. Create environment.
  25. Dify API service uses [Poetry](https://python-poetry.org/docs/) to manage dependencies. You can execute `poetry shell` to activate the environment.
  26. 5. Install dependencies
  27. ```bash
  28. poetry env use 3.10
  29. poetry install
  30. ```
  31. In case of contributors missing to update dependencies for `pyproject.toml`, you can perform the following shell instead.
  32. ```bash
  33. poetry shell # activate current environment
  34. poetry add $(cat requirements.txt) # install dependencies of production and update pyproject.toml
  35. poetry add $(cat requirements-dev.txt) --group dev # install dependencies of development and update pyproject.toml
  36. ```
  37. 6. Run migrate
  38. Before the first launch, migrate the database to the latest version.
  39. ```bash
  40. poetry run python -m flask db upgrade
  41. ```
  42. 7. Start backend
  43. ```bash
  44. poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
  45. ```
  46. 8. Start Dify [web](../web) service.
  47. 9. Setup your application by visiting `http://localhost:3000`...
  48. 10. If you need to handle and debug the async tasks (e.g. dataset importing and documents indexing), please start the worker service.
  49. ```bash
  50. poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
  51. ```
  52. ## Testing
  53. 1. Install dependencies for both the backend and the test environment
  54. ```bash
  55. poetry install -C api --with dev
  56. ```
  57. 2. Run the tests locally with mocked system environment variables in `tool.pytest_env` section in `pyproject.toml`
  58. ```bash
  59. poetry run -C api bash dev/pytest/pytest_all_tests.sh
  60. ```