xtekky/gpt4free
GitHub: xtekky/gpt4free
一个聚合多种大语言模型和媒体生成接口的开源项目,提供统一且兼容 OpenAI 格式的 API 访问。
Stars: 65751 | Forks: 13652
# GPT4Free (g4f)
[](https://pypi.org/project/g4f) [](https://hub.docker.com/r/hlohaus789/g4f) [](https://www.gnu.org/licenses/gpl-3.0.txt) [](https://pepy.tech/projects/g4f)
支持本项目,请访问 GitHub Sponsors ❤️
在线演示 & 文档:https://g4f.dev | 文档:https://g4f.dev/docs
GPT4Free (g4f) 是一个社区驱动的项目,它聚合了多个可访问的 Provider(提供者)和接口,旨在让使用现代 LLM 和媒体生成模型变得更加轻松和灵活。GPT4Free 旨在提供多 Provider 支持、本地 GUI、兼容 OpenAI 的 REST API,以及便捷的 Python 和 JavaScript 客户端——一切都在社区优先的许可下进行。 本 README 是一份整合、改进且完整的指南,涵盖了 GPT4Free 的安装、运行和贡献。 目录 - [包含内容](#whats-included) - [快速链接](#quick-links) - [要求与兼容性](#requirements--compatibility) - [安装](#installation) - [Docker (推荐)](#docker-recommended) - [精简 Docker 镜像](#slim-docker-image) - [Windows (.exe)](#windows-exe) - [Python (pip / 源码 / 部分安装)](#python-pip--from-source--partial-installs) - [运行应用](#running-the-app) - [GUI (Web 客户端)](#gui-web-client) - [FastAPI / Interference API](#fastapi--interference-api) - [CLI](#cli) - [可选 Provider 登录 (容器内桌面)](#optional-provider-login-desktop-in-container) - [使用 Python 客户端](#using-the-python-client) - [同步文本示例](#synchronous-text-example) - [图像生成示例](#image-generation-example) - [异步客户端示例](#async-client-example) - [使用 GPT4Free.js (浏览器 JS 客户端)](#using-gpt4freejs-browser-js-client) - [Provider 与模型 (概述)](#providers--models-overview) - [本地推理与媒体](#local-inference--media) - [配置与自定义](#configuration--customization) - [在智能手机上运行](#running-on-smartphone) - [Interference API (兼容 OpenAI)](#interference-api-openai-compatible) - [示例与常见模式](#examples--common-patterns) - [贡献](#contributing) - [如何创建新的 Provider](#how-to-create-a-new-provider) - [AI 如何帮助你编写代码](#how-ai-can-help-you-write-code) - [安全、隐私与下架政策](#security-privacy--takedown-policy) - [致谢、贡献者与归属](#credits-contributors--attribution) - [技术支持 highlights](#powered-by-highlights) - [更新日志与发布](#changelog--releases) - [宣言 / 项目原则](#manifesto--project-principles) - [许可证](#license) - [联系与赞助](#contact--sponsorship) - [附录:快速命令与示例](#appendix-quick-commands--examples) ## 包含内容 - Python 客户端库和异步客户端。 - 可选的本地 Web GUI。 - 基于 FastAPI 的 OpenAI 兼容 API (Interference API)。 - 官方浏览器 JS 客户端 (g4f.dev 分发)。 - Docker 镜像 (完整版和精简版)。 - 多 Provider 适配器 (LLM、媒体 Provider、本地推理后端)。 - 用于图像/音频/视频生成和媒体持久化的工具。 ## 快速链接 - 网站与文档:https://g4f.dev | https://g4f.dev/docs - PyPI:https://pypi.org/project/g4f - Docker 镜像:https://hub.docker.com/r/hlohaus789/g4f - 发布版:https://github.com/xtekky/gpt4free/releases - 问题反馈:https://github.com/xtekky/gpt4free/issues - 社区:Telegram (https://telegram.me/g4f_channel) · Discord 新闻 (https://discord.gg/5E39JUWUFa) · Discord 支持 (https://discord.gg/qXA4Wf4Fsm) ## 要求与兼容性 - 推荐使用 Python 3.10+。 - 对于使用浏览器自动化的 Provider,需要 Google Chrome/Chromium。 - Docker 用于容器化部署。 - 支持 x86_64 和 arm64 (精简镜像支持两者)。 - 某些 Provider 适配器可能需要特定平台的工具 (Chrome/Chromium 等)。有关详细信息,请查阅 Provider 文档。 ## 安装 ### Docker (推荐) 1. 安装 Docker:https://docs.docker.com/get-docker/ 2. 创建持久化目录: - 示例 (Linux/macOS): mkdir -p ${PWD}/har_and_cookies ${PWD}/generated_media sudo chown -R 1200:1201 ${PWD}/har_and_cookies ${PWD}/generated_media 3. 拉取镜像: docker pull hlohaus789/g4f 4. 运行容器: docker run -p 8080:8080 -p 7900:7900 \ --shm-size="2g" \ -v ${PWD}/har_and_cookies:/app/har_and_cookies \ -v ${PWD}/generated_media:/app/generated_media \ hlohaus789/g4f:latest 注意: - 端口 8080 用于 GUI/API;7900 可以暴露一个类似 VNC 的桌面,用于 Provider 登录 (可选)。 - 对于较重的浏览器自动化任务,请增加 --shm-size。 ### 精简 Docker 镜像 (x64 & arm64) ``` mkdir -p ${PWD}/har_and_cookies ${PWD}/generated_media chown -R 1000:1000 ${PWD}/har_and_cookies ${PWD}/generated_media docker run \ -p 1337:8080 -p 8080:8080 \ -v ${PWD}/har_and_cookies:/app/har_and_cookies \ -v ${PWD}/generated_media:/app/generated_media \ hlohaus789/g4f:latest-slim ``` 注意: - 精简镜像可以在启动时更新 g4f 包,并根据需要安装额外的依赖项。 - 在此示例中,Interference API 映射到 1337 端口。 ### Windows 指南 (.exe) 👉 查看 GPT4Free 的 Windows 启动器: 🔗 [https://github.com/gpt4free/g4f.exe](https://github.com/gpt4free/g4f.exe) 🚀 1. 从以下地址下载发布工件 `g4f.exe.zip`: https://github.com/xtekky/gpt4free/releases/latest 2. 解压并运行 `g4f.exe`。 3. 在此处打开 GUI:http://localhost:8080/chat/ 4. 如果 Windows 防火墙阻止访问,请允许该应用程序。 ### Python 安装 (pip / 源码 / 部分安装) 前置条件: - Python 3.10+ (https://www.python.org/downloads/) - 部分 Provider 需要 Chrome/Chromium。 从 PyPI 安装 (推荐): ``` pip install -U g4f[all] ``` 部分安装 - 要仅安装特定功能,请使用可选的扩展组。请参阅项目文档中的 docs/requirements.md。 从源码安装: ``` git clone https://github.com/xtekky/gpt4free.git cd gpt4free pip install -r requirements.txt pip install -e . ``` 注意: - 某些功能需要 Chrome/Chromium 或其他工具;请遵循特定 Provider 的文档。 ## 运行应用 ### GUI (Web 客户端) - 通过 Python 运行: ``` from g4f.gui import run_gui run_gui() ``` - 或通过 CLI: ``` python -m g4f.cli gui --port 8080 --debug ``` - 打开:http://localhost:8080/chat/ ### FastAPI / Interference API - 启动 FastAPI 服务器: ``` python -m g4f --port 8080 --debug ``` - 如果使用精简 docker 映射,Interference API 可能在 `http://localhost:1337/v1` 可用 - Swagger UI:`http://localhost:1337/docs` ### CLI - 启动 GUI 服务器: ``` python -m g4f.cli gui --port 8080 --debug ``` ### MCP Server GPT4Free 现在包含一个模型上下文协议 (MCP) 服务器,允许像 Claude 这样的 AI 助手访问网络搜索、抓取和图像生成功能。 **启动 MCP 服务器 (stdio 模式):** ``` # 使用 g4f 命令 g4f mcp # 或使用 Python 模块 python -m g4f.mcp ``` **启动 MCP 服务器 (HTTP 模式):** ``` # 在端口 8765 启动 HTTP server g4f mcp --http --port 8765 # 自定义 host 和 port g4f mcp --http --host 127.0.0.1 --port 3000 ``` HTTP 模式提供: - `POST http://localhost:8765/mcp` - JSON-RPC 端点 - `GET http://localhost:8765/health` - 健康检查 **配置 Claude Desktop:** 添加到你的 `claude_desktop_config.json`: ``` { "mcpServers": { "gpt4free": { "command": "python", "args": ["-m", "g4f.mcp"] } } } ``` **可用的 MCP 工具:** - `web_search` - 使用 DuckDuckGo 搜索网络 - `web_scrape` - 从网页提取文本内容 - `image_generation` - 根据文本提示词生成图像 有关详细的 MCP 文档,请参阅 [g4f/mcp/README.md](g4f/mcp/README.md) ### 可选 Provider 登录 (容器内桌面) - 访问地址: http://localhost:7900/?autoconnect=1&resize=scale&password=secret - 用于登录基于 Web 的 Provider 以获取 cookies/HAR 文件非常有用。 ## 使用 Python 客户端 安装: ``` pip install -U g4f[all] ``` 同步文本示例: ``` from g4f.client import Client client = Client() response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello, how are you?"}], web_search=False ) print(response.choices[0].message.content) ``` 预期结果: ``` Hello! How can I assist you today? ``` 图像生成示例: ``` from g4f.client import Client client = Client() response = client.images.generate( model="flux", prompt="a white siamese cat", response_format="url" ) print(f"Generated image URL: {response.data[0].url}") ``` 异步客户端示例: ``` from g4f.client import AsyncClient import asyncio async def main(): client = AsyncClient() response = await client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Explain quantum computing briefly"}], ) print(response.choices[0].message.content) asyncio.run(main()) ``` 注意: - 有关流式传输、工具调用模式和高级选项的完整 API 参考,请参阅:https://g4f.dev/docs/client ## 使用 GPT4Free.js (浏览器 JS 客户端) 在浏览器中使用官方 JS 客户端——无需后端。 示例: ``` ``` 注意: - JS 客户端通过 g4f.dev CDN 分发,以便于使用。请查看 CORS 注意事项和使用限制。 ## Provider 与模型 (概述) - GPT4Free 集成了许多 Provider,包括(但不限于)OpenAI 兼容端点、PerplexityLabs、Gemini、MetaAI、Pollinations (媒体) 和本地推理后端。 - 模型可用性和行为取决于 Provider 的能力。有关当前的、受支持的 Provider/模型列表,请参阅 Provider 文档:https://g4f.dev/docs/providers-and-models Provider 要求可能包括: - API 密钥或令牌 (用于经过身份验证的 Provider) - 用于通过浏览器自动化抓取的 Provider 的浏览器 cookies / HAR 文件 - Chrome/Chromium 或无头浏览器工具 - 本地模型二进制文件和运行时 (用于本地推理) ## 本地推理与媒体 - GPT4Free 支持本地推理后端。有关支持的运行时和硬件指导,请参阅 [docs/local.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/local.md)。 - 媒体生成(图像、音频、视频)通过 Provider(例如 Pollinations)支持。有关格式、选项和示例用法,请参阅 [docs/media.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/media.md)。 ## 配置与自定义 - 通过环境变量、CLI 标志或配置文件进行配置。请参阅 [docs/config.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/config.md)。 - 要减少安装大小,请使用部分需求组。请参阅 [docs/requirements.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/requirements.md)。 - Provider 选择:了解如何在 [docs/selecting_a_provider.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/selecting_a_provider.md) 中设置默认值并按请求覆盖。 - 持久化:HAR 文件、cookies 和生成的媒体保存在映射目录中(例如 har_and_cookies、generated_media)。 ## 在智能手机上运行 - Web GUI 是响应式的,可以通过访问主机 IP:8080 或通过隧道从手机访问。请参阅 [docs/guides/phone.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/guides/phone.md)。 ## Interference API (兼容 OpenAI) - Interference API 能够通过 GPT4Free Provider 选择路由类似 OpenAI 的工作流。 - 文档:[docs/interference-api.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/interference-api.md) - 默认端点(示例精简 docker):`http://localhost:1337/v1` - Swagger UI:`http://localhost:1337/docs` ## 示例与常见模式 - 流式补全、停止标准、系统消息和工具调用模式记录于: - [docs/client.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/client.md) - [docs/async_client.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/async_client.md) - [docs/requests.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/requests.md) - 集成 (LangChain, PydanticAI):[docs/pydantic_ai.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/pydantic_ai.md) - 遗留示例:[docs/legacy.mdhttps://github.com/gpt4free/g4f.dev/blob/main/docs/legacy.md) ## 贡献 欢迎贡献——感谢新的 Provider、功能、文档和修复。 如何贡献: 1. Fork 该仓库。 2. 为你的更改创建一个分支。 3. 运行测试和 Linter。 4. 打开一个 Pull Request,包含清晰的描述,并在适用时提供测试/示例。 仓库:https://github.com/xtekky/gpt4free ### 如何创建新的 Provider - 阅读指南:[docs/guides/create_provider.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/guides/create_provider.md) - 典型步骤: - 在 `g4f/Provider/` 中实现 Provider 适配器 - 添加配置和依赖说明 - 包含测试和用例 - 尊重第三方代码许可证并进行适当归属 ### AI 如何帮助你编写代码 - 请参阅:[docs/guides/help_me.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/guides/help_me.md) 获取提示词模板和工作流程,以加速开发。 ## 安全、隐私与下架政策 - 不要存储或共享敏感凭据。使用针对每个 Provider 推荐的安全实践。 - 如果你的网站出现在项目的链接中,并且你希望将其删除,请将所有权证明发送至 takedown@g4f.ai,我们将迅速将其删除。 - 对于生产环境,请使用 HTTPS、身份验证和防火墙规则保护服务器。限制对 Provider 凭据和 cookie/HAR 存储的访问。 ## 致谢、贡献者与归属 - 核心创建者:[@xtekky](https://github.com/xtekky) (原作者),由 [@hlohaus](https://github.com/hlohaus) 维护。 - 完整贡献者图表:https://github.com/xtekky/gpt4free/graphs/contributors - 著名的代码输入和归属: - `har_file.py` — 源自 [xqdoo00o/ChatGPT-to-API](https://github.com/xqdoo00o/ChatGPT-to-API) - `PerplexityLabs.py` — 源自 [nathanrchn/perplexityai](https://github.com/nathanrchn/perplexityai) - `Gemini.py` — 源自 [dsdanielpark/Gemini-API](https://github.com/dsdanielpark/Gemini-API) 和 [HanaokaYuzu/Gemini-API](https://github.com/HanaokaYuzu/Gemini-API) - `MetaAI.py` — 灵感来自 [meta-ai-api by Strvm](https://github.com/Strvm/meta-ai-api) - `proofofwork.py` — 源自 [missuo/FreeGPT35](https://github.com/missuo/FreeGPT35) 仓库中致谢了更多的贡献者。 ## 技术支持 highlights - Pollinations AI — 生成式媒体:https://github.com/pollinations/pollinations - MoneyPrinter V2 — 使用 GPT4Free 的示例项目:https://github.com/FujiwaraChoki/MoneyPrinterV2 - 有关使用 GPT4Free 的项目和网站的完整列表,请参阅:[docs/powered-by.md](https://github.com/gpt4free/g4f.dev/blob/main/docs/powered-by.md) ## 更新日志与发布 - 发布版和完整更新日志:https://github.com/xtekky/gpt4free/releases - 订阅 Discord/Telegram 以获取公告。 ## 宣言 / 项目原则 GPT4Free 遵循社区原则: 1. 开放访问 AI 工具和模型。 2. 跨 Provider 和项目协作。 3. 反对限制创造力的垄断、封闭系统。 4. 以社区为中心的开发和广泛访问 AI 技术。 5. 促进创新、创造力和可访问性。 https://g4f.dev/manifest ## 许可证 本程序基于 GNU General Public License v3.0 (GPLv3) 授权。查看完整许可证:https://www.gnu.org/licenses/gpl-3.0.txt 摘要: - 你可以根据 GPLv3 的条款重新分发和/或修改。 - 本程序不提供任何保证。 版权声明 ``` xtekky/gpt4free: Copyright (C) 2025 xtekky This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ``` ## 联系与赞助 - 维护者:https://github.com/hlohaus - 赞助:https://github.com/sponsors/hlohaus - 问题与功能请求:https://github.com/xtekky/gpt4free/issues - 下架请求:takedown@g4f.ai ## 附录:快速命令与示例 安装 (pip): ``` pip install -U g4f[all] ``` 运行 GUI (Python): ``` python -m g4f.cli gui --port 8080 --debug # 或 python -c "from g4f.gui import run_gui; run_gui()" ``` Docker (完整版): ``` docker pull hlohaus789/g4f docker run -p 8080:8080 -p 7900:7900 \ --shm-size="2g" \ -v ${PWD}/har_and_cookies:/app/har_and_cookies \ -v ${PWD}/generated_media:/app/generated_media \ hlohaus789/g4f:latest ``` Docker (精简版): ``` docker run -p 1337:8080 -p 8080:8080 \ -v ${PWD}/har_and_cookies:/app/har_and_cookies \ -v ${PWD}/generated_media:/app/generated_media \ hlohaus789/g4f:latest-slim ``` Python 使用模式: - `client.chat.completions.create(...)` - `client.images.generate(...)` - 通过 `AsyncClient` 的异步变体 文档与深度阅读 - 完整文档:https://g4f.dev/docs - 客户端 API 文档:https://g4f.dev/docs/client - 异步客户端文档:https://g4f.dev/docs/async_client - Provider 指南:https://g4f.dev/docs/guides - 本地推理:https://g4f.dev/docs/local 感谢你使用并为 GPT4Free 做出贡献——我们共同让强大的 AI 工具变得可访问、灵活且由社区驱动。标签:AIGC, Apex, API代理, ChatGPT, Claude, CVE检测, DeepSeek, DLL 劫持, DNS解析, Docker, Gemini, GPT, GPT-4, Kimi, Promptflow, Python, 云资产清单, 人工智能, 免费资源, 多模型支持, 大语言模型, 安全监控, 安全防御评估, 开源搜索引擎, 开源项目, 数据可视化, 文本生成, 无后门, 机器学习, 模型聚合, 深度学习, 漏洞管理, 熵值分析, 用户模式Hook绕过, 绕过付费, 脚本工具, 请求拦截, 逆向工具, 逆向工程