Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

model-config.ts 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import type { UserInputFormItem } from '@/types/app'
  2. import type { PromptVariable } from '@/models/debug'
  3. export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null, dataset_query_variable?: string) => {
  4. if (!useInputs)
  5. return []
  6. const promptVariables: PromptVariable[] = []
  7. useInputs.forEach((item: any) => {
  8. const isParagraph = !!item.paragraph
  9. const [type, content] = (() => {
  10. if (isParagraph)
  11. return ['paragraph', item.paragraph]
  12. if (item['text-input'])
  13. return ['string', item['text-input']]
  14. if (item.number)
  15. return ['number', item.number]
  16. if (item.file)
  17. return ['file', item.file]
  18. if (item['file-list'])
  19. return ['file-list', item['file-list']]
  20. if (item.external_data_tool)
  21. return [item.external_data_tool.type, item.external_data_tool]
  22. return ['select', item.select || {}]
  23. })()
  24. const is_context_var = dataset_query_variable === content?.variable
  25. if (type === 'string' || type === 'paragraph') {
  26. promptVariables.push({
  27. key: content.variable,
  28. name: content.label,
  29. required: content.required,
  30. type,
  31. max_length: content.max_length,
  32. options: [],
  33. is_context_var,
  34. hide: content.hide,
  35. })
  36. }
  37. else if (type === 'number') {
  38. promptVariables.push({
  39. key: content.variable,
  40. name: content.label,
  41. required: content.required,
  42. type,
  43. options: [],
  44. hide: content.hide,
  45. })
  46. }
  47. else if (type === 'select') {
  48. promptVariables.push({
  49. key: content.variable,
  50. name: content.label,
  51. required: content.required,
  52. type: 'select',
  53. options: content.options,
  54. is_context_var,
  55. hide: content.hide,
  56. })
  57. }
  58. else if (type === 'file') {
  59. promptVariables.push({
  60. key: content.variable,
  61. name: content.label,
  62. required: content.required,
  63. type,
  64. config: {
  65. allowed_file_types: content.allowed_file_types,
  66. allowed_file_extensions: content.allowed_file_extensions,
  67. allowed_file_upload_methods: content.allowed_file_upload_methods,
  68. number_limits: 1,
  69. },
  70. hide: content.hide,
  71. })
  72. }
  73. else if (type === 'file-list') {
  74. promptVariables.push({
  75. key: content.variable,
  76. name: content.label,
  77. required: content.required,
  78. type,
  79. config: {
  80. allowed_file_types: content.allowed_file_types,
  81. allowed_file_extensions: content.allowed_file_extensions,
  82. allowed_file_upload_methods: content.allowed_file_upload_methods,
  83. number_limits: content.max_length,
  84. },
  85. hide: content.hide,
  86. })
  87. }
  88. else {
  89. promptVariables.push({
  90. key: content.variable,
  91. name: content.label,
  92. required: content.required,
  93. type: content.type,
  94. enabled: content.enabled,
  95. config: content.config,
  96. icon: content.icon,
  97. icon_background: content.icon_background,
  98. is_context_var,
  99. hide: content.hide,
  100. })
  101. }
  102. })
  103. return promptVariables
  104. }
  105. export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => {
  106. const userInputs: UserInputFormItem[] = []
  107. promptVariables.filter(({ key, name }) => {
  108. return key && key.trim() && name && name.trim()
  109. }).forEach((item: any) => {
  110. if (item.type === 'string' || item.type === 'paragraph') {
  111. userInputs.push({
  112. [item.type === 'string' ? 'text-input' : 'paragraph']: {
  113. label: item.name,
  114. variable: item.key,
  115. required: item.required !== false, // default true
  116. max_length: item.max_length,
  117. default: '',
  118. hide: item.hide,
  119. },
  120. } as any)
  121. return
  122. }
  123. if (item.type === 'number') {
  124. userInputs.push({
  125. number: {
  126. label: item.name,
  127. variable: item.key,
  128. required: item.required !== false, // default true
  129. default: '',
  130. hide: item.hide,
  131. },
  132. } as any)
  133. }
  134. else if (item.type === 'select') {
  135. userInputs.push({
  136. select: {
  137. label: item.name,
  138. variable: item.key,
  139. required: item.required !== false, // default true
  140. options: item.options,
  141. default: '',
  142. hide: item.hide,
  143. },
  144. } as any)
  145. }
  146. else {
  147. userInputs.push({
  148. external_data_tool: {
  149. label: item.name,
  150. variable: item.key,
  151. enabled: item.enabled,
  152. type: item.type,
  153. config: item.config,
  154. required: item.required,
  155. icon: item.icon,
  156. icon_background: item.icon_background,
  157. hide: item.hide,
  158. },
  159. } as any)
  160. }
  161. })
  162. return userInputs
  163. }