record time

This commit is contained in:
2023-08-10 12:42:12 +08:00
parent 536540335d
commit 2f2c4464ea
2 changed files with 10 additions and 7 deletions

View File

@@ -97,6 +97,7 @@ func main() {
db.Take(&authConfig, "key = ?", "authorization") db.Take(&authConfig, "key = ?", "authorization")
engine.POST("/v1/*any", func(c *gin.Context) { engine.POST("/v1/*any", func(c *gin.Context) {
begin := time.Now()
trackID := uuid.New() trackID := uuid.New()
// check authorization header // check authorization header
if !*noauth { if !*noauth {
@@ -224,7 +225,7 @@ func main() {
if err != nil { if err != nil {
log.Println("Failed to read from response tee buffer", err) log.Println("Failed to read from response tee buffer", err)
} }
go recordAssistantResponse(contentType, db, trackID, resp) go recordAssistantResponse(contentType, db, trackID, resp, time.Now().Sub(begin))
}) })
// --------------------------------- // ---------------------------------

View File

@@ -17,6 +17,7 @@ type Record struct {
IP string IP string
Body string Body string
Response string Response string
ElapsedTime time.Duration
} }
func recordUserMessage(c *gin.Context, db *gorm.DB, trackID uuid.UUID, body []byte) { func recordUserMessage(c *gin.Context, db *gorm.DB, trackID uuid.UUID, body []byte) {
@@ -62,7 +63,7 @@ type FetchModeUsage struct {
TotalTokens int64 `json:"total_tokens"` TotalTokens int64 `json:"total_tokens"`
} }
func recordAssistantResponse(contentType string, db *gorm.DB, trackID uuid.UUID, body []byte) { func recordAssistantResponse(contentType string, db *gorm.DB, trackID uuid.UUID, body []byte, elapsedTime time.Duration) {
result := "" result := ""
// stream mode // stream mode
if strings.HasPrefix(contentType, "text/event-stream") { if strings.HasPrefix(contentType, "text/event-stream") {
@@ -113,6 +114,7 @@ func recordAssistantResponse(contentType string, db *gorm.DB, trackID uuid.UUID,
return return
} }
record.Response = result record.Response = result
record.ElapsedTime = elapsedTime
if db.Save(&record).Error != nil { if db.Save(&record).Error != nil {
log.Println("Error to save record:", record) log.Println("Error to save record:", record)
return return