This commit is contained in:
renzhiyuan 2026-09-08 11:48:26 +08:00
commit 1537032b71
5 changed files with 115 additions and 0 deletions

50
bert.go Normal file
View File

@ -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
}

19
entity.go Normal file
View File

@ -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"`
}

31
func.go Normal file
View File

@ -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
}

5
go.mod Normal file
View File

@ -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

10
option.go Normal file
View File

@ -0,0 +1,10 @@
package l_bert
type Option func(*Bert)
// WithHost 修改请求地址
func WithHost(host string) Option {
return func(b *Bert) {
b.host = host
}
}