feat 拆出qgdzs
This commit is contained in:
156
internal/grpc_server/server/server_question.go
Normal file
156
internal/grpc_server/server/server_question.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/redis"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
||||
"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"
|
||||
"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
|
||||
// }]
|
||||
//`}
|
||||
|
||||
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 *grpc_pb.GenerateQuestionReq) (*grpc_pb.GenerateQuestionResp, error) {
|
||||
category, err := repository.NewQuestionDao(ctx).FindCategory()
|
||||
if err != nil {
|
||||
log.Errorf("GenerateQuestion FindCategory error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
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"), category),
|
||||
fmt.Sprintf(prompt[1], req.Num, req.Category),
|
||||
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 {
|
||||
log.Errorf("RequestAI json.Unmarshal error: %v, data: %v", err, content)
|
||||
return err
|
||||
}
|
||||
question = append(question, step...)
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.Errorf("RequestAI error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
questionDao := repository.NewQuestionDao(ctx, redis.GetCacheClient())
|
||||
for _, q := range question {
|
||||
marshal, _ := json.Marshal(q.Options)
|
||||
if err = questionDao.Create(&model.Question{
|
||||
Question: q.Question,
|
||||
Options: string(marshal),
|
||||
Answer: q.Answer,
|
||||
Explanation: q.Explanation,
|
||||
Category: q.Category,
|
||||
Difficulty: q.Difficulty,
|
||||
}); err != nil {
|
||||
log.Errorf("GenerateQuestion Create error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *Server) GetQuestion(ctx context.Context, req *grpc_pb.GetQuestionReq) (*grpc_pb.GetQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx).FindByRandom()
|
||||
if err != nil {
|
||||
log.Errorf("GetQuestion error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
log.Errorf("GetQuestion json.Unmarshal error: %v, data: %v", err, question.Options)
|
||||
return nil, err
|
||||
}
|
||||
return &grpc_pb.GetQuestionResp{
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) AnswerQuestion(ctx context.Context, req *grpc_pb.AnswerQuestionReq) (*grpc_pb.AnswerQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx).FindBySn(req.Sn)
|
||||
if err != nil {
|
||||
log.Errorf("AnswerQuestion error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
options := make([]string, 0)
|
||||
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
|
||||
log.Errorf("AnswerQuestion json.Unmarshal error: %v, data: %v", err, question.Options)
|
||||
return nil, err
|
||||
}
|
||||
return &grpc_pb.AnswerQuestionResp{
|
||||
Answer: question.Answer,
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user