Spaces:
No application file
No application file
Upload api\README.md with huggingface_hub
Browse files- api//README.md +89 -0
api//README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dify Backend API
|
| 2 |
+
|
| 3 |
+
## Usage
|
| 4 |
+
|
| 5 |
+
> [!IMPORTANT]
|
| 6 |
+
> In the v0.6.12 release, we deprecated `pip` as the package management tool for Dify API Backend service and replaced it with `poetry`.
|
| 7 |
+
|
| 8 |
+
1. Start the docker-compose stack
|
| 9 |
+
|
| 10 |
+
The backend require some middleware, including PostgreSQL, Redis, and Weaviate, which can be started together using `docker-compose`.
|
| 11 |
+
|
| 12 |
+
```bash
|
| 13 |
+
cd ../docker
|
| 14 |
+
cp middleware.env.example middleware.env
|
| 15 |
+
# change the profile to other vector database if you are not using weaviate
|
| 16 |
+
docker compose -f docker-compose.middleware.yaml --profile weaviate -p dify up -d
|
| 17 |
+
cd ../api
|
| 18 |
+
```
|
| 19 |
+
|
| 20 |
+
2. Copy `.env.example` to `.env`
|
| 21 |
+
|
| 22 |
+
```cli
|
| 23 |
+
cp .env.example .env
|
| 24 |
+
```
|
| 25 |
+
3. Generate a `SECRET_KEY` in the `.env` file.
|
| 26 |
+
|
| 27 |
+
bash for Linux
|
| 28 |
+
```bash for Linux
|
| 29 |
+
sed -i "/^SECRET_KEY=/c\SECRET_KEY=$(openssl rand -base64 42)" .env
|
| 30 |
+
```
|
| 31 |
+
bash for Mac
|
| 32 |
+
```bash for Mac
|
| 33 |
+
secret_key=$(openssl rand -base64 42)
|
| 34 |
+
sed -i '' "/^SECRET_KEY=/c\\
|
| 35 |
+
SECRET_KEY=${secret_key}" .env
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
4. Create environment.
|
| 39 |
+
|
| 40 |
+
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]
|
| 41 |
+
|
| 42 |
+
```bash
|
| 43 |
+
poetry self add poetry-plugin-shell
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
Then, You can execute `poetry shell` to activate the environment.
|
| 47 |
+
|
| 48 |
+
5. Install dependencies
|
| 49 |
+
|
| 50 |
+
```bash
|
| 51 |
+
poetry env use 3.12
|
| 52 |
+
poetry install
|
| 53 |
+
```
|
| 54 |
+
|
| 55 |
+
6. Run migrate
|
| 56 |
+
|
| 57 |
+
Before the first launch, migrate the database to the latest version.
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
poetry run python -m flask db upgrade
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
7. Start backend
|
| 64 |
+
|
| 65 |
+
```bash
|
| 66 |
+
poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
8. Start Dify [web](../web) service.
|
| 70 |
+
9. Setup your application by visiting `http://localhost:3000`...
|
| 71 |
+
10. If you need to handle and debug the async tasks (e.g. dataset importing and documents indexing), please start the worker service.
|
| 72 |
+
|
| 73 |
+
```bash
|
| 74 |
+
poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
## Testing
|
| 78 |
+
|
| 79 |
+
1. Install dependencies for both the backend and the test environment
|
| 80 |
+
|
| 81 |
+
```bash
|
| 82 |
+
poetry install -C api --with dev
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
2. Run the tests locally with mocked system environment variables in `tool.pytest_env` section in `pyproject.toml`
|
| 86 |
+
|
| 87 |
+
```bash
|
| 88 |
+
poetry run -P api bash dev/pytest/pytest_all_tests.sh
|
| 89 |
+
```
|