<!--  参数配置-->
<template>
  <el-table :data="existingTasklist" style="width: 100%">
    <el-table-column prop="hyperParameter" label="超参数" width="150">
    </el-table-column>
    <el-table-column prop="numericalValue" label="数值">
      <template slot-scope="scope">
        <component v-if="configData" :ref="scope.row.numericalValue" :is="scope.row.componentOptions.component" style="width: 100%" v-bind="scope.row.componentOptions.property" :value="configData[scope.row.numericalValue]||scope.row.defaultValue" :size="defaultFormItemSize" @input=" e => {changeValue(e, scope.row.numericalValue);}"></component>
      </template>
    </el-table-column>
    <el-table-column prop="explain" label="说明">
    </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      configData: undefined,
      parameterConfiguration: [
        {
          hyperParameter: '量化等级',
          defaultValue: 'none',
          numericalValue: 'quantizationLevel',
          explain: '启用 4/8 比特模型量化（QLoRA）。',
          componentOptions: {
            component: 'el-cascader',
            property: {
              options: [
                {
                  value: 'none',
                  label: 'none'
                },
                {
                  value: '8',
                  label: '8'
                },
                {
                  value: '4',
                  label: '4'
                }
              ],
              props: { emitPath: false }
            }
          }
        },
        {
          hyperParameter: '提示模板',
          defaultValue: 'chatglm3',
          numericalValue: 'promptTemplate',
          explain: '构建提示词时使用的模板',
          componentOptions: {
            component: 'el-cascader',
            property: {
              options: [
                {
                  value: 'alpaca',
                  label: 'alpaca'
                },
                {
                  value: 'aquila',
                  label: 'aquila'
                },
                {
                  value: 'baichuan',
                  label: 'baichuan'
                },
                {
                  value: 'baichuan2',
                  label: 'baichuan2'
                },
                {
                  value: 'belle',
                  label: 'belle'
                },
                {
                  value: 'bluelm',
                  label: 'bluelm'
                },
                {
                  value: 'chatglm2',
                  label: 'chatglm2'
                },
                {
                  value: 'chatglm3',
                  label: 'chatglm3'
                },
                {
                  value: 'chatglm3_raw',
                  label: 'chatglm3_raw'
                },
                {
                  value: 'deepseek',
                  label: 'deepseek'
                },
                {
                  value: 'default',
                  label: 'default'
                },
                {
                  value: 'falcon',
                  label: 'falcon'
                },
                {
                  value: 'intern',
                  label: 'intern'
                },
                {
                  value: 'llama2',
                  label: 'llama2'
                },
                {
                  value: 'llama2_zh',
                  label: 'llama2_zh'
                },
                {
                  value: 'mistral',
                  label: 'mistral'
                },
                {
                  value: 'openchat',
                  label: 'openchat'
                },
                {
                  value: 'qwen',
                  label: 'qwen'
                },
                {
                  value: 'starchat',
                  label: 'starchat'
                },
                {
                  value: 'vanilla',
                  label: 'vanilla'
                },
                {
                  value: 'vicuna',
                  label: 'vicuna'
                },
                {
                  value: 'xverse',
                  label: 'xverse'
                },
                {
                  value: 'yayi',
                  label: 'yayi'
                },
                {
                  value: 'zephyr',
                  label: 'zephyr'
                },
                {
                  value: 'ziya',
                  label: 'ziya'
                }
              ],
              props: { emitPath: false }
            }
          }
        },
        {
          hyperParameter: '截断长度', //
          defaultValue: 1024,
          numericalValue: 'truncationLength',
          explain: '输入序列分词后的最大长度。',
          componentOptions: {
            component: 'el-input-number',
            property: {
              min: 4,
              max: 8192
            }
          }
        },
        {
          hyperParameter: '最大样本数', //
          defaultValue: 100000,
          numericalValue: 'maximumSampleSize',
          explain: '每个数据集最多使用的样本数',
          componentOptions: {
            component: 'el-input-number',
            property: {
              max: 100000,
              min: 0
            }
          }
        },
        {
          hyperParameter: '批处理大小', //
          defaultValue: 8,
          numericalValue: 'batchSize',
          explain: '批处理大小（BatchSize）表示在每次训练迭代中使用的样本数。较大的批处理大小可以加速训练，但可能会导致内存问题。',
          componentOptions: {
            component: 'el-input-number',
            property: {
              max: 512,
              min: 1
            }
          }
        },
        {
          hyperParameter: '最大生成长度', //
          defaultValue: 128,
          numericalValue: 'maximumGeneratingLength',
          explain: '最大生成长度',
          componentOptions: {
            component: 'el-input-number',
            property: {
              max: 2048,
              min: 10
            }
          }
        },
        {
          hyperParameter: 'Top-p 采样值', //
          defaultValue: 0.7,
          numericalValue: 'ToppSamplingValue',
          explain: 'Top-p 采样值',
          componentOptions: {
            component: 'el-input-number',
            property: {
              max: 1,
              min: 0.01,
              step: 0.01
            }
          }
        },
        {
          hyperParameter: '温度系数', //
          defaultValue: 0.95,
          numericalValue: 'temperatureCoefficient',
          explain: '温度系数',
          componentOptions: {
            component: 'el-input-number',
            property: {
              max: 1.5,
              min: 0.01,
              step: 0.01
            }
          }
        }
      ]
    }
  },
  props: ['existingTask', 'value'],
  components: {},

  computed: {
    existingTasklist() {
      return this.parameterConfiguration
    }
  },

  mounted() {
    this.$nextTick(() => {
      this.init()
    })
  },

  methods: {
    getProperty() {},
    init() {
      let initConfig = {}
      this.parameterConfiguration.forEach((item) => {
        initConfig[item.numericalValue] = item.defaultValue
      })
      if (this.isJSON(this.value)) {
        this.configData = JSON.parse(this.value) || initConfig
      } else {
        this.configData = initConfig
      }
      this.$emit('input', JSON.stringify(this.configData))
    },

    isJSON(str) {
      try {
        JSON.parse(str)
      } catch (e) {
        // 转换出错，抛出异常
        return false
      }
      return true
    },
    changeValue(value, numericalValue) {
      this.configData[numericalValue] = value
      this.$emit('input', JSON.stringify(this.configData))
    }
  }
}
</script>
<style >
div /deep/ .el-cascader-menu__wrap {
  height: auto;
}
</style>
