Skip to content

Commit

Permalink
feat: Introduce AI dialog model "Kimi" (#724)
Browse files Browse the repository at this point in the history
* feat: Introduce AI dialog model "Kimi"

* fix:Update session carry immediately after adding token

* fix: Modify code details

* fix: Remove redundant AI models

* fix: Remove redundant code and normalize naming
  • Loading branch information
Fleurxxx authored Aug 14, 2024
1 parent c7bc599 commit ebd3d54
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 38 deletions.
57 changes: 40 additions & 17 deletions packages/plugins/robot/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<tiny-dropdown-item
v-for="item in AIModelOptions"
:key="item.label"
:class="{ 'selected-model': selectedModel.value === item.value }"
:class="{ 'selected-model': currentModel === item.value }"
@click="changeModel(item)"
>{{ item.label }}</tiny-dropdown-item
>
Expand Down Expand Up @@ -83,7 +83,12 @@
<tiny-button @click="sendContent(inputContent, false)">发送</tiny-button>
</footer>
</div>
<token-dialog :dialog-visible="tokenDialogVisible" @dialog-status="getTokenDialogStatus"></token-dialog>
<token-dialog
:dialog-visible="tokenDialogVisible"
:current-model="selectedModel"
@dialog-status="getTokenDialogStatus"
@token-status="updateTokenStatus"
></token-dialog>
</template>

<script>
Expand Down Expand Up @@ -137,6 +142,7 @@ export default {
const inputContent = ref('')
const inProcesing = ref(false)
const selectedModel = ref(AIModelOptions[0])
let currentModel = AIModelOptions[0]
const { confirm } = useModal()
const { pageSettingState, DEFAULT_PAGE } = usePage()
Expand All @@ -153,8 +159,9 @@ export default {
? JSON.stringify(sessionProcess)
: JSON.stringify({
foundationModel: {
manufacturer: selectedModel.value.manufacturer,
model: selectedModel.value.value
manufacturer: currentModel.manufacturer,
model: currentModel.value,
token: localStorage.getItem(currentModel.modelKey)
},
messages: [],
displayMessages: [] // 专门用来进行展示的消息,非原始消息,仅作为展示但是不作为请求的发送
Expand Down Expand Up @@ -195,8 +202,7 @@ export default {
// 为了不污染存储在localstorage里的用户的原始消息,这里进行了简单的对象拷贝
// 引入区块不存放在localstorage的原因:因为区块是可以变化的,用户可能在同一个会话中,对区块进行了删除和创建。那么存放的数据就不是即时数据了。
const getSendSeesionProcess = () => {
const accessToken = localStorage.getItem('accessToken')
const sendProcess = { ...sessionProcess, accessToken }
const sendProcess = { ...sessionProcess }
const firstMessage = sendProcess.messages[0]
firstMessage.content
sendProcess.messages = [
Expand Down Expand Up @@ -240,7 +246,7 @@ export default {
.catch((error) => {
switch (error.code) {
case 'CM001':
localStorage.removeItem('accessToken')
localStorage.removeItem(currentModel.modelKey)
tokenDialogVisible.value = true
break
default:
Expand Down Expand Up @@ -327,15 +333,19 @@ export default {
// 根据localstorage初始化AI大模型
const initCurrentModel = (aiSession) => {
const currentModelValue = JSON.parse(aiSession)?.foundationModel?.model
selectedModel.value = AIModelOptions.find((item) => item.value === currentModelValue)
currentModel = AIModelOptions.find((item) => item.value === currentModelValue)
}
const initChat = () => {
const aiChatSession = localStorage.getItem('aiChat')
if (!aiChatSession) {
setContextSession()
if (localStorage.getItem(currentModel.modelKey)) {
if (!aiChatSession) {
setContextSession()
} else {
initCurrentModel(aiChatSession) // 如果当前缓存有值,那么则需要根据缓存里的内容去初始化当前选择的模型
}
} else {
initCurrentModel(aiChatSession) // 如果当前缓存有值,那么则需要根据缓存里的内容去初始化当前选择的模型
tokenDialogVisible.value = true
}
sessionProcess = JSON.parse(localStorage.getItem('aiChat'))
messages.value = [...(sessionProcess?.displayMessages || [])]
Expand All @@ -348,10 +358,11 @@ export default {
resetContent()
}
const updateTokenStatus = () => {
initChat()
}
onMounted(async () => {
if (!localStorage.getItem('accessToken')) {
tokenDialogVisible.value = true
}
const loadingInstance = Loading.service({
text: '初始化中,请稍等...',
customClass: 'chat-loading',
Expand All @@ -372,7 +383,7 @@ export default {
}
const changeModel = (model) => {
if (selectedModel.value.value !== model.value) {
if (currentModel.value !== model.value) {
confirm({
title: '切换AI大模型',
message: '切换AI大模型将导致当前会话被清空,重新开启新会话,是否继续?',
Expand All @@ -383,9 +394,19 @@ export default {
})
}
}
watch(
() => selectedModel.value.value,
() => {
currentModel = selectedModel.value
if (!localStorage.getItem(currentModel.modelKey)) {
tokenDialogVisible.value = true
} else {
tokenDialogVisible.value = false
}
}
)
const { startRecognition, stopRecognition, recognizedText } = useSpeechRecognition()
const speechStatus = ref(false)
const speechRecognition = () => {
speechStatus.value = !speechStatus.value
Expand Down Expand Up @@ -414,9 +435,11 @@ export default {
setToken,
AIModelOptions,
selectedModel,
currentModel,
changeModel,
tokenDialogVisible,
getTokenDialogStatus
getTokenDialogStatus,
updateTokenStatus
}
}
}
Expand Down
43 changes: 24 additions & 19 deletions packages/plugins/robot/src/TokenDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,37 @@ export default {
TinyFormItem: FormItem
},
props: {
dialogVisible: Boolean
dialogVisible: Boolean,
currentModel: Object
},
emits: ['dialog-status'],
emits: ['dialog-status', 'token-status'],
setup(props, { emit }) {
const model = ref(props.currentModel)
const keyFormVisible = ref(props.dialogVisible)
const keyFormRef = ref(null)
const keyForm = ref({
accessToken: ''
token: ''
})
const accessTokenReg = /^[A-Za-z0-9\-.]+$/
const accessTokenValidate = (rule, value, callback) => {
if (value.length > 100 || !accessTokenReg.test(value)) {
const tokenReg = /^[A-Za-z0-9\-.]+$/
const tokenValidate = (rule, value, callback) => {
if (value.length > 100 || !tokenReg.test(value)) {
callback(new Error('参数错误,请输入小于100位的英文数字字符串'))
} else {
callback()
}
}
const rules = ref({
accessToken: [
{ required: true, message: '你的ACCESS_TOKEN不能为空', trigger: 'blur' },
{ validator: accessTokenValidate, trigger: 'blur' }
const rules = {
token: [
{ required: true, message: '该项不能为空', trigger: 'blur' },
{ validator: tokenValidate, trigger: 'blur' }
]
})
}
watch(
() => props.dialogVisible,
(newValue) => {
keyFormVisible.value = newValue
(newVisibleState) => {
keyFormVisible.value = newVisibleState
model.value = props.currentModel
}
)
Expand All @@ -58,7 +61,8 @@ export default {
const submitKeyForm = () => {
keyFormRef.value.validate((valid) => {
if (valid) {
localStorage.setItem('accessToken', keyForm.value.accessToken)
localStorage.setItem(props.currentModel.modelKey, keyForm.value.token)
emit('token-status', true)
closeKeyFormDialog()
}
})
Expand All @@ -71,6 +75,7 @@ export default {
closeKeyFormDialog,
submitKeyForm,
keyFormVisible,
model,
TinyIconAssociation: iconAssociation(),
TinyIconCommission: iconCommission()
}
Expand All @@ -84,17 +89,17 @@ export default {
<tiny-alert
:icon="TinyIconAssociation"
:closable="false"
description="当前AI大模型为使用文心一言:ERNIE-Bot-turbo"
:description="`当前AI大模型为使用${model.label}`"
></tiny-alert>
<tiny-alert
:icon="TinyIconCommission"
:closable="false"
description="尝试用自己的ACCESS_TOKEN开启AI对话功能吧!"
:description="`尝试用自己的${model.modelKey}开启AI对话功能吧!`"
></tiny-alert>
<tiny-form-item label="" prop="accessToken">
<tiny-form-item label="" prop="token">
<tiny-input
v-model="keyForm.accessToken"
placeholder="点击这里输入你的access_token"
v-model="keyForm.token"
:placeholder="`点击这里输入你的${model.modelKey}`"
validate-event
></tiny-input>
</tiny-form-item>
Expand Down
6 changes: 4 additions & 2 deletions packages/plugins/robot/src/js/robotSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import { useHttp } from '@opentiny/tiny-engine-http'
const { getBlockList } = useBlock()

export const AIModelOptions = [
{ label: 'ChatGPT:gpt-3.5-turbo', value: 'gpt-3.5-turbo', manufacturer: 'openai' },
{ label: '文心一言:ERNIE-Bot-turbo', value: 'ERNIE-Bot-turbo', manufacturer: 'baiduai' }
// 暂时不能使用,预留模型信息
// { label: 'ChatGPT:gpt-3.5-turbo', value: 'gpt-3.5-turbo', manufacturer: 'openai', modelKey: '' },
{ label: '文心一言:ERNIE-Bot-turbo', value: 'ERNIE-Bot-turbo', manufacturer: 'baiduai', modelKey: 'ACCESS_TOKEN' },
{ label: 'Kimi:moonshot-v1-8k', value: 'moonshot-v1-8k', manufacturer: 'kimi', modelKey: 'MOONSHOT_API_KEY' }
]

// 这里存放的是aichat的响应式数据
Expand Down

0 comments on commit ebd3d54

Please sign in to comment.