record full request body
This commit is contained in:
10
main.go
10
main.go
@@ -46,7 +46,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
db.AutoMigrate(&OPENAI_UPSTREAM{})
|
db.AutoMigrate(&OPENAI_UPSTREAM{})
|
||||||
db.AutoMigrate(&UserMessage{})
|
db.AutoMigrate(&RequestRecord{})
|
||||||
log.Println("Auto migrate database done")
|
log.Println("Auto migrate database done")
|
||||||
|
|
||||||
if *addMode {
|
if *addMode {
|
||||||
@@ -306,18 +306,18 @@ func main() {
|
|||||||
"message": "success",
|
"message": "success",
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
engine.GET("/admin/user_messages", func(c *gin.Context) {
|
engine.GET("/admin/request_records", func(c *gin.Context) {
|
||||||
// check authorization headers
|
// check authorization headers
|
||||||
if handleAuth(c) != nil {
|
if handleAuth(c) != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userMessages := []UserMessage{}
|
requestRecords := []RequestRecord{}
|
||||||
err := db.Order("id desc").Limit(100).Find(&userMessages).Error
|
err := db.Order("id desc").Limit(100).Find(&requestRecords).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(502, err)
|
c.AbortWithError(502, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(200, userMessages)
|
c.JSON(200, requestRecords)
|
||||||
})
|
})
|
||||||
engine.Run(*listenAddr)
|
engine.Run(*listenAddr)
|
||||||
}
|
}
|
||||||
|
|||||||
44
record.go
44
record.go
@@ -1,51 +1,19 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"log"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserMessage struct {
|
type RequestRecord struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
ModelName string
|
Body string
|
||||||
Content string
|
|
||||||
}
|
|
||||||
|
|
||||||
// sturcture to parse request
|
|
||||||
type ChatRequest struct {
|
|
||||||
Model string `json:"model"`
|
|
||||||
Messages []Message `json:"messages"`
|
|
||||||
}
|
|
||||||
type Message struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func recordUserMessage(c *gin.Context, db *gorm.DB, body []byte) {
|
func recordUserMessage(c *gin.Context, db *gorm.DB, body []byte) {
|
||||||
bodyJson := ChatRequest{}
|
bodyStr := string(body)
|
||||||
err := json.Unmarshal(body, &bodyJson)
|
requestRecord := RequestRecord{
|
||||||
if err != nil {
|
Body: bodyStr,
|
||||||
c.AbortWithError(502, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
model := bodyJson.Model
|
db.Create(&requestRecord)
|
||||||
if !strings.HasPrefix(model, "gpt-") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// get message content
|
|
||||||
if len(bodyJson.Messages) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
content := bodyJson.Messages[len(bodyJson.Messages)-1].Content
|
|
||||||
|
|
||||||
log.Println("Record user message", model, content)
|
|
||||||
|
|
||||||
userMessage := UserMessage{
|
|
||||||
ModelName: model,
|
|
||||||
Content: content,
|
|
||||||
}
|
|
||||||
db.Create(&userMessage)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user