feat 包命名规范

This commit is contained in:
2026-02-11 14:49:46 +08:00
parent 1a42e055ae
commit 77cc02fc95
12 changed files with 65 additions and 92 deletions

View File

@@ -0,0 +1,124 @@
package grpcserver
import (
"context"
"encoding/json"
"errors"
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
"git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-qgdzs/internal/ai"
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/model"
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
"gorm.io/gorm"
"time"
)
var prompt = []string{`
你是一个微信小游戏的内容策划,我需要生成“每日趣味题”,要求:
- 避免敏感、争议或超纲内容
- 确保选项有迷惑性,解析有趣
- 绝对要避免重复,现在的时间是:%v
- 这些是我目前的分类:%v
- 输出格式为 JSON 数组,不要任何解释、不要Markdown、不要任何其他字符:
[{
"question": "题目文本", // 简洁,30字以内
"options": ["A. 选项1", "B. 选项2", "C. 选项3", "D. 选项4"], // 提供4个选项(A/B/C/D),其中仅1个正确
"answer": "C", // 答案
"explanation": "解析文本", // 200字以内,尽量幽默有趣,不要在文本中说正确和错误,因为不知道用户选的哪个
"category": "分类", // 尽量从上述分类中选择,你也可以增加,但是命名风格要类似
"difficulty": 100, // 难度分 0 - 100
}]
如果你准备好了,请回答”好的“,不要有其他任何字符
`, `
请帮我生成 %v 道,只允许回答 JSON 数组:
`, `
请继续生成 %v 道,只允许回答 JSON 数组:
`, `
请继续生成 %v 道,只允许回答 JSON 数组:
`, `
请继续生成 %v 道,只允许回答 JSON 数组:
`, `
请继续生成 %v 道,只允许回答 JSON 数组:
`}
type Question struct {
Question string `json:"question"` // 题干
Options []string `json:"options"` // 选项
Answer string `json:"answer"` // 答案
Explanation string `json:"explanation"` // 解析
Category string `json:"category"` // 分类
Difficulty int32 `json:"difficulty"` // 难度分
}
func (s *Server) GenerateQuestion(ctx context.Context, req *rspb.GenerateQuestionReq) (*rspb.GenerateQuestionResp, error) {
categoryDao := repository.NewCategoryDao(ctx, s.query, redis.GetCacheClient())
category, err := categoryDao.FindAll()
if err != nil {
return nil, utils.ErrorsWrap(err)
}
categoryName := make([]string, 0)
for _, c := range category {
categoryName = append(categoryName, c.Category)
}
question := make([]*Question, 0)
err = ai.NewAIClient(false, "", 0.9).
RequestAI(
[]string{
fmt.Sprintf(prompt[0], time.Now().Format("2006-01-02 15:04:05"), categoryName),
fmt.Sprintf(prompt[1], req.Num),
fmt.Sprintf(prompt[2], req.Num),
fmt.Sprintf(prompt[3], req.Num),
fmt.Sprintf(prompt[4], req.Num),
fmt.Sprintf(prompt[5], req.Num),
},
func(content string, i int) error {
if i == 0 {
return nil
}
step := make([]*Question, 0)
if err = json.Unmarshal([]byte(content), &step); err != nil {
return utils.ErrorsWrapF(err, "data: %v", content)
}
question = append(question, step...)
return nil
},
)
if err != nil {
return nil, utils.ErrorsWrap(err)
}
questionDao := repository.NewQuestionDao(ctx, s.query, redis.GetCacheClient())
for _, q := range question {
marshal, _ := json.Marshal(q.Options)
categorySn, err := categoryDao.FindSnByName(q.Category)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
cat, err := categoryDao.Create(&model.Category{
Category: q.Category,
})
if err != nil {
log.Errorf("GenerateQuestion CreateCategory error: %v", err)
continue
}
categorySn = cat.Sn
} else {
log.Errorf("GenerateQuestion FindSnByName error: %v", err)
continue
}
}
if _, err = questionDao.Create(&model.Question{
Question: q.Question,
Options: string(marshal),
Answer: q.Answer,
Explanation: q.Explanation,
CategorySn: categorySn,
Difficulty: q.Difficulty,
}); err != nil {
return nil, utils.ErrorsWrap(err)
}
}
return nil, nil
}