some fixes and binary build optimization
This commit is contained in:
@@ -7,7 +7,7 @@ COPY ./laniakea ./laniakea
|
||||
COPY ./plugins ./plugins
|
||||
COPY ./utils ./utils
|
||||
COPY ./main.go ./
|
||||
RUN --mount=type=cache,target=/go/pkg/mod go build -trimpath -o /usr/local/bin/kurumi ./
|
||||
RUN --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X utils.BuildTime=$(date -u +%Y-%m-%d_%H:%M:%S)" -v -o /usr/local/bin/kurumi ./
|
||||
|
||||
FROM alpine:3.23 AS runner
|
||||
WORKDIR /app
|
||||
|
||||
1
Makefile
1
Makefile
@@ -1,3 +1,4 @@
|
||||
build:
|
||||
go mod tidy
|
||||
docker build -t git.nix13.pw/scuroneko/kurumibotgo:latest -t git.nix13.pw/scuroneko/kurumibotgo:0.2.0 -f ./Dockerfile .
|
||||
docker push git.nix13.pw/scuroneko/kurumibotgo --all-tags
|
||||
10
go.sum
10
go.sum
@@ -20,10 +20,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
|
||||
@@ -34,8 +30,8 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/muir/sqltoken v0.1.0 h1:edosEGsOClOZNfgGQNQSgxR9O6LiVefm2rDRqp2InuI=
|
||||
github.com/muir/sqltoken v0.1.0/go.mod h1:lgOIORnKekMsuc/ZwdPOfwz/PtWLPCke43cEbT3uDuY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -93,5 +89,3 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/gorm v1.31.1 h1:7CA8FTFz/gRfgqgpeKIBcervUn3xSyPUmr6B2WXJ7kg=
|
||||
gorm.io/gorm v1.31.1/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
|
||||
|
||||
4
main.go
4
main.go
@@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"kurumibot/database"
|
||||
"kurumibot/laniakea"
|
||||
"kurumibot/plugins"
|
||||
"kurumibot/utils"
|
||||
"log"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
@@ -40,6 +42,8 @@ func main() {
|
||||
plugins.RegisterAdmin(bot)
|
||||
plugins.RegisterRP(bot)
|
||||
|
||||
fmt.Printf("Build time: %s", utils.BuildTime)
|
||||
|
||||
defer bot.Close()
|
||||
bot.Run()
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"kurumibot/utils"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -36,8 +37,9 @@ func RegisterEconomy(bot *laniakea.Bot) {
|
||||
|
||||
func about(ctx *laniakea.MsgContext, _ *laniakea.DatabaseContext) {
|
||||
out := []string{
|
||||
fmt.Sprintf("Go: %s", "1.25.5"),
|
||||
fmt.Sprintf("Версия laniakea: %s", laniakea.VersionString),
|
||||
fmt.Sprintf("Go: %s", runtime.Version()),
|
||||
fmt.Sprintf("Время сборки: %s", utils.BuildTime),
|
||||
fmt.Sprintf("Версия Laniakea: %s", laniakea.VersionString),
|
||||
}
|
||||
ctx.Answer(strings.Join(out, "\n"))
|
||||
}
|
||||
|
||||
@@ -71,11 +71,13 @@ func NewOpenAIAPI(baseURL, token, model string) *OpenAIAPI {
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
}
|
||||
t := &http.Transport{}
|
||||
if proxy.Host != "" {
|
||||
t.Proxy = http.ProxyURL(proxy)
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyURL(proxy),
|
||||
},
|
||||
Timeout: 5 * time.Minute,
|
||||
Transport: t,
|
||||
}
|
||||
return &OpenAIAPI{
|
||||
Token: token,
|
||||
@@ -97,36 +99,53 @@ type CreateCompletionReq struct {
|
||||
MaxCompletionTokens int `json:"max_completition_tokens,omitempty"`
|
||||
}
|
||||
|
||||
func (o *OpenAIAPI) CreateCompletion(request CreateCompletionReq) (*OpenAIResponse, error) {
|
||||
u := fmt.Sprintf("%s/v1/chat/completions", o.BaseURL)
|
||||
request.Model = o.Model
|
||||
data, err := json.Marshal(request)
|
||||
o.Logger.Debug("REQ", u, string(data))
|
||||
func (o *OpenAIAPI) DoRequest(url string, params any) ([]byte, error) {
|
||||
responseBody := make([]byte, 0)
|
||||
data, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return responseBody, err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(data)
|
||||
req, err := http.NewRequest("POST", u, buf)
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return responseBody, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if o.Token != "" {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.Token))
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
res, err := o.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode == 504 || res.StatusCode == 400 {
|
||||
time.Sleep(5 * time.Second)
|
||||
return o.DoRequest(url, params)
|
||||
}
|
||||
responseBody, err = io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return responseBody, err
|
||||
}
|
||||
return responseBody, err
|
||||
}
|
||||
|
||||
func (o *OpenAIAPI) CreateCompletion(request CreateCompletionReq) (*OpenAIResponse, error) {
|
||||
u := fmt.Sprintf("%s/v1/chat/completions", o.BaseURL)
|
||||
request.Model = o.Model
|
||||
data, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
o.Logger.Debug("REQ", u, string(data))
|
||||
body, err := o.DoRequest(u, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o.Logger.Debug("RES", u, string(body))
|
||||
|
||||
response := new(OpenAIResponse)
|
||||
err = json.Unmarshal(body, response)
|
||||
return response, err
|
||||
@@ -144,39 +163,17 @@ func (o *OpenAIAPI) CompressChat(history []Message) (*OpenAIResponse, error) {
|
||||
}
|
||||
u := fmt.Sprintf("%s/v1/chat/completions", o.BaseURL)
|
||||
data, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
o.Logger.Debug("COMPRESS REQ", u, string(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(data)
|
||||
req, err := http.NewRequest("POST", u, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if o.Token != "" {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.Token))
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode == 504 || resp.StatusCode == 400 {
|
||||
time.Sleep(5 * time.Second)
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
body, err := o.DoRequest(u, request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o.Logger.Debug("COMPRESS RES", u, string(body))
|
||||
|
||||
response := new(OpenAIResponse)
|
||||
err = json.Unmarshal(body, response)
|
||||
return response, err
|
||||
|
||||
3
utils/constants.go
Normal file
3
utils/constants.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package utils
|
||||
|
||||
var BuildTime = ""
|
||||
Reference in New Issue
Block a user