aiSearch_v1_chat.go 4.0 KB

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