package api import ( "api2gpt-mid/model" "bytes" "encoding/json" "io/ioutil" "net/http" ) // 余额消费 func BalanceConsumption(key string, modelStr string, prompt_tokens int, completion_tokens int, total_tokens int, msg_id string) (string, error) { var data = model.Consumption{ SecretKey: key, Model: modelStr, MsgId: msg_id, PromptTokens: prompt_tokens, CompletionTokens: completion_tokens, TotalTokens: total_tokens, } jsonData, err := json.Marshal(data) // 构造post请求的body reqBody := bytes.NewBuffer(jsonData) url := "http://172.17.0.1:8080/other/usageRecord" req2, err := http.NewRequest("POST", url, reqBody) // 设置http请求的header req2.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req2) if err != nil { return "", err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } return string(body), nil }