Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/maxkb-dev/maxkb
Browse files Browse the repository at this point in the history
  • Loading branch information
liqiang-fit2cloud committed Dec 9, 2024
2 parents 89e7581 + 5c64d63 commit 1821640
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 17 deletions.
13 changes: 13 additions & 0 deletions apps/common/handle/impl/pdf_split_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
max_kb = logging.getLogger("max_kb")


def check_links_in_pdf(doc):
for page_number in range(len(doc)):
page = doc[page_number]
links = page.get_links()
if links:
for link in links:
if link['kind'] == 1:
return True
return False

class PdfSplitHandle(BaseSplitHandle):
def handle(self, file, pattern_list: List, with_filter: bool, limit: int, get_buffer, save_image):
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
Expand Down Expand Up @@ -175,6 +185,9 @@ def handle_toc(doc, limit):

@staticmethod
def handle_links(doc, pattern_list, with_filter, limit):
# 检查文档是否包含内部链接
if not check_links_in_pdf(doc):
return
# 创建存储章节内容的数组
chapters = []
toc_start_page = -1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# coding=utf-8
import base64
import os
from typing import Dict

from langchain_core.messages import HumanMessage

from common import forms
from common.exception.app_exception import AppApiException
from common.forms import BaseForm
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode


class ZhiPuImageModelCredential(BaseForm, BaseModelCredential):
api_key = forms.PasswordInputField('API Key', required=True)

def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], provider,
raise_exception=False):
model_type_list = provider.get_model_type_list()
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
raise AppApiException(ValidCode.valid_error.value, f'{model_type} 模型类型不支持')

for key in ['api_key']:
if key not in model_credential:
if raise_exception:
raise AppApiException(ValidCode.valid_error.value, f'{key} 字段为必填字段')
else:
return False
try:
model = provider.get_model(model_type, model_name, model_credential)
res = model.stream([HumanMessage(content=[{"type": "text", "text": "你好"}])])
for chunk in res:
print(chunk)
except Exception as e:
if isinstance(e, AppApiException):
raise e
if raise_exception:
raise AppApiException(ValidCode.valid_error.value, f'校验失败,请检查参数是否正确: {str(e)}')
else:
return False
return True

def encryption_dict(self, model: Dict[str, object]):
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}

def get_model_params_setting_form(self, model_name):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Dict

from langchain_openai.chat_models import ChatOpenAI

from common.config.tokenizer_manage_config import TokenizerManage
from setting.models_provider.base_model_provider import MaxKBBaseModel


def custom_get_token_ids(text: str):
tokenizer = TokenizerManage.get_tokenizer()
return tokenizer.encode(text)


class ZhiPuImage(MaxKBBaseModel, ChatOpenAI):

