index.tsx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useMemo, useRef, useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import { useContext } from 'use-context-selector'
  6. import { usePathname } from 'next/navigation'
  7. import produce from 'immer'
  8. import { useBoolean, useGetState } from 'ahooks'
  9. import { clone, isEqual } from 'lodash-es'
  10. import { CodeBracketIcon } from '@heroicons/react/20/solid'
  11. import { useShallow } from 'zustand/react/shallow'
  12. import Button from '../../base/button'
  13. import Loading from '../../base/loading'
  14. import AppPublisher from '../app-publisher'
  15. import AgentSettingButton from './config/agent-setting-button'
  16. import useAdvancedPromptConfig from './hooks/use-advanced-prompt-config'
  17. import EditHistoryModal from './config-prompt/conversation-histroy/edit-modal'
  18. import {
  19. useDebugWithSingleOrMultipleModel,
  20. useFormattingChangedDispatcher,
  21. } from './debug/hooks'
  22. import type { ModelAndParameter } from './debug/types'
  23. import type {
  24. AnnotationReplyConfig,
  25. DatasetConfigs,
  26. Inputs,
  27. ModelConfig,
  28. ModerationConfig,
  29. MoreLikeThisConfig,
  30. PromptConfig,
  31. PromptVariable,
  32. TextToSpeechConfig,
  33. } from '@/models/debug'
  34. import type { ExternalDataTool } from '@/models/common'
  35. import type { DataSet } from '@/models/datasets'
  36. import type { ModelConfig as BackendModelConfig, VisionSettings } from '@/types/app'
  37. import ConfigContext from '@/context/debug-configuration'
  38. import Config from '@/app/components/app/configuration/config'
  39. import Debug from '@/app/components/app/configuration/debug'
  40. import Confirm from '@/app/components/base/confirm'
  41. import { ModelFeatureEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  42. import { ToastContext } from '@/app/components/base/toast'
  43. import { fetchAppDetail, updateAppModelConfig } from '@/service/apps'
  44. import { promptVariablesToUserInputsForm, userInputsFormToPromptVariables } from '@/utils/model-config'
  45. import { fetchDatasets } from '@/service/datasets'
  46. import { useProviderContext } from '@/context/provider-context'
  47. import { AgentStrategy, AppType, ModelModeType, RETRIEVE_TYPE, Resolution, TransferMethod } from '@/types/app'
  48. import { PromptMode } from '@/models/debug'
  49. import { ANNOTATION_DEFAULT, DEFAULT_AGENT_SETTING, DEFAULT_CHAT_PROMPT_CONFIG, DEFAULT_COMPLETION_PROMPT_CONFIG } from '@/config'
  50. import SelectDataSet from '@/app/components/app/configuration/dataset-config/select-dataset'
  51. import { useModalContext } from '@/context/modal-context'
  52. import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints'
  53. import Drawer from '@/app/components/base/drawer'
  54. import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
  55. import type { FormValue } from '@/app/components/header/account-setting/model-provider-page/declarations'
  56. import { useTextGenerationCurrentProviderAndModelAndModelList } from '@/app/components/header/account-setting/model-provider-page/hooks'
  57. import { fetchCollectionList } from '@/service/tools'
  58. import { type Collection } from '@/app/components/tools/types'
  59. import { useStore as useAppStore } from '@/app/components/app/store'
  60. type PublishConfig = {
  61. modelConfig: ModelConfig
  62. completionParams: FormValue
  63. }
  64. const Configuration: FC = () => {
  65. const { t } = useTranslation()
  66. const { notify } = useContext(ToastContext)
  67. const { appDetail, setAppSiderbarExpand } = useAppStore(useShallow(state => ({
  68. appDetail: state.appDetail,
  69. setAppSiderbarExpand: state.setAppSiderbarExpand,
  70. })))
  71. const [formattingChanged, setFormattingChanged] = useState(false)
  72. const { setShowAccountSettingModal } = useModalContext()
  73. const [hasFetchedDetail, setHasFetchedDetail] = useState(false)
  74. const isLoading = !hasFetchedDetail
  75. const pathname = usePathname()
  76. const matched = pathname.match(/\/app\/([^/]+)/)
  77. const appId = (matched?.length && matched[1]) ? matched[1] : ''
  78. const [mode, setMode] = useState('')
  79. const [publishedConfig, setPublishedConfig] = useState<PublishConfig | null>(null)
  80. const modalConfig = useMemo(() => appDetail?.model_config || {} as BackendModelConfig, [appDetail])
  81. const [conversationId, setConversationId] = useState<string | null>('')
  82. const media = useBreakpoints()
  83. const isMobile = media === MediaType.mobile
  84. const [isShowDebugPanel, { setTrue: showDebugPanel, setFalse: hideDebugPanel }] = useBoolean(false)
  85. const [introduction, setIntroduction] = useState<string>('')
  86. const [suggestedQuestions, setSuggestedQuestions] = useState<string[]>([])
  87. const [controlClearChatMessage, setControlClearChatMessage] = useState(0)
  88. const [prevPromptConfig, setPrevPromptConfig] = useState<PromptConfig>({
  89. prompt_template: '',
  90. prompt_variables: [],
  91. })
  92. const [moreLikeThisConfig, setMoreLikeThisConfig] = useState<MoreLikeThisConfig>({
  93. enabled: false,
  94. })
  95. const [suggestedQuestionsAfterAnswerConfig, setSuggestedQuestionsAfterAnswerConfig] = useState<MoreLikeThisConfig>({
  96. enabled: false,
  97. })
  98. const [speechToTextConfig, setSpeechToTextConfig] = useState<MoreLikeThisConfig>({
  99. enabled: false,
  100. })
  101. const [textToSpeechConfig, setTextToSpeechConfig] = useState<TextToSpeechConfig>({
  102. enabled: false,
  103. voice: '',
  104. language: '',
  105. })
  106. const [citationConfig, setCitationConfig] = useState<MoreLikeThisConfig>({
  107. enabled: false,
  108. })
  109. const [annotationConfig, doSetAnnotationConfig] = useState<AnnotationReplyConfig>({
  110. id: '',
  111. enabled: false,
  112. score_threshold: ANNOTATION_DEFAULT.score_threshold,
  113. embedding_model: {
  114. embedding_provider_name: '',
  115. embedding_model_name: '',
  116. },
  117. })
  118. const formattingChangedDispatcher = useFormattingChangedDispatcher()
  119. const setAnnotationConfig = (config: AnnotationReplyConfig, notSetFormatChanged?: boolean) => {
  120. doSetAnnotationConfig(config)
  121. if (!notSetFormatChanged)
  122. formattingChangedDispatcher()
  123. }
  124. const [moderationConfig, setModerationConfig] = useState<ModerationConfig>({
  125. enabled: false,
  126. })
  127. const [externalDataToolsConfig, setExternalDataToolsConfig] = useState<ExternalDataTool[]>([])
  128. const [inputs, setInputs] = useState<Inputs>({})
  129. const [query, setQuery] = useState('')
  130. const [completionParams, doSetCompletionParams] = useState<FormValue>({})
  131. const [_, setTempStop, getTempStop] = useGetState<string[]>([])
  132. const setCompletionParams = (value: FormValue) => {
  133. const params = { ...value }
  134. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  135. if ((!params.stop || params.stop.length === 0) && (modeModeTypeRef.current === ModelModeType.completion)) {
  136. params.stop = getTempStop()
  137. setTempStop([])
  138. }
  139. doSetCompletionParams(params)
  140. }
  141. const [modelConfig, doSetModelConfig] = useState<ModelConfig>({
  142. provider: 'openai',
  143. model_id: 'gpt-3.5-turbo',
  144. mode: ModelModeType.unset,
  145. configs: {
  146. prompt_template: '',
  147. prompt_variables: [] as PromptVariable[],
  148. },
  149. opening_statement: '',
  150. more_like_this: null,
  151. suggested_questions_after_answer: null,
  152. speech_to_text: null,
  153. text_to_speech: null,
  154. retriever_resource: null,
  155. sensitive_word_avoidance: null,
  156. dataSets: [],
  157. agentConfig: DEFAULT_AGENT_SETTING,
  158. })
  159. const isAgent = mode === 'agent-chat'
  160. const isOpenAI = modelConfig.provider === 'openai'
  161. const [collectionList, setCollectionList] = useState<Collection[]>([])
  162. useEffect(() => {
  163. }, [])
  164. const [datasetConfigs, setDatasetConfigs] = useState<DatasetConfigs>({
  165. retrieval_model: RETRIEVE_TYPE.oneWay,
  166. reranking_model: {
  167. reranking_provider_name: '',
  168. reranking_model_name: '',
  169. },
  170. top_k: 2,
  171. score_threshold_enabled: false,
  172. score_threshold: 0.7,
  173. datasets: {
  174. datasets: [],
  175. },
  176. })
  177. const setModelConfig = (newModelConfig: ModelConfig) => {
  178. doSetModelConfig(newModelConfig)
  179. }
  180. const modelModeType = modelConfig.mode
  181. const modeModeTypeRef = useRef(modelModeType)
  182. useEffect(() => {
  183. modeModeTypeRef.current = modelModeType
  184. }, [modelModeType])
  185. const [dataSets, setDataSets] = useState<DataSet[]>([])
  186. const contextVar = modelConfig.configs.prompt_variables.find((item: any) => item.is_context_var)?.key
  187. const hasSetContextVar = !!contextVar
  188. const [isShowSelectDataSet, { setTrue: showSelectDataSet, setFalse: hideSelectDataSet }] = useBoolean(false)
  189. const selectedIds = dataSets.map(item => item.id)
  190. const handleSelect = (data: DataSet[]) => {
  191. if (isEqual(data.map(item => item.id), dataSets.map(item => item.id))) {
  192. hideSelectDataSet()
  193. return
  194. }
  195. formattingChangedDispatcher()
  196. if (data.find(item => !item.name)) { // has not loaded selected dataset
  197. const newSelected = produce(data, (draft: any) => {
  198. data.forEach((item, index) => {
  199. if (!item.name) { // not fetched database
  200. const newItem = dataSets.find(i => i.id === item.id)
  201. if (newItem)
  202. draft[index] = newItem
  203. }
  204. })
  205. })
  206. setDataSets(newSelected)
  207. }
  208. else {
  209. setDataSets(data)
  210. }
  211. hideSelectDataSet()
  212. }
  213. const [isShowHistoryModal, { setTrue: showHistoryModal, setFalse: hideHistoryModal }] = useBoolean(false)
  214. const syncToPublishedConfig = (_publishedConfig: PublishConfig) => {
  215. const modelConfig = _publishedConfig.modelConfig
  216. setModelConfig(_publishedConfig.modelConfig)
  217. setCompletionParams(_publishedConfig.completionParams)
  218. setDataSets(modelConfig.dataSets || [])
  219. // feature
  220. setIntroduction(modelConfig.opening_statement!)
  221. setMoreLikeThisConfig(modelConfig.more_like_this || {
  222. enabled: false,
  223. })
  224. setSuggestedQuestionsAfterAnswerConfig(modelConfig.suggested_questions_after_answer || {
  225. enabled: false,
  226. })
  227. setSpeechToTextConfig(modelConfig.speech_to_text || {
  228. enabled: false,
  229. })
  230. setTextToSpeechConfig(modelConfig.text_to_speech || {
  231. enabled: false,
  232. voice: '',
  233. language: '',
  234. })
  235. setCitationConfig(modelConfig.retriever_resource || {
  236. enabled: false,
  237. })
  238. }
  239. const { isAPIKeySet } = useProviderContext()
  240. const {
  241. currentModel: currModel,
  242. textGenerationModelList,
  243. } = useTextGenerationCurrentProviderAndModelAndModelList(
  244. {
  245. provider: modelConfig.provider,
  246. model: modelConfig.model_id,
  247. },
  248. )
  249. const isFunctionCall = (() => {
  250. const features = currModel?.features
  251. if (!features)
  252. return false
  253. return features.includes(ModelFeatureEnum.toolCall) || features.includes(ModelFeatureEnum.multiToolCall)
  254. })()
  255. // Fill old app data missing model mode.
  256. useEffect(() => {
  257. if (hasFetchedDetail && !modelModeType) {
  258. const mode = currModel?.model_properties.mode as (ModelModeType | undefined)
  259. if (mode) {
  260. const newModelConfig = produce(modelConfig, (draft: ModelConfig) => {
  261. draft.mode = mode
  262. })
  263. setModelConfig(newModelConfig)
  264. }
  265. }
  266. }, [textGenerationModelList, hasFetchedDetail, modelModeType, currModel, modelConfig])
  267. const [promptMode, doSetPromptMode] = useState(PromptMode.simple)
  268. const isAdvancedMode = promptMode === PromptMode.advanced
  269. const [canReturnToSimpleMode, setCanReturnToSimpleMode] = useState(true)
  270. const setPromptMode = async (mode: PromptMode) => {
  271. if (mode === PromptMode.advanced) {
  272. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  273. await migrateToDefaultPrompt()
  274. setCanReturnToSimpleMode(true)
  275. }
  276. doSetPromptMode(mode)
  277. }
  278. const [visionConfig, doSetVisionConfig] = useState({
  279. enabled: false,
  280. number_limits: 2,
  281. detail: Resolution.low,
  282. transfer_methods: [TransferMethod.local_file],
  283. })
  284. const handleSetVisionConfig = (config: VisionSettings, notNoticeFormattingChanged?: boolean) => {
  285. doSetVisionConfig({
  286. enabled: config.enabled || false,
  287. number_limits: config.number_limits || 2,
  288. detail: config.detail || Resolution.low,
  289. transfer_methods: config.transfer_methods || [TransferMethod.local_file],
  290. })
  291. if (!notNoticeFormattingChanged)
  292. formattingChangedDispatcher()
  293. }
  294. const {
  295. chatPromptConfig,
  296. setChatPromptConfig,
  297. completionPromptConfig,
  298. setCompletionPromptConfig,
  299. currentAdvancedPrompt,
  300. setCurrentAdvancedPrompt,
  301. hasSetBlockStatus,
  302. setConversationHistoriesRole,
  303. migrateToDefaultPrompt,
  304. } = useAdvancedPromptConfig({
  305. appMode: mode,
  306. modelName: modelConfig.model_id,
  307. promptMode,
  308. modelModeType,
  309. prePrompt: modelConfig.configs.prompt_template,
  310. hasSetDataSet: dataSets.length > 0,
  311. onUserChangedPrompt: () => {
  312. setCanReturnToSimpleMode(false)
  313. },
  314. completionParams,
  315. setCompletionParams,
  316. setStop: setTempStop,
  317. })
  318. const setModel = async ({
  319. modelId,
  320. provider,
  321. mode: modeMode,
  322. features,
  323. }: { modelId: string; provider: string; mode: string; features: string[] }) => {
  324. if (isAdvancedMode) {
  325. const appMode = mode
  326. if (modeMode === ModelModeType.completion) {
  327. if (appMode !== AppType.completion) {
  328. if (!completionPromptConfig.prompt?.text || !completionPromptConfig.conversation_histories_role.assistant_prefix || !completionPromptConfig.conversation_histories_role.user_prefix)
  329. await migrateToDefaultPrompt(true, ModelModeType.completion)
  330. }
  331. else {
  332. if (!completionPromptConfig.prompt?.text)
  333. await migrateToDefaultPrompt(true, ModelModeType.completion)
  334. }
  335. }
  336. if (modeMode === ModelModeType.chat) {
  337. if (chatPromptConfig.prompt.length === 0)
  338. await migrateToDefaultPrompt(true, ModelModeType.chat)
  339. }
  340. }
  341. const newModelConfig = produce(modelConfig, (draft: ModelConfig) => {
  342. draft.provider = provider
  343. draft.model_id = modelId
  344. draft.mode = modeMode as ModelModeType
  345. })
  346. setModelConfig(newModelConfig)
  347. const supportVision = features && features.includes(ModelFeatureEnum.vision)
  348. handleSetVisionConfig({
  349. ...visionConfig,
  350. enabled: supportVision,
  351. }, true)
  352. setCompletionParams({})
  353. }
  354. const isShowVisionConfig = !!currModel?.features?.includes(ModelFeatureEnum.vision)
  355. useEffect(() => {
  356. (async () => {
  357. const collectionList = await fetchCollectionList()
  358. setCollectionList(collectionList)
  359. fetchAppDetail({ url: '/apps', id: appId }).then(async (res: any) => {
  360. setMode(res.mode)
  361. const modelConfig = res.model_config
  362. const promptMode = modelConfig.prompt_type === PromptMode.advanced ? PromptMode.advanced : PromptMode.simple
  363. doSetPromptMode(promptMode)
  364. if (promptMode === PromptMode.advanced) {
  365. if (modelConfig.chat_prompt_config && modelConfig.chat_prompt_config.prompt.length > 0)
  366. setChatPromptConfig(modelConfig.chat_prompt_config)
  367. else
  368. setChatPromptConfig(clone(DEFAULT_CHAT_PROMPT_CONFIG) as any)
  369. setCompletionPromptConfig(modelConfig.completion_prompt_config || clone(DEFAULT_COMPLETION_PROMPT_CONFIG) as any)
  370. setCanReturnToSimpleMode(false)
  371. }
  372. const model = res.model_config.model
  373. let datasets: any = null
  374. // old dataset struct
  375. if (modelConfig.agent_mode?.tools?.find(({ dataset }: any) => dataset?.enabled))
  376. datasets = modelConfig.agent_mode?.tools.filter(({ dataset }: any) => dataset?.enabled)
  377. // new dataset struct
  378. else if (modelConfig.dataset_configs.datasets?.datasets?.length > 0)
  379. datasets = modelConfig.dataset_configs?.datasets?.datasets
  380. if (dataSets && datasets?.length && datasets?.length > 0) {
  381. const { data: dataSetsWithDetail } = await fetchDatasets({ url: '/datasets', params: { page: 1, ids: datasets.map(({ dataset }: any) => dataset.id) } })
  382. datasets = dataSetsWithDetail
  383. setDataSets(datasets)
  384. }
  385. setIntroduction(modelConfig.opening_statement)
  386. setSuggestedQuestions(modelConfig.suggested_questions || [])
  387. if (modelConfig.more_like_this)
  388. setMoreLikeThisConfig(modelConfig.more_like_this)
  389. if (modelConfig.suggested_questions_after_answer)
  390. setSuggestedQuestionsAfterAnswerConfig(modelConfig.suggested_questions_after_answer)
  391. if (modelConfig.speech_to_text)
  392. setSpeechToTextConfig(modelConfig.speech_to_text)
  393. if (modelConfig.text_to_speech)
  394. setTextToSpeechConfig(modelConfig.text_to_speech)
  395. if (modelConfig.retriever_resource)
  396. setCitationConfig(modelConfig.retriever_resource)
  397. if (modelConfig.annotation_reply)
  398. setAnnotationConfig(modelConfig.annotation_reply, true)
  399. if (modelConfig.sensitive_word_avoidance)
  400. setModerationConfig(modelConfig.sensitive_word_avoidance)
  401. if (modelConfig.external_data_tools)
  402. setExternalDataToolsConfig(modelConfig.external_data_tools)
  403. const config = {
  404. modelConfig: {
  405. provider: model.provider,
  406. model_id: model.name,
  407. mode: model.mode,
  408. configs: {
  409. prompt_template: modelConfig.pre_prompt || '',
  410. prompt_variables: userInputsFormToPromptVariables(
  411. [
  412. ...modelConfig.user_input_form,
  413. ...(
  414. modelConfig.external_data_tools?.length
  415. ? modelConfig.external_data_tools.map((item: any) => {
  416. return {
  417. external_data_tool: {
  418. variable: item.variable as string,
  419. label: item.label as string,
  420. enabled: item.enabled,
  421. type: item.type as string,
  422. config: item.config,
  423. required: true,
  424. icon: item.icon,
  425. icon_background: item.icon_background,
  426. },
  427. }
  428. })
  429. : []
  430. ),
  431. ],
  432. modelConfig.dataset_query_variable,
  433. ),
  434. },
  435. opening_statement: modelConfig.opening_statement,
  436. more_like_this: modelConfig.more_like_this,
  437. suggested_questions_after_answer: modelConfig.suggested_questions_after_answer,
  438. speech_to_text: modelConfig.speech_to_text,
  439. text_to_speech: modelConfig.text_to_speech,
  440. retriever_resource: modelConfig.retriever_resource,
  441. sensitive_word_avoidance: modelConfig.sensitive_word_avoidance,
  442. external_data_tools: modelConfig.external_data_tools,
  443. dataSets: datasets || [],
  444. // eslint-disable-next-line multiline-ternary
  445. agentConfig: res.mode === 'agent-chat' ? {
  446. max_iteration: DEFAULT_AGENT_SETTING.max_iteration,
  447. ...modelConfig.agent_mode,
  448. // remove dataset
  449. enabled: true, // modelConfig.agent_mode?.enabled is not correct. old app: the value of app with dataset's is always true
  450. tools: modelConfig.agent_mode?.tools.filter((tool: any) => {
  451. return !tool.dataset
  452. }).map((tool: any) => {
  453. return {
  454. ...tool,
  455. isDeleted: res.deleted_tools?.includes(tool.tool_name),
  456. notAuthor: collectionList.find(c => tool.provider_id === c.id)?.is_team_authorization === false,
  457. }
  458. }),
  459. } : DEFAULT_AGENT_SETTING,
  460. },
  461. completionParams: model.completion_params,
  462. }
  463. if (modelConfig.file_upload)
  464. handleSetVisionConfig(modelConfig.file_upload.image, true)
  465. syncToPublishedConfig(config)
  466. setPublishedConfig(config)
  467. setDatasetConfigs({
  468. retrieval_model: RETRIEVE_TYPE.oneWay,
  469. ...modelConfig.dataset_configs,
  470. })
  471. setHasFetchedDetail(true)
  472. })
  473. })()
  474. // eslint-disable-next-line react-hooks/exhaustive-deps
  475. }, [appId])
  476. const promptEmpty = (() => {
  477. if (mode !== AppType.completion)
  478. return false
  479. if (isAdvancedMode) {
  480. if (modelModeType === ModelModeType.chat)
  481. return chatPromptConfig.prompt.every(({ text }: any) => !text)
  482. else
  483. return !completionPromptConfig.prompt?.text
  484. }
  485. else { return !modelConfig.configs.prompt_template }
  486. })()
  487. const cannotPublish = (() => {
  488. if (mode !== AppType.completion) {
  489. if (!isAdvancedMode)
  490. return false
  491. if (modelModeType === ModelModeType.completion) {
  492. if (!hasSetBlockStatus.history || !hasSetBlockStatus.query)
  493. return true
  494. return false
  495. }
  496. return false
  497. }
  498. else { return promptEmpty }
  499. })()
  500. const contextVarEmpty = mode === AppType.completion && dataSets.length > 0 && !hasSetContextVar
  501. const onPublish = async (modelAndParameter?: ModelAndParameter) => {
  502. const modelId = modelAndParameter?.model || modelConfig.model_id
  503. const promptTemplate = modelConfig.configs.prompt_template
  504. const promptVariables = modelConfig.configs.prompt_variables
  505. if (promptEmpty) {
  506. notify({ type: 'error', message: t('appDebug.otherError.promptNoBeEmpty'), duration: 3000 })
  507. return
  508. }
  509. if (isAdvancedMode && mode !== AppType.completion) {
  510. if (modelModeType === ModelModeType.completion) {
  511. if (!hasSetBlockStatus.history) {
  512. notify({ type: 'error', message: t('appDebug.otherError.historyNoBeEmpty'), duration: 3000 })
  513. return
  514. }
  515. if (!hasSetBlockStatus.query) {
  516. notify({ type: 'error', message: t('appDebug.otherError.queryNoBeEmpty'), duration: 3000 })
  517. return
  518. }
  519. }
  520. }
  521. if (contextVarEmpty) {
  522. notify({ type: 'error', message: t('appDebug.feature.dataSet.queryVariable.contextVarNotEmpty'), duration: 3000 })
  523. return
  524. }
  525. const postDatasets = dataSets.map(({ id }) => ({
  526. dataset: {
  527. enabled: true,
  528. id,
  529. },
  530. }))
  531. // new model config data struct
  532. const data: BackendModelConfig = {
  533. // Simple Mode prompt
  534. pre_prompt: !isAdvancedMode ? promptTemplate : '',
  535. prompt_type: promptMode,
  536. chat_prompt_config: {},
  537. completion_prompt_config: {},
  538. user_input_form: promptVariablesToUserInputsForm(promptVariables),
  539. dataset_query_variable: contextVar || '',
  540. opening_statement: introduction || '',
  541. suggested_questions: suggestedQuestions || [],
  542. more_like_this: moreLikeThisConfig,
  543. suggested_questions_after_answer: suggestedQuestionsAfterAnswerConfig,
  544. speech_to_text: speechToTextConfig,
  545. text_to_speech: textToSpeechConfig,
  546. retriever_resource: citationConfig,
  547. sensitive_word_avoidance: moderationConfig,
  548. agent_mode: {
  549. ...modelConfig.agentConfig,
  550. strategy: isFunctionCall ? AgentStrategy.functionCall : AgentStrategy.react,
  551. },
  552. model: {
  553. provider: modelAndParameter?.provider || modelConfig.provider,
  554. name: modelId,
  555. mode: modelConfig.mode,
  556. completion_params: modelAndParameter?.parameters || completionParams as any,
  557. },
  558. dataset_configs: {
  559. ...datasetConfigs,
  560. datasets: {
  561. datasets: [...postDatasets],
  562. } as any,
  563. },
  564. file_upload: {
  565. image: visionConfig,
  566. },
  567. }
  568. if (isAdvancedMode) {
  569. data.chat_prompt_config = chatPromptConfig
  570. data.completion_prompt_config = completionPromptConfig
  571. }
  572. await updateAppModelConfig({ url: `/apps/${appId}/model-config`, body: data })
  573. const newModelConfig = produce(modelConfig, (draft: any) => {
  574. draft.opening_statement = introduction
  575. draft.more_like_this = moreLikeThisConfig
  576. draft.suggested_questions_after_answer = suggestedQuestionsAfterAnswerConfig
  577. draft.speech_to_text = speechToTextConfig
  578. draft.text_to_speech = textToSpeechConfig
  579. draft.retriever_resource = citationConfig
  580. draft.dataSets = dataSets
  581. })
  582. setPublishedConfig({
  583. modelConfig: newModelConfig,
  584. completionParams,
  585. })
  586. notify({ type: 'success', message: t('common.api.success'), duration: 3000 })
  587. setCanReturnToSimpleMode(false)
  588. return true
  589. }
  590. const [restoreConfirmOpen, setRestoreConfirmOpen] = useState(false)
  591. const resetAppConfig = () => {
  592. syncToPublishedConfig(publishedConfig!)
  593. setRestoreConfirmOpen(false)
  594. }
  595. const [showUseGPT4Confirm, setShowUseGPT4Confirm] = useState(false)
  596. const {
  597. debugWithMultipleModel,
  598. multipleModelConfigs,
  599. handleMultipleModelConfigsChange,
  600. } = useDebugWithSingleOrMultipleModel(appId)
  601. const handleDebugWithMultipleModelChange = () => {
  602. handleMultipleModelConfigsChange(
  603. true,
  604. [
  605. { id: `${Date.now()}`, model: modelConfig.model_id, provider: modelConfig.provider, parameters: completionParams },
  606. { id: `${Date.now()}-no-repeat`, model: '', provider: '', parameters: {} },
  607. ],
  608. )
  609. setAppSiderbarExpand('collapse')
  610. }
  611. if (isLoading) {
  612. return <div className='flex items-center justify-center h-full'>
  613. <Loading type='area' />
  614. </div>
  615. }
  616. return (
  617. <ConfigContext.Provider value={{
  618. appId,
  619. isAPIKeySet,
  620. isTrailFinished: false,
  621. mode,
  622. modelModeType,
  623. promptMode,
  624. isAdvancedMode,
  625. isAgent,
  626. isOpenAI,
  627. isFunctionCall,
  628. collectionList,
  629. setPromptMode,
  630. canReturnToSimpleMode,
  631. setCanReturnToSimpleMode,
  632. chatPromptConfig,
  633. completionPromptConfig,
  634. currentAdvancedPrompt,
  635. setCurrentAdvancedPrompt,
  636. conversationHistoriesRole: completionPromptConfig.conversation_histories_role,
  637. showHistoryModal,
  638. setConversationHistoriesRole,
  639. hasSetBlockStatus,
  640. conversationId,
  641. introduction,
  642. setIntroduction,
  643. suggestedQuestions,
  644. setSuggestedQuestions,
  645. setConversationId,
  646. controlClearChatMessage,
  647. setControlClearChatMessage,
  648. prevPromptConfig,
  649. setPrevPromptConfig,
  650. moreLikeThisConfig,
  651. setMoreLikeThisConfig,
  652. suggestedQuestionsAfterAnswerConfig,
  653. setSuggestedQuestionsAfterAnswerConfig,
  654. speechToTextConfig,
  655. setSpeechToTextConfig,
  656. textToSpeechConfig,
  657. setTextToSpeechConfig,
  658. citationConfig,
  659. setCitationConfig,
  660. annotationConfig,
  661. setAnnotationConfig,
  662. moderationConfig,
  663. setModerationConfig,
  664. externalDataToolsConfig,
  665. setExternalDataToolsConfig,
  666. formattingChanged,
  667. setFormattingChanged,
  668. inputs,
  669. setInputs,
  670. query,
  671. setQuery,
  672. completionParams,
  673. setCompletionParams,
  674. modelConfig,
  675. setModelConfig,
  676. showSelectDataSet,
  677. dataSets,
  678. setDataSets,
  679. datasetConfigs,
  680. setDatasetConfigs,
  681. hasSetContextVar,
  682. isShowVisionConfig,
  683. visionConfig,
  684. setVisionConfig: handleSetVisionConfig,
  685. }}
  686. >
  687. <>
  688. <div className="flex flex-col h-full">
  689. <div className='relative flex grow h-[200px] pt-14'>
  690. {/* Header */}
  691. <div className='absolute top-0 left-0 w-full bg-white h-14'>
  692. <div className='flex items-center justify-between px-6 h-14'>
  693. <div className='flex items-center'>
  694. <div className='text-base font-semibold leading-6 text-gray-900'>{t('appDebug.orchestrate')}</div>
  695. <div className='flex items-center h-[14px] space-x-1 text-xs'>
  696. {isAdvancedMode && (
  697. <div className='ml-1 flex items-center h-5 px-1.5 border border-gray-100 rounded-md text-[11px] font-medium text-gray-500 uppercase'>{t('appDebug.promptMode.advanced')}</div>
  698. )}
  699. </div>
  700. </div>
  701. <div className='flex items-center'>
  702. {/* Agent Setting */}
  703. {isAgent && (
  704. <AgentSettingButton
  705. isChatModel={modelConfig.mode === ModelModeType.chat}
  706. agentConfig={modelConfig.agentConfig}
  707. isFunctionCall={isFunctionCall}
  708. onAgentSettingChange={(config) => {
  709. const nextConfig = produce(modelConfig, (draft: ModelConfig) => {
  710. draft.agentConfig = config
  711. })
  712. setModelConfig(nextConfig)
  713. }}
  714. />
  715. )}
  716. {/* Model and Parameters */}
  717. {!debugWithMultipleModel && (
  718. <>
  719. <ModelParameterModal
  720. isAdvancedMode={isAdvancedMode}
  721. mode={mode}
  722. provider={modelConfig.provider}
  723. completionParams={completionParams}
  724. modelId={modelConfig.model_id}
  725. setModel={setModel as any}
  726. onCompletionParamsChange={(newParams: FormValue) => {
  727. setCompletionParams(newParams)
  728. }}
  729. debugWithMultipleModel={debugWithMultipleModel}
  730. onDebugWithMultipleModelChange={handleDebugWithMultipleModelChange}
  731. />
  732. <div className='mx-2 w-[1px] h-[14px] bg-gray-200'></div>
  733. </>
  734. )}
  735. {isMobile && (
  736. <Button className='!h-8 !text-[13px] font-medium' onClick={showDebugPanel}>
  737. <span className='mr-1'>{t('appDebug.operation.debugConfig')}</span>
  738. <CodeBracketIcon className="w-4 h-4 text-gray-500" />
  739. </Button>
  740. )}
  741. <AppPublisher {...{
  742. publishDisabled: cannotPublish,
  743. publishedAt: (modalConfig.created_at || 0) * 1000,
  744. debugWithMultipleModel,
  745. multipleModelConfigs,
  746. onPublish,
  747. onRestore: () => setRestoreConfirmOpen(true),
  748. }} />
  749. </div>
  750. </div>
  751. </div>
  752. <div className={`w-full sm:w-1/2 shrink-0 flex flex-col h-full ${debugWithMultipleModel && 'max-w-[560px]'}`}>
  753. <Config />
  754. </div>
  755. {!isMobile && <div className="relative flex flex-col w-1/2 h-full overflow-y-auto grow " style={{ borderColor: 'rgba(0, 0, 0, 0.02)' }}>
  756. <div className='flex flex-col h-0 border-t border-l grow rounded-tl-2xl bg-gray-50 '>
  757. <Debug
  758. isAPIKeySet={isAPIKeySet}
  759. onSetting={() => setShowAccountSettingModal({ payload: 'provider' })}
  760. inputs={inputs}
  761. modelParameterParams={{
  762. setModel: setModel as any,
  763. onCompletionParamsChange: setCompletionParams,
  764. }}
  765. debugWithMultipleModel={debugWithMultipleModel}
  766. multipleModelConfigs={multipleModelConfigs}
  767. onMultipleModelConfigsChange={handleMultipleModelConfigsChange}
  768. />
  769. </div>
  770. </div>}
  771. </div>
  772. </div>
  773. {restoreConfirmOpen && (
  774. <Confirm
  775. title={t('appDebug.resetConfig.title')}
  776. content={t('appDebug.resetConfig.message')}
  777. isShow={restoreConfirmOpen}
  778. onClose={() => setRestoreConfirmOpen(false)}
  779. onConfirm={resetAppConfig}
  780. onCancel={() => setRestoreConfirmOpen(false)}
  781. />
  782. )}
  783. {showUseGPT4Confirm && (
  784. <Confirm
  785. title={t('appDebug.trailUseGPT4Info.title')}
  786. content={t('appDebug.trailUseGPT4Info.description')}
  787. isShow={showUseGPT4Confirm}
  788. onClose={() => setShowUseGPT4Confirm(false)}
  789. onConfirm={() => {
  790. setShowAccountSettingModal({ payload: 'provider' })
  791. setShowUseGPT4Confirm(false)
  792. }}
  793. onCancel={() => setShowUseGPT4Confirm(false)}
  794. />
  795. )}
  796. {isShowSelectDataSet && (
  797. <SelectDataSet
  798. isShow={isShowSelectDataSet}
  799. onClose={hideSelectDataSet}
  800. selectedIds={selectedIds}
  801. onSelect={handleSelect}
  802. />
  803. )}
  804. {isShowHistoryModal && (
  805. <EditHistoryModal
  806. isShow={isShowHistoryModal}
  807. saveLoading={false}
  808. onClose={hideHistoryModal}
  809. data={completionPromptConfig.conversation_histories_role}
  810. onSave={(data) => {
  811. setConversationHistoriesRole(data)
  812. hideHistoryModal()
  813. }}
  814. />
  815. )}
  816. {isMobile && (
  817. <Drawer showClose isOpen={isShowDebugPanel} onClose={hideDebugPanel} mask footer={null} panelClassname='!bg-gray-50'>
  818. <Debug
  819. isAPIKeySet={isAPIKeySet}
  820. onSetting={() => setShowAccountSettingModal({ payload: 'provider' })}
  821. inputs={inputs}
  822. modelParameterParams={{
  823. setModel: setModel as any,
  824. onCompletionParamsChange: setCompletionParams,
  825. }}
  826. debugWithMultipleModel={debugWithMultipleModel}
  827. multipleModelConfigs={multipleModelConfigs}
  828. onMultipleModelConfigsChange={handleMultipleModelConfigsChange}
  829. />
  830. </Drawer>
  831. )}
  832. </>
  833. </ConfigContext.Provider>
  834. )
  835. }
  836. export default React.memo(Configuration)