MCPcopy Index your code
hub / github.com/SMNETSTUDIO/Groq2API / chat

Function chat

router/chat.go:45–115  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

43}
44
45func chat(c *gin.Context) {
46 var api_req groq.APIRequest
47 if err := c.ShouldBindJSON(&api_req); err != nil {
48 c.JSON(500, gin.H{
49 "error": err.Error(),
50 })
51 }
52 client := groqHttp.NewBasicClient()
53 proxyUrl := global.ProxyPool.GetProxyIP()
54 if proxyUrl != "" {
55 client.SetProxy(proxyUrl)
56 }
57
58 authorization := c.Request.Header.Get("Authorization")
59 account := global.AccountPool.Get()
60 if authorization != "" {
61 customToken := strings.Replace(authorization, "Bearer ", "", 1)
62 if customToken != "" {
63 // 说明传递的是session_token
64 if strings.HasPrefix(customToken, "eyJhbGciOiJSUzI1NiI") {
65 account = groq.NewAccount("", "")
66 err := authSessionHandler(client, account, customToken, "")
67 if err != nil {
68 slog.Error("session_token is invalid", err)
69 c.JSON(400, gin.H{"error": err.Error()})
70 c.Abort()
71 return
72 }
73 }
74 if len(customToken) == 44 {
75 account = groq.NewAccount(customToken, "")
76 err := authRefreshHandler(client, account, customToken, "")
77 if err != nil {
78 slog.Error("customToken is invalid", err)
79 c.JSON(400, gin.H{"error": err.Error()})
80 c.Abort()
81 return
82 }
83 }
84 }
85 }
86
87 // 默认插入中文prompt
88 if global.ChinaPrompt == "true" {
89 prompt := groq.APIMessage{
90 Content: "使用中文回答,输出时不要带英文",
91 Role: "system",
92 }
93 api_req.Messages = append([]groq.APIMessage{prompt}, api_req.Messages...)
94 }
95 if _, ok := global.Cache.Get(account.Organization); !ok {
96 err := authRefreshHandler(client, account, account.SessionToken, "")
97 if err != nil {
98 slog.Error("get refresh err", err)
99 c.JSON(400, gin.H{"error": err.Error()})
100 c.Abort()
101 return
102 }

Callers

nothing calls this directly

Calls 6

SetProxyMethod · 0.95
authSessionHandlerFunction · 0.85
authRefreshHandlerFunction · 0.85
GetProxyIPMethod · 0.80
GetMethod · 0.80
StreamHandlerMethod · 0.80

Tested by

no test coverage detected