This commit is contained in:
commit
1537032b71
|
|
@ -0,0 +1,50 @@
|
|||
package l_bert
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"fmt"
|
||||
"gitea.cdlsxd.cn/self-tools/l_request"
|
||||
)
|
||||
|
||||
type Bert struct {
|
||||
host string
|
||||
auth string
|
||||
}
|
||||
|
||||
func NewClient(args ...Option) *Bert {
|
||||
s := &Bert{
|
||||
host: "https://117.175.169.61:5003",
|
||||
}
|
||||
for _, opt := range args {
|
||||
opt(s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Bert) BatchCreate(in *Predict) (*PredictRes, error) {
|
||||
path := "/predict"
|
||||
|
||||
requestJson, err := StructToMap(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req := l_request.Request{
|
||||
Method: "POST",
|
||||
Url: s.host + path,
|
||||
Json: requestJson,
|
||||
}
|
||||
resp, err := req.Send()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求失败,err: %v", err)
|
||||
}
|
||||
var resData PredictRes
|
||||
if err = json.Unmarshal(resp.Content, &resData); err != nil {
|
||||
return nil, fmt.Errorf("解析响应失败,err: %v", err)
|
||||
}
|
||||
if resData.Status != "success" {
|
||||
return nil, err
|
||||
}
|
||||
return &resData, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package l_bert
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type PredictRes struct {
|
||||
Metadata struct {
|
||||
Device string `json:"device"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
} `json:"metadata"`
|
||||
Model string `json:"model"`
|
||||
Prediction json.RawMessage `json:"prediction"`
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type Predict struct {
|
||||
Model string `json:"model"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package l_bert
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/rand/v2"
|
||||
)
|
||||
|
||||
// 纯小写字母
|
||||
func GenerateRandomLowerString(n int) string {
|
||||
return GenerateRandomStringCustom(n, "abcdefghijklmnopqrstuvwxyz")
|
||||
}
|
||||
|
||||
// GenerateRandomStringCustom 使用自定义字符集
|
||||
func GenerateRandomStringCustom(n int, charset string) string {
|
||||
result := make([]byte, n)
|
||||
for i := range result {
|
||||
result[i] = charset[rand.IntN(len(charset))]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
|
||||
// StructToMap 将结构体转换为 map[string]any
|
||||
func StructToMap(v any) (map[string]any, error) {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var m map[string]any
|
||||
err = json.Unmarshal(b, &m)
|
||||
return m, err
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
module gitea.cdlsxd.cn/self-tools/l_bert
|
||||
|
||||
go 1.26.2
|
||||
|
||||
require gitea.cdlsxd.cn/self-tools/l_request v1.0.8 // indirect
|
||||
Loading…
Reference in New Issue