aiSearch_v1_chat.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package aiSearch
  2. import (
  3. "aiChat/api/aiSearch/v1"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "strings"
  10. "time"
  11. "github.com/gogf/gf/v2/encoding/gjson"
  12. "github.com/gogf/gf/v2/frame/g"
  13. "github.com/gogf/gf/v2/os/gtime"
  14. )
  15. func (c *ControllerV1) Chat(ctx context.Context, req *v1.ChatReq) (res *v1.ChatRes, err error) {
  16. content := fmt.Sprintf(g.Cfg("ai_search.yaml").MustGet(ctx, "doubaoPrompt").String(), gtime.Now().Format("Ymd"), req.Content)
  17. query, largeModelReply, err := c.doubao(ctx, content)
  18. if err != nil {
  19. content = fmt.Sprintf(g.Cfg("ai_search.yaml").MustGet(ctx, "zhipuPrompt").String(), gtime.Now().Format("Ymd"), req.Content)
  20. query, largeModelReply, err = c.zhipu(ctx, content)
  21. }
  22. log.Println(query, largeModelReply, err)
  23. return nil, nil
  24. }
  25. //调用豆包大模型
  26. func (c *ControllerV1) doubao(ctx context.Context, content string) (string, string, error) {
  27. count, err := g.Redis("main").Incr(ctx, fmt.Sprintf("aiSearch_doubaoCall_%s", gtime.Now().Format("Ymd")))
  28. if err != nil {
  29. g.Log().Error(ctx, "从redis获取doubao调用次数出错", err)
  30. return "", "", err
  31. } else if doubaoCallMax := g.Cfg("ai_search.yaml").MustGet(ctx, "doubaoCallMax").Int64(); count > doubaoCallMax {
  32. g.Log().Info(ctx, "doubao调用次数达到上限", doubaoCallMax, count)
  33. return "", "", errors.New("doubao调用次数达到上限")
  34. }
  35. // 构造请求数据
  36. messages := []map[string]interface{}{}
  37. messages = append(messages, map[string]interface{}{
  38. "role": "user",
  39. "content": content,
  40. })
  41. //glm-4-air glm-4-0520 glm-4-flash
  42. requestData := map[string]interface{}{
  43. "model": "ep-20250207170552-g8dsx",
  44. "temperature": 0.1,
  45. "top_p": 0.7,
  46. "messages": messages,
  47. }
  48. return c.post(ctx, "doubao", "https://ark.cn-beijing.volces.com/api/v3/chat/completions", "3dd861bf-b8a7-41d4-bb0b-5076362c572d", requestData)
  49. }
  50. //调用智普大模型
  51. func (c *ControllerV1) zhipu(ctx context.Context, content string) (string, string, error) {
  52. count, err := g.Redis("main").Incr(ctx, fmt.Sprintf("aiSearch_zhipuCall_%s", gtime.Now().Format("YYYYmmdd")))
  53. if err != nil {
  54. g.Log().Error(ctx, "从redis获取zhipu调用次数出错", err)
  55. return "", "", err
  56. } else if doubaoCallMax := g.Cfg("ai_search.yaml").MustGet(ctx, "zhipuCallMax").Int64(); count > doubaoCallMax {
  57. g.Log().Info(ctx, "zhipu调用次数达到上限", doubaoCallMax, count)
  58. return "", "", errors.New("zhipu调用次数达到上限")
  59. }
  60. // 构造请求数据
  61. messages := []map[string]interface{}{}
  62. messages = append(messages, map[string]interface{}{
  63. "role": "user",
  64. "content": content,
  65. })
  66. //glm-4-air glm-4-0520 glm-4-flash
  67. requestData := map[string]interface{}{
  68. "model": "glm-4-flash",
  69. "messages": messages,
  70. "temperature": 0.1,
  71. "max_tokens": 4096,
  72. }
  73. return c.post(ctx, "zhipu", "https://open.bigmodel.cn/api/paas/v4/chat/completions", "3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR", requestData)
  74. }
  75. func (c *ControllerV1) post(ctx context.Context, t, apiURL, pass string, requestData map[string]interface{}) (string, string, error) {
  76. resp, err := g.Client().Timeout(time.Duration(g.Cfg("ai_search.yaml").MustGet(ctx, "timeout").Int())*time.Second).
  77. SetHeader("Authorization", fmt.Sprintf("Bearer %s", pass)).
  78. ContentType("application/json").
  79. Post(ctx, apiURL, requestData)
  80. if err != nil {
  81. g.Log().Error(ctx, t, "请求出错", err)
  82. return "", "", err
  83. }
  84. defer resp.Body.Close()
  85. b, be := ioutil.ReadAll(resp.Body)
  86. if be != nil {
  87. g.Log().Error(ctx, t, "gjson.LoadJson出错", be)
  88. return "", "", err
  89. }
  90. largeModelReply := string(b)
  91. g.Log().Info(ctx, t, "请求回复", largeModelReply)
  92. r, re := gjson.LoadJson(b)
  93. if re != nil {
  94. g.Log().Error(ctx, t, largeModelReply, "gjson.LoadJson出错", re)
  95. return "", largeModelReply, err
  96. }
  97. content := ""
  98. choices := r.GetJsons("choices")
  99. if len(choices) == 0 {
  100. return "", largeModelReply, err
  101. }
  102. message := choices[0].GetJson("message")
  103. if message == nil {
  104. return "", largeModelReply, err
  105. }
  106. content = message.Get("content").String()
  107. content = strings.ReplaceAll(content, "```json", "")
  108. content = strings.ReplaceAll(content, "```", "")
  109. return content, largeModelReply, nil
  110. }