Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenliang123 committed Nov 20, 2024
1 parent 07b5bba commit 75967fc
Show file tree
Hide file tree
Showing 81 changed files with 280 additions and 314 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup-poetry/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ inputs:
python-version:
description: Python version to use and the Poetry installed with
required: true
default: '3.10'
default: '3.11'
poetry-version:
description: Poetry version to set up
required: true
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"

Expand Down
1 change: 0 additions & 1 deletion .github/workflows/vdb-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.10"
- "3.11"
- "3.12"

Expand Down
34 changes: 16 additions & 18 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

## Usage

> [!IMPORTANT]
> In the v0.6.12 release, we deprecated `pip` as the package management tool for Dify API Backend service and replaced it with `poetry`.
1. Start the docker-compose stack

The backend require some middleware, including PostgreSQL, Redis, and Weaviate, which can be started together using `docker-compose`.
Expand All @@ -30,42 +27,43 @@
SECRET_KEY=${secret_key}" .env
```

4. Create environment.
4. Prepare Python environment

Dify API service uses [Poetry](https://python-poetry.org/docs/) to manage dependencies. You can execute `poetry shell` to activate the environment.
Dify API services requires Python 3.11 or 3.12, and the [Poetry](https://python-poetry.org/docs/) for dependency management.
- To install Poetry, please refer to
the [Poetry's installation guide](https://python-poetry.org/docs/#installation). The simplest way is to run the `pip install poetry` command to install Poetry on pip.
- Run `poetry env use 3.12` to switch to the Python version for Poetry, please refer the usage of `poetry env use`
command in [Poetry docs](https://python-poetry.org/docs/managing-environments/#switching-between-environments).
- Run `poetry shell` to activate the shell environment with Poetry support.

5. Install dependencies

```bash
poetry env use 3.10
cd api
poetry env use 3.12
poetry install
```

In case of contributors missing to update dependencies for `pyproject.toml`, you can perform the following shell instead.

```bash
poetry shell # activate current environment
poetry add $(cat requirements.txt) # install dependencies of production and update pyproject.toml
poetry add $(cat requirements-dev.txt) --group dev # install dependencies of development and update pyproject.toml
```

6. Run migrate
6. Run db migration

Before the first launch, migrate the database to the latest version.

```bash
poetry run python -m flask db upgrade
```

7. Start backend
7. Start api service

```bash
poetry run python -m flask run --host 0.0.0.0 --port=5001 --debug
poetry run python -m flask run --host 0.0.0.0 --port=5001
```

8. Start Dify [web](../web) service.

9. Setup your application by visiting `http://localhost:3000`...
10. If you need to handle and debug the async tasks (e.g. dataset importing and documents indexing), please start the worker service.

10. Start the worker service, if you need to handle and debug the async tasks (e.g. dataset importing and documents
indexing), please start the worker service.

