This commit is contained in:
liqiang-fit2cloud 2024-12-09 09:50:32 +08:00
commit 18216401fd
12 changed files with 207 additions and 17 deletions

View File

@ -31,6 +31,16 @@ default_pattern_list = [re.compile('(?<=^)# .*|(?<=\\n)# .*'),
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:
@ -175,6 +185,9 @@ class PdfSplitHandle(BaseSplitHandle):
@staticmethod
def handle_links(doc, pattern_list, with_filter, limit):
# 检查文档是否包含内部链接
if not check_links_in_pdf(doc):
return
# 创建存储章节内容的数组
chapters = []
toc_start_page = -1

View File

@ -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

View File

@ -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,
)

View File

@ -11,18 +11,40 @@ import os
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):

View File

@ -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 \

View File

@ -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">
@ -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>
@ -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
@ -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)
@ -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)
}
@ -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)
@ -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('')
@ -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'
}
}
@ -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) {

View File

@ -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;

View File

@ -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);
@ -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);

View File

@ -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
}

View File

@ -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"
@ -33,7 +33,7 @@
ref="commonList1"
>
<template #default="{ row }">
<div class="flex">
<div class="flex align-center">
<span
:innerHTML="row.icon"
alt=""
@ -59,7 +59,7 @@
ref="commonList2"
>
<template #default="{ row }">
<div class="flex">
<div class="flex align-center">
<span
:innerHTML="row.icon"
alt=""
@ -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;
@ -338,6 +338,9 @@ onMounted(() => {
:deep(.el-collapse-item__wrap) {
border-bottom: none !important;
}
:deep(.el-collapse-item__content) {
padding-bottom: 0 !important;;
}
}
}
</style>

View File

@ -68,6 +68,23 @@
<el-checkbox v-model="form_data.image" />
</div>
</el-card>
<el-card
shadow="hover"
class="card-checkbox cursor w-full mb-8"
:class="form_data.audio ? 'active' : ''"
style="--el-card-padding: 8px 16px"
>
<div class="flex-between">
<div class="flex align-center">
<img class="mr-12" src="@/assets/icon_file-image.svg" alt="" />
<div>
<p class="line-height-22 mt-4">音频MP3</p>
<el-text class="color-secondary">所选模型支持接收音频或与语音转文本节点配合使用</el-text>
</div>
</div>
<el-checkbox v-model="form_data.audio" />
</div>
</el-card>
</el-form-item>
</el-form>
<template #footer>

View File

@ -82,6 +82,12 @@ const refreshFileUploadConfig = () => {
if (form_data[0].image) {
fileUploadFields.push({ label: '图片', value: 'image' })
}
if (form_data[0].audio) {
fileUploadFields.push({ label: '音频', value: 'audio' })
}
if (form_data[0].video) {
fileUploadFields.push({ label: '视频', value: 'video' })
}
set(props.nodeModel.properties.config, 'fields', [...fields, ...fileUploadFields])
}