ai.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package aiser
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/aimod"
  6. "eta_gn/eta_api/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "github.com/PuerkitoBio/goquery"
  12. )
  13. func QueryAskContent(ask string) (result string, err error) {
  14. if ask == "" {
  15. return "", errors.New("ask is empty")
  16. }
  17. doc, err := goquery.NewDocumentFromReader(strings.NewReader(ask))
  18. if err != nil {
  19. return
  20. }
  21. var text string
  22. doc.Find("p").Each(func(i int, s *goquery.Selection) {
  23. if _, ok := s.Attr("data-f-id"); !ok {
  24. text += s.Text()
  25. }
  26. })
  27. tmpText := []rune(text)
  28. if len(tmpText) > 80 {
  29. result = string(tmpText[:80])
  30. } else {
  31. result = text
  32. }
  33. if result == "" {
  34. askLen := len([]rune(ask))
  35. if askLen > 80 {
  36. result = string([]rune(ask))
  37. } else {
  38. result = ask
  39. }
  40. }
  41. return
  42. }
  43. func FormatAskContent(ask string) (result string, err error) {
  44. doc, err := goquery.NewDocumentFromReader(strings.NewReader(ask))
  45. if err != nil {
  46. return
  47. }
  48. doc.Find("p[data-f-id]").Remove()
  49. result, err = doc.Html()
  50. result = strings.ReplaceAll(result, "<html>", "")
  51. result = strings.ReplaceAll(result, "</html>", "")
  52. result = strings.ReplaceAll(result, "<head>", "")
  53. result = strings.ReplaceAll(result, "</head>", "")
  54. result = strings.ReplaceAll(result, "<body>", "")
  55. result = strings.ReplaceAll(result, "</body>", "")
  56. return
  57. }
  58. func ChatAutoMsg(prompt string, historyChatList []aimod.HistoryChat, model string) (result string, err error) {
  59. chatUrl := utils.EtaAiUrl + `chat/auto_msg`
  60. param := make(map[string]interface{})
  61. param["Prompt"] = prompt
  62. param["HistoryChatList"] = historyChatList
  63. param["model"] = model
  64. postData, err := json.Marshal(param)
  65. if err != nil {
  66. return result, err
  67. }
  68. client := &http.Client{}
  69. //提交请求
  70. reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
  71. businessCode := utils.BusinessCode
  72. if businessCode == "" {
  73. return businessCode, errors.New("未获取到商户号")
  74. }
  75. nonce := utils.GetRandStringNoSpecialChar(16)
  76. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  77. signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
  78. //增加header选项
  79. reqest.Header.Add("BusinessCode", businessCode)
  80. reqest.Header.Add("Nonce", nonce)
  81. reqest.Header.Add("Timestamp", timestamp)
  82. reqest.Header.Add("Appid", utils.EtaAppid)
  83. reqest.Header.Add("Signature", signature)
  84. reqest.Header.Set("Content-Type", "application/json")
  85. utils.FileLog.Info("postData:" + string(postData))
  86. response, err := client.Do(reqest)
  87. if err != nil {
  88. return
  89. }
  90. defer response.Body.Close()
  91. body, err := ioutil.ReadAll(response.Body)
  92. utils.FileLog.Info("result:" + string(body))
  93. resp := new(ChatAutoMsgResp)
  94. err = json.Unmarshal(body, &resp)
  95. if err != nil {
  96. return result, err
  97. }
  98. if resp.Ret != 200 {
  99. return resp.Msg, nil
  100. }
  101. result = resp.Data
  102. return result, nil
  103. }
  104. func OpenAiFileUpload(fileUrl, fileName, model string) (result *OpenAiFileUploadResp, err error) {
  105. chatUrl := utils.EtaAiUrl + `chat/file/upload`
  106. param := make(map[string]interface{})
  107. param["FileUrl"] = fileUrl
  108. param["FileName"] = fileName
  109. param["Model"] = model
  110. postData, err := json.Marshal(param)
  111. if err != nil {
  112. return result, err
  113. }
  114. client := &http.Client{}
  115. //提交请求
  116. reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
  117. businessCode := utils.BusinessCode
  118. if businessCode == "" {
  119. return nil, errors.New("未获取到商户号")
  120. }
  121. nonce := utils.GetRandStringNoSpecialChar(16)
  122. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  123. signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
  124. //增加header选项
  125. reqest.Header.Add("BusinessCode", businessCode)
  126. reqest.Header.Add("Nonce", nonce)
  127. reqest.Header.Add("Timestamp", timestamp)
  128. reqest.Header.Add("Appid", utils.EtaAppid)
  129. reqest.Header.Add("Signature", signature)
  130. reqest.Header.Set("Content-Type", "application/json")
  131. utils.FileLog.Info("postData:" + string(postData))
  132. response, err := client.Do(reqest)
  133. if err != nil {
  134. return
  135. }
  136. defer response.Body.Close()
  137. body, err := ioutil.ReadAll(response.Body)
  138. utils.FileLog.Info("result:" + string(body))
  139. resp := new(OpenAiFileUploadResp)
  140. err = json.Unmarshal(body, &resp)
  141. if err != nil {
  142. return result, err
  143. }
  144. return resp, nil
  145. }
  146. func FileRetrieve(assistantId, threadId, model string, historyChatList []aimod.HistoryChat, OpenaiFileId []string) (result *OpenAiFileRetrieveResp, err error) {
  147. chatUrl := utils.EtaAiUrl + `chat/file/retrieve`
  148. param := make(map[string]interface{})
  149. param["FileRetrieveList"] = historyChatList
  150. param["OpenaiFileId"] = OpenaiFileId
  151. param["AssistantId"] = assistantId
  152. param["ThreadId"] = threadId
  153. param["Model"] = model
  154. postData, err := json.Marshal(param)
  155. if err != nil {
  156. return result, err
  157. }
  158. client := &http.Client{}
  159. //提交请求
  160. reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
  161. businessCode := utils.BusinessCode
  162. if businessCode == "" {
  163. return nil, errors.New("未获取到商户号")
  164. }
  165. nonce := utils.GetRandStringNoSpecialChar(16)
  166. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  167. signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
  168. //增加header选项
  169. reqest.Header.Add("BusinessCode", businessCode)
  170. reqest.Header.Add("Nonce", nonce)
  171. reqest.Header.Add("Timestamp", timestamp)
  172. reqest.Header.Add("Appid", utils.EtaAppid)
  173. reqest.Header.Add("Signature", signature)
  174. reqest.Header.Set("Content-Type", "application/json")
  175. utils.FileLog.Info("postData:" + string(postData))
  176. response, err := client.Do(reqest)
  177. if err != nil {
  178. return
  179. }
  180. defer response.Body.Close()
  181. body, err := ioutil.ReadAll(response.Body)
  182. if err != nil {
  183. return
  184. }
  185. utils.FileLog.Info("result:" + string(body))
  186. resp := new(OpenAiFileRetrieveResp)
  187. err = json.Unmarshal(body, &resp)
  188. if err != nil {
  189. return result, err
  190. }
  191. return resp, nil
  192. }
  193. type ChatAutoMsgResp struct {
  194. Ret int
  195. Data string
  196. Msg string
  197. }
  198. type OpenAiFileUploadResp struct {
  199. Ret int
  200. Data *OpenAiFile
  201. Msg string
  202. }
  203. type OpenAiFile struct {
  204. Bytes int `json:"bytes"`
  205. CreatedAt int64 `json:"created_at"`
  206. ID string `json:"id"`
  207. FileName string `json:"filename"`
  208. Object string `json:"object"`
  209. Status string `json:"status"`
  210. Purpose string `json:"purpose"`
  211. StatusDetails string `json:"status_details"`
  212. }
  213. type OpenAiFileRetrieveResp struct {
  214. Ret int
  215. Data *FileRetrieveResp
  216. Msg string
  217. ErrMsg string
  218. }
  219. type FileRetrieveResp struct {
  220. AssistantId string `description:"助手ID"`
  221. ThreadId string `description:"进程id"`
  222. Answer string
  223. }