返回为字节json,排序

This commit is contained in:
renzhiyuan 2025-07-24 16:31:20 +08:00
parent 26a0ee7b2e
commit 0786844aa4
2 changed files with 27 additions and 6 deletions

31
ai.go
View File

@ -8,13 +8,13 @@ import (
"strings"
)
func ExcelMatch(ctx context.Context, matchToExcelHeader, selfExcelHeader []string, key string, model string) (result map[string]interface{}, err error) {
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(selfExcelHeader, ",") + "]",
"-第二个表头:[" + strings.Join(matchToExcelHeader, ",") + "]",
"-第一个表头:[" + strings.Join(matchToExcelHeader, ",") + "]",
"-第二个表头:[" + strings.Join(selfExcelHeader, ",") + "]",
"-第一张表表头为key,第二张表表头为value,返回一个map映射的json字符串",
"-不需要其他额外的表述以及格式",
}
@ -23,5 +23,26 @@ func ExcelMatch(ctx context.Context, matchToExcelHeader, selfExcelHeader []strin
}, text...)
err = json.Unmarshal([]byte(res), &result)
return
if err != nil {
return nil, err
}
return OrderMapWithSlice(result, matchToExcelHeader)
}
func OrderMapWithSlice(data map[string]interface{}, desiredOrder []string) ([]byte, error) {
var orderedSlice []interface{}
for _, key := range desiredOrder {
if value, exists := data[key]; exists {
// 将键值对存入切片(可选:转为 {Key:..., Value:...} 结构)
orderedSlice = append(orderedSlice, map[string]interface{}{
"key": key,
"value": value,
})
}
}
// 编码为 JSON
return json.MarshalIndent(orderedSlice, "", " ")
}

View File

@ -6,7 +6,7 @@ import (
)
func TestAddress(t *testing.T) {
res, err := ExcelMatch(context.Background(), a, b, "", "")
res, err := ExcelMatch(context.Background(), a, b, "236ba4b6-9daa-4755-b22f-2fd274cd223a", "doubao-1-5-lite-32k-250115")
t.Log(res, err)
}