ai.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package aiser
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/aimod"
  6. "eta/eta_api/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. func ChatAutoMsg(prompt string, historyChatList []aimod.HistoryChat) (result string, err error) {
  13. chatUrl := utils.EtaAiUrl + `chat/auto_msg`
  14. param := make(map[string]interface{})
  15. param["Prompt"] = prompt
  16. param["HistoryChatList"] = historyChatList
  17. postData, err := json.Marshal(param)
  18. if err != nil {
  19. return result, err
  20. }
  21. client := &http.Client{}
  22. //提交请求
  23. reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
  24. businessCode := utils.BusinessCode
  25. if businessCode == "" {
  26. return businessCode, errors.New("未获取到商户号")
  27. }
  28. nonce := utils.GetRandStringNoSpecialChar(16)
  29. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  30. signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
  31. //增加header选项
  32. reqest.Header.Add("BusinessCode", businessCode)
  33. reqest.Header.Add("Nonce", nonce)
  34. reqest.Header.Add("Timestamp", timestamp)
  35. reqest.Header.Add("Appid", utils.EtaAppid)
  36. reqest.Header.Add("Signature", signature)
  37. reqest.Header.Set("Content-Type", "application/json")
  38. utils.FileLog.Info("postData:" + string(postData))
  39. response, err := client.Do(reqest)
  40. if err != nil {
  41. return
  42. }
  43. defer response.Body.Close()
  44. body, err := ioutil.ReadAll(response.Body)
  45. utils.FileLog.Info("result:" + string(body))
  46. resp := new(ChatAutoMsgResp)
  47. err = json.Unmarshal(body, &resp)
  48. if err != nil {
  49. return result, err
  50. }
  51. if resp.Ret != 200 {
  52. return resp.Msg, nil
  53. }
  54. result = resp.Data
  55. return result, nil
  56. }
  57. func OpenAiFileUpload(fileUrl, fileName string) (result *OpenAiFileUploadResp, err error) {
  58. chatUrl := utils.EtaAiUrl + `chat/file/upload`
  59. param := make(map[string]interface{})
  60. param["FileUrl"] = fileUrl
  61. param["FileName"] = fileName
  62. postData, err := json.Marshal(param)
  63. if err != nil {
  64. return result, err
  65. }
  66. client := &http.Client{}
  67. //提交请求
  68. reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
  69. businessCode := utils.BusinessCode
  70. if businessCode == "" {
  71. return nil, errors.New("未获取到商户号")
  72. }
  73. nonce := utils.GetRandStringNoSpecialChar(16)
  74. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  75. signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
  76. //增加header选项
  77. reqest.Header.Add("BusinessCode", businessCode)
  78. reqest.Header.Add("Nonce", nonce)
  79. reqest.Header.Add("Timestamp", timestamp)
  80. reqest.Header.Add("Appid", utils.EtaAppid)
  81. reqest.Header.Add("Signature", signature)
  82. reqest.Header.Set("Content-Type", "application/json")
  83. utils.FileLog.Info("postData:" + string(postData))
  84. response, err := client.Do(reqest)
  85. if err != nil {
  86. return
  87. }
  88. defer response.Body.Close()
  89. body, err := ioutil.ReadAll(response.Body)
  90. utils.FileLog.Info("result:" + string(body))
  91. resp := new(OpenAiFileUploadResp)
  92. err = json.Unmarshal(body, &resp)
  93. if err != nil {
  94. return result, err
  95. }
  96. return resp, nil
  97. }
  98. func FileRetrieve(assistantId, threadId string, historyChatList []aimod.HistoryChat, OpenaiFileId []string) (result *OpenAiFileRetrieveResp, err error) {
  99. chatUrl := utils.EtaAiUrl + `chat/file/retrieve`
  100. param := make(map[string]interface{})
  101. param["FileRetrieveList"] = historyChatList
  102. param["OpenaiFileId"] = OpenaiFileId
  103. param["AssistantId"] = assistantId
  104. param["ThreadId"] = threadId
  105. postData, err := json.Marshal(param)
  106. if err != nil {
  107. return result, err
  108. }
  109. client := &http.Client{}
  110. //提交请求
  111. reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
  112. businessCode := utils.BusinessCode
  113. if businessCode == "" {
  114. return nil, errors.New("未获取到商户号")
  115. }
  116. nonce := utils.GetRandStringNoSpecialChar(16)
  117. timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
  118. signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
  119. //增加header选项
  120. reqest.Header.Add("BusinessCode", businessCode)
  121. reqest.Header.Add("Nonce", nonce)
  122. reqest.Header.Add("Timestamp", timestamp)
  123. reqest.Header.Add("Appid", utils.EtaAppid)
  124. reqest.Header.Add("Signature", signature)
  125. reqest.Header.Set("Content-Type", "application/json")
  126. utils.FileLog.Info("postData:" + string(postData))
  127. response, err := client.Do(reqest)
  128. if err != nil {
  129. return
  130. }
  131. defer response.Body.Close()
  132. body, err := ioutil.ReadAll(response.Body)
  133. if err != nil {
  134. return
  135. }
  136. utils.FileLog.Info("result:" + string(body))
  137. resp := new(OpenAiFileRetrieveResp)
  138. err = json.Unmarshal(body, &resp)
  139. if err != nil {
  140. return result, err
  141. }
  142. return resp, nil
  143. }
  144. type ChatAutoMsgResp struct {
  145. Ret int
  146. Data string
  147. Msg string
  148. }
  149. type OpenAiFileUploadResp struct {
  150. Ret int
  151. Data *OpenAiFile
  152. Msg string
  153. }
  154. type OpenAiFile struct {
  155. Bytes int `json:"bytes"`
  156. CreatedAt int64 `json:"created_at"`
  157. ID string `json:"id"`
  158. FileName string `json:"filename"`
  159. Object string `json:"object"`
  160. Status string `json:"status"`
  161. Purpose string `json:"purpose"`
  162. StatusDetails string `json:"status_details"`
  163. }
  164. type OpenAiFileRetrieveResp struct {
  165. Ret int
  166. Data *FileRetrieveResp
  167. Msg string
  168. ErrMsg string
  169. }
  170. type FileRetrieveResp struct {
  171. AssistantId string `description:"助手ID"`
  172. ThreadId string `description:"进程id"`
  173. Answer string
  174. }