Skip to content

Commit

Permalink
feat: 添加自定义 temperature 和 top_p (#1260)
Browse files Browse the repository at this point in the history
* 在设置的高级面板里自定义temperature和top_p

* change default temperature from 0.8 to 0.5

* pref: 检查代码,增加仅 api 的接口判断

* chore: 锁定 pnpm-lock.yaml
  • Loading branch information
Kerwin committed Apr 15, 2023
1 parent 3c9a92e commit 796d731
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 14 deletions.
5 changes: 4 additions & 1 deletion service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ export async function initApi() {
}

async function chatReplyProcess(options: RequestOptions) {
const { message, lastContext, process, systemMessage } = options
const config = await getCacheConfig()
const model = isNotEmptyString(config.apiModel) ? config.apiModel : 'gpt-3.5-turbo'
const { message, lastContext, process, systemMessage, temperature, top_p } = options
try {
const timeoutMs = (await getCacheConfig()).timeoutMs
let options: SendMessageOptions = { timeoutMs }

if (apiModel === 'ChatGPTAPI') {
if (isNotEmptyString(systemMessage))
options.systemMessage = systemMessage
options.completionParams = { model, temperature, top_p }
}

if (lastContext != null) {
Expand Down
2 changes: 2 additions & 0 deletions service/src/chatgpt/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface RequestOptions {
lastContext?: { conversationId?: string; parentMessageId?: string }
process?: (chat: ChatMessage) => void
systemMessage?: string
temperature?: number
top_p?: number
}

export interface BalanceResponse {
Expand Down
4 changes: 3 additions & 1 deletion service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
res.setHeader('Content-type', 'application/octet-stream')

try {
const { roomId, uuid, regenerate, prompt, options = {}, systemMessage } = req.body as RequestProps
const { roomId, uuid, regenerate, prompt, options = {}, systemMessage, temperature, top_p } = req.body as RequestProps
const message = regenerate
? await getChat(roomId, uuid)
: await insertChat(uuid, prompt, roomId, options as ChatOptions)
Expand All @@ -297,6 +297,8 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
firstChunk = false
},
systemMessage,
temperature,
top_p,
})
if (result.status === 'Success') {
if (regenerate && message.options.messageId) {
Expand Down
2 changes: 2 additions & 0 deletions service/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface RequestProps {
prompt: string
options?: ChatContext
systemMessage: string
temperature?: number
top_p?: number
}

export interface ChatContext {
Expand Down
22 changes: 20 additions & 2 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AxiosProgressEvent, GenericAbortSignal } from 'axios'
import { get, post } from '@/utils/request'
import type { ConfigState, MailConfig, SiteConfig } from '@/components/common/Setting/model'
import { useSettingStore } from '@/store'
import { useAuthStore, useSettingStore } from '@/store'

export function fetchChatAPI<T = any>(
prompt: string,
Expand Down Expand Up @@ -32,10 +32,28 @@ export function fetchChatAPIProcess<T = any>(
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void },
) {
const settingStore = useSettingStore()
const authStore = useAuthStore()

let data: Record<string, any> = {
roomId: params.roomId,
uuid: params.uuid,
regenerate: params.regenerate || false,
prompt: params.prompt,
options: params.options,
}

if (authStore.isChatGPTAPI) {
data = {
...data,
systemMessage: settingStore.systemMessage,
temperature: settingStore.temperature,
top_p: settingStore.top_p,
}
}

return post<T>({
url: '/chat-process',
data: { roomId: params.roomId, uuid: params.uuid, regenerate: params.regenerate || false, prompt: params.prompt, options: params.options, systemMessage: settingStore.systemMessage },
data,
signal: params.signal,
onDownloadProgress: params.onDownloadProgress,
})
Expand Down
30 changes: 27 additions & 3 deletions src/components/common/Setting/Advanced.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup>
import { ref } from 'vue'
import { NButton, NInput, useMessage } from 'naive-ui'
import { NButton, NInput, NSlider, useMessage } from 'naive-ui'
import { useSettingStore } from '@/store'
import type { SettingsState } from '@/store/modules/settings/helper'
import { t } from '@/locales'
Expand All @@ -11,6 +11,10 @@ const ms = useMessage()
const systemMessage = ref(settingStore.systemMessage ?? '')
const temperature = ref(settingStore.temperature ?? 0.5)
const top_p = ref(settingStore.top_p ?? 1)
function updateSettings(options: Partial<SettingsState>) {
settingStore.updateSetting(options)
ms.success(t('common.success'))
Expand All @@ -27,7 +31,7 @@ function handleReset() {
<div class="p-4 space-y-5 min-h-[200px]">
<div class="space-y-6">
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[100px]">{{ $t('setting.role') }}</span>
<span class="flex-shrink-0 w-[120px]">{{ $t('setting.role') }}</span>
<div class="flex-1">
<NInput v-model:value="systemMessage" type="textarea" :autosize="{ minRows: 1, maxRows: 4 }" />
</div>
Expand All @@ -36,7 +40,27 @@ function handleReset() {
</NButton>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[100px]">&nbsp;</span>
<span class="flex-shrink-0 w-[120px]">{{ $t('setting.temperature') }} </span>
<div class="flex-1">
<NSlider v-model:value="temperature" :max="1" :min="0" :step="0.1" />
</div>
<span>{{ temperature }}</span>
<NButton size="tiny" text type="primary" @click="updateSettings({ temperature })">
{{ $t('common.save') }}
</NButton>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[120px]">{{ $t('setting.top_p') }} </span>
<div class="flex-1">
<NSlider v-model:value="top_p" :max="1" :min="0" :step="0.1" />
</div>
<span>{{ top_p }}</span>
<NButton size="tiny" text type="primary" @click="updateSettings({ top_p })">
{{ $t('common.save') }}
</NButton>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[120px]">&nbsp;</span>
<NButton size="small" @click="handleReset">
{{ $t('common.reset') }}
</NButton>
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export default {
avatarLink: 'Avatar Link',
name: 'Name',
description: 'Description',
temperature: 'Temperature',
top_p: 'Top_p',
saveUserInfo: 'Save User Info',
role: 'Role',
chatHistory: 'ChatHistory',
Expand Down
4 changes: 3 additions & 1 deletion src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ export default {
avatarLink: '头像链接',
name: '名称',
description: '描述',
saveUserInfo: '保存用户信息',
role: '角色设定',
temperature: 'Temperature',
saveUserInfo: '保存用户信息',
top_p: 'Top_p',
chatHistory: '聊天记录',
theme: '主题',
language: '语言',
Expand Down
4 changes: 3 additions & 1 deletion src/locales/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ export default {
avatarLink: '頭貼連結',
name: '名稱',
description: '描述',
saveUserInfo: '保存用户資訊',
role: '角色設定',
temperature: 'Temperature',
top_p: 'Top_p',
saveUserInfo: '保存用户資訊',
chatHistory: '紀錄',
theme: '主題',
language: '語言',
Expand Down
4 changes: 4 additions & 0 deletions src/store/modules/settings/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ const LOCAL_NAME = 'settingsStorage'

export interface SettingsState {
systemMessage: string
temperature: number
top_p: number
}

export function defaultSetting(): SettingsState {
return {
systemMessage: 'You are ChatGPT, a large language model trained by OpenAI. Follow the user\'s instructions carefully. Respond using markdown.',
temperature: 0.8,
top_p: 1,
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ async function onRegenerate(index: number) {
const data = JSON.parse(chunk)
const usage = data.detail.usage
? {
completion_tokens: data.detail.usage.completion_tokens || null,
prompt_tokens: data.detail.usage.prompt_tokens || null,
total_tokens: data.detail.usage.total_tokens || null,
estimated: data.detail.usage.estimated || null,
}
completion_tokens: data.detail.usage.completion_tokens || null,
prompt_tokens: data.detail.usage.prompt_tokens || null,
total_tokens: data.detail.usage.total_tokens || null,
estimated: data.detail.usage.estimated || null,
}
: undefined
updateChat(
+uuid,
Expand Down

0 comments on commit 796d731

Please sign in to comment.