gpt4free源码分析
字数 1118 2025-08-19 12:42:28
GPT4Free源码分析与使用指南
项目概述
GPT4Free是一个开源项目,允许用户免费使用GPT模型(包括GPT-3.5和GPT-4)的接口。该项目通过分析公开可用的GPT服务的安全漏洞和认证机制,实现了绕过认证直接使用这些服务的目的。
项目地址:https://github.com/xtekky/gpt4free
支持的接口
GPT-3.5接口
- Bing
- ChatgptAi
- GptGo
- GeekGpt
- You
- Chatgpt4Online
GPT-4接口
- 项目中也包含GPT-4的接口实现
安装与运行
Web界面运行
python .\g4f\gui\run.py
默认使用GPT-3.5模型,切换到GPT-4可能会失败
Python包使用方式
import g4f
g4f.debug.logging = False # 禁用调试日志
g4f.debug.check_version = False # 禁用自动版本检查
# 流式响应
response = g4f.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "昨天的当天是明天的什么"}],
stream=True,
)
for message in response:
print(message, flush=True, end='')
# GPT-4接口使用
response = g4f.ChatCompletion.create(
model=g4f.models.gpt_4,
messages=[{"role": "user", "content": "昨天的当天是明天的什么"}],
)
print(response)
技术实现分析
整体架构
- 前端:基于Flask框架
- 后端API接口:
/backend-api/v2/conversation - 默认模型配置:
default = Model(
name="",
base_provider="",
best_provider=RetryProvider([
Bing,
ChatgptAi, GptGo, GeekGpt,
You,
Chatgpt4Online
])
)
各接口实现机制
1. Bing接口实现
实现步骤:
- 获取
sec_access_token - 建立WebSocket连接
- 发送请求并接收响应
关键代码:
# 获取token和其他参数
url1 = 'https://www.bing.com/turing/conversation/create?bundleVersion=1.1467.3'
response = session.get(url1,headers=headers)
data = json.loads(response.text)
conversationId = data.get('conversationId')
clientId = data.get('clientId')
conversationSignature = response.headers.get('X-Sydney-Encryptedconversationsignature')
# WebSocket连接
async with session.ws_connect('wss://sydney.bing.com/sydney/ChatHub',
autoping=False,
params={'sec_access_token': conversation.conversationSignature},
proxy=proxy) as wss:
await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
await wss.receive(timeout=900)
await wss.send_str(create_message(conversation, prompt, tone, context, web_search, gpt4_turbo))
2. Chatgpt4Online接口实现
实现步骤:
- 获取
_wpnonce值 - 构造请求发送到API接口
关键代码:
# 获取_wpnonce
response = requests.get(f"{url}/",headers=headers)
result = re.search(r'restNonce":"(.*?)"', response.text)
_wpnonce = result.group(1)
# 发送请求
response = requests.post(
"https://chatgpt4online.org/wp-json/mwai-ui/v1/chats/submit",
json=data,
headers={
"Content-Type":"application/json",
"x-wp-nonce": _wpnonce
}
)
3. GptGo接口实现
实现步骤:
- 获取token
- 使用token查询结果
关键代码:
# 获取token
response = session.post(
"https://gptgo.ai/get_token.php",
data={"ask": message},
headers=headers
)
token = base64.b64decode(response.text[10:-20]).decode()
# 查询结果
response = session.get(
"https://api.gptgo.ai/web.php",
params={"array_chat": token},
headers=headers
)
4. GeekGpt接口实现
实现特点:
- 每个用户有一定初始额度
- 使用特定token访问
关键代码:
headers = {
'authorization': 'Bearer pk-this-is-a-real-free-pool-token-for-everyone',
'content-type': 'application/json',
}
response = requests.post("https://ai.fakeopen.com/v1/chat/completions",
headers=headers, data=data, stream=True)
5. You接口实现
实现特点:
- 使用
curl_cffi库绕过Akamai防护 - 访问
/api/streamingSearch接口
关键代码:
# 使用curl_cffi绕过检测
from curl_cffi import requests
response = requests.get('https://tls.browserleaks.com/json',
headers=headers,
impersonate="chrome110")
反爬虫绕过技术
Akamai防护绕过
Akamai提供的内容传输网络(CDN)和网络安全解决方案会检测客户端特征,项目使用curl_cffi库生成浏览器指纹信息来绕过检测。
对比测试:
- 浏览器访问:
akamai_hash和akamai_text字段有值 - 普通requests访问:这两个字段为空
- 使用curl_cffi访问:能生成正确的指纹信息
from curl_cffi import requests
response = requests.get('https://tls.browserleaks.com/json',
headers=headers,
impersonate="chrome110")
参考资源
注意事项
- 部分接口需要特定网络环境才能访问
- 接口稳定性不一,可能会被服务提供商检测和封锁
- 使用这些接口可能违反服务条款,请谨慎使用
- 国内IP访问某些接口可能会失败
- 部分接口有使用频率限制或初始额度限制
通过分析GPT4Free项目的实现,我们可以学习到如何逆向分析各类GPT服务的API接口,以及如何绕过常见的反爬虫机制。这些技术不仅适用于GPT服务,也可以应用于其他类似场景的API分析和调用。