Aitrainee | 公众号:AI进修生
总之 我们很多场景会需要OpenAI API服务的需求(比如我们使用第三方插件如沉浸式翻译、学术gpt等等),储备一些免费获取api的资源项目是不错的选择 ,今天这个就是一个免费调用gpt3.5的项目,后面还会有gpt4free-ts、gpt4free等等
https://github.com/Ai-trainee/FreeGPT35
https://github.com/xtekky/gpt4free
https://github.com/xiangsx/gpt4free-ts
利用免登录 ChatGPT Web 的无限制免费 GPT-3.5-Turbo API 服务
这个项目的简单部署如下:
mkdir
freegpt35
&&
cd
freegpt35
wget
-
O compose
.
yaml https
://
raw
.
githubusercontent
.
com
/
missuo
/
FreeGPT35
/
main
/
compose
.
yaml
docker compose up
-
d
部署完毕后,你可以使用以下命令来测试 API 是否正常运行:
curl http
://
127.0
.
0.1
:
3040
/
v1
/
chat
/
completions \
-
H
"Content-Type: application/json"
\
-
H
"Authorization: Bearer any\_string\_you\_like"
\
-
d
'{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Hello!"
}
],
"stream": true
}'
官方提示 IP 地址可能会导致运行错误,我们一般部署选择:本地 Linux 电脑、Windows WSL或Docker Desktop、或者云服务器上进行部署。
我使用aws(亚马逊云)部署后正常使用,而且速度很快,如果你不知道安装docker等部署前置环境以及获取服务器请阅读我的这两篇文章:
或者你可以加入我们的社群:公众号回复:“群聊”即可。
部署成功后,你可以使用以下方式利用 API:
方式一:使用 Python 脚本调用 API
你可以参考 OpenAI 官方的 API 调用文档编写一个 Python 脚本来使用 API。下面是一个示例脚本,它可以实现多轮对话以及记忆上一轮的信息:
import
requests
import
json
def
chat\_with\_openai
(
api\_url
,
headers
,
model
=
"gpt-3.5-turbo"
):
session\_messages
=
[]
# 用于存储会话历史的列表
while
True
:
# 用户输入
user\_input
=
input
(
"You: "
)
if
user\_input
.
lower
()
==
"exit"
:
# 提供退出对话的选项
print
(
"Exiting chat..."
)
break
# 更新会话历史
session\_messages
.
append
({
"role"
:
"user"
,
"content"
:
user\_input
})
# 构建请求数据
data
=
{
"model"
:
model
,
"messages"
:
session\_messages
,
"stream"
:
False
# 设置为 False 以获取一次性回答
}
# 发送请求并获取响应
response
=
requests
.
post
(
api\_url
,
headers
=
headers
,
json
=
data
)
response\_data
=
response
.
json
()
# 输出模型的回答
bot\_message
=
response\_data
[
'choices'
][
0
][
'message'
][
'content'
]
print
(
"ChatGPT:"
,
bot\_message
)
# 将模型的回答也添加到会话历史中
session\_messages
.
append
({
"role"
:
"system"
,
"content"
:
bot\_message
})
# API URL
url
=
"http://[IP]:3040/v1/chat/completions"
# 请求头
headers
=
{
"Content-Type"
:
"application/json"
,
"Authorization"
:
"Bearer your\_access\_token"
}
# 启动对话
chat\_with\_openai
(
url
,
headers
)
方式二:配合其他开源对话聊天项目
你也可以使用其他开源对话聊天项目来配合使用这个 API。以下是两个示例:
-
- FreeGPT35 Service with ChatGPT-Next-Web :
mkdir
freegpt35
&&
cd
freegpt35
wget
-
O compose
.
yaml https
://
raw
.
githubusercontent
.
com
/
missuo
/
FreeGPT35
/
main
/
compose\_with\_next\_chat
.
yaml
docker compose up
-
d
部署完毕后,你可以直接访问 http://[IP]:3040/v1/chat/completions
使用 API,或者使用 http://[IP]:3000
直接使用 ChatGPT-Next-Web。
-
- FreeGPT35 Service with lobe-chat :
mkdir
freegpt35
&&
cd
freegpt35
wget
-
O compose
.
yaml https
://
raw
.
githubusercontent
.
com
/
missuo
/
FreeGPT35
/
main
/
compose\_with\_lobe\_chat
.
yaml
docker compose up
-
d
部署完毕后,你可以直接访问 http://[IP]:3040/v1/chat/completions
使用 API,或者使用 http://[IP]:3210
直接使用 lobe-chat。
lobe-chat 的设置和上述类似,在登录时选择随机输入字符作为 API key 即可。
— 完 —
点这里👇关注我,记得标星噢