l_ai_address/ai.go

53 lines
1.5 KiB
Go

package l_ai_address
import (
"context"
"fmt"
"gitea.cdlsxd.cn/self-tools/l_ai_address/ai/doubao"
"strings"
)
type Address struct {
P string
C string
D string
}
func GetAddress(ctx context.Context, address, key, model string) (Address, error) {
modelObj := doubao.NewDouBao(model, key)
text := []string{
"解析省市区地址[QUESTION]" + address + "[/QUESTION]",
"-根据国家统计局的地址信息 帮我匹配出对应的省市区信息,如果是直辖县级行政区,直接按照区来处理,然后市一级显示上级代管市",
"-只需要给出省市区具体名称,不需要解释",
"-省市区之间用\",\"隔开",
"-区给具体的名称,不要返回市辖区",
}
res, err := modelObj.GetData(ctx, doubao.Text, func(input string) (string, error) {
var resStr string
inputSlice := strings.Split(input, ",")
if len(inputSlice) < 3 {
switch len(inputSlice) {
case 2:
if strings.HasSuffix(inputSlice[0], "市") {
runes := []rune(inputSlice[0])
resStr = fmt.Sprintf("%s,%s", string(runes[:len(runes)-1]), input)
} else {
resStr = fmt.Sprintf("%s,%s,%s", inputSlice[0], inputSlice[0]+"市", inputSlice[1])
}
default:
return input, fmt.Errorf("地址解析失败:%s", input)
}
} else {
resStr = input
}
return resStr, nil
}, text...)
if err != nil {
return Address{}, err
}
resSlice := strings.Split(res, ",")
return Address{P: resSlice[0], C: resSlice[1], D: resSlice[2]}, nil
}