Skip to content

Commit

Permalink
Merge branch 'refs/heads/main' into poetry-docker
Browse files Browse the repository at this point in the history
  • Loading branch information
takatost committed Jun 21, 2024
2 parents 4e4c4f0 + 91d38a5 commit 8ca0136
Show file tree
Hide file tree
Showing 168 changed files with 1,068 additions and 329 deletions.
41 changes: 20 additions & 21 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from configs.app_configs import DifyConfigs

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

monkey.patch_all()
Expand Down Expand Up @@ -152,27 +152,26 @@ def initialize_extensions(app):
@login_manager.request_loader
def load_user_from_request(request_from_flask_login):
"""Load user based on the request."""
if request.blueprint in ['console', 'inner_api']:
# Check if the user_id contains a dot, indicating the old format
auth_header = request.headers.get('Authorization', '')
if not auth_header:
auth_token = request.args.get('_token')
if not auth_token:
raise Unauthorized('Invalid Authorization token.')
else:
if ' ' not in auth_header:
raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')
auth_scheme, auth_token = auth_header.split(None, 1)
auth_scheme = auth_scheme.lower()
if auth_scheme != 'bearer':
raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')

decoded = PassportService().verify(auth_token)
user_id = decoded.get('user_id')

return AccountService.load_user(user_id)
else:
if request.blueprint not in ['console', 'inner_api']:
return None
# Check if the user_id contains a dot, indicating the old format
auth_header = request.headers.get('Authorization', '')
if not auth_header:
auth_token = request.args.get('_token')
if not auth_token:
raise Unauthorized('Invalid Authorization token.')
else:
if ' ' not in auth_header:
raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')
auth_scheme, auth_token = auth_header.split(None, 1)
auth_scheme = auth_scheme.lower()
if auth_scheme != 'bearer':
raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')

decoded = PassportService().verify(auth_token)
user_id = decoded.get('user_id')

return AccountService.load_logged_in_account(account_id=user_id, token=auth_token)


@login_manager.unauthorized_handler
Expand Down
4 changes: 3 additions & 1 deletion api/controllers/console/app/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def parse_app_site_args():
required=False,
location='json')
parser.add_argument('prompt_public', type=bool, required=False, location='json')
parser.add_argument('show_workflow_steps', type=bool, required=False, location='json')
return parser.parse_args()


Expand Down Expand Up @@ -59,7 +60,8 @@ def post(self, app_model):
'privacy_policy',
'custom_disclaimer',
'customize_token_strategy',
'prompt_public'
'prompt_public',
'show_workflow_steps'
]:
value = args.get(attr_name)
if value is not None:
Expand Down
13 changes: 8 additions & 5 deletions api/controllers/console/auth/login.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from typing import cast

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

import services
from controllers.console import api
from controllers.console.setup import setup_required
from libs.helper import email
from libs.helper import email, get_remote_ip
from libs.password import valid_password
from models.account import Account
from services.account_service import AccountService, TenantService


Expand Down Expand Up @@ -34,10 +37,7 @@ def post(self):
if len(tenants) == 0:
return {'result': 'fail', 'data': 'workspace not found, please contact system admin to invite you to join in a workspace'}

AccountService.update_last_login(account, request)

# todo: return the user info
token = AccountService.get_account_jwt_token(account)
token = AccountService.login(account, ip_address=get_remote_ip(request))

return {'result': 'success', 'data': token}

Expand All @@ -46,6 +46,9 @@ class LogoutApi(Resource):

@setup_required
def get(self):
account = cast(Account, flask_login.current_user)
token = request.headers.get('Authorization', '').split(' ')[1]
AccountService.logout(account=account, token=token)
flask_login.logout_user()
return {'result': 'success'}

Expand Down
5 changes: 2 additions & 3 deletions api/controllers/console/auth/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from constants.languages import languages
from extensions.ext_database import db
from libs.helper import get_remote_ip
from libs.oauth import GitHubOAuth, GoogleOAuth, OAuthUserInfo
from models.account import Account, AccountStatus
from services.account_service import AccountService, RegisterService, TenantService
Expand Down Expand Up @@ -78,9 +79,7 @@ def get(self, provider: str):

TenantService.create_owner_tenant_if_not_exist(account)

AccountService.update_last_login(account, request)

token = AccountService.get_account_jwt_token(account)
token = AccountService.login(account, ip_address=get_remote_ip(request))

