forked from linweiyuan/go-chatgpt-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (97 loc) · 3.11 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
package main
import (
"log"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/linweiyuan/go-chatgpt-api/api/chatgpt"
"github.com/linweiyuan/go-chatgpt-api/api/platform"
_ "github.com/linweiyuan/go-chatgpt-api/env"
"github.com/linweiyuan/go-chatgpt-api/middleware"
)
func init() {
gin.ForceConsoleColor()
gin.SetMode(gin.ReleaseMode)
}
func main() {
router := gin.Default()
router.Use(middleware.CORSMiddleware())
router.Use(middleware.CheckHeaderMiddleware())
setupChatGPTAPIs(router)
setupPlatformAPIs(router)
router.NoRoute(handleFallbackRoute)
port := os.Getenv("GO_CHATGPT_API_PORT")
if port == "" {
port = "8080"
}
err := router.Run(":" + port)
if err != nil {
log.Fatal("Failed to start server: " + err.Error())
}
}
func setupChatGPTAPIs(router *gin.Engine) {
chatgptGroup := router.Group("/chatgpt")
{
chatgptGroup.POST("/login", chatgpt.Login)
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)
}
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)
}
// misc
chatgptGroup.GET("/models", chatgpt.GetModels)
chatgptGroup.GET("/accounts/check", chatgpt.GetAccountCheck)
}
}
func setupPlatformAPIs(router *gin.Engine) {
platformGroup := router.Group("/platform")
{
platformGroup.POST("/login", platform.Login)
apiGroup := platformGroup.Group("/v1")
{
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)
}
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 handleFallbackRoute(c *gin.Context) {
path := c.Request.URL.Path
if strings.HasPrefix(path, "/chatgpt") {
trimmedPath := strings.TrimPrefix(path, "/chatgpt")
c.Request.URL.Path = trimmedPath
chatgpt.Fallback(c)
} else {
c.JSON(http.StatusNotFound, gin.H{"message": "Route not found"})
}
}