Skip to content

Commit

Permalink
Merge commit '400392230b47fff5d011b55afd8b0f8b8083ade0'
Browse files Browse the repository at this point in the history
* commit '400392230b47fff5d011b55afd8b0f8b8083ade0': (74 commits)
  fixed: variable reference error (langgenius#9722)
  add vdb py test (langgenius#9706)
  downgrade unstructured nltk version (langgenius#9726)
  remove ppt import (langgenius#9721)
  Modify characters (langgenius#9707)
  chore: update version to 0.10.1 (langgenius#9689)
  fix: refresh current page if url contains token (langgenius#9718)
  Revert "Feat: use file size limit from api" (langgenius#9714)
  feat: support user-defined configuration of log file size and retention count (langgenius#9610)
  build(deps): bump next from 14.2.4 to 14.2.10 in /web (langgenius#9713)
  Feat: use file size limit from api (langgenius#9711)
  feat(model_runtime): add new model 'claude-3-5-sonnet-20241022' (langgenius#9708)
  build(deps): bump mermaid from 10.4.0 to 10.9.3 in /web (langgenius#9709)
  nltk security issue and upgrade unstructured (langgenius#9558)
  feat: added claude 3.5 sonnet v2 model to Google Cloud Vertex AI (langgenius#9688)
  fix: revert ref usage in handleFormChange to fix IME input issues (langgenius#9672)
  Help documentation URL correction (langgenius#9704)
  fix: workflow [if node] checklist (langgenius#9699)
  feat(api): add video and audio file size limits to upload config (langgenius#9703)
  fix: remove email code login redirect (langgenius#9698)
  ...

# Conflicts:
#	web/yarn.lock
  • Loading branch information
Scorpion1221 committed Oct 24, 2024
2 parents 3196cc2 + 4003922 commit 82886de
Show file tree
Hide file tree
Showing 225 changed files with 4,230 additions and 3,296 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Star Dify on GitHub and be instantly notified of new releases.
> Before installing Dify, make sure your machine meets the following minimum system requirements:
>
>- CPU >= 2 Core
>- RAM >= 4GB
>- RAM >= 4 GiB
</br>

Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Dify 是一个开源的 LLM 应用开发平台。其直观的界面结合了 AI
在安装 Dify 之前,请确保您的机器满足以下最低系统要求:

- CPU >= 2 Core
- RAM >= 4GB
- RAM >= 4 GiB

### 快速启动

Expand Down
11 changes: 10 additions & 1 deletion api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ SUPABASE_URL=your-server-url
WEB_API_CORS_ALLOW_ORIGINS=http://127.0.0.1:3000,*
CONSOLE_CORS_ALLOW_ORIGINS=http://127.0.0.1:3000,*

# Vector database configuration, support: weaviate, qdrant, milvus, myscale, relyt, pgvecto_rs, pgvector, pgvector, chroma, opensearch, tidb_vector, vikingdb
# Vector database configuration, support: weaviate, qdrant, milvus, myscale, relyt, pgvecto_rs, pgvector, pgvector, chroma, opensearch, tidb_vector, vikingdb, upstash
VECTOR_STORE=weaviate

# Weaviate configuration
Expand Down Expand Up @@ -220,6 +220,10 @@ BAIDU_VECTOR_DB_DATABASE=dify
BAIDU_VECTOR_DB_SHARD=1
BAIDU_VECTOR_DB_REPLICAS=3

# Upstash configuration
UPSTASH_VECTOR_URL=your-server-url
UPSTASH_VECTOR_TOKEN=your-access-token

# ViKingDB configuration
VIKINGDB_ACCESS_KEY=your-ak
VIKINGDB_SECRET_KEY=your-sk
Expand All @@ -239,6 +243,7 @@ UPLOAD_AUDIO_FILE_SIZE_LIMIT=50
# Model Configuration
MULTIMODAL_SEND_IMAGE_FORMAT=base64
PROMPT_GENERATION_MAX_TOKENS=512
CODE_GENERATION_MAX_TOKENS=1024

# Mail configuration, support: resend, smtp
MAIL_TYPE=
Expand Down Expand Up @@ -304,6 +309,10 @@ RESPECT_XFORWARD_HEADERS_ENABLED=false

# Log file path
LOG_FILE=
# Log file max size, the unit is MB
LOG_FILE_MAX_SIZE=20
# Log file max backup count
LOG_FILE_BACKUP_COUNT=5

# Indexing configuration
INDEXING_MAX_SEGMENTATION_TOKENS_LENGTH=1000
Expand Down
16 changes: 6 additions & 10 deletions api/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from configs import dify_config

if os.environ.get("DEBUG", "false").lower() != "true":
from gevent import monkey

Expand Down Expand Up @@ -36,33 +38,27 @@
time.tzset()


# -------------
# Configuration
# -------------
config_type = os.getenv("EDITION", default="SELF_HOSTED") # ce edition first


# create app
app = create_app()
celery = app.extensions["celery"]

if app.config.get("TESTING"):
if dify_config.TESTING:
print("App is running in TESTING mode")


@app.after_request
def after_request(response):
"""Add Version headers to the response."""
response.set_cookie("remember_token", "", expires=0)
response.headers.add("X-Version", app.config["CURRENT_VERSION"])
response.headers.add("X-Env", app.config["DEPLOY_ENV"])
response.headers.add("X-Version", dify_config.CURRENT_VERSION)
response.headers.add("X-Env", dify_config.DEPLOY_ENV)
return response


@app.route("/health")
def health():
return Response(
json.dumps({"pid": os.getpid(), "status": "ok", "version": app.config["CURRENT_VERSION"]}),
json.dumps({"pid": os.getpid(), "status": "ok", "version": dify_config.CURRENT_VERSION}),
status=200,
content_type="application/json",
)
Expand Down
47 changes: 5 additions & 42 deletions api/app_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
grpc.experimental.gevent.init_gevent()

import json
import logging
import sys
from logging.handlers import RotatingFileHandler

from flask import Flask, Response, request
from flask_cors import CORS
Expand All @@ -27,6 +24,7 @@
ext_compress,
ext_database,
ext_hosting_provider,
ext_logging,
ext_login,
ext_mail,
ext_migrate,
Expand Down Expand Up @@ -70,43 +68,7 @@ def create_flask_app_with_configs() -> Flask:

def create_app() -> Flask:
app = create_flask_app_with_configs()

app.secret_key = app.config["SECRET_KEY"]

log_handlers = None
log_file = app.config.get("LOG_FILE")
if log_file:
log_dir = os.path.dirname(log_file)
os.makedirs(log_dir, exist_ok=True)
log_handlers = [
RotatingFileHandler(
filename=log_file,
maxBytes=1024 * 1024 * 1024,
backupCount=5,
),
logging.StreamHandler(sys.stdout),
]

logging.basicConfig(
level=app.config.get("LOG_LEVEL"),
format=app.config.get("LOG_FORMAT"),
datefmt=app.config.get("LOG_DATEFORMAT"),
handlers=log_handlers,
force=True,
)
log_tz = app.config.get("LOG_TZ")
if log_tz:
from datetime import datetime

import pytz

timezone = pytz.timezone(log_tz)

def time_converter(seconds):
return datetime.utcfromtimestamp(seconds).astimezone(timezone).timetuple()

for handler in logging.root.handlers:
handler.formatter.converter = time_converter
app.secret_key = dify_config.SECRET_KEY
initialize_extensions(app)
register_blueprints(app)
register_commands(app)
Expand All @@ -117,6 +79,7 @@ def time_converter(seconds):
def initialize_extensions(app):
# Since the application instance is now created, pass it to each Flask
# extension instance to bind it to the Flask application instance (app)
ext_logging.init_app(app)
ext_compress.init_app(app)
ext_code_based_extension.init()
ext_database.init_app(app)
Expand Down Expand Up @@ -187,7 +150,7 @@ def register_blueprints(app):

CORS(
web_bp,
resources={r"/*": {"origins": app.config["WEB_API_CORS_ALLOW_ORIGINS"]}},
resources={r"/*": {"origins": dify_config.WEB_API_CORS_ALLOW_ORIGINS}},
supports_credentials=True,
allow_headers=["Content-Type", "Authorization", "X-App-Code"],
methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
Expand All @@ -198,7 +161,7 @@ def register_blueprints(app):

CORS(
console_app_bp,
resources={r"/*": {"origins": app.config["CONSOLE_CORS_ALLOW_ORIGINS"]}},
resources={r"/*": {"origins": dify_config.CONSOLE_CORS_ALLOW_ORIGINS}},
supports_credentials=True,
allow_headers=["Content-Type", "Authorization"],
methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
Expand Down
1 change: 1 addition & 0 deletions api/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def migrate_knowledge_vector_database():
VectorType.TENCENT,
VectorType.BAIDU,
VectorType.VIKINGDB,
VectorType.UPSTASH,
}
page = 1
while True:
Expand Down
25 changes: 25 additions & 0 deletions api/configs/feature/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ class SecurityConfig(BaseSettings):
default=5,
)

LOGIN_DISABLED: bool = Field(
description="Whether to disable login checks",
default=False,
)

ADMIN_API_KEY_ENABLE: bool = Field(
description="Whether to enable admin api key for authentication",
default=False,
)

ADMIN_API_KEY: Optional[str] = Field(
description="admin api key for authentication",
default=None,
)


class AppExecutionConfig(BaseSettings):
"""
Expand Down Expand Up @@ -304,6 +319,16 @@ class LoggingConfig(BaseSettings):
default=None,
)

LOG_FILE_MAX_SIZE: PositiveInt = Field(
description="Maximum file size for file rotation retention, the unit is megabytes (MB)",
default=20,
)

LOG_FILE_BACKUP_COUNT: PositiveInt = Field(
description="Maximum file backup count file rotation retention",
default=5,
)

LOG_FORMAT: str = Field(
description="Format string for log messages",
default="%(asctime)s.%(msecs)03d %(levelname)s [%(threadName)s] [%(filename)s:%(lineno)d] - %(message)s",
Expand Down
2 changes: 2 additions & 0 deletions api/configs/middleware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from configs.middleware.vdb.relyt_config import RelytConfig
from configs.middleware.vdb.tencent_vector_config import TencentVectorDBConfig
from configs.middleware.vdb.tidb_vector_config import TiDBVectorConfig
from configs.middleware.vdb.upstash_config import UpstashConfig
from configs.middleware.vdb.vikingdb_config import VikingDBConfig
from configs.middleware.vdb.weaviate_config import WeaviateConfig

Expand Down Expand Up @@ -246,5 +247,6 @@ class MiddlewareConfig(
ElasticsearchConfig,
InternalTestConfig,
VikingDBConfig,
UpstashConfig,
):
pass
20 changes: 20 additions & 0 deletions api/configs/middleware/vdb/upstash_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import Optional

from pydantic import Field
from pydantic_settings import BaseSettings


class UpstashConfig(BaseSettings):
"""
Configuration settings for Upstash vector database
"""

UPSTASH_VECTOR_URL: Optional[str] = Field(
description="URL of the upstash server (e.g., 'https://vector.upstash.io')",
default=None,
)

UPSTASH_VECTOR_TOKEN: Optional[str] = Field(
description="Token for authenticating with the upstash server",
default=None,
)
2 changes: 1 addition & 1 deletion api/configs/packaging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PackagingInfo(BaseSettings):

CURRENT_VERSION: str = Field(
description="Dify version",
default="0.10.0",
default="0.10.1",
)

COMMIT_SHA: str = Field(
Expand Down
4 changes: 3 additions & 1 deletion api/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

if dify_config.ETL_TYPE == "Unstructured":
DOCUMENT_EXTENSIONS = ["txt", "markdown", "md", "pdf", "html", "htm", "xlsx", "xls"]
DOCUMENT_EXTENSIONS.extend(("docx", "csv", "eml", "msg", "pptx", "ppt", "xml", "epub"))
DOCUMENT_EXTENSIONS.extend(("docx", "csv", "eml", "msg", "pptx", "xml", "epub"))
if dify_config.UNSTRUCTURED_API_URL:
DOCUMENT_EXTENSIONS.append("ppt")
DOCUMENT_EXTENSIONS.extend([ext.upper() for ext in DOCUMENT_EXTENSIONS])
else:
DOCUMENT_EXTENSIONS = ["txt", "markdown", "md", "pdf", "html", "htm", "xlsx", "xls", "docx", "csv"]
Expand Down
6 changes: 3 additions & 3 deletions api/controllers/console/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from functools import wraps

from flask import request
from flask_restful import Resource, reqparse
from werkzeug.exceptions import NotFound, Unauthorized

from configs import dify_config
from constants.languages import supported_language
from controllers.console import api
from controllers.console.wraps import only_edition_cloud
Expand All @@ -15,7 +15,7 @@
def admin_required(view):
@wraps(view)
def decorated(*args, **kwargs):
if not os.getenv("ADMIN_API_KEY"):
if not dify_config.ADMIN_API_KEY:
raise Unauthorized("API key is invalid.")

auth_header = request.headers.get("Authorization")
Expand All @@ -31,7 +31,7 @@ def decorated(*args, **kwargs):
if auth_scheme != "bearer":
raise Unauthorized("Invalid Authorization header format. Expected 'Bearer <api-key>' format.")

if os.getenv("ADMIN_API_KEY") != auth_token:
if dify_config.ADMIN_API_KEY != auth_token:
raise Unauthorized("API key is invalid.")

return view(*args, **kwargs)
Expand Down
35 changes: 35 additions & 0 deletions api/controllers/console/app/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,39 @@ def post(self):
return rules


class RuleCodeGenerateApi(Resource):
@setup_required
@login_required
@account_initialization_required
def post(self):
parser = reqparse.RequestParser()
parser.add_argument("instruction", type=str, required=True, nullable=False, location="json")
parser.add_argument("model_config", type=dict, required=True, nullable=False, location="json")
parser.add_argument("no_variable", type=bool, required=True, default=False, location="json")
parser.add_argument("code_language", type=str, required=False, default="javascript", location="json")
args = parser.parse_args()

account = current_user
CODE_GENERATION_MAX_TOKENS = int(os.getenv("CODE_GENERATION_MAX_TOKENS", "1024"))
try:
code_result = LLMGenerator.generate_code(
tenant_id=account.current_tenant_id,
instruction=args["instruction"],
model_config=args["model_config"],
code_language=args["code_language"],
max_tokens=CODE_GENERATION_MAX_TOKENS,
)
except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description)
except QuotaExceededError:
raise ProviderQuotaExceededError()
except ModelCurrentlyNotSupportError:
raise ProviderModelCurrentlyNotSupportError()
except InvokeError as e:
raise CompletionRequestError(e.description)

return code_result


api.add_resource(RuleGenerateApi, "/rule-generate")
api.add_resource(RuleCodeGenerateApi, "/rule-code-generate")
8 changes: 2 additions & 6 deletions api/controllers/console/auth/login.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import cast

import flask_login
from flask import redirect, request
from flask import request
from flask_restful import Resource, reqparse

import services
from configs import dify_config
from constants.languages import languages
from controllers.console import api
from controllers.console.auth.error import (
Expand Down Expand Up @@ -196,10 +195,7 @@ def post(self):
email=user_email, name=user_email, interface_language=languages[0]
)
except WorkSpaceNotAllowedCreateError:
return redirect(
f"{dify_config.CONSOLE_WEB_URL}/signin"
"?message=Workspace not found, please contact system admin to invite you to join in a workspace."
)
return NotAllowedCreateWorkspace()
token_pair = AccountService.login(account, ip_address=extract_remote_ip(request))
AccountService.reset_login_error_rate_limit(args["email"])
return {"result": "success", "data": token_pair.model_dump()}
Expand Down
Loading

0 comments on commit 82886de

Please sign in to comment.