diff --git a/ai.go b/ai.go index 57a9d76..2e46f0e 100644 --- a/ai.go +++ b/ai.go @@ -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, "", " ") + } diff --git a/ai_test.go b/ai_test.go index 058c22a..c6e2606 100644 --- a/ai_test.go +++ b/ai_test.go @@ -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) }