Skip to content

Commit 2ca06bb

Browse files
committed
feat: set chat model by per chat room
Signed-off-by: BobDu <i@bobdu.cc>
1 parent ddb64df commit 2ca06bb

File tree

8 files changed

+56
-26
lines changed

8 files changed

+56
-26
lines changed

service/src/chatgpt/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getCacheApiKeys, getCacheConfig, getOriginConfig } from '../storage/con
1515
import { sendResponse } from '../utils'
1616
import { hasAnyRole, isNotEmptyString } from '../utils/is'
1717
import type { ChatContext, ChatGPTUnofficialProxyAPIOptions, JWT, ModelConfig } from '../types'
18-
import { getChatByMessageId, updateRoomAccountId, updateRoomChatModel } from '../storage/mongo'
18+
import { getChatByMessageId, updateRoomAccountId } from '../storage/mongo'
1919
import type { RequestOptions } from './types'
2020

2121
const { HttpsProxyAgent } = httpsProxyAgent
@@ -113,8 +113,8 @@ export async function initApi(key: KeyConfig, chatModel: string, maxContextCount
113113
}
114114
const processThreads: { userId: string; abort: AbortController; messageId: string }[] = []
115115
async function chatReplyProcess(options: RequestOptions) {
116-
const model = options.user.config.chatModel
117-
const key = await getRandomApiKey(options.user, options.user.config.chatModel, options.room.accountId)
116+
const model = options.room.chatModel
117+
const key = await getRandomApiKey(options.user, model, options.room.accountId)
118118
const userId = options.user._id.toString()
119119
const maxContextCount = options.user.advanced.maxContextCount ?? 20
120120
const messageId = options.messageId
@@ -130,8 +130,6 @@ async function chatReplyProcess(options: RequestOptions) {
130130
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')
131131
}
132132

133-
updateRoomChatModel(userId, options.room.roomId, model)
134-
135133
const { message, lastContext, process, systemMessage, temperature, top_p } = options
136134

137135
try {

service/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ router.get('/chatrooms', auth, async (req, res) => {
9595
router.post('/room-create', auth, async (req, res) => {
9696
try {
9797
const userId = req.headers.userId as string
98-
const { title, roomId } = req.body as { title: string; roomId: number }
99-
const room = await createChatRoom(userId, title, roomId)
98+
const { title, roomId, chatModel } = req.body as { title: string; roomId: number; chatModel: string }
99+
const room = await createChatRoom(userId, title, roomId, chatModel)
100100
res.send({ status: 'Success', message: null, data: room })
101101
}
102102
catch (error) {

service/src/storage/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ export class ChatRoom {
6767
// only access token used
6868
accountId?: string
6969
chatModel: string
70-
constructor(userId: string, title: string, roomId: number) {
70+
constructor(userId: string, title: string, roomId: number, chatModel: string) {
7171
this.userId = userId
7272
this.title = title
7373
this.prompt = undefined
7474
this.roomId = roomId
7575
this.usingContext = true
7676
this.accountId = null
77-
this.chatModel = null
77+
this.chatModel = chatModel
7878
}
7979
}
8080

service/src/storage/mongo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export async function insertChatUsage(userId: ObjectId, roomId: number, chatId:
7070
return chatUsage
7171
}
7272

73-
export async function createChatRoom(userId: string, title: string, roomId: number) {
74-
const room = new ChatRoom(userId, title, roomId)
73+
export async function createChatRoom(userId: string, title: string, roomId: number, chatModel: string) {
74+
const room = new ChatRoom(userId, title, roomId, chatModel)
7575
await roomCol.insertOne(room)
7676
return room
7777
}

src/api/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ export function fetchGetChatRooms<T = any>() {
185185
})
186186
}
187187

188-
export function fetchCreateChatRoom<T = any>(title: string, roomId: number) {
188+
export function fetchCreateChatRoom<T = any>(title: string, roomId: number, chatModel?: string) {
189189
return post<T>({
190190
url: '/room-create',
191-
data: { title, roomId },
191+
data: { title, roomId, chatModel },
192192
})
193193
}
194194

@@ -206,6 +206,13 @@ export function fetchUpdateChatRoomPrompt<T = any>(prompt: string, roomId: numbe
206206
})
207207
}
208208

209+
export function fetchUpdateChatRoomChatModel<T = any>(chatModel: string, roomId: number) {
210+
return post<T>({
211+
url: '/room-chatmodel',
212+
data: { chatModel, roomId },
213+
})
214+
}
215+
209216
export function fetchUpdateChatRoomUsingContext<T = any>(using: boolean, roomId: number) {
210217
return post<T>({
211218
url: '/room-context',

src/store/modules/chat/index.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import { defineStore } from 'pinia'
22
import { getLocalState, setLocalState } from './helper'
3+
import { useUserStore } from '@/store'
34
import { router } from '@/router'
4-
import { fetchClearChat, fetchCreateChatRoom, fetchDeleteChat, fetchDeleteChatRoom, fetchGetChatHistory, fetchGetChatRooms, fetchRenameChatRoom, fetchUpdateChatRoomUsingContext } from '@/api'
5+
import {
6+
fetchClearChat,
7+
fetchCreateChatRoom,
8+
fetchDeleteChat,
9+
fetchDeleteChatRoom,
10+
fetchGetChatHistory,
11+
fetchGetChatRooms,
12+
fetchRenameChatRoom,
13+
fetchUpdateChatRoomChatModel,
14+
fetchUpdateChatRoomUsingContext,
15+
} from '@/api'
516

617
export const useChatStore = defineStore('chat-store', {
718
state: (): Chat.ChatState => getLocalState(),
@@ -39,7 +50,7 @@ export const useChatStore = defineStore('chat-store', {
3950
this.chat.unshift({ uuid: r.uuid, data: [] })
4051
}
4152
if (uuid == null) {
42-
await this.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false, usingContext: true })
53+
await this.addNewHistory()
4354
}
4455
else {
4556
this.active = uuid
@@ -94,12 +105,27 @@ export const useChatStore = defineStore('chat-store', {
94105
this.recordState()
95106
},
96107

108+
async setChatModel(chatModel: string, roomId: number) {
109+
await fetchUpdateChatRoomChatModel(chatModel, roomId)
110+
},
111+
97112
async addHistory(history: Chat.History, chatData: Chat.Chat[] = []) {
98-
await fetchCreateChatRoom(history.title, history.uuid)
113+
await fetchCreateChatRoom(history.title, history.uuid, history.chatModel)
99114
this.history.unshift(history)
100115
this.chat.unshift({ uuid: history.uuid, data: chatData })
101116
this.active = history.uuid
102-
this.reloadRoute(history.uuid)
117+
await this.reloadRoute(history.uuid)
118+
},
119+
120+
async addNewHistory() {
121+
const userStore = useUserStore()
122+
await this.addHistory({
123+
title: 'New Chat',
124+
uuid: Date.now(),
125+
isEdit: false,
126+
usingContext: true,
127+
chatModel: userStore.userInfo.config.chatModel,
128+
})
103129
},
104130

105131
updateHistory(uuid: number, edit: Partial<Chat.History>) {
@@ -118,7 +144,7 @@ export const useChatStore = defineStore('chat-store', {
118144
this.chat.splice(index, 1)
119145

120146
if (this.history.length === 0) {
121-
await this.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false, usingContext: true })
147+
await this.addNewHistory()
122148
return
123149
}
124150

src/views/chat/index.vue

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import HeaderComponent from './components/Header/index.vue'
1313
import { HoverButton, SvgIcon } from '@/components/common'
1414
import { useBasicLayout } from '@/hooks/useBasicLayout'
1515
import { useAuthStore, useChatStore, usePromptStore, useUserStore } from '@/store'
16-
import { fetchChatAPIProcess, fetchChatResponseoHistory, fetchChatStopResponding, fetchUpdateUserChatModel } from '@/api'
16+
import {
17+
fetchChatAPIProcess,
18+
fetchChatResponseoHistory,
19+
fetchChatStopResponding,
20+
} from '@/api'
1721
import { t } from '@/locales'
1822
import { debounce } from '@/utils/functions/debounce'
1923
import IconPrompt from '@/icons/Prompt.vue'
20-
import { UserConfig } from '@/components/common/Setting/model'
2124
const Prompt = defineAsyncComponent(() => import('@/components/common/Setting/Prompt.vue'))
2225
2326
let controller = new AbortController()
@@ -582,11 +585,7 @@ const footerClass = computed(() => {
582585
583586
async function handleSyncChatModel(chatModel: string) {
584587
nowSelectChatModel.value = chatModel
585-
if (userStore.userInfo.config == null)
586-
userStore.userInfo.config = new UserConfig()
587-
userStore.userInfo.config.chatModel = chatModel
588-
userStore.recordState()
589-
await fetchUpdateUserChatModel(chatModel)
588+
await chatStore.setChatModel(chatModel, +uuid)
590589
}
591590
592591
onMounted(() => {

src/views/chat/layout/sider/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const show = ref(false)
1818
const collapsed = computed(() => appStore.siderCollapsed)
1919
2020
async function handleAdd() {
21-
await chatStore.addHistory({ title: 'New Chat', uuid: Date.now(), isEdit: false, usingContext: true })
21+
await chatStore.addNewHistory()
2222
if (isMobile.value)
2323
appStore.setSiderCollapsed(true)
2424
}

0 commit comments

Comments
 (0)