/
glmEfficientTune.go
56 lines (45 loc) · 1.16 KB
/
glmEfficientTune.go
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
package dataformat
import (
"context"
"github.com/xuri/excelize/v2"
"mime/multipart"
"strings"
)
type GLMEfficientTune struct {
}
func NewGLMEfficientTune() *GLMEfficientTune {
return &GLMEfficientTune{}
}
type SelfCognition struct {
Instruction string `json:"instruction"`
Input string `json:"input"`
Output string `json:"output"`
}
func (tune *GLMEfficientTune) ConvertExcelToSelfCognitionData(ctx context.Context, excelFileData *multipart.File) ([]*SelfCognition, error) {
data := []*SelfCognition{}
// 不支持csv格式
file, err := excelize.OpenReader(*excelFileData)
if err != nil {
return nil, err
}
defer (*excelFileData).Close()
// 假设数据在第一个工作表中
sheetName := file.GetSheetName(0)
rows, err := file.GetRows(sheetName)
if err != nil {
return nil, err
}
// 遍历每一行并提取问题和答案
for _, row := range rows {
if len(row) >= 2 {
question := strings.Replace(row[0], "\n", "", -1)
answer := strings.Replace(row[1], "\n", "", -1)
selfCognition := &SelfCognition{
Instruction: question,
Output: answer,
}
data = append(data, selfCognition)
}
}
return data, nil
}