Merge upstream changes

This commit is contained in:
tongque 2024-04-28 13:36:09 +08:00
commit 6f15ebba05
17 changed files with 263 additions and 100 deletions

View File

@ -0,0 +1,24 @@
# coding=utf-8
"""
@project: maxkb
@Author
@file tokenizer_manage_config.py
@date2024/4/28 10:17
@desc:
"""
class TokenizerManage:
tokenizer = None
@staticmethod
def get_tokenizer():
from transformers import GPT2TokenizerFast
if TokenizerManage.tokenizer is None:
TokenizerManage.tokenizer = GPT2TokenizerFast.from_pretrained(
'gpt2',
cache_dir="/opt/maxkb/model/tokenizer",
local_files_only=True,
resume_download=False,
force_download=False)
return TokenizerManage.tokenizer

View File

@ -19,6 +19,7 @@ from common.util.file_util import get_file_content
from setting.models_provider.base_model_provider import IModelProvider, ModelProvideInfo, BaseModelCredential, \
ModelInfo, \
ModelTypeConst, ValidCode
from setting.models_provider.impl.azure_model_provider.model.azure_chat_model import AzureChatModel
from smartdoc.conf import PROJECT_DIR
@ -119,8 +120,8 @@ class AzureModelProvider(IModelProvider):
def get_model(self, model_type, model_name, model_credential: Dict[str, object], **model_kwargs) -> AzureChatOpenAI:
model_info: ModelInfo = model_dict.get(model_name)
azure_chat_open_ai = AzureChatOpenAI(
openai_api_base=model_credential.get('api_base'),
azure_chat_open_ai = AzureChatModel(
azure_endpoint=model_credential.get('api_base'),
openai_api_version=model_info.api_version if model_name in model_dict else model_credential.get(
'api_version'),
deployment_name=model_credential.get('deployment_name'),

View File

@ -0,0 +1,24 @@
# coding=utf-8
"""
@project: maxkb
@Author
@file azure_chat_model.py
@date2024/4/28 11:45
@desc:
"""
from typing import List
from langchain_core.messages import BaseMessage, get_buffer_string
from langchain_openai import AzureChatOpenAI
from common.config.tokenizer_manage_config import TokenizerManage
class AzureChatModel(AzureChatOpenAI):
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages])
def get_num_tokens(self, text: str) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return len(tokenizer.encode(text))

View File

@ -11,19 +11,7 @@ from typing import List
from langchain_community.chat_models import ChatOpenAI
from langchain_core.messages import BaseMessage, get_buffer_string
class TokenizerManage:
tokenizer = None
@staticmethod
def get_tokenizer():
from transformers import GPT2TokenizerFast
if TokenizerManage.tokenizer is None:
TokenizerManage.tokenizer = GPT2TokenizerFast.from_pretrained('gpt2',
cache_dir="/opt/maxkb/model/tokenizer",
resume_download=False,
force_download=False)
return TokenizerManage.tokenizer
from common.config.tokenizer_manage_config import TokenizerManage
class KimiChatModel(ChatOpenAI):

View File

@ -11,19 +11,7 @@ from typing import List
from langchain_community.chat_models import ChatOpenAI
from langchain_core.messages import BaseMessage, get_buffer_string
class TokenizerManage:
tokenizer = None
@staticmethod
def get_tokenizer():
from transformers import GPT2TokenizerFast
if TokenizerManage.tokenizer is None:
TokenizerManage.tokenizer = GPT2TokenizerFast.from_pretrained('gpt2',
cache_dir="/opt/maxkb/model/tokenizer",
resume_download=False,
force_download=False)
return TokenizerManage.tokenizer
from common.config.tokenizer_manage_config import TokenizerManage
class OllamaChatModel(ChatOpenAI):

View File