```bash
poetry run python -m celery -A app.celery worker -P gevent -c 1 --loglevel INFO -Q dataset,generation,mail,ops_trace,app_deletion
Expand Down
8 changes: 5 additions & 3 deletions api/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os
import sys

python_version = sys.version_info
if not ((3, 11) <= python_version < (3, 13)):
print(f"Python 3.11 or 3.12 is required, current version is {python_version.major}.{python_version.minor}")
raise SystemExit(1)

from configs import dify_config

if not dify_config.DEBUG:
Expand Down Expand Up @@ -30,9 +35,6 @@

# DO NOT REMOVE ABOVE

if sys.version_info[:2] == (3, 10):
print("Warning: Python 3.10 will not be supported in the next version.")


warnings.simplefilter("ignore", ResourceWarning)

Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/app/conversation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime

import pytz
from flask_login import current_user
Expand Down Expand Up @@ -314,7 +314,7 @@ def _get_conversation(app_model, conversation_id):
raise NotFound("Conversation Not Exists.")

if not conversation.read_at:
conversation.read_at = datetime.now(timezone.utc).replace(tzinfo=None)
conversation.read_at = datetime.now(UTC).replace(tzinfo=None)
conversation.read_account_id = current_user.id
db.session.commit()

Expand Down
6 changes: 3 additions & 3 deletions api/controllers/console/app/site.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime

from flask_login import current_user
from flask_restful import Resource, marshal_with, reqparse
Expand Down Expand Up @@ -75,7 +75,7 @@ def post(self, app_model):
setattr(site, attr_name, value)

site.updated_by = current_user.id
site.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
site.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

return site
Expand All @@ -99,7 +99,7 @@ def post(self, app_model):

site.code = Site.generate_code(16)
site.updated_by = current_user.id
site.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
site.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

return site
Expand Down
2 changes: 1 addition & 1 deletion api/controllers/console/auth/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def post(self):
account.timezone = args["timezone"]
account.interface_theme = "light"
account.status = AccountStatus.ACTIVE.value
account.initialized_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
account.initialized_at = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
db.session.commit()

token_pair = AccountService.login(account, ip_address=extract_remote_ip(request))
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/auth/oauth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import datetime, timezone
from datetime import UTC, datetime
from typing import Optional

import requests
Expand Down Expand Up @@ -106,7 +106,7 @@ def get(self, provider: str):

if account.status == AccountStatus.PENDING.value:
account.status = AccountStatus.ACTIVE.value
account.initialized_at = datetime.now(timezone.utc).replace(tzinfo=None)
account.initialized_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

try:
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/datasets/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def patch(self, binding_id, action):
if action == "enable":
if data_source_binding.disabled:
data_source_binding.disabled = False
data_source_binding.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
data_source_binding.updated_at = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
db.session.add(data_source_binding)
db.session.commit()
else:
Expand All @@ -92,7 +92,7 @@ def patch(self, binding_id, action):
if action == "disable":
if not data_source_binding.disabled:
data_source_binding.disabled = True
data_source_binding.updated_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
data_source_binding.updated_at = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
db.session.add(data_source_binding)
db.session.commit()
else:
Expand Down
18 changes: 9 additions & 9 deletions api/controllers/console/datasets/datasets_document.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from argparse import ArgumentTypeError
from datetime import datetime, timezone
from datetime import UTC, datetime

from flask import request
from flask_login import current_user
Expand Down Expand Up @@ -665,7 +665,7 @@ def patch(self, dataset_id, document_id, action):
raise InvalidActionError("Document not in indexing state.")

document.paused_by = current_user.id
document.paused_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.paused_at = datetime.now(UTC).replace(tzinfo=None)
document.is_paused = True
db.session.commit()

Expand Down Expand Up @@ -745,7 +745,7 @@ def put(self, dataset_id, document_id):
document.doc_metadata[key] = value

document.doc_type = doc_type
document.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

return {"result": "success", "message": "Document metadata updated."}, 200
Expand Down Expand Up @@ -787,7 +787,7 @@ def patch(self, dataset_id, document_id, action):
document.enabled = True
document.disabled_at = None
document.disabled_by = None
document.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

# Set cache to prevent indexing the same document multiple times
Expand All @@ -804,9 +804,9 @@ def patch(self, dataset_id, document_id, action):
raise InvalidActionError("Document already disabled.")

document.enabled = False
document.disabled_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.disabled_at = datetime.now(UTC).replace(tzinfo=None)
document.disabled_by = current_user.id
document.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

# Set cache to prevent indexing the same document multiple times
Expand All @@ -821,9 +821,9 @@ def patch(self, dataset_id, document_id, action):
raise InvalidActionError("Document already archived.")

document.archived = True
document.archived_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.archived_at = datetime.now(UTC).replace(tzinfo=None)
document.archived_by = current_user.id
document.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

if document.enabled:
Expand All @@ -840,7 +840,7 @@ def patch(self, dataset_id, document_id, action):
document.archived = False
document.archived_at = None
document.archived_by = None
document.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
document.updated_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

# Set cache to prevent indexing the same document multiple times
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/datasets/datasets_segments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from datetime import datetime, timezone
from datetime import UTC, datetime

import pandas as pd
from flask import request
Expand Down Expand Up @@ -188,7 +188,7 @@ def patch(self, dataset_id, segment_id, action):
raise InvalidActionError("Segment is already disabled.")

segment.enabled = False
segment.disabled_at = datetime.now(timezone.utc).replace(tzinfo=None)
segment.disabled_at = datetime.now(UTC).replace(tzinfo=None)
segment.disabled_by = current_user.id
db.session.commit()

Expand Down
6 changes: 3 additions & 3 deletions api/controllers/console/explore/completion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from datetime import datetime, timezone
from datetime import UTC, datetime

from flask_login import current_user
from flask_restful import reqparse
Expand Down Expand Up @@ -46,7 +46,7 @@ def post(self, installed_app):
streaming = args["response_mode"] == "streaming"
args["auto_generate_name"] = False

installed_app.last_used_at = datetime.now(timezone.utc).replace(tzinfo=None)
installed_app.last_used_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

try:
Expand Down Expand Up @@ -106,7 +106,7 @@ def post(self, installed_app):

args["auto_generate_name"] = False

installed_app.last_used_at = datetime.now(timezone.utc).replace(tzinfo=None)
installed_app.last_used_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

try:
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/explore/installed_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import UTC, datetime

from flask_login import current_user
from flask_restful import Resource, inputs, marshal_with, reqparse
Expand Down Expand Up @@ -81,7 +81,7 @@ def post(self):
tenant_id=current_tenant_id,
app_owner_tenant_id=app.tenant_id,
is_pinned=False,
last_used_at=datetime.now(timezone.utc).replace(tzinfo=None),
last_used_at=datetime.now(UTC).replace(tzinfo=None),
)
db.session.add(new_installed_app)
db.session.commit()
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/workspace/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def post(self):
raise InvalidInvitationCodeError()

invitation_code.status = "used"
invitation_code.used_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
invitation_code.used_at = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
invitation_code.used_by_tenant_id = account.current_tenant_id
invitation_code.used_by_account_id = account.id

account.interface_language = args["interface_language"]
account.timezone = args["timezone"]
account.interface_theme = "light"
account.status = "active"
account.initialized_at = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
account.initialized_at = datetime.datetime.now(datetime.UTC).replace(tzinfo=None)
db.session.commit()

return {"result": "success"}
Expand Down
4 changes: 2 additions & 2 deletions api/controllers/service_api/wraps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Callable
from datetime import datetime, timezone
from datetime import UTC, datetime
from enum import Enum
from functools import wraps
from typing import Optional
Expand Down Expand Up @@ -198,7 +198,7 @@ def validate_and_get_api_token(scope=None):
if not api_token:
raise Unauthorized("Access token is invalid")

api_token.last_used_at = datetime.now(timezone.utc).replace(tzinfo=None)
api_token.last_used_at = datetime.now(UTC).replace(tzinfo=None)
db.session.commit()

return api_token
Expand Down
4 changes: 2 additions & 2 deletions api/core/agent/base_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import uuid
from collections.abc import Mapping, Sequence
from datetime import datetime, timezone
from datetime import UTC, datetime
from typing import Optional, Union, cast

from core.agent.entities import AgentEntity, AgentToolEntity
Expand Down Expand Up @@ -419,7 +419,7 @@ def update_db_variables(self, tool_variables: ToolRuntimeVariablePool, db_variab
.first()
)

db_variables.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
db_variables.updated_at = datetime.now(UTC).replace(tzinfo=None)
db_variables.variables_str = json.dumps(jsonable_encoder(tool_variables.pool))
db.session.commit()
db.session.close()
Expand Down
4 changes: 2 additions & 2 deletions api/core/app/app_config/entities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Sequence
from enum import Enum
from enum import Enum, StrEnum
from typing import Any, Optional

from pydantic import BaseModel, Field, field_validator
Expand Down Expand Up @@ -88,7 +88,7 @@ def value_of(cls, value: str):
advanced_completion_prompt_template: Optional[AdvancedCompletionPromptTemplateEntity] = None


class VariableEntityType(str, Enum):
class VariableEntityType(StrEnum):
TEXT_INPUT = "text-input"
SELECT = "select"
PARAGRAPH = "paragraph"
Expand Down
Loading

0 comments on commit 75967fc

Please sign in to comment.