clean code
This commit is contained in:
64
record.go
64
record.go
@@ -1,13 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Record struct {
|
||||
@@ -55,61 +49,3 @@ type FetchModeUsage struct {
|
||||
CompletionTokens int64 `json:"completion_tokens"`
|
||||
TotalTokens int64 `json:"total_tokens"`
|
||||
}
|
||||
|
||||
func recordAssistantResponse(contentType string, db *gorm.DB, trackID uuid.UUID, body []byte, elapsedTime time.Duration) {
|
||||
result := ""
|
||||
// stream mode
|
||||
if strings.HasPrefix(contentType, "text/event-stream") {
|
||||
resp := string(body)
|
||||
for _, line := range strings.Split(resp, "\n") {
|
||||
chunk := StreamModeChunk{}
|
||||
line = strings.TrimPrefix(line, "data:")
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
err := json.Unmarshal([]byte(line), &chunk)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(chunk.Choices) == 0 {
|
||||
continue
|
||||
}
|
||||
result += chunk.Choices[0].Delta.Content
|
||||
}
|
||||
} else if strings.HasPrefix(contentType, "application/json") {
|
||||
var fetchResp FetchModeResponse
|
||||
err := json.Unmarshal(body, &fetchResp)
|
||||
if err != nil {
|
||||
log.Println("Error parsing fetch response:", err)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(fetchResp.Model, "gpt-") {
|
||||
log.Println("Not GPT model, skip recording response:", fetchResp.Model)
|
||||
return
|
||||
}
|
||||
if len(fetchResp.Choices) == 0 {
|
||||
log.Println("Error: fetch response choice length is 0")
|
||||
return
|
||||
}
|
||||
result = fetchResp.Choices[0].Message.Content
|
||||
} else {
|
||||
log.Println("Unknown content type", contentType)
|
||||
return
|
||||
}
|
||||
log.Println("Record result:", result)
|
||||
record := Record{}
|
||||
if db.Find(&record, "id = ?", trackID).Error != nil {
|
||||
log.Println("Error find request record with trackID:", trackID)
|
||||
return
|
||||
}
|
||||
record.Response = result
|
||||
record.ElapsedTime = elapsedTime
|
||||
if db.Save(&record).Error != nil {
|
||||
log.Println("Error to save record:", record)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user