@staticmethod
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
return ZhiPuImage(
model_name=model_name,
openai_api_key=model_credential.get('api_key'),
openai_api_base='https://open.bigmodel.cn/api/paas/v4',
# stream_options={"include_usage": True},
streaming=True,
**optional_params,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,40 @@
from common.util.file_util import get_file_content
from setting.models_provider.base_model_provider import ModelProvideInfo, ModelTypeConst, ModelInfo, IModelProvider, \
ModelInfoManage
from setting.models_provider.impl.zhipu_model_provider.credential.image import ZhiPuImageModelCredential
from setting.models_provider.impl.zhipu_model_provider.credential.llm import ZhiPuLLMModelCredential
from setting.models_provider.impl.zhipu_model_provider.model.image import ZhiPuImage
from setting.models_provider.impl.zhipu_model_provider.model.llm import ZhipuChatModel
from smartdoc.conf import PROJECT_DIR

qwen_model_credential = ZhiPuLLMModelCredential()
zhipu_image_model_credential = ZhiPuImageModelCredential()

model_info_list = [
ModelInfo('glm-4', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel),
ModelInfo('glm-4v', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel),
ModelInfo('glm-3-turbo', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel)
]
model_info_manage = ModelInfoManage.builder().append_model_info_list(model_info_list).append_default_model_info(
ModelInfo('glm-4', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel)).build()

model_info_image_list = [
ModelInfo('glm-4v-plus', '具有强大的多模态理解能力。能够同时理解多达五张图像,并支持视频内容理解',
ModelTypeConst.IMAGE, zhipu_image_model_credential,
ZhiPuImage),
ModelInfo('glm-4v', '专注于单图理解。适用于需要高效图像解析的场景',
ModelTypeConst.IMAGE, zhipu_image_model_credential,
ZhiPuImage),
ModelInfo('glm-4v-flash', '专注于单图理解。适用于需要高效图像解析的场景(免费)',
ModelTypeConst.IMAGE, zhipu_image_model_credential,
ZhiPuImage),
]

model_info_manage = (
ModelInfoManage.builder()
.append_model_info_list(model_info_list)
.append_default_model_info(ModelInfo('glm-4', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel))
.append_model_info_list(model_info_image_list)
.build()
)


class ZhiPuModelProvider(IModelProvider):
Expand Down
2 changes: 1 addition & 1 deletion installer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ENV MAXKB_VERSION="${DOCKER_IMAGE_TAG} (build at ${BUILD_AT}, commit: ${GITHUB_C
MAXKB_DB_PASSWORD=Password123@postgres \
MAXKB_EMBEDDING_MODEL_NAME=/opt/maxkb/model/embedding/shibing624_text2vec-base-chinese \
MAXKB_EMBEDDING_MODEL_PATH=/opt/maxkb/model/embedding \
MAXKB_SANDBOX=true \
MAXKB_SANDBOX=1 \
LANG=en_US.UTF-8 \
PATH=/opt/py3/bin:$PATH \
POSTGRES_USER=root \
Expand Down
60 changes: 52 additions & 8 deletions ui/src/components/ai-chat/component/chat-input-operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div
class="p-8-12"
v-loading="localLoading"
v-if="uploadDocumentList.length || uploadImageList.length"
v-if="uploadDocumentList.length || uploadImageList.length || uploadAudioList.length || uploadVideoList.length"
>
<el-space wrap>
<template v-for="(item, index) in uploadDocumentList" :key="index">
Expand Down Expand Up @@ -53,6 +53,27 @@
/>
</div>
</template>
<template v-for="(item, index) in uploadAudioList" :key="index">
<el-card shadow="never" style="--el-card-padding: 8px" class="file cursor">
<div
class="flex align-center"
@mouseenter.stop="mouseenter(item)"
@mouseleave.stop="mouseleave()"
>
<div
@click="deleteFile(index, 'audio')"
class="delete-icon color-secondary"
v-if="showDelete === item.url"
>
<el-icon><CircleCloseFilled /></el-icon>
</div>
<img :src="getImgUrl(item && item?.name)" alt="" width="24" />
<div class="ml-4 ellipsis" style="max-width: 160px" :title="item && item?.name">
{{ item && item?.name }}
</div>
</div>
</el-card>
</template>
</el-space>
</div>
</el-scrollbar>
Expand Down Expand Up @@ -200,7 +221,7 @@ const localLoading = computed({
const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp']
const documentExtensions = ['pdf', 'docx', 'txt', 'xls', 'xlsx', 'md', 'html', 'csv']
const videoExtensions = ['mp4', 'avi', 'mov', 'mkv', 'flv']
const audioExtensions = ['mp3', 'wav', 'aac', 'flac']
const audioExtensions = ['mp3']
const getAcceptList = () => {
const { image, document, audio, video } = props.applicationDetails.file_upload_setting
Expand All @@ -227,14 +248,14 @@ const getAcceptList = () => {
const checkMaxFilesLimit = () => {
return (
props.applicationDetails.file_upload_setting.maxFiles <=
uploadImageList.value.length + uploadDocumentList.value.length
uploadImageList.value.length + uploadDocumentList.value.length + uploadAudioList.value.length + uploadVideoList.value.length
)
}
const uploadFile = async (file: any, fileList: any) => {
const { maxFiles, fileLimit } = props.applicationDetails.file_upload_setting
// 单次上传文件数量限制
const file_limit_once = uploadImageList.value.length + uploadDocumentList.value.length
const file_limit_once = uploadImageList.value.length + uploadDocumentList.value.length + uploadAudioList.value.length + uploadVideoList.value.length
if (file_limit_once >= maxFiles) {
MsgWarning('最多上传' + maxFiles + '个文件')
fileList.splice(0, fileList.length)
Expand All @@ -257,9 +278,9 @@ const uploadFile = async (file: any, fileList: any) => {
} else if (documentExtensions.includes(extension)) {
uploadDocumentList.value.push(file)
} else if (videoExtensions.includes(extension)) {
// videos.push(file)
uploadVideoList.value.push(file)
} else if (audioExtensions.includes(extension)) {
// audios.push(file)
uploadAudioList.value.push(file)
}
Expand Down Expand Up @@ -297,7 +318,20 @@ const uploadFile = async (file: any, fileList: any) => {
file.file_id = f[0].file_id
}
})
console.log(uploadDocumentList.value, uploadImageList.value)
uploadAudioList.value.forEach((file: any) => {
const f = response.data.filter((f: any) => f.name === file.name)
if (f.length > 0) {
file.url = f[0].url
file.file_id = f[0].file_id
}
})
uploadVideoList.value.forEach((file: any) => {
const f = response.data.filter((f: any) => f.name === file.name)
if (f.length > 0) {
file.url = f[0].url
file.file_id = f[0].file_id
}
})
})
}
const recorderTime = ref(0)
Expand All @@ -306,6 +340,8 @@ const recorderLoading = ref(false)
const inputValue = ref<string>('')
const uploadImageList = ref<Array<any>>([])
const uploadDocumentList = ref<Array<any>>([])
const uploadVideoList = ref<Array<any>>([])
const uploadAudioList = ref<Array<any>>([])
const mediaRecorderStatus = ref(true)
const showDelete = ref('')
Expand Down Expand Up @@ -433,11 +469,15 @@ function sendChatHandle(event: any) {
if (inputValue.value.trim()) {
props.sendMessage(inputValue.value, {
image_list: uploadImageList.value,
document_list: uploadDocumentList.value
document_list: uploadDocumentList.value,
audio_list: uploadAudioList.value,
video_list: uploadVideoList.value,
})
inputValue.value = ''
uploadImageList.value = []
uploadDocumentList.value = []
uploadAudioList.value = []
uploadVideoList.value = []
quickInputRef.value.textareaStyle.height = '45px'
}
}
Expand All @@ -452,6 +492,10 @@ function deleteFile(index: number, val: string) {
uploadImageList.value.splice(index, 1)
} else if (val === 'document') {
uploadDocumentList.value.splice(index, 1)
} else if (val === 'video') {
uploadVideoList.value.splice(index, 1)
} else if (val === 'audio') {
uploadAudioList.value.splice(index, 1)
}
}
function mouseenter(row: any) {
Expand Down
2 changes: 2 additions & 0 deletions ui/src/components/common-list/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ defineExpose({
li {
padding: 10px 16px;
font-weight: 400;
color: var(--el-text-color-regular);
font-size: 14px;
&.active {
background: var(--el-color-primary-light-9);
border-radius: 4px;
Expand Down
4 changes: 4 additions & 0 deletions ui/src/styles/element-plus.scss
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@

.el-card {
--el-card-padding: calc(var(--app-base-px) * 2);
color: var(--el-text-color-regular);
}
.el-dropdown {
color: var(--app-text-color);
Expand Down Expand Up @@ -267,6 +268,9 @@
.el-select-group .el-select-dropdown__item {
padding-left: 11px;
}
.el-select-dropdown__item {
font-weight: 400;
}

.el-select__caret {
color: var(--app-text-color-secondary);
Expand Down
8 changes: 7 additions & 1 deletion ui/src/views/authentication/component/EditModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@ const open = async (platform: Platform) => {
let defaultCallbackUrl = window.location.origin
switch (platform.key) {
case 'wecom':
if (currentPlatform.config.app_key) {
currentPlatform.config.agent_id = currentPlatform.config.app_key
delete currentPlatform.config.app_key
}
currentPlatform.config.callback_url = `${defaultCallbackUrl}/api/wecom`
break
case 'dingtalk':
if (currentPlatform.config.agent_id && currentPlatform.key === 'dingtalk') {
if (currentPlatform.config.agent_id) {
currentPlatform.config.corp_id = currentPlatform.config.agent_id
delete currentPlatform.config.agent_id
}
Expand Down
13 changes: 8 additions & 5 deletions ui/src/views/template/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<LayoutContainer header="模型设置">
<div class="template-manage flex main-calc-height">
<div class="template-manage__left p-8 border-r">
<h4 style="padding-bottom: 8px">供应商</h4>
<h4 class="p-16">供应商</h4>
<div class="model-list-height-left">
<div
class="all-mode flex cursor"
Expand Down Expand Up @@ -33,7 +33,7 @@
ref="commonList1"
>
<template #default="{ row }">
<div class="flex">
<div class="flex align-center">
<span
:innerHTML="row.icon"
alt=""
Expand All @@ -59,7 +59,7 @@
ref="commonList2"
>
<template #default="{ row }">
<div class="flex">
<div class="flex align-center">
<span
:innerHTML="row.icon"
alt=""
Expand Down Expand Up @@ -301,11 +301,11 @@ onMounted(() => {
}
.model-list-height {
height: calc(var(--create-dataset-height) - 70px);
height: calc(var(--create-dataset-height) - 80px);
}
.model-list-height-left {
height: calc(var(--create-dataset-height));
height: calc(var(--create-dataset-height) - 40px);
}
.all-mode {
padding: 10px 16px;
Expand Down Expand Up @@ -338,6 +338,9 @@ onMounted(() => {
:deep(.el-collapse-item__wrap) {
border-bottom: none !important;
}
:deep(.el-collapse-item__content) {
padding-bottom: 0 !important;;
}
}
}
</style>
Loading

0 comments on commit 1821640

Please sign in to comment.