Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of presence_penalty and frequency_penalty parameter #2111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
})()

async function chatReplyProcess(options: RequestOptions) {
const { message, lastContext, process, systemMessage, temperature, top_p } = options
const { message, lastContext, process, systemMessage, temperature, top_p, presence_penalty, frequency_penalty } = options
try {
let options: SendMessageOptions = { timeoutMs }

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

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 @@ -8,6 +8,8 @@ export interface RequestOptions {
systemMessage?: string
temperature?: number
top_p?: number
presence_penalty?: number
frequency_penalty?: number
}

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

try {
const { prompt, options = {}, systemMessage, temperature, top_p } = req.body as RequestProps
const { prompt, options = {}, systemMessage, temperature, top_p, presence_penalty, frequency_penalty } = req.body as RequestProps
let firstChunk = true
await chatReplyProcess({
message: prompt,
Expand All @@ -35,6 +35,8 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
systemMessage,
temperature,
top_p,
presence_penalty,
frequency_penalty,
})
}
catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions service/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface RequestProps {
systemMessage: string
temperature?: number
top_p?: number
presence_penalty?: number
frequency_penalty?: number
}

export interface ChatContext {
Expand Down
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export function fetchChatAPIProcess<T = any>(
systemMessage: settingStore.systemMessage,
temperature: settingStore.temperature,
top_p: settingStore.top_p,
presence_penalty: settingStore.presence_penalty,
frequency_penalty: settingStore.frequency_penalty,
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/components/common/Setting/Advanced.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const temperature = ref(settingStore.temperature ?? 0.5)

const top_p = ref(settingStore.top_p ?? 1)

const presence_penalty = ref(settingStore.presence_penalty ?? 0)

const frequency_penalty = ref(settingStore.frequency_penalty ?? 0)

function updateSettings(options: Partial<SettingsState>) {
settingStore.updateSetting(options)
ms.success(t('common.success'))
Expand Down Expand Up @@ -59,6 +63,26 @@ function handleReset() {
{{ $t('common.save') }}
</NButton>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[120px]">{{ $t('setting.presence_penalty') }} </span>
<div class="flex-1">
<NSlider v-model:value="presence_penalty" :max="2" :min="-2" :step="0.1" />
</div>
<span>{{ presence_penalty }}</span>
<NButton size="tiny" text type="primary" @click="updateSettings({ presence_penalty })">
{{ $t('common.save') }}
</NButton>
</div>
<div class="flex items-center space-x-4">
<span class="flex-shrink-0 w-[120px]">{{ $t('setting.frequency_penalty') }} </span>
<div class="flex-1">
<NSlider v-model:value="frequency_penalty" :max="2" :min="-2" :step="0.1" />
</div>
<span>{{ frequency_penalty }}</span>
<NButton size="tiny" text type="primary" @click="updateSettings({ frequency_penalty })">
{{ $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">
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 @@ -79,6 +79,8 @@ export default {
openSource: 'This project is open sourced at',
freeMIT: 'free and based on the MIT license, without any form of paid behavior!',
stars: 'If you find this project helpful, please give me a Star on GitHub or give a little sponsorship, thank you!',
presence_penalty: 'Presence Penalty',
frequency_penalty: 'Frequency Penalty',
},
store: {
siderButton: 'Prompt Store',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/es-ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export default {
openSource: 'Este proyecto es de código abierto en',
freeMIT: 'gratis y basado en la licencia MIT, ¡sin ningún tipo de comportamiento de pago!',
stars: 'Si encuentras este proyecto útil, por favor dame una Estrella en GitHub o da un pequeño patrocinio, ¡gracias!',
presence_penalty: 'Penalización de Presencia',
frequency_penalty: 'Penalización de Frecuencia',
},
store: {
siderButton: 'Tienda rápida',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/ko-KR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export default {
openSource: '이 프로젝트는 다음에서 오픈 소스로 제공됩니다:',
freeMIT: '무료이며 MIT 라이선스에 기반하며, 어떠한 형태의 유료 행동도 없습니다!',
stars: '이 프로젝트가 도움이 되었다면, GitHub에서 별을 주거나 조금의 후원을 해주시면 감사하겠습니다!',
presence_penalty: '존재 패널티',
frequency_penalty: '빈도 패널티',
},
store: {
siderButton: '프롬프트 저장소',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/ru-RU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export default {
openSource: 'Этот проект опубликован в открытом доступе на',
freeMIT: 'бесплатно и основан на лицензии MIT, без каких-либо форм оплаты!',
stars: 'Если вы считаете этот проект полезным, пожалуйста, поставьте мне звезду на GitHub или сделайте небольшое пожертвование, спасибо!',
presence_penalty: 'Штраф за присутствие',
frequency_penalty: 'Штраф за частоту',
},
store: {
siderButton: 'Хранилище подсказок',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/vi-VN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export default {
openSource: 'Dự án này được mở nguồn tại',
freeMIT: 'miễn phí và dựa trên giấy phép MIT, không có bất kỳ hình thức hành vi trả phí nào!',
stars: 'Nếu bạn thấy dự án này hữu ích, vui lòng cho tôi một Star trên GitHub hoặc tài trợ một chút, cảm ơn bạn!',
presence_penalty: 'Hình phạt Hiện diện',
frequency_penalty: 'Hình phạt Tần suất',
},
store: {
siderButton: 'Prompt Store',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export default {
openSource: '此项目开源于',
freeMIT: '免费且基于 MIT 协议,没有任何形式的付费行为',
stars: '如果你觉得此项目对你有帮助,请在 GitHub 上给我一个星星或者给予一点赞助,谢谢!',
presence_penalty: '出现惩罚',
frequency_penalty: '频率惩罚',
},
store: {
siderButton: '提示词商店',
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export default {
openSource: '此專案在此開源:',
freeMIT: '免費且基於 MIT 授權,沒有任何形式的付費行為!',
stars: '如果你覺得此專案對你有幫助,請在 GitHub 上給我一顆星,或者贊助我,謝謝!',
presence_penalty: '出現懲罰',
frequency_penalty: '頻率懲罰',
},
store: {
siderButton: '提示詞商店',
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 @@ -6,13 +6,17 @@ export interface SettingsState {
systemMessage: string
temperature: number
top_p: number
presence_penalty: number
frequency_penalty: 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,
presence_penalty: 0,
frequency_penalty: 0,
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/store/modules/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ export const useSettingStore = defineStore('setting-store', {
state: (): SettingsState => getLocalState(),
actions: {
updateSetting(settings: Partial<SettingsState>) {
if (settings.presence_penalty !== undefined && (settings.presence_penalty < -2.0 || settings.presence_penalty > 2.0))
throw new Error('presence_penalty must be between -2.0 and 2.0')

if (settings.frequency_penalty !== undefined && (settings.frequency_penalty < -2.0 || settings.frequency_penalty > 2.0))
throw new Error('frequency_penalty must be between -2.0 and 2.0')

this.$state = { ...this.$state, ...settings }
this.recordState()
},
Expand Down
Loading