prompt.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "shudao-chat-go/utils"
  5. "github.com/beego/beego/v2/server/web"
  6. )
  7. type PromptController struct {
  8. web.Controller
  9. }
  10. // BuildExamPrompt handles frontend requests to build exam prompts on the server
  11. func (c *PromptController) BuildExamPrompt() {
  12. var req utils.ExamPromptRequest
  13. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  14. c.Data["json"] = map[string]interface{}{
  15. "statusCode": 400,
  16. "msg": "JSON解析失败: " + err.Error(),
  17. }
  18. c.ServeJSON()
  19. return
  20. }
  21. prompt, err := utils.BuildExamPrompt(req)
  22. if err != nil {
  23. c.Data["json"] = map[string]interface{}{
  24. "statusCode": 500,
  25. "msg": "生成提示词失败: " + err.Error(),
  26. }
  27. c.ServeJSON()
  28. return
  29. }
  30. c.Data["json"] = map[string]interface{}{
  31. "statusCode": 200,
  32. "msg": "success",
  33. "data": map[string]string{
  34. "prompt": prompt,
  35. },
  36. }
  37. c.ServeJSON()
  38. }
  39. // BuildSingleQuestionPrompt handles requests for regenerating a single question prompt
  40. func (c *PromptController) BuildSingleQuestionPrompt() {
  41. var req utils.SingleQuestionPromptRequest
  42. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  43. c.Data["json"] = map[string]interface{}{
  44. "statusCode": 400,
  45. "msg": "JSON解析失败: " + err.Error(),
  46. }
  47. c.ServeJSON()
  48. return
  49. }
  50. prompt, err := utils.BuildSingleQuestionPrompt(req)
  51. if err != nil {
  52. c.Data["json"] = map[string]interface{}{
  53. "statusCode": 500,
  54. "msg": "生成提示词失败: " + err.Error(),
  55. }
  56. c.ServeJSON()
  57. return
  58. }
  59. c.Data["json"] = map[string]interface{}{
  60. "statusCode": 200,
  61. "msg": "success",
  62. "data": map[string]string{
  63. "prompt": prompt,
  64. },
  65. }
  66. c.ServeJSON()
  67. }