return redirect(f'{current_app.config.get("CONSOLE_WEB_URL")}?console_token={token}')

Expand Down
4 changes: 2 additions & 2 deletions api/controllers/console/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from flask_restful import Resource, reqparse

from extensions.ext_database import db
from libs.helper import email, str_len
from libs.helper import email, get_remote_ip, str_len
from libs.password import valid_password
from models.model import DifySetup
from services.account_service import AccountService, RegisterService, TenantService
Expand Down Expand Up @@ -61,7 +61,7 @@ def post(self):
TenantService.create_owner_tenant_if_not_exist(account)

setup()
AccountService.update_last_login(account, request)
AccountService.update_last_login(account, ip_address=get_remote_ip(request))

return {'result': 'success'}, 201

Expand Down
3 changes: 2 additions & 1 deletion api/controllers/web/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class AppSiteApi(WebApiResource):
'privacy_policy': fields.String,
'custom_disclaimer': fields.String,
'default_language': fields.String,
'prompt_public': fields.Boolean
'prompt_public': fields.Boolean,
'show_workflow_steps': fields.Boolean,
}

app_fields = {
Expand Down
4 changes: 2 additions & 2 deletions api/core/entities/provider_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def custom_credentials_validate(self, credentials: dict) -> tuple[Provider, dict
credentials[key] = encrypter.decrypt_token(self.tenant_id, original_credentials[key])

credentials = model_provider_factory.provider_credentials_validate(
self.provider.provider,
credentials
provider=self.provider.provider,
credentials=credentials
)

for key, value in credentials.items():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- claude-3-5-sonnet-20240620
- claude-3-haiku-20240307
- claude-3-opus-20240229
- claude-3-sonnet-20240229
- claude-2.1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
model: claude-3-5-sonnet-20240620
label:
en_US: claude-3-5-sonnet-20240620
model_type: llm
features:
- agent-thought
- vision
- tool-call
- stream-tool-call
model_properties:
mode: chat
context_size: 200000
parameter_rules:
- name: temperature
use_template: temperature
- name: top_p
use_template: top_p
- name: top_k
label:
zh_Hans: 取样数量
en_US: Top k
type: int
help:
zh_Hans: 仅从每个后续标记的前 K 个选项中采样。
en_US: Only sample from the top K options for each subsequent token.
required: false
- name: max_tokens
use_template: max_tokens
required: true
default: 4096
min: 1
max: 4096
- name: response_format
use_template: response_format
pricing:
input: '3.00'
output: '15.00'
unit: '0.000001'
currency: USD
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
model: anthropic.claude-3-5-sonnet-20240620-v1:0
label:
en_US: Claude 3.5 Sonnet
model_type: llm
features:
- agent-thought
- vision
model_properties:
mode: chat
context_size: 200000
# docs: https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html
parameter_rules:
- name: max_tokens
use_template: max_tokens
required: true
type: int
default: 4096
min: 1
max: 4096
help:
zh_Hans: 停止前生成的最大令牌数。请注意,Anthropic Claude 模型可能会在达到 max_tokens 的值之前停止生成令牌。不同的 Anthropic Claude 模型对此参数具有不同的最大值。
en_US: The maximum number of tokens to generate before stopping. Note that Anthropic Claude models might stop generating tokens before reaching the value of max_tokens. Different Anthropic Claude models have different maximum values for this parameter.
- name: temperature
use_template: temperature
required: false
type: float
default: 1
min: 0.0
max: 1.0
help:
zh_Hans: 生成内容的随机性。
en_US: The amount of randomness injected into the response.
- name: top_p
required: false
type: float
default: 0.999
min: 0.000
max: 1.000
help:
zh_Hans: 在核采样中,Anthropic Claude 按概率递减顺序计算每个后续标记的所有选项的累积分布,并在达到 top_p 指定的特定概率时将其切断。您应该更改温度或top_p,但不能同时更改两者。
en_US: In nucleus sampling, Anthropic Claude computes the cumulative distribution over all the options for each subsequent token in decreasing probability order and cuts it off once it reaches a particular probability specified by top_p. You should alter either temperature or top_p, but not both.
- name: top_k
required: false
type: int
default: 0
min: 0
# tip docs from aws has error, max value is 500
max: 500
help:
zh_Hans: 对于每个后续标记,仅从前 K 个选项中进行采样。使用 top_k 删除长尾低概率响应。
en_US: Only sample from the top K options for each subsequent token. Use top_k to remove long tail low probability responses.
pricing:
input: '0.003'
output: '0.015'
unit: '0.001'
currency: USD
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ parameter_rules:
- name: max_tokens
use_template: max_tokens
type: int
default: 1500
default: 2000
min: 1
max: 1500
max: 2000
help:
zh_Hans: 用于指定模型在生成内容时token的最大数量,它定义了生成的上限,但不保证每次都会生成到这个数量。
en_US: It is used to specify the maximum number of tokens when the model generates content. It defines the upper limit of generation, but does not guarantee that this number will be generated every time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ parameter_rules:
- name: max_tokens
use_template: max_tokens
type: int
default: 1500
default: 2000
min: 1
max: 1500
max: 2000
help:
zh_Hans: 用于指定模型在生成内容时token的最大数量,它定义了生成的上限,但不保证每次都会生成到这个数量。
en_US: It is used to specify the maximum number of tokens when the model generates content. It defines the upper limit of generation, but does not guarantee that this number will be generated every time.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
model: claude-3-5-sonnet@20240620
label:
en_US: Claude 3.5 Sonnet
model_type: llm
features:
- agent-thought
- vision
model_properties:
mode: chat
context_size: 200000
parameter_rules:
- name: max_tokens
use_template: max_tokens
required: true
type: int
default: 4096
min: 1
max: 4096
help:
zh_Hans: 停止前生成的最大令牌数。请注意,Anthropic Claude 模型可能会在达到 max_tokens 的值之前停止生成令牌。不同的 Anthropic Claude 模型对此参数具有不同的最大值。
en_US: The maximum number of tokens to generate before stopping. Note that Anthropic Claude models might stop generating tokens before reaching the value of max_tokens. Different Anthropic Claude models have different maximum values for this parameter.
- name: temperature
use_template: temperature
required: false
type: float
default: 1
min: 0.0
max: 1.0
help:
zh_Hans: 生成内容的随机性。
en_US: The amount of randomness injected into the response.
- name: top_p
required: false
type: float
default: 0.999
min: 0.000
max: 1.000
help:
zh_Hans: 在核采样中,Anthropic Claude 按概率递减顺序计算每个后续标记的所有选项的累积分布,并在达到 top_p 指定的特定概率时将其切断。您应该更改温度或top_p,但不能同时更改两者。
en_US: In nucleus sampling, Anthropic Claude computes the cumulative distribution over all the options for each subsequent token in decreasing probability order and cuts it off once it reaches a particular probability specified by top_p. You should alter either temperature or top_p, but not both.
- name: top_k
required: false
type: int
default: 0
min: 0
# tip docs from aws has error, max value is 500
max: 500
help:
zh_Hans: 对于每个后续标记,仅从前 K 个选项中进行采样。使用 top_k 删除长尾低概率响应。
en_US: Only sample from the top K options for each subsequent token. Use top_k to remove long tail low probability responses.
pricing:
input: '0.003'
output: '0.015'
unit: '0.001'
currency: USD
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def _generate_anthropic(self, model: str, credentials: dict, prompt_messages: li
token = credentials.token

# Vertex AI Anthropic Claude3 Opus model available in us-east5 region, Sonnet and Haiku available in us-central1 region
if 'opus' in model:
if 'opus' or 'claude-3-5-sonnet' in model:
location = 'us-east5'
else:
location = 'us-central1'
Expand Down
4 changes: 3 additions & 1 deletion api/fields/app_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
'customize_token_strategy': fields.String,
'prompt_public': fields.Boolean,
'app_base_url': fields.String,
'show_workflow_steps': fields.Boolean,
}

app_detail_fields_with_site = {
Expand Down Expand Up @@ -149,5 +150,6 @@
'privacy_policy': fields.String,
'custom_disclaimer': fields.String,
'customize_token_strategy': fields.String,
'prompt_public': fields.Boolean
'prompt_public': fields.Boolean,
'show_workflow_steps': fields.Boolean,
}
2 changes: 1 addition & 1 deletion api/libs/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def generate_string(n):
return result


def get_remote_ip(request):
def get_remote_ip(request) -> str:
if request.headers.get('CF-Connecting-IP'):
return request.headers.get('Cf-Connecting-Ip')
elif request.headers.getlist("X-Forwarded-For"):
Expand Down
Loading

0 comments on commit 8ca0136

Please sign in to comment.