/
server.go
121 lines (104 loc) · 2.73 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package backend
import (
"context"
"net/http"
"github.com/billikeu/ChatGPT-App/backend/middlewares"
"github.com/billikeu/ChatGPT-App/backend/chatbot/bot"
"github.com/billikeu/ChatGPT-App/backend/chatbot/factory"
"github.com/billikeu/ChatGPT-App/backend/chatbot/strategy"
"github.com/gin-gonic/gin"
)
type Server struct {
ctx context.Context
setting *Setting
botV2 *bot.Bot
}
func NewServer(sk, proxy string) *Server {
// create strategies for each engine
gptStrategy := strategy.NewChatGPTStrategy(&factory.ChatGPTFactory{})
bingStrategy := strategy.NewBingStrategy(&factory.BingFactory{})
bardStrategy := strategy.NewBardStrategy(&factory.BardFactory{})
gptunoStrategy := strategy.NewChatGPTUnoStrategy(&factory.ChatGPTUnoFactory{})
// create a list of strategies
strategies := []strategy.Strategy{
gptStrategy,
bingStrategy,
bardStrategy,
gptunoStrategy,
}
// create a chatbot
botV2 := bot.NewChatbot(strategies)
s := &Server{
botV2: botV2,
setting: NewSetting(""),
}
return s
}
func (s *Server) SetAccountState(accountState AccountState) {
s.setting.accountState = &accountState
}
func (s *Server) Init(ctx context.Context) {
s.ctx = ctx
go s.Start()
}
func (s *Server) Start() {
r := gin.Default()
r.Use(middlewares.Cors())
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
r.POST("/chat-process", s.chatProcess)
r.POST("/config", s.config)
r.POST("/session", s.session)
r.POST("/verify", s.verify)
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
// /chat-process
/*
req: {"prompt":"2","options":{"conversationId":"2d1fe5e1-fedc-4e2b-bf75-065b1da01abc","parentMessageId":"chatcmpl-70LeQVv1CupSiy6aeio4rt5safFYA"},"systemMessage":"You are ChatGPT, "}
err: {"message":"","data":null,"status":"Fail"}
*/
func (s *Server) chatProcess(c *gin.Context) {
if s.setting.Engine() == ChatGPT {
s.chatProcessChatGPT(c)
} else if s.setting.Engine() == ChatGPTUnofficial {
s.chatProcessChatGPTUno(c)
} else if s.setting.Engine() == NewBingUnofficial {
s.chatProcessBing(c)
} else {
c.JSON(403, gin.H{
"msg": "unsupported model engine",
})
}
}
// /config
func (s *Server) config(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": nil,
"data": gin.H{
"apiModel": "ChatGPTAPI",
"reverseProxy": "-",
"timeoutMs": 60000,
"socksProxy": "-",
"httpsProxy": "-",
"balance": "-",
},
"status": "Success",
})
}
// /session
func (s *Server) session(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": "Success",
"message": "",
"data": gin.H{
"auth": false,
"model": "ChatGPTAPI",
}})
}
// /verify
func (s *Server) verify(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{})
}