ai.go 5.4 KB

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