package gpt import ( "bytes" "encoding/json" "fmt" "io/ioutil" "net/http" "github.com/eryajf/chatgpt-dingtalk/config" "github.com/eryajf/chatgpt-dingtalk/public/logger" ) const BASEURL = "https://api.openai.com/v1/" // ChatGPTResponseBody 请求体 type ChatGPTResponseBody struct { ID string `json:"id"` Object string `json:"object"` Created int `json:"created"` Model string `json:"model"` Choices []ChoiceItem `json:"choices"` Usage map[string]interface{} `json:"usage"` } type ChoiceItem struct { Text string `json:"text"` Index int `json:"index"` Logprobs int `json:"logprobs"` FinishReason string `json:"finish_reason"` } // ChatGPTRequestBody 响应体 type ChatGPTRequestBody struct { Model string `json:"model"` Prompt string `json:"prompt"` MaxTokens uint `json:"max_tokens"` Temperature float64 `json:"temperature"` } // Completions gtp文本模型回复 //curl https://api.openai.com/v1/completions //-H "Content-Type: application/json" //-H "Authorization: Bearer your chatGPT key" //-d '{"model": "text-davinci-003", "prompt": "give me good song", "temperature": 0, "max_tokens": 7}' func Completions(msg string) (string, error) { cfg := config.LoadConfig() requestBody := ChatGPTRequestBody{ Model: cfg.Model, Prompt: msg, MaxTokens: cfg.MaxTokens, Temperature: cfg.Temperature, } requestData, err := json.Marshal(requestBody) if err != nil { return "", err } logger.Info(fmt.Sprintf("request gtp json string : %v", string(requestData))) req, err := http.NewRequest("POST", BASEURL+"completions", bytes.NewBuffer(requestData)) if err != nil { return "", err } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+cfg.ApiKey) client := &http.Client{Timeout: cfg.SessionTimeout} response, err := client.Do(req) if err != nil { return "", err } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) if err != nil { return "", err } if response.StatusCode != 200 { return "", fmt.Errorf("请求GTP出错了,gtp api status code not equals 200,code is %d ,details: %v ", response.StatusCode, string(body)) } logger.Info(fmt.Sprintf("response gtp json string : %v", string(body))) gptResponseBody := &ChatGPTResponseBody{} err = json.Unmarshal(body, gptResponseBody) if err != nil { return "", err } var reply string if len(gptResponseBody.Choices) > 0 { reply = gptResponseBody.Choices[0].Text } return reply, nil }