forked from linweiyuan/go-chatgpt-api
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
176 lines (146 loc) · 4.87 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"flag"
"fmt"
"github.com/denisbrodbeck/machineid"
"github.com/gin-gonic/gin"
"github.com/leokwsw/go-chatgpt-api/api"
"github.com/leokwsw/go-chatgpt-api/api/chatgpt"
"github.com/leokwsw/go-chatgpt-api/api/imitate"
"github.com/leokwsw/go-chatgpt-api/api/platform"
_ "github.com/leokwsw/go-chatgpt-api/env"
"github.com/leokwsw/go-chatgpt-api/middleware"
"github.com/linweiyuan/go-logger/logger"
"log"
"strings"
http "github.com/bogdanfinn/fhttp"
)
func init() {
gin.ForceConsoleColor()
gin.SetMode(gin.ReleaseMode)
}
func main() {
id, err := machineid.ProtectedID("go-chatgpt-api")
if err != nil {
log.Fatal(err)
}
logger.Info("id : " + id)
flatPort := flag.String("port", "8080", "")
flag.Parse()
router := gin.Default()
router.Use(middleware.CORS())
router.Use(middleware.Authorization())
setupChatGPTAPIs(router)
setupPlatformAPIs(router)
setupPandoraAPIs(router)
setupImitateAPIs(router)
router.NoRoute(api.Proxy)
router.GET("/", func(c *gin.Context) {
c.Header("Content-Type", "text/plain")
c.String(http.StatusOK, api.ReadyHint)
})
router.GET("/robots.txt", func(c *gin.Context) {
c.Header("Content-Type", "text/plain")
c.String(http.StatusOK, api.RobotsHint)
})
port := *flatPort
rErr := router.Run(":" + port)
if rErr != nil {
log.Fatal("Failed to start Http server: " + rErr.Error())
} else {
fmt.Println("run on port :" + port)
}
}
func setupChatGPTAPIs(router *gin.Engine) {
chatgptGroup := router.Group("/chatgpt")
{
chatgptGroup.POST("/login", chatgpt.Login)
chatgptGroup.POST("/backend-api/login", chatgpt.Login) // add support for other projects
conversationsGroup := chatgptGroup.Group("/conversations")
{
conversationsGroup.GET("", chatgpt.GetConversations)
// PATCH is official method, POST is added for Java support
conversationsGroup.PATCH("", chatgpt.ClearConversations)
conversationsGroup.POST("", chatgpt.ClearConversations)
}
backendConversationGroup := chatgptGroup.Group("/backend-api/conversation")
{
backendConversationGroup.POST("", chatgpt.CreateConversation)
}
conversationGroup := chatgptGroup.Group("/conversation")
{
conversationGroup.POST("", chatgpt.CreateConversation)
conversationGroup.POST("/gen_title/:id", chatgpt.GenerateTitle)
conversationGroup.GET("/:id", chatgpt.GetConversation)
// rename or delete conversation use a same API with different parameters
conversationGroup.PATCH("/:id", chatgpt.UpdateConversation)
conversationGroup.POST("/:id", chatgpt.UpdateConversation)
conversationGroup.POST("/message_feedback", chatgpt.FeedbackMessage)
}
synthesizeGroup := chatgptGroup.Group("/synthesize")
{
synthesizeGroup.GET("/", chatgpt.GetSynthesize)
}
settingGroup := chatgptGroup.Group("/settings")
{
settingGroup.GET("/user", chatgpt.GetUserSetting)
settingGroup.PATCH("/account_user_setting", chatgpt.UpdateUserSetting)
}
// misc
chatgptGroup.GET("/models", chatgpt.GetModels)
chatgptGroup.GET("/accounts/check", chatgpt.GetAccountCheck)
chatgptGroup.GET("/me", chatgpt.GetMe)
chatgptGroup.GET("/prompt_library/", chatgpt.GetPromptLibrary)
chatgptGroup.GET("/gizmos", chatgpt.GetGizmos)
chatgptGroup.GET("/ping", chatgpt.Ping)
}
}
func setupPlatformAPIs(router *gin.Engine) {
platformGroup := router.Group("/platform")
{
platformGroup.POST("/login", platform.Login)
apiGroup := platformGroup.Group("/v1")
{
apiGroup.POST("/login", platform.Login)
apiGroup.GET("/models", platform.ListModels)
apiGroup.GET("/models/:model", platform.RetrieveModel)
apiGroup.POST("/completions", platform.CreateCompletions)
apiGroup.POST("/chat/completions", platform.CreateChatCompletions)
apiGroup.POST("/edits", platform.CreateEdit)
apiGroup.POST("/images/generations", platform.CreateImage)
apiGroup.POST("/embeddings", platform.CreateEmbeddings)
apiGroup.GET("/files", platform.ListFiles)
apiGroup.POST("/moderations", platform.CreateModeration)
apiGroup.POST("/audio/transcriptions", platform.CreateTranscriptions)
apiGroup.POST("/audio/speech", platform.CreateSpeech)
}
dashboardGroup := platformGroup.Group("/dashboard")
{
billingGroup := dashboardGroup.Group("/billing")
{
billingGroup.GET("/credit_grants", platform.GetCreditGrants)
billingGroup.GET("/subscription", platform.GetSubscription)
}
userGroup := dashboardGroup.Group("/user")
{
userGroup.GET("/api_keys", platform.GetApiKeys)
}
}
}
}
func setupPandoraAPIs(router *gin.Engine) {
router.Any("/api/*path", func(c *gin.Context) {
c.Request.URL.Path = strings.ReplaceAll(c.Request.URL.Path, "/api", "/chatgpt/backend-api")
router.HandleContext(c)
})
}
func setupImitateAPIs(router *gin.Engine) {
imitateGroup := router.Group("/imitate")
{
imitateGroup.POST("/login", chatgpt.Login)
apiGroup := imitateGroup.Group("/v1")
{
apiGroup.POST("/chat/completions", imitate.CreateChatCompletions)
}
}
}