@ -74,9 +74,49 @@ model_dict = {
'llama2-chinese:13b',
'由于Llama2本身的中文对齐较弱我们采用中文指令集对meta-llama/Llama-2-13b-chat-hf进行LoRA微调使其具备较强的中文对话能力。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen-14b': ModelInfo(
'llama3:8b': ModelInfo(
'llama3:8b',
'Meta Llama 3迄今为止最有能力的公开产品LLM。8亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'llama3:70b': ModelInfo(
'llama3:70b',
'Meta Llama 3迄今为止最有能力的公开产品LLM。70亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:0.5b': ModelInfo(
'qwen:0.5b',
'qwen 1.5 0.5b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。0.5亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:1.8b': ModelInfo(
'qwen:1.8b',
'qwen 1.5 1.8b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。1.8亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:4b': ModelInfo(
'qwen:4b',
'qwen 1.5 4b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。4亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:7b': ModelInfo(
'qwen:7b',
'qwen 1.5 7b 相较于以往版本模型与人类偏好的对齐程度以及多语1言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。7亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:14b': ModelInfo(
'qwen:14b',
'qwen 1.5 14b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。',
'qwen 1.5 14b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。14亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:32b': ModelInfo(
'qwen:32b',
'qwen 1.5 32b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。32亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:72b': ModelInfo(
'qwen:72b',
'qwen 1.5 72b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。72亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'qwen:110b': ModelInfo(
'qwen:110b',
'qwen 1.5 110b 相较于以往版本模型与人类偏好的对齐程度以及多语言处理能力上有显著增强。所有规模的模型都支持32768个tokens的上下文长度。110亿参数。',
ModelTypeConst.LLM, ollama_llm_model_credential),
'phi3': ModelInfo(
'phi3',
'Phi-3 Mini是Microsoft的3.8B参数,轻量级,最先进的开放模型。',
ModelTypeConst.LLM, ollama_llm_model_credential),
}

View File

@ -11,19 +11,7 @@ from typing import List
from langchain_core.messages import BaseMessage, get_buffer_string
from langchain_openai import ChatOpenAI
class TokenizerManage:
tokenizer = None
@staticmethod
def get_tokenizer():
from transformers import GPT2TokenizerFast
if TokenizerManage.tokenizer is None:
TokenizerManage.tokenizer = GPT2TokenizerFast.from_pretrained('gpt2',
cache_dir="/opt/maxkb/model/tokenizer",
resume_download=False,
force_download=False)
return TokenizerManage.tokenizer
from common.config.tokenizer_manage_config import TokenizerManage
class OpenAIChatModel(ChatOpenAI):

View File

@ -57,12 +57,39 @@ class OpenAILLMModelCredential(BaseForm, BaseModelCredential):
openai_llm_model_credential = OpenAILLMModelCredential()
model_dict = {
'gpt-3.5-turbo': ModelInfo('gpt-3.5-turbo', '', ModelTypeConst.LLM, openai_llm_model_credential,
'gpt-3.5-turbo': ModelInfo('gpt-3.5-turbo', '最新的gpt-3.5-turbo随OpenAI调整而更新', ModelTypeConst.LLM,
openai_llm_model_credential,
),
'gpt-3.5-turbo-0613': ModelInfo('gpt-3.5-turbo-0613', '', ModelTypeConst.LLM, openai_llm_model_credential,
'gpt-3.5-turbo-0125': ModelInfo('gpt-3.5-turbo-0125',
'2024年1月25日的gpt-3.5-turbo快照支持上下文长度16,385 tokens', ModelTypeConst.LLM,
openai_llm_model_credential,
),
'gpt-3.5-turbo-1106': ModelInfo('gpt-3.5-turbo-1106',
'2023年11月6日的gpt-3.5-turbo快照支持上下文长度16,385 tokens', ModelTypeConst.LLM,
openai_llm_model_credential,
),
'gpt-3.5-turbo-0613': ModelInfo('gpt-3.5-turbo-0613',
'[Legacy] 2023年6月13日的gpt-3.5-turbo快照将于2024年6月13日弃用',
ModelTypeConst.LLM, openai_llm_model_credential,
),
'gpt-4': ModelInfo('gpt-4', '最新的gpt-4随OpenAI调整而更新', ModelTypeConst.LLM, openai_llm_model_credential,
),
'gpt-4-turbo': ModelInfo('gpt-4-turbo', '最新的gpt-4-turbo随OpenAI调整而更新', ModelTypeConst.LLM,
openai_llm_model_credential,
),
'gpt-4-turbo-preview': ModelInfo('gpt-4-turbo-preview', '最新的gpt-4-turbo-preview随OpenAI调整而更新',
ModelTypeConst.LLM, openai_llm_model_credential,
),
'gpt-4-turbo-2024-04-09': ModelInfo('gpt-4-turbo-2024-04-09',
'2024年4月9日的gpt-4-turbo快照支持上下文长度128,000 tokens',
ModelTypeConst.LLM, openai_llm_model_credential,
),
'gpt-4-0125-preview': ModelInfo('gpt-4-0125-preview', '2024年1月25日的gpt-4-turbo快照支持上下文长度128,000 tokens',
ModelTypeConst.LLM, openai_llm_model_credential,
),
'gpt-4-1106-preview': ModelInfo('gpt-4-1106-preview', '2023年11月6日的gpt-4-turbo快照支持上下文长度128,000 tokens',
ModelTypeConst.LLM, openai_llm_model_credential,
),
'gpt-4': ModelInfo('gpt-4', '', ModelTypeConst.LLM, openai_llm_model_credential,
)
}

View File

@ -0,0 +1,24 @@
# coding=utf-8
"""
@project: maxkb
@Author
@file qwen_chat_model.py
@date2024/4/28 11:44
@desc:
"""
from typing import List
from langchain_community.chat_models import ChatTongyi
from langchain_core.messages import BaseMessage, get_buffer_string
from common.config.tokenizer_manage_config import TokenizerManage
class QwenChatModel(ChatTongyi):
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages])
def get_num_tokens(self, text: str) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return len(tokenizer.encode(text))

View File

@ -18,6 +18,7 @@ from common.forms import BaseForm
from common.util.file_util import get_file_content
from setting.models_provider.base_model_provider import ModelProvideInfo, ModelTypeConst, BaseModelCredential, \
ModelInfo, IModelProvider, ValidCode
from setting.models_provider.impl.qwen_model_provider.model.qwen_chat_model import QwenChatModel
from smartdoc.conf import PROJECT_DIR
@ -66,7 +67,7 @@ class QwenModelProvider(IModelProvider):
return 3
def get_model(self, model_type, model_name, model_credential: Dict[str, object], **model_kwargs) -> ChatTongyi:
chat_tong_yi = ChatTongyi(
chat_tong_yi = QwenChatModel(
model_name=model_name,
dashscope_api_key=model_credential.get('api_key')
)

View File

@ -18,19 +18,7 @@ from langchain.schema.output import ChatGenerationChunk
from langchain.schema.runnable import RunnableConfig
from langchain_community.chat_models import QianfanChatEndpoint
class TokenizerManage:
tokenizer = None
@staticmethod
def get_tokenizer():
from transformers import GPT2TokenizerFast
if TokenizerManage.tokenizer is None:
TokenizerManage.tokenizer = GPT2TokenizerFast.from_pretrained('gpt2',
cache_dir="/opt/maxkb/model/tokenizer",
resume_download=False,
force_download=False)
return TokenizerManage.tokenizer
from common.config.tokenizer_manage_config import TokenizerManage
class QianfanChatModel(QianfanChatEndpoint):

View File

@ -12,11 +12,21 @@ from typing import List, Optional, Any, Iterator
from langchain_community.chat_models import ChatSparkLLM
from langchain_community.chat_models.sparkllm import _convert_message_to_dict, _convert_delta_to_message_chunk
from langchain_core.callbacks import CallbackManagerForLLMRun
from langchain_core.messages import BaseMessage, AIMessageChunk
from langchain_core.messages import BaseMessage, AIMessageChunk, get_buffer_string
from langchain_core.outputs import ChatGenerationChunk
from common.config.tokenizer_manage_config import TokenizerManage
class XFChatSparkLLM(ChatSparkLLM):
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages])
def get_num_tokens(self, text: str) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return len(tokenizer.encode(text))
def _stream(
self,
messages: List[BaseMessage],

View File

@ -0,0 +1,24 @@
# coding=utf-8
"""
@project: maxkb
@Author
@file zhipu_chat_model.py
@date2024/4/28 11:42
@desc:
"""
from typing import List
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import BaseMessage, get_buffer_string
from common.config.tokenizer_manage_config import TokenizerManage
class ZhipuChatModel(ChatZhipuAI):
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return sum([len(tokenizer.encode(get_buffer_string([m]))) for m in messages])
def get_num_tokens(self, text: str) -> int:
tokenizer = TokenizerManage.get_tokenizer()
return len(tokenizer.encode(text))

View File

@ -18,6 +18,7 @@ from common.forms import BaseForm
from common.util.file_util import get_file_content
from setting.models_provider.base_model_provider import ModelProvideInfo, ModelTypeConst, BaseModelCredential, \
ModelInfo, IModelProvider, ValidCode
from setting.models_provider.impl.zhipu_model_provider.model.zhipu_chat_model import ZhipuChatModel
from smartdoc.conf import PROJECT_DIR
@ -66,7 +67,7 @@ class ZhiPuModelProvider(IModelProvider):
return 3
def get_model(self, model_type, model_name, model_credential: Dict[str, object], **model_kwargs) -> ChatZhipuAI:
zhipuai_chat = ChatZhipuAI(
zhipuai_chat = ZhipuChatModel(
temperature=0.5,
api_key=model_credential.get('api_key'),
model=model_name

View File

@ -12,25 +12,47 @@
</div>
<div class="flex-center avatar">
<el-tooltip effect="dark" :content="$t('layout.topbar.github')" placement="top">
<AppIcon iconName="app-github" class="cursor color-secondary mr-8 ml-8" style="font-size: 20px"
@click="toUrl('https://github.com/1Panel-dev/MaxKB')"></AppIcon>
<AppIcon
iconName="app-github"
class="cursor color-secondary mr-8 ml-8"
style="font-size: 20px"
@click="toUrl('https://github.com/1Panel-dev/MaxKB')"
></AppIcon>
</el-tooltip>
<el-tooltip effect="dark" :content="$t('layout.topbar.handbook')" placement="top">
<AppIcon iconName="app-reading" class="cursor color-secondary mr-8 ml-8" style="font-size: 20px"
@click="toUrl('https://github.com/1Panel-dev/MaxKB/wiki')"></AppIcon>
<AppIcon
iconName="app-reading"
class="cursor color-secondary mr-8 ml-8"
style="font-size: 20px"
@click="toUrl('https://github.com/1Panel-dev/MaxKB/wiki')"
></AppIcon>
</el-tooltip>
<el-tooltip effect="dark" :content="$t('layout.topbar.forum')" placement="top">
<AppIcon iconName="app-help" class="cursor color-secondary mr-8 ml-8" style="font-size: 20px"
@click="toUrl('https://bbs.fit2cloud.com/c/mk/11')"></AppIcon>
<AppIcon
iconName="app-help"
class="cursor color-secondary mr-8 ml-8"
style="font-size: 20px"
@click="toUrl('https://bbs.fit2cloud.com/c/mk/11')"
></AppIcon>
</el-tooltip>
<el-dropdown trigger="click" type="primary">
<el-dropdown v-if="false" trigger="click" type="primary">
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item v-for="(lang, index) in langList" :key="index" :value="lang.value"
@click="changeLang(lang.value)">{{ lang.label }}</el-dropdown-item>
<el-dropdown-item
v-for="(lang, index) in langList"
:key="index"
:value="lang.value"
@click="changeLang(lang.value)"
>{{ lang.label }}</el-dropdown-item
>
</el-dropdown-menu>
</template>
<AppIcon iconName="app-translate" class="cursor color-secondary mr-16 ml-8" style="font-size: 20px" @click="">
<AppIcon
iconName="app-translate"
class="cursor color-secondary mr-16 ml-8"
style="font-size: 20px"
@click=""
>
</AppIcon>
</el-dropdown>
<Avatar></Avatar>
@ -41,15 +63,15 @@
import TopMenu from './top-menu/index.vue'
import Avatar from './avatar/index.vue'
import { useRouter } from 'vue-router'
import { langList } from '@/locales/index';
import { useLocale } from '@/locales/useLocale';
import { langList } from '@/locales/index'
import { useLocale } from '@/locales/useLocale'
const router = useRouter()
const defaultTitle = import.meta.env.VITE_APP_TITLE
const { changeLocale } = useLocale();
const { changeLocale } = useLocale()
const changeLang = (lang: string) => {
changeLocale(lang);
};
changeLocale(lang)
}
function toUrl(url: string) {
window.open(url, '_blank')
}

View File

@ -81,11 +81,8 @@
</div>
<el-tooltip effect="dark" placement="right">
<template #content>
<p>为供应商的 LLM 模型支持自定义输入</p>
<p>
下拉选项是 OpenAI
常用的一些大语言模型如gpt-3.5-turbo-0613gpt-3.5-turbogpt-4
</p>
<p>若下拉选项没有列出想要添加的LLM模型自定义输入模型名称后回车即可</p>
<p>注意基础模型需要与供应商的模型名称一致</p>
</template>
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
</el-tooltip>
@ -101,12 +98,21 @@
allow-create
default-first-option
>
<el-option
v-for="item in base_model_list"
:key="item.name"
:label="item.name"
:value="item.name"
></el-option>
<el-option v-for="item in base_model_list" :key="item.name" :value="item.name">
<template #default>
<div class="flex align-center" style="display: inline-flex">
<div class="flex-between mr-4">
<span>{{ item.name }} </span>
</div>
<el-tooltip effect="dark" placement="right" v-if="item.desc">
<template #content>
<p>{{ item.desc }}</p>
</template>
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
</el-tooltip>
</div>
</template>
</el-option>
</el-select>
</el-form-item>
</template>
@ -204,6 +210,7 @@ const list_base_model = (model_type: any) => {
)
}
}
const close = () => {
base_form_data.value = { name: '', model_type: '', model_name: '' }
credential_form_data.value = {}

View File

@ -75,11 +75,8 @@
</div>
<el-tooltip effect="dark" placement="right">
<template #content>
<p>为供应商的 LLM 模型支持自定义输入</p>
<p>
下拉选项是 OpenAI
常用的一些大语言模型如gpt-3.5-turbo-0613gpt-3.5-turbogpt-4
</p>
<p>若下拉选项没有列出想要添加的LLM模型自定义输入模型名称后回车即可</p>
<p>注意基础模型需要与供应商的模型名称一致</p>
</template>
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
</el-tooltip>
@ -95,12 +92,21 @@
allow-create
default-first-option
>
<el-option
v-for="item in base_model_list"
:key="item.name"
:label="item.name"
:value="item.name"
></el-option>
<el-option v-for="item in base_model_list" :key="item.name" :value="item.name">
<template #default>
<div class="flex align-center" style="display: inline-flex">
<div class="flex-between mr-4">
<span>{{ item.name }} </span>
</div>
<el-tooltip effect="dark" placement="right" v-if="item.desc">
<template #content>
<p>{{ item.desc }}</p>
</template>
<AppIcon iconName="app-warning" class="app-warning-icon"></AppIcon>
</el-tooltip>
</div>
</template>
</el-option>
</el-select>
</el-form-item>
</template>