GPT4Free Python Library: A Beginner’s Guide
gpt4free is an open-source Python library that lets you access a variety of powerful language models through multiple providers — all without needing official API keys in many cases. Designed as a proof-of-concept, this library demonstrates how to build an API package that handles multi-provider requests, timeouts, load balancing, and flow control.
With support for models like GPT-3.5, GPT-4, and others, gpt4free enables developers to explore AI-powered text generation from different sources using a unified interface.
This tutorial will walk you through how to install, configure, and use gpt4free for various AI tasks.
⚠️ Legal Notice
Before using gpt4free, please read the legal disclaimer:
- This project is provided for educational and proof-of-concept purposes only.
- The original author does not endorse or take responsibility for how this repository is used.
- Bypassing official APIs may violate the terms of service of the model providers.
- You must ensure you have proper authorization before using gpt4free in any production environment.
💡 Pro Tip: Use Apidog for Smarter API Testing
Looking for a powerful alternative to Postman? Apidog is an all-in-one API development platform that supports API design, debugging, automation, and documentation. When working with gpt4free, Apidog helps you:
- Send requests to interference API endpoints
- Experiment with different parameters and settings
- Visualize and inspect API responses
- Save and reuse API request collections
Apidog even supports MCP server integration, making it ideal for testing local APIs in AI agent mode.
🔧 How to Install gpt4free
📋 Prerequisites
- Python 3.10 or higher
- Google Chrome (required for some providers using browser automation)
📦 Installation Methods
Method 1: Install via PyPI
For full-featured installation:
pip install gpt4free
For partial installs (customized providers):
pip install gpt4free[provider_name]
Method 2: Clone from GitHub
git clone https://github.com/xtekky/gpt4free.git
cd gpt4free
pip install -r requirements.txt
Method 3: Using Docker
To run in a Docker container:
docker build -t gpt4free .
docker run -p 8080:8080 gpt4free
For slim version (x64 and arm64 compatible):
docker build -t gpt4free-slim -f Dockerfile.slim .
🧠 Using gpt4free in Python
✍️ Basic Text Generation
Simple Example
from g4f import ChatCompletion
response = ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello, AI!"}]
)
print(response)
Streaming Response
for chunk in ChatCompletion.create(..., stream=True):
print(chunk, end="")
Using a Specific Provider
ChatCompletion.create(
provider=g4f.Provider.Bing,
model="gpt-4",
messages=[...]
)
Using the Client API
ChatCompletion.create(
provider=g4f.Provider.Bing,
model="gpt-4",
messages=[...]
)
⚙️ Advanced Usage
✅ Client API
A modern and more flexible method:
client = g4f.Client(provider=g4f.Provider.Bing)
response = client.chat.completions.create(...)
🖼️ Image Generation
image = g4f.ImageCompletion.create(prompt="A sunset on Mars")
🔐 Provider Authentication
Some providers require cookies or tokens:
g4f.Provider.Bing.cookies = {...}
🧭 Browser Automation
Used for providers needing browser interaction:
python start_browser.py
⚡ Async Support
Boost performance with asynchronous calls:
await ChatCompletion.create_async(...)
🌐 Proxy & Timeout
Set global proxy via environment variable:
export http_proxy=http://localhost:7890
🌍 Web Interface
gpt4free includes a simple web UI:
python3 -m g4f.ui
Access it at: http://localhost:8080/chat
🔌 Using the Interference API
Run gpt4free’s OpenAI-compatible API:
python3 -m g4f.api
Then connect using the OpenAI client:
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
api_base="http://localhost:1337",
...
)
📚 Supported Models & Providers
GPT-4 Providers
g4f.Provider.Bing
g4f.Provider.GeekGpt
g4f.Provider.GptChatly
g4f.Provider.Liaobots
g4f.Provider.Raycast
GPT-3.5 Providers
g4f.Provider.AItianhu
g4f.Provider.ChatBase
g4f.Provider.ChatgptAi
g4f.Provider.ChatForAi
- And more…
Other Model Support
- Bard (Google PaLM)
- DeepInfra
- HuggingChat
- LLaMA 2
- OpenAssistant
🏁 Conclusion
gpt4free offers a flexible way to experiment with multiple language models using a single interface — often without official API keys. It’s ideal for:
- Learning about AI capabilities
- Rapid prototyping and testing
- Comparing provider performance
However, always use it responsibly. For production use, it’s strongly recommended to stick with official APIs and proper credentials.
Stay updated via the GitHub repository for new features, providers, and best practices as this project evolves alongside the rapidly growing AI ecosystem.