123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package aiser
- import (
- "encoding/json"
- "errors"
- "eta_gn/eta_api/models/aimod"
- "eta_gn/eta_api/utils"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- )
- func ChatAutoMsg(prompt string, historyChatList []aimod.HistoryChat, model string) (result string, err error) {
- chatUrl := utils.EtaAiUrl + `chat/auto_msg`
- param := make(map[string]interface{})
- param["Prompt"] = prompt
- param["HistoryChatList"] = historyChatList
- param["model"] = model
- postData, err := json.Marshal(param)
- if err != nil {
- return result, err
- }
- client := &http.Client{}
- //提交请求
- reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
- businessCode := utils.BusinessCode
- if businessCode == "" {
- return businessCode, errors.New("未获取到商户号")
- }
- nonce := utils.GetRandStringNoSpecialChar(16)
- timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
- signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
- //增加header选项
- reqest.Header.Add("BusinessCode", businessCode)
- reqest.Header.Add("Nonce", nonce)
- reqest.Header.Add("Timestamp", timestamp)
- reqest.Header.Add("Appid", utils.EtaAppid)
- reqest.Header.Add("Signature", signature)
- reqest.Header.Set("Content-Type", "application/json")
- utils.FileLog.Info("postData:" + string(postData))
- response, err := client.Do(reqest)
- if err != nil {
- return
- }
- defer response.Body.Close()
- body, err := ioutil.ReadAll(response.Body)
- utils.FileLog.Info("result:" + string(body))
- resp := new(ChatAutoMsgResp)
- err = json.Unmarshal(body, &resp)
- if err != nil {
- return result, err
- }
- if resp.Ret != 200 {
- return resp.Msg, nil
- }
- result = resp.Data
- return result, nil
- }
- func OpenAiFileUpload(fileUrl, fileName, model string) (result *OpenAiFileUploadResp, err error) {
- chatUrl := utils.EtaAiUrl + `chat/file/upload`
- param := make(map[string]interface{})
- param["FileUrl"] = fileUrl
- param["FileName"] = fileName
- param["Model"] = model
- postData, err := json.Marshal(param)
- if err != nil {
- return result, err
- }
- client := &http.Client{}
- //提交请求
- reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
- businessCode := utils.BusinessCode
- if businessCode == "" {
- return nil, errors.New("未获取到商户号")
- }
- nonce := utils.GetRandStringNoSpecialChar(16)
- timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
- signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
- //增加header选项
- reqest.Header.Add("BusinessCode", businessCode)
- reqest.Header.Add("Nonce", nonce)
- reqest.Header.Add("Timestamp", timestamp)
- reqest.Header.Add("Appid", utils.EtaAppid)
- reqest.Header.Add("Signature", signature)
- reqest.Header.Set("Content-Type", "application/json")
- utils.FileLog.Info("postData:" + string(postData))
- response, err := client.Do(reqest)
- if err != nil {
- return
- }
- defer response.Body.Close()
- body, err := ioutil.ReadAll(response.Body)
- utils.FileLog.Info("result:" + string(body))
- resp := new(OpenAiFileUploadResp)
- err = json.Unmarshal(body, &resp)
- if err != nil {
- return result, err
- }
- return resp, nil
- }
- func FileRetrieve(assistantId, threadId, model string, historyChatList []aimod.HistoryChat, OpenaiFileId []string) (result *OpenAiFileRetrieveResp, err error) {
- chatUrl := utils.EtaAiUrl + `chat/file/retrieve`
- param := make(map[string]interface{})
- param["FileRetrieveList"] = historyChatList
- param["OpenaiFileId"] = OpenaiFileId
- param["AssistantId"] = assistantId
- param["ThreadId"] = threadId
- param["Model"] = model
- postData, err := json.Marshal(param)
- if err != nil {
- return result, err
- }
- client := &http.Client{}
- //提交请求
- reqest, err := http.NewRequest("POST", chatUrl, strings.NewReader(string(postData)))
- businessCode := utils.BusinessCode
- if businessCode == "" {
- return nil, errors.New("未获取到商户号")
- }
- nonce := utils.GetRandStringNoSpecialChar(16)
- timestamp := time.Now().Format(utils.FormatDateTimeUnSpace)
- signature := utils.GetSign(nonce, timestamp, utils.EtaAppid, utils.EtaSecret)
- //增加header选项
- reqest.Header.Add("BusinessCode", businessCode)
- reqest.Header.Add("Nonce", nonce)
- reqest.Header.Add("Timestamp", timestamp)
- reqest.Header.Add("Appid", utils.EtaAppid)
- reqest.Header.Add("Signature", signature)
- reqest.Header.Set("Content-Type", "application/json")
- utils.FileLog.Info("postData:" + string(postData))
- response, err := client.Do(reqest)
- if err != nil {
- return
- }
- defer response.Body.Close()
- body, err := ioutil.ReadAll(response.Body)
- if err != nil {
- return
- }
- utils.FileLog.Info("result:" + string(body))
- resp := new(OpenAiFileRetrieveResp)
- err = json.Unmarshal(body, &resp)
- if err != nil {
- return result, err
- }
- return resp, nil
- }
- type ChatAutoMsgResp struct {
- Ret int
- Data string
- Msg string
- }
- type OpenAiFileUploadResp struct {
- Ret int
- Data *OpenAiFile
- Msg string
- }
- type OpenAiFile struct {
- Bytes int `json:"bytes"`
- CreatedAt int64 `json:"created_at"`
- ID string `json:"id"`
- FileName string `json:"filename"`
- Object string `json:"object"`
- Status string `json:"status"`
- Purpose string `json:"purpose"`
- StatusDetails string `json:"status_details"`
- }
- type OpenAiFileRetrieveResp struct {
- Ret int
- Data *FileRetrieveResp
- Msg string
- ErrMsg string
- }
- type FileRetrieveResp struct {
- AssistantId string `description:"助手ID"`
- ThreadId string `description:"进程id"`
- Answer string
- }
|