feat: count.
This commit is contained in:
234
controllers/ai.go
Normal file
234
controllers/ai.go
Normal file
@ -0,0 +1,234 @@
|
||||
// controllers/ai.go
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context" // 在此处添加 context 导入
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"hr_receiver/util"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// 配置文件 (与 main.go 保持一致)
|
||||
const (
|
||||
BaseURL = "https://api.lkeap.cloud.tencent.com/v1"
|
||||
APIKey = "sk-Y4zjnwulSuSlf60mrzwCxq2ipktHSs4jZHgWeQOArWuWJEOd" // 请替换为实际的 API Key
|
||||
Model = "deepseek-v3"
|
||||
)
|
||||
|
||||
// readDocxContent 读取 .docx 文件并将其转换为结构化文本
|
||||
// 修改为先保存临时文件再读取
|
||||
func readDocxContent(fileHeader *multipart.FileHeader) (string, error) {
|
||||
// 1. 创建临时文件
|
||||
tempFile, err := os.CreateTemp("", "upload_*.docx")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create temporary file: %w", err)
|
||||
}
|
||||
defer os.Remove(tempFile.Name()) // 确保函数结束时删除临时文件
|
||||
defer tempFile.Close()
|
||||
|
||||
// 2. 打开上传的文件流
|
||||
src, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open uploaded file: %w", err)
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
// 3. 将上传的文件内容复制到临时文件
|
||||
_, err = io.Copy(tempFile, src)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to copy file to temporary location: %w", err)
|
||||
}
|
||||
|
||||
// 4. 获取临时文件的完整路径
|
||||
tempFilePath := tempFile.Name()
|
||||
str, err := util.DocxToStructuredPrompt(tempFilePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse docx with go-docx: %w", err)
|
||||
}
|
||||
// 注意:表格、图片等复杂元素的处理可能需要更复杂的逻辑,这里仅处理简单文本
|
||||
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// readCSVContent 读取 .csv 文件内容
|
||||
// 修改为先保存临时文件再读取
|
||||
func readCSVContent(fileHeader *multipart.FileHeader) (string, error) {
|
||||
// 1. 创建临时文件
|
||||
tempFile, err := os.CreateTemp("", "upload_*.csv")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create temporary file: %w", err)
|
||||
}
|
||||
defer os.Remove(tempFile.Name()) // 确保函数结束时删除临时文件
|
||||
defer tempFile.Close()
|
||||
|
||||
// 2. 打开上传的文件流
|
||||
src, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open uploaded file: %w", err)
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
// 3. 将上传的文件内容复制到临时文件
|
||||
_, err = io.Copy(tempFile, src)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to copy file to temporary location: %w", err)
|
||||
}
|
||||
|
||||
// 4. 读取临时文件内容
|
||||
content, err := ioutil.ReadFile(tempFile.Name())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read CSV content from temporary file: %w", err)
|
||||
}
|
||||
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// buildAnalysisPrompt 构建发送给 AI 的提示词
|
||||
func buildAnalysisPrompt(teachingPlanContent, heartRateContent string) string {
|
||||
return fmt.Sprintf(`请根据以下体育课堂的教案和心率监测数据,生成一份详细的课堂分析报告:
|
||||
|
||||
## 教案内容:
|
||||
%s
|
||||
|
||||
## 心率监测数据:
|
||||
%s
|
||||
|
||||
这是一份幼儿园体育课的教案和课程心率监测数据,请帮对照分析课程教学效果,运动量和运动负荷情况是否科学,并提出课程设计的优化方案。
|
||||
|
||||
优化方案参考如下格式,教学过程需要详细一些:
|
||||
# 幼儿体育教案(华侨大学版本)
|
||||
|
||||
| 项目 | 内容 |
|
||||
| ------------ | -------------------------------- |
|
||||
| **课程名** | |
|
||||
| **年段** | 小 中 大 |
|
||||
| **教师姓名** | |
|
||||
| **时间** | 年 月 日 |
|
||||
| **地点** | |
|
||||
| **人数** | 男: 女: |
|
||||
| **时长** | 分钟 |
|
||||
| **天气预报** | 晴 雨 阴;温度 ℃ |
|
||||
| **器材准备** | |
|
||||
|
||||
## 教学目标
|
||||
|
||||
| 类型 | 目标 |
|
||||
| -------- | ------------ |
|
||||
| **体能目标** | |
|
||||
| **技能目标** | |
|
||||
| **情感目标** | |
|
||||
|
||||
## 教学过程
|
||||
|
||||
| 阶段 | 阶段 | 项目名称 | 引导语及教学方法 | 队形/站位/留意点 | 目标心率区间 | 时间(分) |
|
||||
| ---------- | -------- | ----------------------------- | ------------------------ | --------------------- | ------------ | ---------- |
|
||||
| **准备部分** | 热身 | | | | | 3 |
|
||||
| | 注意力游戏 | | | | | 3 |
|
||||
| **正课部分** | 基本素质练习及常规意识培养环节 | | | | | 5 |
|
||||
| | 复习环节 | | | | | 5 |
|
||||
| | 新授环节 | | | | | 8 |
|
||||
| **结束部分** | 社会性及情感目标游戏 | | | | | 4 |
|
||||
| | 整理放松 | | | | | 2 |
|
||||
|
||||
请以专业体育教师的视角,提供详细的数据分析和教学建议。`, teachingPlanContent, heartRateContent)
|
||||
}
|
||||
|
||||
// callAIForAnalysis 调用大模型进行分析
|
||||
func callAIForAnalysis(prompt string) (string, error) {
|
||||
config := openai.DefaultConfig(APIKey)
|
||||
config.BaseURL = BaseURL
|
||||
client := openai.NewClientWithConfig(config)
|
||||
|
||||
resp, err := client.CreateChatCompletion(
|
||||
context.Background(),
|
||||
openai.ChatCompletionRequest{
|
||||
Model: Model,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: prompt,
|
||||
},
|
||||
},
|
||||
Temperature: 0.6, // 可调整
|
||||
TopP: 0.6, // 可调整
|
||||
MaxTokens: 4000, // 根据需要调整
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("API call failed: %w", err)
|
||||
}
|
||||
|
||||
if len(resp.Choices) == 0 {
|
||||
return "", fmt.Errorf("no choices returned from API")
|
||||
}
|
||||
|
||||
return resp.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
// AnalyzeByAI Gin 控制器方法
|
||||
func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
// 1. 解析多部分表单请求
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
log.Printf("Error parsing multipart form: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Failed to parse form: %v", err)})
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 获取文件列表
|
||||
docxFiles := form.File["teaching_plan"] // 假设前端字段名为 'teaching_plan'
|
||||
csvFiles := form.File["heart_rate_data"] // 假设前端字段名为 'heart_rate_data'
|
||||
|
||||
if len(docxFiles) == 0 || len(csvFiles) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing required files: teaching_plan (.docx) or heart_rate_data (.csv)"})
|
||||
return
|
||||
}
|
||||
|
||||
// 3. 读取文件内容
|
||||
// 注意:这里我们只取第一个上传的文件
|
||||
teachingPlanFileHeader := docxFiles[0]
|
||||
heartRateFileHeader := csvFiles[0]
|
||||
|
||||
teachingPlanContent, err := readDocxContent(teachingPlanFileHeader)
|
||||
if err != nil {
|
||||
log.Printf("Error reading teaching plan file (%s): %v", teachingPlanFileHeader.Filename, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to process teaching plan file: %v", err)})
|
||||
return
|
||||
}
|
||||
|
||||
heartRateContent, err := readCSVContent(heartRateFileHeader)
|
||||
if err != nil {
|
||||
log.Printf("Error reading heart rate file (%s): %v", heartRateFileHeader.Filename, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to process heart rate file: %v", err)})
|
||||
return
|
||||
}
|
||||
|
||||
// 4. 构建 Prompt
|
||||
prompt := buildAnalysisPrompt(teachingPlanContent, heartRateContent)
|
||||
|
||||
// 5. 调用 AI 分析
|
||||
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)
|
||||
|
||||
// 6. 返回结果
|
||||
// 方式一:返回 JSON 结构
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "success",
|
||||
"data": analysisResult,
|
||||
})
|
||||
|
||||
}
|
||||
3
go.mod
3
go.mod
@ -3,9 +3,11 @@ module hr_receiver
|
||||
go 1.23.3
|
||||
|
||||
require (
|
||||
github.com/fumiama/go-docx v0.0.0-20250506085032-0c30fd09304b
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||
github.com/sajari/regression v1.0.1
|
||||
github.com/sashabaranov/go-openai v1.41.2
|
||||
github.com/spf13/viper v1.20.0
|
||||
gonum.org/v1/gonum v0.16.0
|
||||
gorm.io/driver/postgres v1.5.11
|
||||
@ -18,6 +20,7 @@ require (
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.8.0 // indirect
|
||||
github.com/fumiama/imgsz v0.0.2 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
|
||||
6
go.sum
6
go.sum
@ -13,6 +13,10 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
|
||||
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/fumiama/go-docx v0.0.0-20250506085032-0c30fd09304b h1:/mxSugRc4SgN7XgBtT19dAJ7cAXLTbPmlJLJE4JjRkE=
|
||||
github.com/fumiama/go-docx v0.0.0-20250506085032-0c30fd09304b/go.mod h1:ssRF0IaB1hCcKIObp3FkZOsjTcAHpgii70JelNb4H8M=
|
||||
github.com/fumiama/imgsz v0.0.2 h1:fAkC0FnIscdKOXwAxlyw3EUba5NzxZdSxGaq3Uyfxak=
|
||||
github.com/fumiama/imgsz v0.0.2/go.mod h1:dR71mI3I2O5u6+PCpd47M9TZptzP+39tRBcbdIkoqM4=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
@ -77,6 +81,8 @@ github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsF
|
||||
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
|
||||
github.com/sajari/regression v1.0.1 h1:iTVc6ZACGCkoXC+8NdqH5tIreslDTT/bXxT6OmHR5PE=
|
||||
github.com/sajari/regression v1.0.1/go.mod h1:NeG/XTW1lYfGY7YV/Z0nYDV/RGh3wxwd1yW46835flM=
|
||||
github.com/sashabaranov/go-openai v1.41.2 h1:vfPRBZNMpnqu8ELsclWcAvF19lDNgh1t6TVfFFOPiSM=
|
||||
github.com/sashabaranov/go-openai v1.41.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
|
||||
|
||||
@ -20,6 +20,7 @@ func SetupRouter() *gin.Engine {
|
||||
{
|
||||
records.POST("", trainingController.CreateTrainingRecord)
|
||||
records.GET("/analysis", trainingController.HandleCurveAnalysis)
|
||||
records.POST("/analysis-by-ai", trainingController.AnalyzeByAI)
|
||||
// 可扩展其他路由:GET, PUT, DELETE等
|
||||
}
|
||||
steps := v1.Group("/step") //.Use(middleware.AuthMiddleware())
|
||||
|
||||
188
test/main.go
Normal file
188
test/main.go
Normal file
@ -0,0 +1,188 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
// 配置文件
|
||||
const (
|
||||
BaseURL = "https://api.lkeap.cloud.tencent.com/v1"
|
||||
APIKey = "sk-Y4zjnwulSuSlf60mrzwCxq2ipktHSs4jZHgWeQOArWuWJEOd" // 替换为实际的API Key
|
||||
Model = "deepseek-v3" // 推荐使用terminus版本
|
||||
)
|
||||
|
||||
// 读取文件内容
|
||||
func readFileContent(filename string) (string, error) {
|
||||
content, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("读取文件 %s 失败: %v", filename, err)
|
||||
}
|
||||
return string(content), nil
|
||||
}
|
||||
|
||||
// 构建分析提示词
|
||||
func buildAnalysisPrompt(teachingPlanContent, heartRateContent string) string {
|
||||
return fmt.Sprintf(`请根据以下体育课堂的教案和心率监测数据,生成一份详细的课堂分析报告:
|
||||
|
||||
## 教案内容:
|
||||
%s
|
||||
|
||||
## 心率监测数据:
|
||||
%s
|
||||
|
||||
这是一份幼儿园体育课的教案和课程心率监测数据,请帮对照分析课程教学效果,运动量和运动负荷情况是否科学,并提出课程设计的优化方案。
|
||||
优化方案参考如下格式,教学过程需要详细一些:
|
||||
# 幼儿体育教案(华侨大学版本)
|
||||
|
||||
| 项目 | 内容 |
|
||||
| ------------ | -------------------------------- |
|
||||
| **课程名** | |
|
||||
| **年段** | 小 中 大 |
|
||||
| **教师姓名** | |
|
||||
| **时间** | 年 月 日 |
|
||||
| **地点** | |
|
||||
| **人数** | 男: 女: |
|
||||
| **时长** | 分钟 |
|
||||
| **天气预报** | 晴 雨 阴;温度 ℃ |
|
||||
| **器材准备** | |
|
||||
|
||||
## 教学目标
|
||||
|
||||
| 类型 | 目标 |
|
||||
| -------- | ------------ |
|
||||
| **体能目标** | |
|
||||
| **技能目标** | |
|
||||
| **情感目标** | |
|
||||
|
||||
## 教学过程
|
||||
|
||||
| 阶段 | 阶段 | 项目名称 | 引导语及教学方法 | 队形/站位/留意点 | 目标心率区间 | 时间(分) |
|
||||
| ---------- | -------- | ----------------------------- | ------------------------ | --------------------- | ------------ | ---------- |
|
||||
| **准备部分** | 热身 | | | | | 3 |
|
||||
| | 注意力游戏 | | | | | 3 |
|
||||
| **正课部分** | 基本素质练习及常规意识培养环节 | | | | | 5 |
|
||||
| | 复习环节 | | | | | 5 |
|
||||
| | 新授环节 | | | | | 8 |
|
||||
| **结束部分** | 社会性及情感目标游戏 | | | | | 4 |
|
||||
| | 整理放松 | | | | | 2 |
|
||||
|
||||
|
||||
请以专业体育教师的视角,提供详细的数据分析和教学建议。`, teachingPlanContent, heartRateContent)
|
||||
}
|
||||
|
||||
// 调用大模型进行分析
|
||||
func analyzeClassData(teachingPlanFile, heartRateFile string) (string, error) {
|
||||
// 读取文件内容
|
||||
teachingPlanContent, err := readFileContent(teachingPlanFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
heartRateContent, err := readFileContent(heartRateFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 构建客户端
|
||||
config := openai.DefaultConfig(APIKey)
|
||||
config.BaseURL = BaseURL
|
||||
client := openai.NewClientWithConfig(config)
|
||||
|
||||
// 构建提示词
|
||||
prompt := buildAnalysisPrompt(teachingPlanContent, heartRateContent)
|
||||
|
||||
// 调用API
|
||||
resp, err := client.CreateChatCompletion(
|
||||
context.Background(),
|
||||
openai.ChatCompletionRequest{
|
||||
Model: Model,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: prompt,
|
||||
},
|
||||
},
|
||||
Temperature: 0.6, // 使用默认值
|
||||
TopP: 0.6, // 使用默认值
|
||||
MaxTokens: 5000, // 适当限制输出长度
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("API调用失败: %v", err)
|
||||
}
|
||||
|
||||
if len(resp.Choices) == 0 {
|
||||
return "", fmt.Errorf("未收到有效响应")
|
||||
}
|
||||
|
||||
return resp.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
// 保存分析结果到文件
|
||||
func saveAnalysisResult(result, outputFile string) error {
|
||||
// 添加时间戳和分隔符
|
||||
timestamp := "生成时间: " + getCurrentTime()
|
||||
separator := strings.Repeat("=", 80)
|
||||
|
||||
formattedResult := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n",
|
||||
separator, timestamp, separator, result, separator)
|
||||
|
||||
return ioutil.WriteFile(outputFile, []byte(formattedResult), 0644)
|
||||
}
|
||||
|
||||
// 获取当前时间(简化版)
|
||||
func getCurrentTime() string {
|
||||
// 实际使用时可以导入time包
|
||||
return time.Now().Format("2006-01-02 15:04:05") // 替换为 time.Now().Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 文件路径配置
|
||||
teachingPlanFile := "D:\\projects\\IdeaProjects\\hr_receiver\\test\\b.md"
|
||||
heartRateFile := "D:\\projects\\IdeaProjects\\hr_receiver\\test\\b.csv"
|
||||
outputFile := "小班.md"
|
||||
|
||||
// 检查文件是否存在
|
||||
if _, err := os.Stat(teachingPlanFile); os.IsNotExist(err) {
|
||||
log.Fatalf("教案文件不存在: %s", teachingPlanFile)
|
||||
}
|
||||
if _, err := os.Stat(heartRateFile); os.IsNotExist(err) {
|
||||
log.Fatalf("心率数据文件不存在: %s", heartRateFile)
|
||||
}
|
||||
|
||||
fmt.Println("开始分析体育课堂数据...")
|
||||
fmt.Printf("教案文件: %s\n", teachingPlanFile)
|
||||
fmt.Printf("心率数据: %s\n", heartRateFile)
|
||||
|
||||
// 进行分析
|
||||
result, err := analyzeClassData(teachingPlanFile, heartRateFile)
|
||||
if err != nil {
|
||||
log.Fatalf("分析失败: %v", err)
|
||||
}
|
||||
|
||||
// 保存结果
|
||||
err = saveAnalysisResult(result, outputFile)
|
||||
if err != nil {
|
||||
log.Fatalf("保存结果失败: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("分析完成!结果已保存到: %s\n", outputFile)
|
||||
fmt.Println("\n分析报告摘要:")
|
||||
fmt.Println(strings.Repeat("-", 50))
|
||||
|
||||
// 显示前200字符作为预览
|
||||
preview := result
|
||||
if len(result) > 200 {
|
||||
preview = result[:200] + "..."
|
||||
}
|
||||
fmt.Println(preview)
|
||||
}
|
||||
86
test/util.go
Normal file
86
test/util.go
Normal file
@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
docx "github.com/fumiama/go-docx"
|
||||
)
|
||||
|
||||
func DocxToStructuredPrompt(filename string) (string, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
doc, err := docx.Parse(f, fi.Size())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString(fmt.Sprintf("# 文件:%s\n\n", filename))
|
||||
|
||||
for _, item := range doc.Document.Body.Items {
|
||||
switch v := item.(type) {
|
||||
case *docx.Paragraph:
|
||||
// 直接用 fmt.Sprint 利用庫的 Stringer
|
||||
text := fmt.Sprint(v)
|
||||
text = strings.TrimSpace(text)
|
||||
if text != "" {
|
||||
sb.WriteString(text + "\n\n")
|
||||
}
|
||||
|
||||
case *docx.Table:
|
||||
sb.WriteString("## 表格\n")
|
||||
|
||||
// 先印表頭(可選)
|
||||
sb.WriteString("| ")
|
||||
|
||||
// 假設第一行是表頭(很多文件如此),或全部當內容
|
||||
for i, row := range v.TableRows {
|
||||
var cells []string
|
||||
for _, cell := range row.TableCells {
|
||||
// 這裡是重點:cell 本身沒有 String(),但可以遍歷它的 Paragraphs
|
||||
var cellText strings.Builder
|
||||
for _, p := range cell.Paragraphs {
|
||||
cellText.WriteString(fmt.Sprint(p))
|
||||
cellText.WriteString(" ")
|
||||
}
|
||||
cells = append(cells, strings.TrimSpace(cellText.String()))
|
||||
}
|
||||
|
||||
sb.WriteString(strings.Join(cells, " | "))
|
||||
sb.WriteString(" |\n")
|
||||
|
||||
// 如果想加 markdown 表頭分隔線(只在第一行後加)
|
||||
if i == 0 {
|
||||
sb.WriteString("| " + strings.Repeat("--- | ", len(cells)) + "\n")
|
||||
}
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
|
||||
default:
|
||||
// 忽略圖片、頁首等
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
func main1() {
|
||||
// 測試用
|
||||
prompt, err := docxToStructuredPrompt("D:\\myDocument\\tencent\\weChat\\WeChat Files\\wxid_pv6rg3z2l28y22\\FileStorage\\File\\2026-01\\(改)小班体育活动《蚂蚁运粮》(泉秀实幼吴思莹).docx")
|
||||
if err != nil {
|
||||
fmt.Println("錯誤:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println(prompt)
|
||||
}
|
||||
Reference in New Issue
Block a user