瀏覽代碼

feat: 支持gpt-4并解决退出不掉的问题 (#144)

Co-authored-by: eryajf <eryajf@users.noreply.github.com>
二丫讲梵 2 年之前
父節點
當前提交
ad758822f4
共有 5 個文件被更改,包括 57 次插入9 次删除
  1. 13 2
      README.md
  2. 1 1
      go.mod
  3. 2 2
      go.sum
  4. 37 3
      main.go
  5. 4 1
      public/public.go

+ 13 - 2
README.md

@@ -37,6 +37,7 @@
   - [日常问题](#%E6%97%A5%E5%B8%B8%E9%97%AE%E9%A2%98)
   - [通过内置prompt聊天](#%E9%80%9A%E8%BF%87%E5%86%85%E7%BD%AEprompt%E8%81%8A%E5%A4%A9)
   - [生成图片](#%E7%94%9F%E6%88%90%E5%9B%BE%E7%89%87)
+  - [支持 gpt-4](#%E6%94%AF%E6%8C%81-gpt-4)
 - [本地开发](#%E6%9C%AC%E5%9C%B0%E5%BC%80%E5%8F%91)
 - [配置文件说明](#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%B4%E6%98%8E)
 - [常见问题](#%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98)
@@ -49,7 +50,7 @@
 
 ## 前言
 
-本项目可以助你将GPT机器人集成到钉钉群聊当中。当前默认模型为 gpt-3.5。
+本项目可以助你将GPT机器人集成到钉钉群聊当中。当前默认模型为 gpt-3.5,支持gpt-4
 
 
 > 🥳 **欢迎关注我的其他开源项目:**
@@ -300,6 +301,16 @@ $ tail -f run.log
 
 ![image_20230323_150547](https://cdn.staticaly.com/gh/eryajf/tu/main/img/image_20230323_150547.jpg)
 
+### 支持 gpt-4
+
+如果你的账号通过了官方的白名单,那么可以将模型配置为:`gpt-4-0314`或`gpt-4`,目前gpt-4的余额查询以及图片生成功能暂不可用,可能是接口限制,也可能是其他原因,等我有条件的时候,会对这些功能进行测试验证。
+
+> 以下是gpt-3.5与gpt-4对数学计算方面的区别。
+
+![image_20230330_180308](https://cdn.staticaly.com/gh/eryajf/tu/main/img/image_20230330_180308.jpg)
+
+感谢[@PIRANHACHAN](https://github.com/PIRANHACHAN)同学提供的gpt-4的key,使得项目在gpt-4的对接上能够进行验证测试,达到了可用状态。
+
 ##  本地开发
 
 ```sh
@@ -322,7 +333,7 @@ $ go run main.go
 {
     "api_key": "xxxxxxxxx",   // openai api_key
     "base_url": "api.openai.com", //  如果你想指定请求url的地址,可通过这个参数进行配置,默认为官方地址,不需要再添加 /v1
-    "model": "gpt-3.5-turbo", // 指定模型,默认为 gpt-3.5-turbo , 可选参数有: "gpt-4-32k-0314", "gpt-4-32k", "gpt-4-0314", "gpt-4", "gpt-3.5-turbo-0301", "gpt-3.5-turbo", "text-davinci-003", "text-davinci-002", "text-curie-001", "text-babbage-001", "text-ada-001", "text-davinci-001", "davinci-instruct-beta", "davinci", "curie-instruct-beta", "curie", "ada", "babbage"
+    "model": "gpt-3.5-turbo", // 指定模型,默认为 gpt-3.5-turbo , 可选参数有: "gpt-4-0314", "gpt-4", "gpt-3.5-turbo-0301", "gpt-3.5-turbo"
     "session_timeout": 600,   // 会话超时时间,默认600秒,在会话时间内所有发送给机器人的信息会作为上下文
     "http_proxy": "",         // 指定请求时使用的代理,如果为空,则不使用代理
     "default_mode": "单聊",    // 默认对话模式,可根据实际场景自定义,如果不设置,默认为单聊

+ 1 - 1
go.mod

@@ -22,7 +22,7 @@ require (
 	github.com/muesli/reflow v0.3.0 // indirect
 	github.com/muesli/termenv v0.15.1 // indirect
 	github.com/rivo/uniseg v0.2.0 // indirect
-	github.com/sashabaranov/go-openai v1.5.3 // indirect
+	github.com/sashabaranov/go-openai v1.5.7 // indirect
 	golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect
 	golang.org/x/sys v0.6.0 // indirect
 )

+ 2 - 2
go.sum

@@ -28,8 +28,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
 github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
 github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
-github.com/sashabaranov/go-openai v1.5.3 h1:o6n6dj0h9u+5mE1m+D8eT0zYhh7229o8ymDd2zDwAXU=
-github.com/sashabaranov/go-openai v1.5.3/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
+github.com/sashabaranov/go-openai v1.5.7 h1:8DGgRG+P7yWixte5j720y6yiXgY3Hlgcd0gcpHdltfo=
+github.com/sashabaranov/go-openai v1.5.7/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
 github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
 github.com/xgfone/ship/v5 v5.3.1 h1:e5qhMT6DSQOE6A/xDBL/Ftf28BGptNw6Etq+w+pme6E=
 github.com/xgfone/ship/v5 v5.3.1/go.mod h1:mGI+65lLL3kaOseMkWUYgy+OFl27WV2LY1NSsecu/9g=

+ 37 - 3
main.go

@@ -1,9 +1,14 @@
 package main
 
 import (
+	"context"
 	"fmt"
+	"net/http"
+	"os"
+	"os/signal"
 	"path/filepath"
 	"strings"
+	"time"
 
 	"github.com/eryajf/chatgpt-dingtalk/pkg/dingbot"
 	"github.com/eryajf/chatgpt-dingtalk/pkg/logger"
@@ -71,10 +76,39 @@ func Start() {
 		return c.File(filepath.Join(root, filename))
 	})
 
-	// 服务端口
 	port := ":" + public.Config.Port
-	// 启动服务器
-	ship.StartServer(port, app)
+	srv := &http.Server{
+		Addr:    port,
+		Handler: app,
+	}
+
+	// Initializing the server in a goroutine so that
+	// it won't block the graceful shutdown handling below
+	go func() {
+		logger.Info("🚀 The HTTP Server is running on", port)
+		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+			logger.Fatal("listen: %s\n", err)
+		}
+	}()
+
+	// Wait for interrupt signal to gracefully shutdown the server with
+	// a timeout of 5 seconds.
+	quit := make(chan os.Signal, 1)
+	// kill (no param) default send syscall.SIGTERM
+	// kill -2 is syscall.SIGINT
+	// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
+	// signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
+	signal.Notify(quit, os.Interrupt)
+	<-quit
+	logger.Info("Shutting down server...")
+
+	// 5秒后强制退出
+	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+	defer cancel()
+	if err := srv.Shutdown(ctx); err != nil {
+		logger.Fatal("Server forced to shutdown:", err)
+	}
+	logger.Info("Server exiting!")
 }
 
 var Welcome string = `# 发送信息

+ 4 - 1
public/public.go

@@ -8,6 +8,7 @@ import (
 	"github.com/eryajf/chatgpt-dingtalk/pkg/cache"
 	"github.com/eryajf/chatgpt-dingtalk/pkg/dingbot"
 	"github.com/eryajf/chatgpt-dingtalk/pkg/logger"
+	"github.com/sashabaranov/go-openai"
 )
 
 var UserService cache.UserServiceInterface
@@ -18,7 +19,9 @@ func InitSvc() {
 	Config = config.LoadConfig()
 	Prompt = config.LoadPrompt()
 	UserService = cache.NewUserService()
-	_, _ = GetBalance()
+	if Config.Model == openai.GPT3Dot5Turbo0301 || Config.Model == openai.GPT3Dot5Turbo {
+		_, _ = GetBalance()
+	}
 }
 
 func FirstCheck(rmsg *dingbot.ReceiveMsg) bool {