linweiyuan / go-chatgpt-api

一个尝试绕过 Cloudflare 来使用 ChatGPT 接口的程序
MIT License
1.43k stars 449 forks source link

如何模拟 https://api.openai.com/v1/models接口呢 #283

Closed DanielZhao1990 closed 8 months ago

DanielZhao1990 commented 8 months ago

`import openai

openai.api_key = "" openai.api_base = "https://go-chatgpt-api-1y7c.onrender.com/chatgpt/backend-api/"

print(openai.Model.list()) ` 似乎不太一致,无法成功返回

linweiyuan commented 8 months ago

api key 应该走 platform 接口,参考 example 文件夹 platform.http 里面的例子

DanielZhao1990 commented 8 months ago

找到问题了,代理接口返回的数据,没有返回Content-Type, 直接返回了taxt,而openai标准库是使用这个进行解析的,最终导致无法解析报. 增加了一行代码解决了:c.Writer.Header().Set("Content-Type", resp.Header.Get("Content-Type")) ` defer resp.Body.Close() if resp.StatusCode != http.StatusOK { if resp.StatusCode == http.StatusUnauthorized { logger.Error(fmt.Sprintf(AccountDeactivatedErrorMessage, c.GetString(EmailKey))) }

    responseMap := make(map[string]interface{})
    json.NewDecoder(resp.Body).Decode(&responseMap)
    c.AbortWithStatusJSON(resp.StatusCode, responseMap)
    return
}
c.Writer.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
io.Copy(c.Writer, resp.Body)

} `