1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| func chatGPT() { apiKey := "YourAPIkey" apiEndpoint := "https://api.openai.com/v1/engines/gpt-3.5-turbo/completions"
data := `{ "prompt": "你好这是一个用来测试API的信息", "max_tokens": 50 }`
req, err := http.NewRequest("POST", apiEndpoint, bytes.NewBuffer([]byte(data))) if err != nil { fmt.Println("Error creating request:", err) return }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey)
client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending request:", err) return } defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return }
fmt.Println("API Response:", string(body)) }
|