mahshid1378/Gpt4free

GitHub: mahshid1378/Gpt4free

该项目聚合了多种在线 AI 大模型与媒体生成接口,提供统一的兼容 OpenAI 的 Python/JS 客户端、REST API 及本地 Web GUI。

Stars: 0 | Forks: 0

# GPT4Free (g4f) [![PyPI](https://img.shields.io/pypi/v/g4f)](https://pypi.org/project/g4f) [![Docker Hub](https://img.shields.io/badge/docker-hlohaus789%2Fg4f-blue)](https://hub.docker.com/r/hlohaus789/g4f) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-red.svg)](https://www.gnu.org/licenses/gpl-3.0.txt) [![PyPI Downloads](https://static.pepy.tech/personalized-badge/g4f?period=total&units=INTERNATIONAL_SYSTEM&left_color=GREY&right_color=GREEN&left_text=downloads)](https://pepy.tech/projects/g4f) ![GitHub Repo stars](https://img.shields.io/github/stars/xtekky/gpt4free)

GPT4Free logo

@xtekky 创建,
@hlohaus 维护

支持本项目请访问 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) - [Powered-by 亮点](#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 现在包含一个 Model Context Protocol (MCP) 服务器,允许像 Claude 这样的 AI 助手访问网络搜索、抓取和图像生成功能。 **启动 MCP 服务器(stdio 模式):** ``` # 使用 g4f 命令 g4f mcp # 或使用 Python 模块 python -m g4f.mcp ``` **启动 MCP 服务器(HTTP 模式):** ``` # 在端口 8765 上启动 HTTP 服务器 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 endpoint - `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` - 根据文本 prompt 生成图像 有关详细的 MCP 文档,请参见 [g4f/mcp/README.md](g4f/mcp/README.md) ### 可选的 provider 登录(容器内桌面) - 访问地址: http://localhost:7900/?autoconnect=1&resize=scale&password=secret - 可用于登录基于 Web 的 provider,以获取 cookie/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 的 endpoint、PerplexityLabs、Gemini、MetaAI、Pollinations(媒体)以及本地推理后端。 - 模型可用性和行为取决于 provider 的能力。有关当前受支持的 provider/模型列表,请参见 provider 文档:https://g4f.dev/docs/providers-and-models Provider 的要求可能包括: - API 密钥或 token(用于经过身份验证的 provider) - 浏览器 cookie / HAR 文件(用于通过浏览器自动化抓取的 provider) - Chrome/Chromium 或无头浏览器工具 - 本地模型二进制文件和 runtime(用于本地推理) ## 本地推理与媒体 - GPT4Free 支持本地推理后端。有关受支持的 runtime 和硬件指南,请参见 [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 文件、cookie 和生成的媒体将持久化保存在映射的目录中(例如 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) - 默认 endpoint(精简版 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.md](https://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) 获取 prompt 模板和工作流,以加速开发。 ## 安全、隐私与下架政策 - 请勿存储或共享敏感凭据。请使用各 provider 推荐的安全实践。 - 如果您的网站出现在本项目的链接中,并且您希望将其移除,请将所有权证明发送至 takedown@g4f.ai,我们将 promptly 予以移除。 - 对于生产环境,请使用 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) 在仓库中还感谢了许多其他贡献者。 ## Powered-by 亮点 - 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/manifesto ## 许可证 本程序根据 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 install -U g4f[all] ``` 运行 GUI: ``` 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/providers - 本地推理:https://g4f.dev/docs/local 感谢您使用并为 GPT4Free 贡献力量——让我们共同努力,使强大的 AI 工具变得易于访问、灵活且受社区驱动。
标签:API聚合, CMS安全, DLL 劫持, JavaScript, OpenAI兼容, Python, 免费API, 大语言模型, 数据可视化, 无后门, 请求拦截, 逆向工具