> ## Content Index
> Fetch the complete content index at: https://www.hubwiz.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# GPT4Free新手指南
- URL: https://www.hubwiz.com/blog/gpt4free-beginners-guide/
- Published: 2025-04-15T01:26:03.000Z
- Updated: 2025-04-15T01:26:03.000Z
- Description: gpt4free 是一个开源的Python库，它允许你通过多个提供者访问各种强大的语言模型——在许多情况下无需使用官方API密钥。
- Author: admin
- Tags: LIBRARY, GPT4FREE, #Import 2026-10-14 14:41

[**gpt4free**](https://github.com/xtekky/gpt4free?ref=hubwiz.com) 是一个开源的Python库，它允许你通过多个提供者访问各种强大的语言模型——在许多情况下无需使用官方API密钥。作为一个概念验证，这个库展示了如何构建一个处理**多提供者请求**、**超时**、**负载均衡**和**流控制**的API包。

由于支持模型如**GPT-3.5**、**GPT-4**等，gpt4free使开发人员能够使用统一接口从不同来源探索AI驱动的文本生成。

本教程将引导你完成如何**安装**、**配置**和**使用**gpt4free进行各种AI任务。

## 1、安装gpt4free

先决条件：

- Python **3.10或更高版本**
- Google Chrome（某些提供者需要浏览器自动化）

有三种安装方法：通过PyPI安装、从Github克隆或者使用Docker。

### 1.1 通过PyPI安装

完整功能安装：

```
pip install gpt4free

```

部分安装（自定义提供者）：

```
pip install gpt4free[provider_name]

```

### 1.2 从GitHub克隆

```
git clone https://github.com/xtekky/gpt4free.git  
cd gpt4free  
pip install -r requirements.txt

```

### 1.3 使用Docker

在Docker容器中运行：

```
docker build -t gpt4free .  
docker run -p 8080:8080 gpt4free

```

瘦版本（兼容x64和arm64）：

```
docker build -t gpt4free-slim -f Dockerfile.slim .

```

## 2、在Python中使用gpt4free

**简单示例**

```
from g4f import ChatCompletion  
  
response = ChatCompletion.create(  
    model="gpt-3.5-turbo",  
    messages=[{"role": "user", "content": "你好，AI！"}]  
)  
print(response)

```

**流式响应**

```
for chunk in ChatCompletion.create(..., stream=True):  
    print(chunk, end="")

```

**使用特定提供者**

```
ChatCompletion.create(  
    provider=g4f.Provider.Bing,  
    model="gpt-4",  
    messages=[...]  
)

```

**使用客户端API**

```
ChatCompletion.create(  
    provider=g4f.Provider.Bing,  
    model="gpt-4",  
    messages=[...]  
)

```

## 3、高级用法

### 3.1 客户端API

现代且更灵活的方法：

```
client = g4f.Client(provider=g4f.Provider.Bing)  
response = client.chat.completions.create(...)

```

### 3.2 图像生成

```
image = g4f.ImageCompletion.create(prompt="火星上的日落")

```

### 3.3 提供者认证

某些提供者需要cookies或令牌：

```
g4f.Provider.Bing.cookies = {...}

```

### 3.4 浏览器自动化

用于需要浏览器交互的提供者：

```
python start_browser.py

```

### 3.5 异步支持

通过异步调用提升性能：

```
await ChatCompletion.create_async(...)

```

### 3.6 代理与超时

通过环境变量设置全局代理：

```
export http_proxy=http://localhost:7890

```

### 3.7 网页界面

gpt4free包含一个简单的网页UI：

```
python3 -m g4f.ui

```

访问地址：[http://localhost:8080/chat](http://localhost:8080/chat?ref=hubwiz.com)

### 3.8 使用推理API

运行gpt4free的OpenAI兼容API：

```
python3 -m g4f.api

```

然后使用OpenAI客户端连接：

```
openai.ChatCompletion.create(  
    model="gpt-3.5-turbo",  
    api_base="http://localhost:1337",  
    ...  
)

```

## 4、支持的模型与提供者

GPT-4提供者：

- `g4f.Provider.Bing`
- `g4f.Provider.GeekGpt`
- `g4f.Provider.GptChatly`
- `g4f.Provider.Liaobots`
- `g4f.Provider.Raycast`

GPT-3.5提供者：

- `g4f.Provider.AItianhu`
- `g4f.Provider.ChatBase`
- `g4f.Provider.ChatgptAi`
- `g4f.Provider.ChatForAi`
- 更多……

**其他模型支持：**

- Bard（Google PaLM）
- DeepInfra
- HuggingChat
- LLaMA 2
- OpenAssistant

## 5、结束语

**gpt4free** 提供了一种灵活的方式来使用单一接口实验多种语言模型——通常无需官方API密钥。它非常适合：

- 学习AI能力
- 快速原型设计和测试
- 比较提供者性能

然而，始终要负责任地使用它。对于生产环境，强烈建议使用官方API和适当的凭据。

---

原文链接：[GPT4Free Python Library: A Beginner’s Guide](https://sebastian-petrus.medium.com/gpt4free-python-library-621aefb6745a?ref=hubwiz.com)

汇智网翻译整理，转载请标明出处