File size: 10,705 Bytes
4304c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import type { FC } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import MemoryConfig from '../_base/components/memory-config'
import VarReferencePicker from '../_base/components/variable/var-reference-picker'
import useConfig from './use-config'
import ResolutionPicker from './components/resolution-picker'
import type { LLMNodeType } from './types'
import ConfigPrompt from './components/config-prompt'
import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
import AddButton2 from '@/app/components/base/button/add-button'
import Field from '@/app/components/workflow/nodes/_base/components/field'
import Split from '@/app/components/workflow/nodes/_base/components/split'
import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
import { Resolution } from '@/types/app'
import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
import ResultPanel from '@/app/components/workflow/run/result-panel'
import TooltipPlus from '@/app/components/base/tooltip-plus'
import { HelpCircle } from '@/app/components/base/icons/src/vender/line/general'
import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
import Switch from '@/app/components/base/switch'
const i18nPrefix = 'workflow.nodes.llm'

const Panel: FC<NodePanelProps<LLMNodeType>> = ({

  id,

  data,

}) => {
  const { t } = useTranslation()

  const {
    readOnly,
    inputs,
    isChatModel,
    isChatMode,
    isCompletionModel,
    shouldShowContextTip,
    isShowVisionConfig,
    handleModelChanged,
    hasSetBlockStatus,
    handleCompletionParamsChange,
    handleContextVarChange,
    filterInputVar,
    filterVar,
    availableVars,
    availableNodes,
    isShowVars,
    handlePromptChange,
    handleAddEmptyVariable,
    handleAddVariable,
    handleVarListChange,
    handleVarNameChange,
    handleSyeQueryChange,
    handleMemoryChange,
    handleVisionResolutionEnabledChange,
    handleVisionResolutionChange,
    isShowSingleRun,
    hideSingleRun,
    inputVarValues,
    setInputVarValues,
    visionFiles,
    setVisionFiles,
    contexts,
    setContexts,
    runningStatus,
    handleRun,
    handleStop,
    varInputs,
    runResult,
  } = useConfig(id, data)

  const model = inputs.model

  const singleRunForms = (() => {
    const forms: FormProps[] = []

    if (varInputs.length > 0) {
      forms.push(
        {
          label: t(`${i18nPrefix}.singleRun.variable`)!,
          inputs: varInputs,
          values: inputVarValues,
          onChange: setInputVarValues,
        },
      )
    }

    if (inputs.context?.variable_selector && inputs.context?.variable_selector.length > 0) {
      forms.push(
        {
          label: t(`${i18nPrefix}.context`)!,
          inputs: [{
            label: '',
            variable: '#context#',
            type: InputVarType.contexts,
            required: false,
          }],
          values: { '#context#': contexts },
          onChange: keyValue => setContexts((keyValue as any)['#context#']),
        },
      )
    }

    if (isShowVisionConfig) {
      forms.push(
        {
          label: t(`${i18nPrefix}.vision`)!,
          inputs: [{
            label: t(`${i18nPrefix}.files`)!,
            variable: '#files#',
            type: InputVarType.files,
            required: false,
          }],
          values: { '#files#': visionFiles },
          onChange: keyValue => setVisionFiles((keyValue as any)['#files#']),
        },
      )
    }

    return forms
  })()

  return (
    <div className='mt-2'>

      <div className='px-4 pb-4 space-y-4'>

        <Field

          title={t(`${i18nPrefix}.model`)}

        >

          <ModelParameterModal

            popupClassName='!w-[387px]'

            isInWorkflow

            isAdvancedMode={true}

            mode={model?.mode}

            provider={model?.provider}

            completionParams={model?.completion_params}

            modelId={model?.name}

            setModel={handleModelChanged}

            onCompletionParamsChange={handleCompletionParamsChange}

            hideDebugWithMultipleModel

            debugWithMultipleModel={false}

            readonly={readOnly}

          />

        </Field>



        {/* knowledge */}

        <Field

          title={t(`${i18nPrefix}.context`)}

          tooltip={t(`${i18nPrefix}.contextTooltip`)!}

        >

          <>

            <VarReferencePicker

              readonly={readOnly}

              nodeId={id}

              isShowNodeName

              value={inputs.context?.variable_selector || []}

              onChange={handleContextVarChange}

              filterVar={filterVar}

            />

            {shouldShowContextTip && (

              <div className='leading-[18px] text-xs font-normal text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>

            )}

          </>

        </Field>



        {/* Prompt */}

        {model.name && (

          <ConfigPrompt

            readOnly={readOnly}

            nodeId={id}

            filterVar={filterInputVar}

            isChatModel={isChatModel}

            isChatApp={isChatMode}

            isShowContext

            payload={inputs.prompt_template}

            onChange={handlePromptChange}

            hasSetBlockStatus={hasSetBlockStatus}

            varList={inputs.prompt_config?.jinja2_variables || []}

            handleAddVariable={handleAddVariable}

          />

        )}



        {isShowVars && (

          <Field

            title={t('workflow.nodes.templateTransform.inputVars')}

            operations={

              !readOnly ? <AddButton2 onClick={handleAddEmptyVariable} /> : undefined

            }

          >

            <VarList

              nodeId={id}

              readonly={readOnly}

              list={inputs.prompt_config?.jinja2_variables || []}

              onChange={handleVarListChange}

              onVarNameChange={handleVarNameChange}

              filterVar={filterVar}

            />

          </Field>

        )}



        {/* Memory put place examples. */}

        {isChatMode && isChatModel && !!inputs.memory && (

          <div className='mt-4'>

            <div className='flex justify-between items-center h-8 pl-3 pr-2 rounded-lg bg-gray-100'>

              <div className='flex items-center space-x-1'>

                <div className='text-xs font-semibold text-gray-700 uppercase'>{t('workflow.nodes.common.memories.title')}</div>

                <TooltipPlus

                  popupContent={t('workflow.nodes.common.memories.tip')}

                >

                  <HelpCircle className='w-3.5 h-3.5 text-gray-400' />

                </TooltipPlus>

              </div>

              <div className='flex items-center h-[18px] px-1 rounded-[5px] border border-black/8 text-xs font-semibold text-gray-500 uppercase'>{t('workflow.nodes.common.memories.builtIn')}</div>

            </div>

            {/* Readonly User Query */}

            <div className='mt-4'>

              <Editor

                title={<div className='flex items-center space-x-1'>

                  <div className='text-xs font-semibold text-gray-700 uppercase'>user</div>

                  <TooltipPlus

                    popupContent={

                      <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>

                    }

                  >

                    <HelpCircle className='w-3.5 h-3.5 text-gray-400' />

                  </TooltipPlus>

                </div>}

                value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}

                onChange={handleSyeQueryChange}

                readOnly={readOnly}

                isShowContext={false}

                isChatApp

                isChatModel

                hasSetBlockStatus={hasSetBlockStatus}

                nodesOutputVars={availableVars}

                availableNodes={availableNodes}

              />



              {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (

                <div className='leading-[18px] text-xs font-normal text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>

              )}

            </div>

          </div>

        )}



        {/* Memory */}

        {isChatMode && (

          <>

            <Split />

            <MemoryConfig

              readonly={readOnly}

              config={{ data: inputs.memory }}

              onChange={handleMemoryChange}

              canSetRoleName={isCompletionModel}

            />

          </>
        )}

        {/* Vision: GPT4-vision and so on */}
        {isShowVisionConfig && (
          <>

            <Split />

            <Field

              title={t(`${i18nPrefix}.vision`)}

              tooltip={t('appDebug.vision.description')!}

              operations={

                <Switch size='md' defaultValue={inputs.vision.enabled} onChange={handleVisionResolutionEnabledChange} />

              }

            >

              {inputs.vision.enabled

                ? (

                  <ResolutionPicker

                    value={inputs.vision.configs?.detail || Resolution.high}

                    onChange={handleVisionResolutionChange}

                  />

                )

                : null}



            </Field>

          </>
        )}
      </div>
      <Split />
      <div className='px-4 pt-4 pb-2'>

        <OutputVars>

          <>

            <VarItem

              name='text'

              type='string'

              description={t(`${i18nPrefix}.outputVars.output`)}

            />

          </>

        </OutputVars>
      </div>
      {isShowSingleRun && (
        <BeforeRunForm

          nodeName={inputs.title}

          onHide={hideSingleRun}

          forms={singleRunForms}

          runningStatus={runningStatus}

          onRun={handleRun}

          onStop={handleStop}

          result={<ResultPanel {...runResult} showSteps={false} />}

        />
      )}
    </div>
  )
}

export default React.memo(Panel)