Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: i18n #2011

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions apps/application/serializers/application_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import uuid
from functools import reduce
from typing import Dict, List

from django.contrib.postgres.fields import ArrayField
from django.core import cache, validators
from django.core import signing
Expand Down Expand Up @@ -54,8 +53,7 @@
from setting.serializers.provider_serializers import ModelSerializer
from smartdoc.conf import PROJECT_DIR
from users.models import User
from django.db.models import Value
from django.db.models.fields.json import KeyTextTransform
from django.utils.translation import gettext_lazy as _

chat_cache = cache.caches['chat_cache']

Expand Down Expand Up @@ -194,10 +192,11 @@ def get_base_node_work_flow(work_flow):


class ApplicationSerializer(serializers.Serializer):
name = serializers.CharField(required=True, max_length=64, min_length=1, error_messages=ErrMessage.char("应用名称"))
name = serializers.CharField(required=True, max_length=64, min_length=1,
error_messages=ErrMessage.char(_("application name")))
desc = serializers.CharField(required=False, allow_null=True, allow_blank=True,
max_length=256, min_length=1,
error_messages=ErrMessage.char("应用描述"))
error_messages=ErrMessage.char(_("application describe")))
model_id = serializers.CharField(required=False, allow_null=True, allow_blank=True,
error_messages=ErrMessage.char("模型"))
dialogue_number = serializers.IntegerField(required=True,
Expand Down
14 changes: 7 additions & 7 deletions apps/common/auth/authenticate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from common.exception.app_exception import AppAuthenticationFailed, AppEmbedIdentityFailed, AppChatNumOutOfBoundsFailed, \
ChatException, AppApiException

from django.utils.translation import gettext_lazy as _
token_cache = cache.caches['token_cache']


Expand Down Expand Up @@ -59,19 +59,19 @@ def authenticate(self, request):
auth = auth.replace('Bearer ', '')
# 未认证
if auth is None:
raise AppAuthenticationFailed(1003, '未登录,请先登录')
raise AppAuthenticationFailed(1003, _('Not logged in, please log in first'))
try:
token_details = TokenDetails(auth)
for handle in handles:
if handle.support(request, auth, token_details.get_token_details):
return handle.handle(request, auth, token_details.get_token_details)
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
except Exception as e:
traceback.format_exc()
if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e,
AppApiException):
raise e
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))


class TokenAuth(TokenAuthentication):
Expand All @@ -80,16 +80,16 @@ def authenticate(self, request):
auth = request.META.get('HTTP_AUTHORIZATION')
# 未认证
if auth is None:
raise AppAuthenticationFailed(1003, '未登录,请先登录')
raise AppAuthenticationFailed(1003, _('Not logged in, please log in first'))
try:
token_details = TokenDetails(auth)
for handle in handles:
if handle.support(request, auth, token_details.get_token_details):
return handle.handle(request, auth, token_details.get_token_details)
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
except Exception as e:
traceback.format_exc()
if isinstance(e, AppEmbedIdentityFailed) or isinstance(e, AppChatNumOutOfBoundsFailed) or isinstance(e,
AppApiException):
raise e
raise AppAuthenticationFailed(1002, "身份验证信息不正确!非法用户")
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect! illegal user'))
4 changes: 2 additions & 2 deletions apps/common/auth/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from common.constants.permission_constants import ViewPermission, CompareConstants, RoleConstants, PermissionConstants, \
Permission
from common.exception.app_exception import AppUnauthorizedFailed

from django.utils.translation import gettext_lazy as _

def exist_permissions_by_permission_constants(user_permission: List[PermissionConstants],
permission_list: List[PermissionConstants]):
Expand Down Expand Up @@ -91,7 +91,7 @@ def run(view, request, **kwargs):
# 判断是否有权限
if any(exit_list) if compare == CompareConstants.OR else all(exit_list):
return func(view, request, **kwargs)
raise AppUnauthorizedFailed(403, "没有权限访问")
raise AppUnauthorizedFailed(403, _('No permission to access'))

return run

Expand Down
5 changes: 3 additions & 2 deletions apps/common/auth/handle/impl/application_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
from common.constants.authentication_type import AuthenticationType
from common.constants.permission_constants import Permission, Group, Operate, RoleConstants, Auth
from common.exception.app_exception import AppAuthenticationFailed
from django.utils.translation import gettext_lazy as _


