l_ai_excel_header_match/ai.go

46 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package l_ai_excel_header_match
import (
"context"
"encoding/json"
"gitea.cdlsxd.cn/self-tools/l_ai_excel_header_match/doubao"
"github.com/iancoleman/orderedmap"
"strings"
)
func ExcelMatch(ctx context.Context, selfExcelHeader, matchToExcelHeader []string, key string, model string) (resultByte []byte, err error) {
var result map[string]interface{}
modelObj := doubao.NewDouBao(model, key)
text := []string{
"第一张表表头为key第二张表表头为value如果未匹配到则返回空",
"-第一个表头:[" + strings.Join(matchToExcelHeader, ",") + "]",
"-第二个表头:[" + strings.Join(selfExcelHeader, ",") + "]",
"-第一张表表头为key,第二张表表头为value,返回一个map映射的json字符串不需要其他额外的表述以及格式",
"-输出的json长度必须和第一张表的长度一致",
}
res, err := modelObj.GetData(ctx, doubao.Text, func(input string) (string, error) {
return input, nil
}, text...)
err = json.Unmarshal([]byte(res), &result)
if err != nil {
return nil, err
}
return OrderMapWithSlice(result, matchToExcelHeader)
}
func OrderMapWithSlice(data map[string]interface{}, desiredOrder []string) ([]byte, error) {
m := orderedmap.New()
for _, key := range desiredOrder {
if value, exists := data[key]; exists {
m.Set(key, value)
}
}
// 编码为 JSON
return json.Marshal(m)
}