linweiyuan / go-chatgpt-api

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

The settings and invocation methods for the corresponding endpoints of this project. #236

Closed hongyi-zhao closed 1 year ago

hongyi-zhao commented 1 year ago

运行成功:

$ go build
$ PORT=18080 TZ=Asia/Shanghai PROXY=socks5://127.0.0.1:18890 OPENAI_EMAIL="bgrandstaff9@ramons.site" OPENAI_PASSWORD="xxx" ./go-chatgpt-api 
INFO[0000] PROXY: socks5://127.0.0.1:18890   
INFO[0000] Service go-chatgpt-api is ready.  

但是仍有如下问题:

$ curl http://127.0.0.1:18080/platform/v1/chat/completions   -H "Authorization: sk-xxx"   -d '{"model": "gpt-3.5-turbo-16k", "messages": [{"role": "user", "content": "Say this is a test!"}], "stream": true}'
{

"error": {

"message": "You exceeded your current quota, please check your plan and billing details.",

"type": "insufficient_quota",

"param": null,

"code": "insufficient_quota"

}

}

同样的账号,基于 https://github.com/acheong08/ChatGPT-to-API 则没有上述问题:

$ curl http://127.0.0.1:18080/v1/chat/completions -d '{"messages": [{"role": "user", "content": "Say this is a test!"}],"stream": true}'
data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"This"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" is"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" a"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":" test"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{"content":"!"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-QXlha2FBbmROaXhpZUFyZUF3ZXNvbWUK","object":"chat.completion.chunk","created":0,"model":"gpt-3.5-turbo-0301","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}

data: [DONE]

See https://github.com/linweiyuan/go-chatgpt-api/issues/235 for the related discussion.

linweiyuan commented 1 year ago

/imitate/v1/chat/completions

https://github.com/linweiyuan/go-chatgpt-api/blob/main/example/vscode/imitate.http

hongyi-zhao commented 1 year ago

See below:

werner@X10DAi:~$ curl http://127.0.0.1:18080/imitate/v1/chat/completions -d '{"messages": [{"role": "user", "content": "Say this is a test!"}],"stream": true}'
{"errorMessage":"Please provide a valid access token or api key in 'Authorization' header."}

werner@X10DAi:~$ curl http://127.0.0.1:18080/imitate/v1/chat/completions   -H "Authorization: sk-xxx"   -d '{"model": "gpt-3.5-turbo-16k", "messages": [{"role": "user", "content": "Say this is a test!"}], "stream": true}'
{"detail":{"message":"Unauthorized - Access token is missing"}}{"error":"error sending request"}

How to let it retrieve access token automatically and so that I can use it only based on API_URL and API_KEY?

linweiyuan commented 1 year ago

不会自动,账号密码是用来刷新 puid 的(plus用户),不是自动登录

curl -H 'Authorization: 你的 access token'

hongyi-zhao commented 1 year ago

那怎么配合 https://github.com/binary-husky/gpt_academic 使用啊?它的配置文件里面没有 access token 这个项。

linweiyuan commented 1 year ago

如果你要使用官方免费网页版: 用 access token,调用 /chatgpt/backend-api/conversation

如果使用官方收费 api: 用 api key,调用 /platform/v1/chat/completions

如果要免费使用官方收费 api: 用 access token,调用 /imitate/v1/chat/completions

对号入座

hongyi-zhao commented 1 year ago

能否将access token设在环境变量中,从而直接使用对应的模拟处理的 URL 接口地址?

总而言之,我需要一个能直接使用的URL调用接口,它需要的所有的东西,以环境变量在启动程序时提供,或者程序自己在运行过程中解决。而不是需要用户在调用时交互式提供。

linweiyuan commented 1 year ago

IMITATE_ACCESS_TOKEN

hongyi-zhao commented 1 year ago

最好的方案是:用户提供"用户名/密码",程序自己在运行过程中动态检查和更新 access token。 这也是 https://github.com/acheong08/ChatGPT-to-API 目前的实现。

linweiyuan commented 1 year ago

那不继续用他那个,而换到这个项目的理由是什么

这个项目更多的是通用目的考虑,如果填自己的账号密码,那么绑定自己,只能自己用了

其实可以 fork 去魔改,不懂 go 先用其他语言写,再叫 gpt 翻译成 go

hongyi-zhao commented 1 year ago

IMITATE_ACCESS_TOKEN

这样设定了以后,实际上,根本不再需要API_KEY, 但是调用时,形式上的那个 Authorization header 不能少:

werner@X10DAi:~$ curl http://127.0.0.1:18080/imitate/v1/chat/completions   -H "Authorization: ''"   -d '{"model": "gpt-3.5-turbo-16k", "messages": [{"role": "user", "content": "Say this is a test!"}], "stream": true}'
data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{"content":"This"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{"content":" is"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{"content":" a"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{"content":" test"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{"content":"!"},"index":0,"finish_reason":null}]}

data: {"id":"chatcmpl-YzA3YzUxMTUzYzY4NGQzY2ExOTJkMzNmODNmYzA1NGM","object":"chat.completion.chunk","created":1690853654,"model":"gpt-3.5-turbo-0613","choices":[{"delta":{},"index":0,"finish_reason":"stop"}]}

data: [DONE]
hongyi-zhao commented 1 year ago

那不继续用他那个,而换到这个项目的理由是什么

学习和尝试不同的思路。

这个项目更多的是通用目的考虑,如果填自己的账号密码,那么绑定自己,只能自己用了

在实际使用中,只有自己用自己的账号,才是最安全和不容易拥堵的。

其实可以 fork 去魔改,不懂 go 先用其他语言写,再叫 gpt 翻译成 go

赞同。

hongyi-zhao commented 1 year ago

另外,据我测试,这个项目不支持socks5h代理。

linweiyuan commented 1 year ago

其实用第三方程序,都不安全,看何时秋后算账

上面的填了环境变量还要请求头,是一个 bug

hongyi-zhao commented 1 year ago

上面的填了环境变量还要请求头,是一个 bug

谁的bug?官方的还是这个程序的?

linweiyuan commented 1 year ago

上面的填了环境变量还要请求头,是一个 bug

谁的bug?官方的还是这个程序的?

这个程序的 bug,因为这个功能是别人提交的,我自己又用不到,所以改的时候没有测试到

socks5h 还没用过,回头试一下

hongyi-zhao commented 1 year ago

其实用第三方程序,都不安全,看何时秋后算账

严格说,openai本身对用户就是第三方。除非我们每个人能够拥有自己的数据集、算法和相应的配套强大硬件资源来完全进行本地化实现,否则都不能算绝对安全。但是,基本上不会有单独的某个人有这种实力。所以,在这个世界上,第三方是永远不可能避免的。