Skip to content

Commit d1f1844

Browse files
author
Kerwin
committed
feat: fixed model for each chat room (Close #300)
1 parent cf88467 commit d1f1844

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

service/src/chatgpt/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { getCacheApiKeys, getCacheConfig, getOriginConfig } from '../storage/con
1414
import { sendResponse } from '../utils'
1515
import { hasAnyRole, isNotEmptyString } from '../utils/is'
1616
import type { ChatContext, ChatGPTUnofficialProxyAPIOptions, JWT, ModelConfig } from '../types'
17-
import { getChatByMessageId, updateRoomAccountId } from '../storage/mongo'
17+
import { getChatByMessageId, updateRoomAccountId, updateRoomChatModel } from '../storage/mongo'
1818
import type { RequestOptions } from './types'
1919

2020
const { HttpsProxyAgent } = httpsProxyAgent
@@ -114,6 +114,8 @@ async function chatReplyProcess(options: RequestOptions) {
114114
throw new Error('无法在一个房间同时使用 AccessToken 以及 Api,请联系管理员,或新开聊天室进行对话 | Unable to use AccessToken and Api at the same time in the same room, please contact the administrator or open a new chat room for conversation')
115115
}
116116

117+
updateRoomChatModel(userId, options.room.roomId, model)
118+
117119
const { message, lastContext, process, systemMessage, temperature, top_p } = options
118120

119121
try {

service/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
updateApiKeyStatus,
3333
updateChat,
3434
updateConfig,
35+
updateRoomChatModel,
3536
updateRoomPrompt,
3637
updateRoomUsingContext,
3738
updateUser,
@@ -75,6 +76,7 @@ router.get('/chatrooms', auth, async (req, res) => {
7576
isEdit: false,
7677
prompt: r.prompt,
7778
usingContext: r.usingContext === undefined ? true : r.usingContext,
79+
chatModel: r.chatModel,
7880
})
7981
})
8082
res.send({ status: 'Success', message: null, data: result })
@@ -127,6 +129,22 @@ router.post('/room-prompt', auth, async (req, res) => {
127129
}
128130
})
129131

132+
router.post('/room-chatmodel', auth, async (req, res) => {
133+
try {
134+
const userId = req.headers.userId as string
135+
const { chatModel, roomId } = req.body as { chatModel: CHATMODEL; roomId: number }
136+
const success = await updateRoomChatModel(userId, roomId, chatModel)
137+
if (success)
138+
res.send({ status: 'Success', message: 'Saved successfully', data: null })
139+
else
140+
res.send({ status: 'Fail', message: 'Saved Failed', data: null })
141+
}
142+
catch (error) {
143+
console.error(error)
144+
res.send({ status: 'Fail', message: 'Rename error', data: null })
145+
}
146+
})
147+
130148
router.post('/room-context', auth, async (req, res) => {
131149
try {
132150
const userId = req.headers.userId as string

service/src/storage/model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ export class ChatRoom {
8383
status: Status = Status.Normal
8484
// only access token used
8585
accountId?: string
86+
chatModel: CHATMODEL
8687
constructor(userId: string, title: string, roomId: number) {
8788
this.userId = userId
8889
this.title = title
8990
this.prompt = undefined
9091
this.roomId = roomId
9192
this.usingContext = true
9293
this.accountId = null
94+
this.chatModel = null
9395
}
9496
}
9597

service/src/storage/mongo.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ export async function updateRoomAccountId(userId: string, roomId: number, accoun
120120
return result.modifiedCount > 0
121121
}
122122

123+
export async function updateRoomChatModel(userId: string, roomId: number, chatModel: CHATMODEL) {
124+
const query = { userId, roomId }
125+
const update = {
126+
$set: {
127+
chatModel,
128+
},
129+
}
130+
const result = await roomCol.updateOne(query, update)
131+
return result.modifiedCount > 0
132+
}
133+
123134
export async function getChatRooms(userId: string) {
124135
const cursor = await roomCol.find({ userId, status: { $ne: Status.Deleted } })
125136
const rooms = []

src/typings/chat.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
declare namespace Chat {
2+
import { CHATMODEL } from "@/components/common/Setting/model"
23

34
interface Chat {
45
uuid?: number
@@ -26,6 +27,7 @@ declare namespace Chat {
2627
all?: boolean
2728
prompt?: string
2829
usingContext: boolean
30+
chatModel?: CHATMODEL
2931
}
3032

3133
interface ChatState {

src/views/chat/index.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const firstLoading = ref<boolean>(false)
4949
const loading = ref<boolean>(false)
5050
const inputRef = ref<Ref | null>(null)
5151
const showPrompt = ref(false)
52+
const nowSelectChatModel = ref<CHATMODEL | null>(null)
53+
const currentChatModel = computed(() => nowSelectChatModel.value ?? currentChatHistory.value?.chatModel ?? userStore.userInfo.config.chatModel)
5254
5355
let loadingms: MessageReactive
5456
let allmsg: MessageReactive
@@ -79,6 +81,9 @@ async function onConversation() {
7981
if (!message || message.trim() === '')
8082
return
8183
84+
if (nowSelectChatModel.value && currentChatHistory.value)
85+
currentChatHistory.value.chatModel = nowSelectChatModel.value
86+
8287
controller = new AbortController()
8388
8489
const chatUuid = Date.now()
@@ -577,6 +582,7 @@ const footerClass = computed(() => {
577582
})
578583
579584
async function handleSyncChatModel(chatModel: CHATMODEL) {
585+
nowSelectChatModel.value = chatModel
580586
if (userStore.userInfo.config == null)
581587
userStore.userInfo.config = new UserConfig()
582588
userStore.userInfo.config.chatModel = chatModel
@@ -684,7 +690,7 @@ onUnmounted(() => {
684690
</HoverButton>
685691
<NSelect
686692
style="width: 250px"
687-
:value="userStore.userInfo.config.chatModel"
693+
:value="currentChatModel"
688694
:options="authStore.session?.chatModels"
689695
:disabled="!!authStore.session?.auth && !authStore.token"
690696
@update-value="(val) => handleSyncChatModel(val)"

0 commit comments

Comments
 (0)