v1
This commit is contained in:
+41
-5
@@ -14,13 +14,14 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 配置文件 (与 main.go 保持一致)
|
||||
const (
|
||||
BaseURL = "https://api.lkeap.cloud.tencent.com/v1"
|
||||
APIKey = "sk-Y4zjnwulSuSlf60mrzwCxq2ipktHSs4jZHgWeQOArWuWJEOd" // 请替换为实际的 API Key
|
||||
Model = "deepseek-v3"
|
||||
BaseURL = "https://tokenhub.tencentmaas.com/v1/"
|
||||
APIKey = "sk-KJNOFMltNzhSKh2IxW3G3MKmZF3q2RrOlvSk497CfTHp1Z4u" // 请替换为实际的 API Key
|
||||
Model = "deepseek-v4-flash"
|
||||
)
|
||||
|
||||
// readDocxContent 读取 .docx 文件并将其转换为结构化文本
|
||||
@@ -60,6 +61,8 @@ func readDocxContent(fileHeader *multipart.FileHeader) (string, error) {
|
||||
|
||||
// readCSVContent 读取 .csv 文件内容
|
||||
// 修改为先保存临时文件再读取
|
||||
// readCSVContent 读取 .csv 文件内容
|
||||
// 修改为先保存临时文件再读取,并增加了数据压缩逻辑以解决 token 超长问题
|
||||
func readCSVContent(fileHeader *multipart.FileHeader) (string, error) {
|
||||
// 1. 创建临时文件
|
||||
tempFile, err := os.CreateTemp("", "upload_*.csv")
|
||||
@@ -88,7 +91,35 @@ func readCSVContent(fileHeader *multipart.FileHeader) (string, error) {
|
||||
return "", fmt.Errorf("failed to read CSV content from temporary file: %w", err)
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
lines := strings.Split(string(content), "\n")
|
||||
var compressedLines []string
|
||||
|
||||
for i, line := range lines {
|
||||
// 1. 必须保留第一行(表头),让 AI 知道每一列是什么
|
||||
if i == 0 {
|
||||
compressedLines = append(compressedLines, line)
|
||||
continue
|
||||
}
|
||||
|
||||
// 2. 跳过空行
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
// 3. 抽样逻辑:每 4 行保留 1 行
|
||||
// i=1 是数据第1行,i=2 是数据第2行...
|
||||
// (i-1)%4 == 0 意味着:数据第1, 5, 9, 13... 行会被保留
|
||||
if (i-1)%4 == 0 {
|
||||
compressedLines = append(compressedLines, line)
|
||||
}
|
||||
}
|
||||
// --- 修改逻辑结束 ---
|
||||
|
||||
// 将处理后的行重新组合成字符串
|
||||
resultContent := strings.Join(compressedLines, "\n")
|
||||
// --- 新增逻辑结束 ---
|
||||
|
||||
return resultContent, nil
|
||||
}
|
||||
|
||||
// buildAnalysisPrompt 构建发送给 AI 的提示词
|
||||
@@ -138,11 +169,16 @@ func buildAnalysisPrompt(teachingPlanContent, heartRateContent string) string {
|
||||
| **结束部分** | 社会性及情感目标游戏 | | | | | 4 |
|
||||
| | 整理放松 | | | | | 2 |
|
||||
|
||||
请以专业体育教师的视角,提供详细的数据分析和教学建议。`, teachingPlanContent, heartRateContent)
|
||||
请以专业体育教师的视角,提供详细的数据分析和教学建议。请直接输出报告内容,不要包含“好的”、“收到”、“作为一名...”等任何开场白或客套话。`, teachingPlanContent, heartRateContent)
|
||||
}
|
||||
|
||||
// callAIForAnalysis 调用大模型进行分析
|
||||
func callAIForAnalysis(prompt string) (string, error) {
|
||||
sizeInBytes := len(prompt)
|
||||
sizeInKB := float64(sizeInBytes) / 1024.0
|
||||
|
||||
// 在日志中打印大小,保留两位小数
|
||||
log.Printf("=== 发送给 AI 的内容大小: %.2f KB (%d 字节) ===", sizeInKB, sizeInBytes)
|
||||
config := openai.DefaultConfig(APIKey)
|
||||
config.BaseURL = BaseURL
|
||||
client := openai.NewClientWithConfig(config)
|
||||
|
||||
Reference in New Issue
Block a user