Skip to content

Commit

Permalink
feat: switch to generation model set default stop word (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjoel authored Oct 13, 2023
1 parent 695841a commit a07a6d8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
2 changes: 2 additions & 0 deletions web/app/components/app/configuration/config-model/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ const ConfigModel: FC<IConfigModelProps> = ({
const handleParamChange = (key: string, value: number | string[]) => {
if (value === undefined)
return
if ((completionParams as any)[key] === value)
return

if (key === 'stop') {
onCompletionParamsChange({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from 'react'
import { clone } from 'lodash-es'
import produce from 'immer'
import type { ChatPromptConfig, CompletionPromptConfig, ConversationHistoriesRole, PromptItem } from '@/models/debug'
import type { ChatPromptConfig, CompletionParams, CompletionPromptConfig, ConversationHistoriesRole, PromptItem } from '@/models/debug'
import { PromptMode } from '@/models/debug'
import { AppType, ModelModeType } from '@/types/app'
import { DEFAULT_CHAT_PROMPT_CONFIG, DEFAULT_COMPLETION_PROMPT_CONFIG } from '@/config'
Expand All @@ -16,6 +16,9 @@ type Param = {
prePrompt: string
onUserChangedPrompt: () => void
hasSetDataSet: boolean
completionParams: CompletionParams
setCompletionParams: (params: CompletionParams) => void
setStop: (stop: string[]) => void
}

const useAdvancedPromptConfig = ({
Expand All @@ -26,6 +29,9 @@ const useAdvancedPromptConfig = ({
prePrompt,
onUserChangedPrompt,
hasSetDataSet,
completionParams,
setCompletionParams,
setStop,
}: Param) => {
const isAdvancedPrompt = promptMode === PromptMode.advanced
const [chatPromptConfig, setChatPromptConfig] = useState<ChatPromptConfig>(clone(DEFAULT_CHAT_PROMPT_CONFIG))
Expand Down Expand Up @@ -98,7 +104,7 @@ const useAdvancedPromptConfig = ({
const mode = modelModeType
const toReplacePrePrompt = prePrompt || ''
if (!isAdvancedPrompt) {
const { chat_prompt_config, completion_prompt_config } = await fetchPromptTemplate({
const { chat_prompt_config, completion_prompt_config, stop } = await fetchPromptTemplate({
appMode,
mode,
modelName,
Expand All @@ -121,12 +127,16 @@ const useAdvancedPromptConfig = ({
draft.prompt.text = draft.prompt.text.replace(PRE_PROMPT_PLACEHOLDER_TEXT, toReplacePrePrompt)
})
setCompletionPromptConfig(newPromptConfig)
setCompletionParams({
...completionParams,
stop,
})
}
return
}

if (isMigrateToCompetition) {
const { completion_prompt_config, chat_prompt_config } = await fetchPromptTemplate({
const { completion_prompt_config, chat_prompt_config, stop } = await fetchPromptTemplate({
appMode,
mode: toModelModeType as ModelModeType,
modelName,
Expand All @@ -145,6 +155,13 @@ const useAdvancedPromptConfig = ({
draft.conversation_histories_role = completionPromptConfig.conversation_histories_role
})
setCompletionPromptConfig(newPromptConfig)
if (!completionParams.stop || completionParams.stop.length === 0) {
setCompletionParams({
...completionParams,
stop,
})
}
setStop(stop) // switch mode's params is async. It may override the stop value.
}
else {
const newPromptConfig = produce(chat_prompt_config, (draft) => {
Expand Down
19 changes: 19 additions & 0 deletions web/app/components/app/configuration/images/prompt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 30 additions & 9 deletions web/app/components/app/configuration/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use client'
import type { FC } from 'react'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useContext } from 'use-context-selector'
import { usePathname } from 'next/navigation'
import produce from 'immer'
import { useBoolean } from 'ahooks'
import { useBoolean, useGetState } from 'ahooks'
import cn from 'classnames'
import { clone, isEqual } from 'lodash-es'
import Button from '../../base/button'
Expand Down Expand Up @@ -33,6 +33,7 @@ import { FlipBackward } from '@/app/components/base/icons/src/vender/line/arrows
import { PromptMode } from '@/models/debug'
import { DEFAULT_CHAT_PROMPT_CONFIG, DEFAULT_COMPLETION_PROMPT_CONFIG } from '@/config'
import SelectDataSet from '@/app/components/app/configuration/dataset-config/select-dataset'
import I18n from '@/context/i18n'

type PublichConfig = {
modelConfig: ModelConfig
Expand Down Expand Up @@ -74,13 +75,26 @@ const Configuration: FC = () => {
const [formattingChanged, setFormattingChanged] = useState(false)
const [inputs, setInputs] = useState<Inputs>({})
const [query, setQuery] = useState('')
const [completionParams, setCompletionParams] = useState<CompletionParams>({
const [completionParams, doSetCompletionParams] = useState<CompletionParams>({
max_tokens: 16,
temperature: 1, // 0-2
top_p: 1,
presence_penalty: 1, // -2-2
frequency_penalty: 1, // -2-2
stop: [],
})
const [tempStop, setTempStop, getTempStop] = useGetState<string[]>([])
const setCompletionParams = (value: CompletionParams) => {
const params = { ...value }

// eslint-disable-next-line @typescript-eslint/no-use-before-define
if ((!params.stop || params.stop.length === 0) && (modeModeTypeRef.current === ModelModeType.completion)) {
params.stop = getTempStop()
setTempStop([])
}
doSetCompletionParams(params)
}

const [modelConfig, doSetModelConfig] = useState<ModelConfig>({
provider: ProviderEnum.openai,
model_id: 'gpt-3.5-turbo',
Expand Down Expand Up @@ -110,6 +124,10 @@ const Configuration: FC = () => {
}

const modelModeType = modelConfig.mode
const modeModeTypeRef = useRef(modelModeType)
useEffect(() => {
modeModeTypeRef.current = modelModeType
}, [modelModeType])

const [dataSets, setDataSets] = useState<DataSet[]>([])
const contextVar = modelConfig.configs.prompt_variables.find(item => item.is_context_var)?.key
Expand Down Expand Up @@ -197,7 +215,7 @@ const Configuration: FC = () => {
const hasSetAPIKEY = hasSetCustomAPIKEY || !isTrailFinished

const [isShowSetAPIKey, { setTrue: showSetAPIKey, setFalse: hideSetAPIkey }] = useBoolean()
const [promptMode, doSetPromptMode] = useState(PromptMode.advanced)
const [promptMode, doSetPromptMode] = useState(PromptMode.simple)
const isAdvancedMode = promptMode === PromptMode.advanced
const [canReturnToSimpleMode, setCanReturnToSimpleMode] = useState(true)
const setPromptMode = async (mode: PromptMode) => {
Expand Down Expand Up @@ -230,6 +248,9 @@ const Configuration: FC = () => {
onUserChangedPrompt: () => {
setCanReturnToSimpleMode(false)
},
completionParams,
setCompletionParams,
setStop: setTempStop,
})

const setModel = async ({
Expand Down Expand Up @@ -449,6 +470,7 @@ const Configuration: FC = () => {

const [showUseGPT4Confirm, setShowUseGPT4Confirm] = useState(false)
const [showSetAPIKeyModal, setShowSetAPIKeyModal] = useState(false)
const { locale } = useContext(I18n)

if (isLoading) {
return <div className='flex h-full items-center justify-center'>
Expand Down Expand Up @@ -513,10 +535,9 @@ const Configuration: FC = () => {
<>
<div className="flex flex-col h-full">
<div className='flex items-center justify-between px-6 shrink-0 h-14'>
<div>
<div className='italic text-base font-bold text-gray-900 leading-[18px]'>{t('appDebug.pageTitle.line1')}</div>
<div className='flex items-center h-6 space-x-1 text-xs'>
<div className='text-gray-500 font-medium italic'>{t('appDebug.pageTitle.line2')}</div>
<div className='flex items-end'>
<div className={s.promptTitle}></div>
<div className='flex items-center h-[14px] space-x-1 text-xs'>
{/* modelModeType missing can not load template */}
{(!isAdvancedMode && modelModeType) && (
<div
Expand All @@ -528,7 +549,7 @@ const Configuration: FC = () => {
)}
{isAdvancedMode && (
<div className='flex items-center space-x-2'>
<div className={`${s.advancedPromptMode} italic text-indigo-600`}>{t('appDebug.promptMode.advanced')}</div>
<div className={cn(locale === 'en' && 'italic', `${s.advancedPromptMode} text-indigo-600`)}>{t('appDebug.promptMode.advanced')}</div>
{canReturnToSimpleMode && (
<div
onClick={() => setPromptMode(PromptMode.simple)}
Expand Down
7 changes: 7 additions & 0 deletions web/app/components/app/configuration/style.module.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.promptTitle {
width: 72px;
height: 31px;
background: url(./images/prompt.svg) no-repeat 0 0;
background-size: contain;
}

.advancedPromptMode {
position: relative;
}
Expand Down
Loading

0 comments on commit a07a6d8

Please sign in to comment.