gptbots_message_logic.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package xiaoice
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/russross/blackfriday"
  8. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  9. "github.com/zeromicro/go-zero/core/errorx"
  10. "io"
  11. "net/http"
  12. "net/url"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "wechat-api/internal/svc"
  17. "wechat-api/internal/types"
  18. "github.com/zeromicro/go-zero/core/logx"
  19. )
  20. type GptbotsMessageLogic struct {
  21. logx.Logger
  22. ctx context.Context
  23. svcCtx *svc.ServiceContext
  24. }
  25. func NewGptbotsMessageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GptbotsMessageLogic {
  26. return &GptbotsMessageLogic{
  27. Logger: logx.WithContext(ctx),
  28. ctx: ctx,
  29. svcCtx: svcCtx}
  30. }
  31. func (l *GptbotsMessageLogic) GptbotsMessage(req *types.MessageReq) (resp *types.BaseDataInfo, err error) {
  32. apikey := l.svcCtx.Config.Xiaoice.GptbotsAuthorization
  33. conversationId, err := l.GetConversation(apikey, strconv.FormatUint(*req.UserId, 10))
  34. if conversationId == nil || err != nil {
  35. return nil, err
  36. }
  37. baseURL, err := url.Parse("https://api.gptbots.ai/v1/conversation/message")
  38. if err != nil {
  39. return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
  40. }
  41. // 构建请求体
  42. requestBody := map[string]string{
  43. "text": *req.Text,
  44. "conversation_id": *conversationId,
  45. "response_mode": "blocking",
  46. }
  47. jsonBody, err := json.Marshal(requestBody)
  48. if err != nil {
  49. return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
  50. }
  51. // 创建HTTP请求
  52. httpReq, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(jsonBody))
  53. if err != nil {
  54. return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
  55. }
  56. // 添加必要的Header信息
  57. httpReq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey))
  58. httpReq.Header.Set("Content-Type", "application/json")
  59. // 创建HTTP客户端并执行请求
  60. client := &http.Client{}
  61. response, err := client.Do(httpReq)
  62. if err != nil {
  63. return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
  64. }
  65. defer func(Body io.ReadCloser) {
  66. err := Body.Close()
  67. if err != nil {
  68. l.Error("生成内容失败: %v", err)
  69. }
  70. }(response.Body)
  71. // 读取和输出响应
  72. body, err := io.ReadAll(response.Body)
  73. if err != nil {
  74. return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
  75. }
  76. // 检查响应状态
  77. if response.StatusCode != http.StatusOK {
  78. //log.Fatalf("请求失败,状态码:%d,响应: %s", response.StatusCode, string(body))
  79. return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败:%d,响应: %s", response.StatusCode, string(body)))
  80. }
  81. // 解析 JSON 响应
  82. var responseMap types.GptbotsMessageResp
  83. if err := json.Unmarshal(body, &responseMap); err != nil {
  84. return nil, errorx.NewDefaultError(fmt.Sprintf("生成内容失败: %+v", err))
  85. }
  86. data := ""
  87. if responseMap.FlowOutput != nil && len(responseMap.FlowOutput) > 0 {
  88. data = TrimHtml(Markdown2Html(responseMap.FlowOutput[0].Content))
  89. }
  90. return &types.BaseDataInfo{Msg: errormsg.Success, Data: data}, nil
  91. }
  92. func (l *GptbotsMessageLogic) GetConversation(apikey string, userId string) (conversationId *string, err error) {
  93. val, _ := l.svcCtx.Rds.HGet(l.ctx, "xiaoice_conversation", userId).Result()
  94. if val == "" {
  95. baseURL, err := url.Parse("https://api.gptbots.ai/v1/conversation")
  96. if err != nil {
  97. return nil, err
  98. }
  99. // 构建请求体
  100. requestBody := map[string]string{
  101. "user_id": userId,
  102. }
  103. jsonBody, err := json.Marshal(requestBody)
  104. if err != nil {
  105. return nil, err
  106. }
  107. // 创建HTTP请求
  108. req, err := http.NewRequest("POST", baseURL.String(), bytes.NewBuffer(jsonBody))
  109. if err != nil {
  110. return nil, err
  111. }
  112. // 添加必要的Header信息
  113. req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apikey))
  114. req.Header.Set("Content-Type", "application/json")
  115. // 创建HTTP客户端并执行请求
  116. client := &http.Client{}
  117. response, err := client.Do(req)
  118. if err != nil {
  119. return nil, err
  120. }
  121. defer func(Body io.ReadCloser) {
  122. err := Body.Close()
  123. if err != nil {
  124. l.Error("创建会话失败败: %v", err)
  125. }
  126. }(response.Body)
  127. // 读取和输出响应
  128. body, err := io.ReadAll(response.Body)
  129. if err != nil {
  130. return nil, err
  131. }
  132. // 检查响应状态
  133. if response.StatusCode != http.StatusOK {
  134. //log.Fatalf("请求失败,状态码:%d,响应: %s", response.StatusCode, string(body))
  135. return nil, errorx.NewDefaultError(fmt.Sprintf("创建会话失败:%d,响应: %s", response.StatusCode, string(body)))
  136. }
  137. // 解析 JSON 响应
  138. var responseMap types.ConversationResp
  139. if err := json.Unmarshal(body, &responseMap); err != nil {
  140. return nil, err
  141. }
  142. l.svcCtx.Rds.HSet(l.ctx, "xiaoice_conversation", userId, *responseMap.ConversationId)
  143. return responseMap.ConversationId, nil
  144. }
  145. return &val, nil
  146. }
  147. // TrimHtml 去除HTML标签
  148. func TrimHtml(html string) string {
  149. //将HTML标签全转换成小写
  150. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  151. html = re.ReplaceAllStringFunc(html, strings.ToLower)
  152. //去除STYLE
  153. re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
  154. html = re.ReplaceAllString(html, "")
  155. //去除SCRIPT
  156. re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
  157. html = re.ReplaceAllString(html, "")
  158. //去除所有尖括号内的HTML代码,并换成换行符
  159. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  160. html = re.ReplaceAllString(html, "\n")
  161. //去除连续的换行符
  162. re, _ = regexp.Compile("\\s{2,}")
  163. html = re.ReplaceAllString(html, "\n")
  164. return strings.TrimSpace(html)
  165. }
  166. // Markdown2Html Markdown format to HTML format
  167. func Markdown2Html(markdown string) string {
  168. html := blackfriday.MarkdownCommon([]byte(markdown))
  169. return string(html)
  170. }