Skip to content

Commit f4c3d0c

Browse files
authored
Merge pull request #402 from BobDu/room-chat-model
feat: set user deafult model & set chat model by per chat room
2 parents ddb64df + adcbf4b commit f4c3d0c

File tree

13 files changed

+96
-35
lines changed

13 files changed

+96
-35
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/components/common/Setting/General.vue

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<script lang="ts" setup>
22
import { computed, ref } from 'vue'
3-
import { NButton, NInput, NPopconfirm, NSelect, useMessage } from 'naive-ui'
3+
import { NButton, NDivider, NInput, NPopconfirm, NSelect, useMessage } from 'naive-ui'
4+
import { UserConfig } from '@/components/common/Setting/model'
45
import type { Language, Theme } from '@/store/modules/app/helper'
56
import { SvgIcon } from '@/components/common'
6-
import { useAppStore, useUserStore } from '@/store'
7+
import { useAppStore, useAuthStore, useUserStore } from '@/store'
78
import type { UserInfo } from '@/store/modules/user/helper'
89
import { getCurrentDate } from '@/utils/functions'
910
import { useBasicLayout } from '@/hooks/useBasicLayout'
1011
import { t } from '@/locales'
11-
import { fetchClearAllChat } from '@/api'
12+
import { fetchClearAllChat, fetchUpdateUserChatModel } from '@/api'
1213
1314
const appStore = useAppStore()
1415
const userStore = useUserStore()
16+
const authStore = useAuthStore()
1517
1618
const { isMobile } = useBasicLayout()
1719
@@ -66,6 +68,14 @@ async function updateUserInfo(options: Partial<UserInfo>) {
6668
ms.success(t('common.success'))
6769
}
6870
71+
async function updateUserChatModel(chatModel: string) {
72+
if (!userStore.userInfo.config)
73+
userStore.userInfo.config = new UserConfig()
74+
userStore.userInfo.config.chatModel = chatModel
75+
userStore.recordState()
76+
await fetchUpdateUserChatModel(chatModel)
77+
}
78+
6979
function exportData(): void {
7080
const date = getCurrentDate()
7181
const data: string = localStorage.getItem('chatStorage') || '{}'
@@ -138,6 +148,25 @@ function handleImportButtonClick(): void {
138148
<NInput v-model:value="avatar" placeholder="" />
139149
</div>
140150
</div>
151+
<div class="flex items-center space-x-4">
152+
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.saveUserInfo') }}</span>
153+
<NButton type="primary" @click="updateUserInfo({ avatar, name, description })">
154+
{{ $t('common.save') }}
155+
</NButton>
156+
</div>
157+
<NDivider />
158+
<div class="flex items-center space-x-4">
159+
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.defaultChatModel') }}</span>
160+
<div class="w-[200px]">
161+
<NSelect
162+
style="width: 200px"
163+
:value="userInfo.config.chatModel"
164+
:options="authStore.session?.chatModels"
165+
:disabled="!!authStore.session?.auth && !authStore.token"
166+
@update-value="(val) => updateUserChatModel(val)"
167+
/>
168+
</div>
169+
</div>
141170
<div
142171
class="flex items-center space-x-4"
143172
:class="isMobile && 'items-start'"
@@ -200,12 +229,6 @@ function handleImportButtonClick(): void {
200229
/>
201230
</div>
202231
</div>
203-
<div class="flex items-center space-x-4">
204-
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.saveUserInfo') }}</span>
205-
<NButton type="primary" @click="updateUserInfo({ avatar, name, description })">
206-
{{ $t('common.save') }}
207-
</NButton>
208-
</div>
209232
</div>
210233
</div>
211234
</template>

src/locales/en-US.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export default {
142142
userRoles: 'User Role',
143143
status: 'Status',
144144
chatModels: 'Chat Models',
145+
defaultChatModel: 'Default Chat Model',
145146
remark: 'Remark',
146147
email: 'Email',
147148
password: 'Password',

src/locales/ko-KR.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export default {
140140
userRoles: 'User Role',
141141
status: 'Status',
142142
chatModels: 'Chat Models',
143+
defaultChatModel: '기본 대화 모델',
143144
remark: 'Remark',
144145
email: 'Email',
145146
password: 'Password',

src/locales/zh-CN.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export default {
142142
userRoles: '用户权限',
143143
status: '状态',
144144
chatModels: '对话模型',
145+
defaultChatModel: '默认对话模型',
145146
remark: '备注',
146147
email: '电子邮箱',
147148
password: '密码',

src/locales/zh-TW.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export default {
142142
userRoles: '用戶權限',
143143
status: '狀態',
144144
chatModels: '對話模型',
145+
defaultChatModel: '預設對話模型',
145146
remark: '備註',
146147
email: '電子郵箱',
147148
password: '密碼',

0 commit comments

Comments
 (0)