diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f8919eb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM centos:7 +USER root +ENV TZ=Asia/Shanghai +COPY aiweek /usr/local/bin/ \ + ./option/option.yaml /conf/ +RUN chmod +x /usr/local/bin/aiweek && mkdir /data /conf +ENTRYPOINT ["aiweek"] +CMD ["--livemode=false"] \ No newline at end of file diff --git a/aichat/chat.go b/aichat/chat.go index 410776c..1c0a9a7 100644 --- a/aichat/chat.go +++ b/aichat/chat.go @@ -1,6 +1,7 @@ package aichat import ( + "aiweek/option" "bytes" "fmt" "github.com/tidwall/gjson" @@ -15,23 +16,18 @@ type KimiResp string type AiReq struct { token string body string - url string } -const token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ1c2VyLWNlbnRlciIsImV4cCI6MTcyMzYyMTk1NywiaWF0IjoxNzE1ODQ1OTU3LCJqdGkiOiJjcDJybWg5a3FxNGxraGNqbTVtZyIsInR5cCI6InJlZnJlc2giLCJzdWIiOiJjbzU4anJxbG5sOTZlanF1czVnMCIsInNwYWNlX2lkIjoiY281OGpycWxubDk2ZWpxdXM1ZmciLCJhYnN0cmFjdF91c2VyX2lkIjoiY281OGpycWxubDk2ZWpxdXM1ZjAifQ.WNq2OH5egQKlnnuM4ygY2MjjmgsjhEOwHJdV7oQA66_mrHgGKluilcBuMZ5dMpClpAOnVY6wJ021dYHajzuInQ" - func NewAiReq(url string) *AiReq { body := fmt.Sprintf("{\"model\":\"kimi\",\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":\"file\",\"file_url\":{\"url\":\"%s\"}},{\"type\":\"text\",\"text\":\"帮忙分析下工单中的错别字,告诉我对应的工单id\"}]}],\"use_search\":false}", url) return &AiReq{ - token: token, + token: option.MODEL_TOKEN, body: body, - url: url, } } -func (a *AiReq) StartChat(kimiUrl string) KimiResp { +func (a *AiReq) StartChat(url string) KimiResp { // 定义 POST 请求的 URL - url := kimiUrl // 创建一个带有 JSON 负载的 POST 请求 payload := []byte(a.body) // 根据需要修改 JSON 负载 req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload)) @@ -53,6 +49,7 @@ func (a *AiReq) StartChat(kimiUrl string) KimiResp { defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) + log.Println(KimiResp(body)) return KimiResp(body) } @@ -60,11 +57,5 @@ func (k KimiResp) PrepareWechatBody() string { wechatBody := gjson.Get(string(k), "choices.#.message.content").String() r := strings.NewReplacer("\\n", "\n", "\\", "", "[", "", "]", "", "\"", "") wechatBody = r.Replace(wechatBody) - fmt.Println("body", wechatBody) - //wechatBody = strings.Replace(wechatBody, "\\n", "\n", -1) - //wechatBody = strings.Replace(wechatBody, "\\", "", -1) - //wechatBody = strings.Replace(wechatBody, "[", "", -1) - //wechatBody = strings.Replace(wechatBody, "]", "", -1) - //wechatBody = strings.Replace(wechatBody, "\"", "", 1) return wechatBody } diff --git a/buildandrun.sh b/buildandrun.sh new file mode 100644 index 0000000..a68c33f --- /dev/null +++ b/buildandrun.sh @@ -0,0 +1,39 @@ +#!/usr/bin/bash +# 创建gopath目录 +mkdir /root/go +cd /root/go + +# 获取go安装包(如果下载不了,可以直接去浏览器下载,然后上传到linux) +wget https://go.dev/dl/go1.22.1.linux-amd64.tar.gz + +# 解压并加入/usr/local目录 +tar -C /usr/local -zxf go1.22.1.linux-amd64.tar.gz + +# 导出go环境变量 +PROFILE_CONTENT=" +export GOROOT=/usr/local/go +export GOPATH=/root/go +export GOPROXY=https://goproxy.cn,direct +export PATH=\$PATH:$GOROOT/bin:$GOPATH/bin +" + +# 检查 /etc/profile 文件是否存在 +if [ -f /etc/profile ]; then + # 使用 echo 命令和 >> 操作符追加内容到 /etc/profile 文件末尾 + echo "$PROFILE_CONTENT" >> /etc/profile + echo "已成功添加配置到 /etc/profile。" +else + echo "/etc/profile 文件不存在。" +fi + +# 刷新环境变量 +source /etc/profile + +# 查看go版本,检查是否安装成功 +go version + +cd cmd && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ../aiweek && cd .. +timestamp=$(date +%s) +docker build -t aiweek:$timestamp . + +docker run -d --name aiweek -v /data:/data aiweek:$timestamp \ No newline at end of file diff --git a/main.go b/cmd/main.go similarity index 70% rename from main.go rename to cmd/main.go index 26b6859..8312478 100644 --- a/main.go +++ b/cmd/main.go @@ -3,6 +3,7 @@ package main import ( "aiweek/aichat" "aiweek/notifer" + "aiweek/option" "aiweek/tools" "aiweek/udesk/reply" "flag" @@ -13,14 +14,11 @@ import ( "time" ) -const ToNginxFilepath = "/data/" -const kimiUrl = "http://127.0.0.1:8000/v1/chat/completions" - var liveMode bool var operateTime = 1 func main() { - log.Println(` + fmt.Println(` _____ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____ \ __\\_ __ \/ _ \ / \ \__ \ / _ \ \ \/ \/ /\__ \ / \ / ___\ @@ -31,47 +29,47 @@ _/ ____\______ ____ _____ _____ ____ __ _ _______ ____ ____ flag.Parse() if !liveMode { log.Println("目前是定时任务模式") - log.Printf("等待任务的第%v次执行...", operateTime) + log.Printf("等待任务的第%v次执行...\n", operateTime) // 定义任务,每周五的五点执行 - gocron.Every(1).Friday().At("16:00").Do(retryJob) + gocron.Every(1).Saturday().At("16:00").Do(retryJob) // 开始定时任务 <-gocron.Start() } else { log.Println("目前是即时任务模式") - job() + err := job() + if err != nil { + log.Printf("执行任务失败,错误是:%v\n", err) + return + } } } func job() error { - e := reply.NewExcelObj() - replyObj, _ := e.SetReplyContent() - e.CreateNewExcel(replyObj) - e.SetAddress() - //fmt.Println("struct:", e.ExcelName, e.ExcelPath, e.ExcelAddress) - err := reply.CopyFile(e.ExcelPath, ToNginxFilepath+reply.ExcelName) + excel := reply.NewExcelObj() + excel.SetReplyContent().CreateNewExcel() + err := reply.CopyFile(excel.ExcelPath, option.VOLUMEPATH+excel.ExcelName) if err != nil { return err } - err = os.Remove(e.ExcelPath) + err = os.Remove(excel.ExcelPath) if err != nil { return err } //校验文件数量,超过4个删除最老的文件 - fileCount, err := tools.CountFilesInDir(ToNginxFilepath) + fileCount, err := tools.CountFilesInDir(option.VOLUMEPATH) if err != nil { return err } if fileCount > 4 { - cleanFile := tools.ReadFilename(ToNginxFilepath) - err := os.Remove(ToNginxFilepath + cleanFile) + cleanFile := tools.ReadFilename(option.VOLUMEPATH) + err := os.Remove(option.VOLUMEPATH + cleanFile) if err != nil { return err } } - result := aichat.NewAiReq(e.ExcelAddress).StartChat(kimiUrl).PrepareWechatBody() + result := aichat.NewAiReq(excel.ExcelAddress).StartChat(option.MODEL_API).PrepareWechatBody() notifer.SendWechat(result) - operateTime++ return nil } @@ -84,7 +82,9 @@ func retryJob() { for attempt := 0; attempt < maxRetries; attempt++ { err := job() if err == nil { + operateTime++ log.Println("执行成功") + log.Printf("等待第%v次任务执行\n", operateTime) return } fmt.Printf("尝试 %d 次失败,错误是: %s\n", attempt+1, err) diff --git a/createfileserver.sh b/createfileserver.sh new file mode 100644 index 0000000..4f5047e --- /dev/null +++ b/createfileserver.sh @@ -0,0 +1,43 @@ +#!/usr/bin/bash +CONFIG_FILE="/etc/nginx/nginx.conf" +CONFIG_BLOCK=$(cat <