class ApplicationKey(AuthBaseHandle):
def handle(self, request, token: str, get_token_details):
application_api_key = QuerySet(ApplicationApiKey).filter(secret_key=token).first()
if application_api_key is None:
raise AppAuthenticationFailed(500, "secret_key 无效")
raise AppAuthenticationFailed(500, _('Secret key is invalid'))
if not application_api_key.is_active:
raise AppAuthenticationFailed(500, "secret_key 无效")
raise AppAuthenticationFailed(500, _('Secret key is invalid'))
permission_list = [Permission(group=Group.APPLICATION,
operate=Operate.USE,
dynamic_tag=str(
Expand Down
10 changes: 5 additions & 5 deletions apps/common/auth/handle/impl/public_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from common.exception.app_exception import AppAuthenticationFailed, ChatException
from common.models.db_model_manage import DBModelManage
from common.util.common import password_encrypt

from django.utils.translation import gettext_lazy as _

class PublicAccessToken(AuthBaseHandle):
def support(self, request, token: str, get_token_details):
Expand Down Expand Up @@ -45,13 +45,13 @@ def handle(self, request, token: str, get_token_details):
if application_setting.authentication_value.get('type') != authentication.get(
'type') or password_encrypt(
application_setting.authentication_value.get('value')) != authentication.get('value'):
raise ChatException(1002, "身份验证信息不正确")
raise ChatException(1002, _('Authentication information is incorrect'))
if application_access_token is None:
raise AppAuthenticationFailed(1002, "身份验证信息不正确")
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect'))
if not application_access_token.is_active:
raise AppAuthenticationFailed(1002, "身份验证信息不正确")
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect'))
if not application_access_token.access_token == auth_details.get('access_token'):
raise AppAuthenticationFailed(1002, "身份验证信息不正确")
raise AppAuthenticationFailed(1002, _('Authentication information is incorrect'))

return application_access_token.application.user, Auth(
role_list=[RoleConstants.APPLICATION_ACCESS_TOKEN],
Expand Down
4 changes: 2 additions & 2 deletions apps/common/auth/handle/impl/user_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from django.core import cache

from users.models.user import get_user_dynamics_permission

from django.utils.translation import gettext_lazy as _
token_cache = cache.caches['token_cache']


Expand All @@ -31,7 +31,7 @@ def support(self, request, token: str, get_token_details):
def handle(self, request, token: str, get_token_details):
cache_token = token_cache.get(token)
if cache_token is None:
raise AppAuthenticationFailed(1002, "登录过期")
raise AppAuthenticationFailed(1002, _('Login expired'))
auth_details = get_token_details()
user = QuerySet(User).get(id=auth_details['id'])
# 续期
Expand Down
24 changes: 14 additions & 10 deletions apps/common/constants/exception_code_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from enum import Enum

from common.exception.app_exception import AppApiException
from django.utils.translation import gettext_lazy as _


class ExceptionCodeConstantsValue:
Expand All @@ -27,13 +28,16 @@ def to_app_api_exception(self):


class ExceptionCodeConstants(Enum):
INCORRECT_USERNAME_AND_PASSWORD = ExceptionCodeConstantsValue(1000, "用户名或者密码不正确")
NOT_AUTHENTICATION = ExceptionCodeConstantsValue(1001, "请先登录,并携带用户Token")
EMAIL_SEND_ERROR = ExceptionCodeConstantsValue(1002, "邮件发送失败")
EMAIL_FORMAT_ERROR = ExceptionCodeConstantsValue(1003, "邮箱格式错误")
EMAIL_IS_EXIST = ExceptionCodeConstantsValue(1004, "邮箱已经被注册,请勿重复注册")
EMAIL_IS_NOT_EXIST = ExceptionCodeConstantsValue(1005, "邮箱尚未注册,请先注册")
CODE_ERROR = ExceptionCodeConstantsValue(1005, "验证码不正确,或者验证码过期")
USERNAME_IS_EXIST = ExceptionCodeConstantsValue(1006, "用户名已被使用,请使用其他用户名")
USERNAME_ERROR = ExceptionCodeConstantsValue(1006, "用户名不能为空,并且长度在6-20")
PASSWORD_NOT_EQ_RE_PASSWORD = ExceptionCodeConstantsValue(1007, "密码与确认密码不一致")
INCORRECT_USERNAME_AND_PASSWORD = ExceptionCodeConstantsValue(1000, _('The username or password is incorrect'))
NOT_AUTHENTICATION = ExceptionCodeConstantsValue(1001, _('Please log in first and bring the user Token'))
EMAIL_SEND_ERROR = ExceptionCodeConstantsValue(1002, _('Email sending failed'))
EMAIL_FORMAT_ERROR = ExceptionCodeConstantsValue(1003, _('Email format error'))
EMAIL_IS_EXIST = ExceptionCodeConstantsValue(1004, _('The email has been registered, please log in directly'))
EMAIL_IS_NOT_EXIST = ExceptionCodeConstantsValue(1005, _('The email is not registered, please register first'))
CODE_ERROR = ExceptionCodeConstantsValue(1005,
_('The verification code is incorrect or the verification code has expired'))
USERNAME_IS_EXIST = ExceptionCodeConstantsValue(1006, _('The username has been registered, please log in directly'))
USERNAME_ERROR = ExceptionCodeConstantsValue(1006,
_('The username cannot be empty and must be between 6 and 20 characters long.'))
PASSWORD_NOT_EQ_RE_PASSWORD = ExceptionCodeConstantsValue(1007,
_('Password and confirmation password are inconsistent'))
10 changes: 5 additions & 5 deletions apps/common/constants/permission_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""
from enum import Enum
from typing import List

from django.utils.translation import gettext_lazy as _

class Group(Enum):
"""
Expand Down Expand Up @@ -58,10 +58,10 @@ def __init__(self, name: str, decs: str, group: RoleGroup):


class RoleConstants(Enum):
ADMIN = Role("管理员", "管理员,预制目前不会使用", RoleGroup.USER)
USER = Role("用户", "用户所有权限", RoleGroup.USER)
APPLICATION_ACCESS_TOKEN = Role("会话", "只拥有应用会话框接口权限", RoleGroup.APPLICATION_ACCESS_TOKEN),
APPLICATION_KEY = Role("应用私钥", "应用私钥", RoleGroup.APPLICATION_KEY)
ADMIN = Role(_("ADMIN"), _('Admin, prefabs are not currently used'), RoleGroup.USER)
USER = Role(_("USER"), _('All user permissions'), RoleGroup.USER)
APPLICATION_ACCESS_TOKEN = Role(_('chat'), _('Only has application dialog interface permissions'), RoleGroup.APPLICATION_ACCESS_TOKEN),
APPLICATION_KEY = Role(_('Apply private key'), _('Apply private key'), RoleGroup.APPLICATION_KEY)


class Permission:
Expand Down
6 changes: 4 additions & 2 deletions apps/common/event/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import setting.models
from setting.models import Model
from .listener_manage import *
from common.db.sql_execute import update_execute
from django.utils.translation import gettext as _

from ..db.sql_execute import update_execute

update_document_status_sql = """
UPDATE "public"."document"
Expand All @@ -20,5 +22,5 @@
def run():
# QuerySet(Document).filter(status__in=[Status.embedding, Status.queue_up]).update(**{'status': Status.error})
QuerySet(Model).filter(status=setting.models.Status.DOWNLOAD).update(status=setting.models.Status.ERROR,
meta={'message': "下载程序被中断,请重试"})
meta={'message': _('The download process was interrupted, please try again')})
update_execute(update_document_status_sql, [])
36 changes: 22 additions & 14 deletions apps/common/event/listener_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from dataset.models import Paragraph, Status, Document, ProblemParagraphMapping, TaskType, State
from embedding.models import SourceType, SearchMode
from smartdoc.conf import PROJECT_DIR
from django.utils.translation import gettext_lazy as _

max_kb_error = logging.getLogger(__file__)
max_kb = logging.getLogger(__file__)
Expand Down Expand Up @@ -86,11 +87,12 @@ def embedding_by_paragraph_list(paragraph_id_list, embedding_model: Embeddings):
ListenerManagement.embedding_by_paragraph_data_list(data_list, paragraph_id_list=paragraph_id_list,
embedding_model=embedding_model)
except Exception as e:
max_kb_error.error(f'查询向量数据:{paragraph_id_list}出现错误{str(e)}{traceback.format_exc()}')
max_kb_error.error(_('Query vector data: {paragraph_id_list} error {error} {traceback}').format(
paragraph_id_list=paragraph_id_list, error=str(e), traceback=traceback.format_exc()))

@staticmethod
def embedding_by_paragraph_data_list(data_list, paragraph_id_list, embedding_model: Embeddings):
max_kb.info(f'开始--->向量化段落:{paragraph_id_list}')
max_kb.info(_('Start--->Embedding paragraph: {paragraph_id_list}').format(paragraph_id_list=paragraph_id_list))
status = Status.success
try:
# 删除段落
Expand All @@ -102,11 +104,13 @@ def is_save_function():
# 批量向量化
VectorStore.get_embedding_vector().batch_save(data_list, embedding_model, is_save_function)
except Exception as e:
max_kb_error.error(f'向量化段落:{paragraph_id_list}出现错误{str(e)}{traceback.format_exc()}')
max_kb_error.error(_('Vectorized paragraph: {paragraph_id_list} error {error} {traceback}').format(
paragraph_id_list=paragraph_id_list, error=str(e), traceback=traceback.format_exc()))
status = Status.error
finally:
QuerySet(Paragraph).filter(id__in=paragraph_id_list).update(**{'status': status})
max_kb.info(f'结束--->向量化段落:{paragraph_id_list}')
max_kb.info(
_('End--->Embedding paragraph: {paragraph_id_list}').format(paragraph_id_list=paragraph_id_list))

@staticmethod
def embedding_by_paragraph(paragraph_id, embedding_model: Embeddings):
Expand All @@ -115,7 +119,7 @@ def embedding_by_paragraph(paragraph_id, embedding_model: Embeddings):
@param paragraph_id: 段落id
@param embedding_model: 向量模型
"""
max_kb.info(f"开始--->向量化段落:{paragraph_id}")
max_kb.info(_('Start--->Embedding paragraph: {paragraph_id}').format(paragraph_id=paragraph_id))
# 更新到开始状态
ListenerManagement.update_status(QuerySet(Paragraph).filter(id=paragraph_id), TaskType.EMBEDDING, State.STARTED)
try:
Expand All @@ -140,11 +144,12 @@ def is_the_task_interrupted():
ListenerManagement.update_status(QuerySet(Paragraph).filter(id=paragraph_id), TaskType.EMBEDDING,
State.SUCCESS)
except Exception as e:
max_kb_error.error(f'向量化段落:{paragraph_id}出现错误{str(e)}{traceback.format_exc()}')
max_kb_error.error(_('Vectorized paragraph: {paragraph_id} error {error} {traceback}').format(
paragraph_id=paragraph_id, error=str(e), traceback=traceback.format_exc()))
ListenerManagement.update_status(QuerySet(Paragraph).filter(id=paragraph_id), TaskType.EMBEDDING,
State.FAILURE)
finally:
max_kb.info(f'结束--->向量化段落:{paragraph_id}')
max_kb.info(_('End--->Embedding paragraph: {paragraph_id}').format(paragraph_id=paragraph_id))

@staticmethod
def embedding_by_data_list(data_list: List, embedding_model: Embeddings):
Expand Down Expand Up @@ -258,7 +263,8 @@ def is_the_task_interrupted():

if is_the_task_interrupted():
return
max_kb.info(f"开始--->向量化文档:{document_id}")
max_kb.info(_('Start--->Embedding document: {document_id}').format(document_id=document_id)
)
# 批量修改状态为PADDING
ListenerManagement.update_status(QuerySet(Document).filter(id=document_id), TaskType.EMBEDDING,
State.STARTED)
Expand All @@ -279,11 +285,12 @@ def is_the_task_interrupted():
document_id)),
is_the_task_interrupted)
except Exception as e:
max_kb_error.error(f'向量化文档:{document_id}出现错误{str(e)}{traceback.format_exc()}')
max_kb_error.error(_('Vectorized document: {document_id} error {error} {traceback}').format(
document_id=document_id, error=str(e), traceback=traceback.format_exc()))
finally:
ListenerManagement.post_update_document_status(document_id, TaskType.EMBEDDING)
ListenerManagement.get_aggregation_document_status(document_id)()
max_kb.info(f"结束--->向量化文档:{document_id}")
max_kb.info(_('End--->Embedding document: {document_id}').format(document_id=document_id))
un_lock('embedding' + str(document_id))

@staticmethod
Expand All @@ -294,17 +301,18 @@ def embedding_by_dataset(dataset_id, embedding_model: Embeddings):
@param embedding_model 向量模型
:return: None
"""
max_kb.info(f"开始--->向量化数据集:{dataset_id}")
max_kb.info(_('Start--->Embedding dataset: {dataset_id}').format(dataset_id=dataset_id))
try:
ListenerManagement.delete_embedding_by_dataset(dataset_id)
document_list = QuerySet(Document).filter(dataset_id=dataset_id)
max_kb.info(f"数据集文档:{[d.name for d in document_list]}")
max_kb.info(_('Start--->Embedding document: {document_list}').format(document_list=document_list))
for document in document_list:
ListenerManagement.embedding_by_document(document.id, embedding_model=embedding_model)
except Exception as e:
max_kb_error.error(f'向量化数据集:{dataset_id}出现错误{str(e)}{traceback.format_exc()}')
max_kb_error.error(_('Vectorized dataset: {dataset_id} error {error} {traceback}').format(
dataset_id=dataset_id, error=str(e), traceback=traceback.format_exc()))
finally:
max_kb.info(f"结束--->向量化数据集:{dataset_id}")
max_kb.info(_('End--->Embedding dataset: {dataset_id}').format(dataset_id=dataset_id))

@staticmethod
def delete_embedding_by_document(document_id):
Expand Down
Loading
Loading