108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
package handler
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"tencent_ocr/pkg/service"
|
||
)
|
||
|
||
type RateHandler struct {
|
||
geminiService *service.GeminiService
|
||
}
|
||
|
||
func NewRateHandler(geminiService *service.GeminiService) *RateHandler {
|
||
return &RateHandler{
|
||
geminiService: geminiService,
|
||
}
|
||
}
|
||
|
||
type RateRequest struct {
|
||
Text string `json:"text" binding:"required"`
|
||
Criteria string `json:"criteria"`
|
||
WritingRequirement string `json:"writing_requirement"`
|
||
}
|
||
|
||
type RateResponse struct {
|
||
Rate int `json:"rate"`
|
||
Summary string `json:"summary"`
|
||
DetailedReview string `json:"detailed_review"`
|
||
Success bool `json:"success"`
|
||
Result string `json:"result"`
|
||
}
|
||
|
||
func (h *RateHandler) HandleRate(c *gin.Context) {
|
||
var req RateRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, RateResponse{
|
||
Success: false,
|
||
Result: "Invalid request format",
|
||
})
|
||
return
|
||
}
|
||
|
||
// Process with Gemini
|
||
criteria := req.Criteria
|
||
if criteria == "" {
|
||
criteria = `你是一名语文老师。你正在给学生的作文打分。根据以下中考作文评分标准,给作文打分。
|
||
## 评分总分值:100分。
|
||
### 88-100分 符合题意;写作目的和对象明确;思考充分,立意深刻,感情真挚;选材精当,内容充实;中心突出,条理清晰;表达准确,语言流畅。
|
||
### 75-87分 符合题意;写作目的和对象较明确;思考较充分,立意清楚,感情真实;选材合理,内容具体;中心明确,有一定条理;表达较准确,语言通畅。
|
||
### 60-74分 符合题意;写作目的和对象较模糊;有一定思考,感情真实;有一定内容;结构基本完整;语言尚通畅。
|
||
### 60分以下 不符合题意;缺乏写作目的和对象;基本没有思考,感情虚假;内容空洞;结构混乱;不成篇。`
|
||
}
|
||
writingRequirement := req.WritingRequirement
|
||
if writingRequirement == "" {
|
||
writingRequirement = `写一篇不少于600字的作文,体裁不限,题目自拟。`
|
||
}
|
||
format := `请按照以下JSON格式输出:
|
||
{
|
||
"rate": 分数, // 最多100分制的分数,int类型
|
||
"summary": "总体评价", // 100字以内的总体评价,string类型
|
||
"detailed_review": "详细点评" // 300字以内的详细点评,包含优点和建议,string类型
|
||
}`
|
||
|
||
prompt := "作文要求:\n" + writingRequirement + "\n\n" + "评分标准:\n" + criteria + format + "\n\n" + "作文内容:\n" + req.Text
|
||
result, err := h.geminiService.ProcessText(c.Request.Context(), prompt)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, RateResponse{
|
||
Success: false,
|
||
Result: "Text processing failed",
|
||
})
|
||
return
|
||
}
|
||
|
||
// 去除\n \t
|
||
result = strings.ReplaceAll(result, "\n", "")
|
||
result = strings.ReplaceAll(result, "\t", "")
|
||
result = strings.ReplaceAll(result, "```json", "")
|
||
result = strings.ReplaceAll(result, "```", "")
|
||
result = strings.TrimSpace(result)
|
||
//检查是否json
|
||
if !strings.HasPrefix(result, "{") || !strings.HasSuffix(result, "}") {
|
||
c.JSON(http.StatusOK, RateResponse{
|
||
Success: false,
|
||
Result: "Invalid JSON format",
|
||
})
|
||
return
|
||
}
|
||
// 解析json
|
||
err = json.Unmarshal([]byte(result), &resp)
|
||
if err != nil {
|
||
c.JSON(http.StatusOK, RateResponse{
|
||
Success: false,
|
||
Result: "Invalid JSON format2",
|
||
})
|
||
return
|
||
}
|
||
if resp.Rate <=0 || resp.Rate > 100 || resp.Summary == "" || resp.DetailedReview == "" {
|
||
c.JSON(http.StatusOK, RateResponse{
|
||
Success: false,
|
||
Result: "Invalid JSON format3",
|
||
})
|
||
return
|
||
}
|
||
|
||
return resp
|
||
}
|