refactor: ai usage statics.
This commit is contained in:
+70
-19
@@ -17,7 +17,9 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -235,8 +237,16 @@ func buildAnalysisPrompt(teachingPlanContent, heartRateContent, analysisType, st
|
||||
请以专业体育教师的视角,提供详细的数据分析和教学建议。`, teachingPlanContent, heartRateContent)
|
||||
}
|
||||
|
||||
type aiAnalysisResult struct {
|
||||
Content string
|
||||
InputTokens int
|
||||
OutputTokens int
|
||||
InputSizeBytes int
|
||||
OutputSizeBytes int
|
||||
}
|
||||
|
||||
// callAIForAnalysis 调用大模型进行分析
|
||||
func callAIForAnalysis(prompt string) (string, error) {
|
||||
func callAIForAnalysis(prompt string) (*aiAnalysisResult, error) {
|
||||
sizeInBytes := len(prompt)
|
||||
sizeInKB := float64(sizeInBytes) / 1024.0
|
||||
|
||||
@@ -244,7 +254,7 @@ func callAIForAnalysis(prompt string) (string, error) {
|
||||
log.Printf("=== 发送给 AI 的内容大小: %.2f KB (%d 字节) ===", sizeInKB, sizeInBytes)
|
||||
baseURL, apiKey, model, err := config.GetAIConfig()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clientConfig := openai.DefaultConfig(apiKey)
|
||||
@@ -267,14 +277,21 @@ func callAIForAnalysis(prompt string) (string, error) {
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("API call failed: %w", err)
|
||||
return nil, fmt.Errorf("API call failed: %w", err)
|
||||
}
|
||||
|
||||
if len(resp.Choices) == 0 {
|
||||
return "", fmt.Errorf("no choices returned from API")
|
||||
return nil, fmt.Errorf("no choices returned from API")
|
||||
}
|
||||
|
||||
return resp.Choices[0].Message.Content, nil
|
||||
content := resp.Choices[0].Message.Content
|
||||
return &aiAnalysisResult{
|
||||
Content: content,
|
||||
InputTokens: resp.Usage.PromptTokens,
|
||||
OutputTokens: resp.Usage.CompletionTokens,
|
||||
InputSizeBytes: len(prompt),
|
||||
OutputSizeBytes: len(content),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AnalyzeByAI Gin 控制器方法
|
||||
@@ -292,6 +309,7 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
stepFiles := form.File["step_data"]
|
||||
analysisType := c.PostForm("analysis_type")
|
||||
teachingPlanSource := c.PostForm("teaching_plan_source")
|
||||
regionIDStr := c.PostForm("regionid")
|
||||
if analysisType == "" {
|
||||
analysisType = analysisTypeHeartRateOnly
|
||||
}
|
||||
@@ -308,10 +326,12 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
uploadTime := time.Now().UnixMilli()
|
||||
|
||||
// 3. 读取文件内容
|
||||
// 注意:这里我们只取第一个上传的文件
|
||||
heartRateFileHeader := csvFiles[0]
|
||||
teachingPlanContent, err := resolveTeachingPlanContent(c, form, teachingPlanSource)
|
||||
teachingPlanContent, teachingPlanSize, err := resolveTeachingPlanContent(c, form, teachingPlanSource)
|
||||
if err != nil {
|
||||
log.Printf("Error resolving teaching plan: %v", err)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -330,8 +350,10 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
}
|
||||
|
||||
stepContent := ""
|
||||
var stepFileSize int64 = 0
|
||||
if analysisType == analysisTypeHeartRateWithSteps {
|
||||
stepFileHeader := stepFiles[0]
|
||||
stepFileSize = stepFileHeader.Size
|
||||
stepContent, err = readCSVContent(stepFileHeader)
|
||||
if err != nil {
|
||||
log.Printf("Error reading step file (%s): %v", stepFileHeader.Filename, err)
|
||||
@@ -340,47 +362,76 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 计算文件大小
|
||||
originalFileSize := heartRateFileHeader.Size + teachingPlanSize + stepFileSize
|
||||
compressedContentSize := int64(len(heartRateContent)) + int64(len(teachingPlanContent)) + int64(len(stepContent))
|
||||
|
||||
// 4. 构建 Prompt
|
||||
prompt := buildAnalysisPrompt(teachingPlanContent, heartRateContent, analysisType, stepContent)
|
||||
|
||||
// 5. 调用 AI 分析
|
||||
startTime := time.Now()
|
||||
analysisResult, err := callAIForAnalysis(prompt)
|
||||
if err != nil {
|
||||
log.Printf("Error calling AI for analysis: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("AI analysis failed: %v", err)})
|
||||
return
|
||||
}
|
||||
//outputFile := ".md"
|
||||
//ioutil.WriteFile(outputFile, []byte(analysisResult), 0644)
|
||||
durationMs := time.Since(startTime).Milliseconds()
|
||||
|
||||
// 6. 返回结果
|
||||
// 方式一:返回 JSON 结构
|
||||
// 6. 保存分析记录
|
||||
var regionID *uint32
|
||||
if regionIDStr != "" {
|
||||
if parsed, err := strconv.ParseUint(regionIDStr, 10, 32); err == nil {
|
||||
id := uint32(parsed)
|
||||
regionID = &id
|
||||
}
|
||||
}
|
||||
|
||||
record := models.AIAnalysisRecord{
|
||||
RegionID: regionID,
|
||||
SourceType: teachingPlanSource,
|
||||
InputTokens: analysisResult.InputTokens,
|
||||
OutputTokens: analysisResult.OutputTokens,
|
||||
InputSizeBytes: analysisResult.InputSizeBytes,
|
||||
OutputSizeBytes: analysisResult.OutputSizeBytes,
|
||||
DurationMs: durationMs,
|
||||
OriginalFileSize: originalFileSize,
|
||||
CompressedContentSize: compressedContentSize,
|
||||
UploadTime: uploadTime,
|
||||
}
|
||||
if err := config.DB.Create(&record).Error; err != nil {
|
||||
log.Printf("Failed to save analysis record: %v", err)
|
||||
}
|
||||
|
||||
// 7. 返回结果
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "success",
|
||||
"data": analysisResult,
|
||||
"data": analysisResult.Content,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func resolveTeachingPlanContent(c *gin.Context, form *multipart.Form, source string) (string, error) {
|
||||
func resolveTeachingPlanContent(c *gin.Context, form *multipart.Form, source string) (string, int64, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(source)) {
|
||||
case "upload":
|
||||
docxFiles := form.File["teaching_plan"]
|
||||
if len(docxFiles) == 0 {
|
||||
return "", fmt.Errorf("Missing required file: teaching_plan (.docx)")
|
||||
return "", 0, fmt.Errorf("Missing required file: teaching_plan (.docx)")
|
||||
}
|
||||
return readDocxContent(docxFiles[0])
|
||||
content, err := readDocxContent(docxFiles[0])
|
||||
return content, docxFiles[0].Size, err
|
||||
case "cloud":
|
||||
lessonPlanID := c.PostForm("lesson_plan_id")
|
||||
if strings.TrimSpace(lessonPlanID) == "" {
|
||||
return "", fmt.Errorf("missing required field: lesson_plan_id")
|
||||
return "", 0, fmt.Errorf("missing required field: lesson_plan_id")
|
||||
}
|
||||
var fileRecord models.AppFile
|
||||
if err := config.DB.Where("id = ? AND file_type = ?", lessonPlanID, models.AppFileTypeLessonPlan).First(&fileRecord).Error; err != nil {
|
||||
return "", err
|
||||
return "", 0, err
|
||||
}
|
||||
return readDocxContentFromPath(fileRecord.FilePath)
|
||||
content, err := readDocxContentFromPath(fileRecord.FilePath)
|
||||
return content, fileRecord.FileSize, err
|
||||
default:
|
||||
return "", fmt.Errorf("invalid teaching_plan_source, expected upload or cloud")
|
||||
return "", 0, fmt.Errorf("invalid teaching_plan_source, expected upload or cloud")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user