mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-15 14:51:19 -08:00
Update model configurations, provider implementations, and documentation (#2577)
* Update model configurations, provider implementations, and documentation - Updated model names and aliases for Qwen QVQ 72B and Qwen 2 72B (@TheFirstNoob) - Revised HuggingSpace class configuration, added default_image_model - Added llama-3.2-70b alias for Llama 3.2 70B model in AutonomousAI - Removed BlackboxCreateAgent class - Added gpt-4o alias for Copilot model - Moved api_key to Mhystical class attribute - Added models property with default_model value for Free2GPT - Simplified Jmuz class implementation - Improved image generation and model handling in DeepInfra - Standardized default models and removed aliases in Gemini - Replaced model aliases with direct model list in GlhfChat (@TheFirstNoob) - Removed trailing slash from image generation URL in PollinationsAI (https://github.com/xtekky/gpt4free/issues/2571) - Updated llama and qwen model configurations - Enhanced provider documentation and model details * Removed from (g4f/models.py) 'Yqcloud' provider from Default due to error 'ResponseStatusError: Response 429: 文字过长,请删减后重试。' * Update docs/providers-and-models.md * refactor(g4f/Provider/DDG.py): Add error handling and rate limiting to DDG provider - Add custom exception classes for rate limits, timeouts, and conversation limits - Implement rate limiting with sleep between requests (0.75s minimum delay) - Add model validation method to check supported models - Add proper error handling for API responses with custom exceptions - Improve session cookie handling for conversation persistence - Clean up User-Agent string and remove redundant code - Add proper error propagation through async generator Breaking changes: - New custom exceptions may require updates to error handling code - Rate limiting affects request timing and throughput - Model validation is now stricter Related: - Adds error handling similar to standard API clients - Improves reliability and robustness of chat interactions * Update g4f/models.py g4f/Provider/PollinationsAI.py * Update g4f/models.py * Restored provider which was not working and was disabled (g4f/Provider/DeepInfraChat.py) * Fixing a bug with Streaming Completions * Update g4f/Provider/PollinationsAI.py * Update g4f/Provider/Blackbox.py g4f/Provider/DDG.py * Added another model for generating images 'ImageGeneration2' to the 'Blackbox' provider * Update docs/providers-and-models.md * Update g4f/models.py g4f/Provider/Blackbox.py * Added a new OIVSCode provider from the Text Models and Vision (Image Upload) model * Update docs/providers-and-models.md * docs: add Conversation Memory class with context handling requested by @TheFirstNoob * Simplified README.md documentation added new docs/configuration.md documentation * Update add README.md docs/configuration.md * Update README.md * Update docs/providers-and-models.md g4f/models.py g4f/Provider/PollinationsAI.py * Added new model deepseek-r1 to Blackbox provider. @TheFirstNoob * Fixed bugs and updated docs/providers-and-models.md etc/unittest/client.py g4f/models.py g4f/Provider/. --------- Co-authored-by: kqlio67 <> Co-authored-by: H Lohaus <hlohaus@users.noreply.github.com>
This commit is contained in:
parent
a9fde5bf88
commit
9def1aa71f
42 changed files with 1464 additions and 1769 deletions
|
|
@ -1,139 +1,266 @@
|
|||
# G4F Authentication Setup Guide
|
||||
**# G4F - Authentication Guide**
|
||||
This documentation explains how to authenticate with G4F providers and configure GUI security. It covers API key management, cookie-based authentication, rate limiting, and GUI access controls.
|
||||
|
||||
This documentation explains how to set up Basic Authentication for the GUI and API key authentication for the API when running the G4F server.
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
## **Table of Contents**
|
||||
1. **[Provider Authentication](#provider-authentication)**
|
||||
- [Prerequisites](#prerequisites)
|
||||
- [API Key Setup](#api-key-setup)
|
||||
- [Synchronous Usage](#synchronous-usage)
|
||||
- [Asynchronous Usage](#asynchronous-usage)
|
||||
- [Multiple Providers](#multiple-providers-with-api-keys)
|
||||
- [Cookie-Based Authentication](#cookie-based-authentication)
|
||||
- [Rate Limiting](#rate-limiting)
|
||||
- [Error Handling](#error-handling)
|
||||
- [Supported Providers](#supported-providers)
|
||||
2. **[GUI Authentication](#gui-authentication)**
|
||||
- [Server Setup](#server-setup)
|
||||
- [Browser Access](#browser-access)
|
||||
- [Programmatic Access](#programmatic-access)
|
||||
3. **[Best Practices](#best-practices)**
|
||||
4. **[Troubleshooting](#troubleshooting)**
|
||||
|
||||
Before proceeding, ensure you have the following installed:
|
||||
- Python 3.x
|
||||
- G4F package installed (ensure it is set up and working)
|
||||
- Basic knowledge of using environment variables on your operating system
|
||||
---
|
||||
|
||||
## Steps to Set Up Authentication
|
||||
## **Provider Authentication**
|
||||
|
||||
### 1. API Key Authentication for Both GUI and API
|
||||
### **Prerequisites**
|
||||
- Python 3.7+
|
||||
- Installed `g4f` package:
|
||||
```bash
|
||||
pip install g4f
|
||||
```
|
||||
- API keys or cookies from providers (if required).
|
||||
|
||||
To secure both the GUI and the API, you'll authenticate using an API key. The API key should be injected via an environment variable and passed to both the GUI (via Basic Authentication) and the API.
|
||||
|
||||
#### Steps to Inject the API Key Using Environment Variables:
|
||||
|
||||
1. **Set the environment variable** for your API key:
|
||||
|
||||
On Linux/macOS:
|
||||
```bash
|
||||
export G4F_API_KEY="your-api-key-here"
|
||||
```
|
||||
|
||||
On Windows (Command Prompt):
|
||||
```bash
|
||||
set G4F_API_KEY="your-api-key-here"
|
||||
```
|
||||
|
||||
On Windows (PowerShell):
|
||||
```bash
|
||||
$env:G4F_API_KEY="your-api-key-here"
|
||||
```
|
||||
|
||||
Replace `your-api-key-here` with your actual API key.
|
||||
|
||||
2. **Run the G4F server with the API key injected**:
|
||||
|
||||
Use the following command to start the G4F server. The API key will be passed to both the GUI and the API:
|
||||
|
||||
```bash
|
||||
python -m g4f --debug --port 8080 --g4f-api-key $G4F_API_KEY
|
||||
```
|
||||
|
||||
- `--debug` enables debug mode for more verbose logs.
|
||||
- `--port 8080` specifies the port on which the server will run (you can change this if needed).
|
||||
- `--g4f-api-key` specifies the API key for both the GUI and the API.
|
||||
|
||||
#### Example:
|
||||
---
|
||||
|
||||
### **API Key Setup**
|
||||
#### **Step 1: Set Environment Variables**
|
||||
**For Linux/macOS (Terminal)**:
|
||||
```bash
|
||||
export G4F_API_KEY="my-secret-api-key"
|
||||
python -m g4f --debug --port 8080 --g4f-api-key $G4F_API_KEY
|
||||
# Example for Anthropic
|
||||
export ANTHROPIC_API_KEY="your_key_here"
|
||||
|
||||
# Example for HuggingFace
|
||||
export HUGGINGFACE_API_KEY="another_key_here"
|
||||
```
|
||||
|
||||
Now, both the GUI and API will require the correct API key for access.
|
||||
**For Windows (Command Prompt)**:
|
||||
```cmd
|
||||
:: Example for Anthropic
|
||||
set ANTHROPIC_API_KEY=your_key_here
|
||||
|
||||
:: Example for HuggingFace
|
||||
set HUGGINGFACE_API_KEY=another_key_here
|
||||
```
|
||||
|
||||
**For Windows (PowerShell)**:
|
||||
```powershell
|
||||
# Example for Anthropic
|
||||
$env:ANTHROPIC_API_KEY = "your_key_here"
|
||||
|
||||
# Example for HuggingFace
|
||||
$env:HUGGINGFACE_API_KEY = "another_key_here"
|
||||
```
|
||||
|
||||
#### **Step 2: Initialize Client**
|
||||
```python
|
||||
from g4f.client import Client
|
||||
|
||||
# Example for Anthropic
|
||||
client = Client(
|
||||
provider="g4f.Provider.Anthropic",
|
||||
api_key="your_key_here" # Or use os.getenv("ANTHROPIC_API_KEY")
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 2. Accessing the GUI with Basic Authentication
|
||||
### **Synchronous Usage**
|
||||
```python
|
||||
from g4f.client import Client
|
||||
|
||||
The GUI uses **Basic Authentication**, where the **username** can be any value, and the **password** is your API key.
|
||||
# Initialize with Anthropic
|
||||
client = Client(provider="g4f.Provider.Anthropic", api_key="your_key_here")
|
||||
|
||||
#### Example:
|
||||
|
||||
To access the GUI, open your web browser and navigate to `http://localhost:8080/chat/`. You will be prompted for a username and password.
|
||||
|
||||
- **Username**: You can use any username (e.g., `user` or `admin`).
|
||||
- **Password**: Enter your API key (the same key you set in the `G4F_API_KEY` environment variable).
|
||||
# Simple request
|
||||
response = client.chat.completions.create(
|
||||
model="claude-3.5-sonnet",
|
||||
messages=[{"role": "user", "content": "Hello!"}]
|
||||
)
|
||||
print(response.choices[0].message.content)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 3. Python Example for Accessing the API
|
||||
### **Asynchronous Usage**
|
||||
```python
|
||||
import asyncio
|
||||
from g4f.client import AsyncClient
|
||||
|
||||
To interact with the API, you can send requests by including the `g4f-api-key` in the headers. Here's an example of how to do this using the `requests` library in Python.
|
||||
async def main():
|
||||
# Initialize with Groq
|
||||
client = AsyncClient(provider="g4f.Provider.Groq", api_key="your_key_here")
|
||||
|
||||
response = await client.chat.completions.create(
|
||||
model="mixtral-8x7b",
|
||||
messages=[{"role": "user", "content": "Hello!"}]
|
||||
)
|
||||
print(response.choices[0].message.content)
|
||||
|
||||
#### Example Code to Send a Request:
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **Multiple Providers with API Keys**
|
||||
```python
|
||||
import os
|
||||
from g4f.client import Client
|
||||
|
||||
# Using environment variables
|
||||
providers = {
|
||||
"Anthropic": os.getenv("ANTHROPIC_API_KEY"),
|
||||
"Groq": os.getenv("GROQ_API_KEY")
|
||||
}
|
||||
|
||||
for provider_name, api_key in providers.items():
|
||||
client = Client(provider=f"g4f.Provider.{provider_name}", api_key=api_key)
|
||||
response = client.chat.completions.create(
|
||||
model="claude-3.5-sonnet",
|
||||
messages=[{"role": "user", "content": f"Hello from {provider_name}!"}]
|
||||
)
|
||||
print(f"{provider_name}: {response.choices[0].message.content}")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **Cookie-Based Authentication**
|
||||
**For Providers Like Gemini/Bing**:
|
||||
1. Open your browser and log in to the provider's website.
|
||||
2. Use developer tools (F12) to copy cookies:
|
||||
- Chrome/Edge: **Application** → **Cookies**
|
||||
- Firefox: **Storage** → **Cookies**
|
||||
|
||||
```python
|
||||
from g4f.Provider import Gemini
|
||||
|
||||
# Initialize with cookies
|
||||
client = Client(
|
||||
provider=Gemini,
|
||||
cookies={
|
||||
"__Secure-1PSID": "your_cookie_value_here",
|
||||
"__Secure-1PSIDTS": "timestamp_value_here"
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **Rate Limiting**
|
||||
```python
|
||||
from aiolimiter import AsyncLimiter
|
||||
|
||||
# Limit to 5 requests per second
|
||||
rate_limiter = AsyncLimiter(max_rate=5, time_period=1)
|
||||
|
||||
async def make_request():
|
||||
async with rate_limiter:
|
||||
return await client.chat.completions.create(...)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **Error Handling**
|
||||
```python
|
||||
from tenacity import retry, stop_after_attempt, wait_exponential
|
||||
|
||||
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
||||
def safe_request():
|
||||
try:
|
||||
return client.chat.completions.create(...)
|
||||
except Exception as e:
|
||||
print(f"Attempt failed: {str(e)}")
|
||||
raise
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **Supported Providers**
|
||||
| Provider | Auth Type | Example Models |
|
||||
|----------------|-----------------|----------------------|
|
||||
| Anthropic | API Key | `claude-3.5-sonnet` |
|
||||
| Gemini | Cookies | `gemini-1.5-pro` |
|
||||
| Groq | API Key | `mixtral-8x7b` |
|
||||
| HuggingFace | API Key | `llama-3.1-70b` |
|
||||
|
||||
*Full list: [Providers and Models](providers-and-models.md)*
|
||||
|
||||
---
|
||||
|
||||
## **GUI Authentication**
|
||||
|
||||
### **Server Setup**
|
||||
1. Create a password:
|
||||
```bash
|
||||
# Linux/macOS
|
||||
export G4F_API_KEY="your_password_here"
|
||||
|
||||
# Windows (Command Prompt)
|
||||
set G4F_API_KEY=your_password_here
|
||||
|
||||
# Windows (PowerShell)
|
||||
$env:G4F_API_KEY = "your_password_here"
|
||||
```
|
||||
2. Start the server:
|
||||
```bash
|
||||
python -m g4f --debug --port 8080 --g4f-api-key $G4F_API_KEY
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### **Browser Access**
|
||||
1. Navigate to `http://localhost:8080/chat/`.
|
||||
2. Use credentials:
|
||||
- **Username**: Any value (e.g., `admin`).
|
||||
- **Password**: Your `G4F_API_KEY`.
|
||||
|
||||
---
|
||||
|
||||
### **Programmatic Access**
|
||||
```python
|
||||
import requests
|
||||
|
||||
url = "http://localhost:8080/v1/chat/completions"
|
||||
|
||||
# Body of the request
|
||||
body = {
|
||||
"model": "your-model-name", # Replace with your model name
|
||||
"provider": "your-provider", # Replace with the provider name
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Hello"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
# API Key (can be set as an environment variable)
|
||||
api_key = "your-api-key-here" # Replace with your actual API key
|
||||
|
||||
# Send the POST request
|
||||
response = requests.post(url, json=body, headers={"g4f-api-key": api_key})
|
||||
|
||||
# Check the response
|
||||
print(response.status_code)
|
||||
print(response.json())
|
||||
response = requests.get(
|
||||
"http://localhost:8080/chat/",
|
||||
auth=("admin", "your_password_here")
|
||||
)
|
||||
print("Success!" if response.status_code == 200 else f"Failed: {response.status_code}")
|
||||
```
|
||||
|
||||
In this example:
|
||||
- Replace `"your-api-key-here"` with your actual API key.
|
||||
- `"model"` and `"provider"` should be replaced with the appropriate model and provider you're using.
|
||||
- The `messages` array contains the conversation you want to send to the API.
|
||||
---
|
||||
|
||||
#### Response:
|
||||
|
||||
The response will contain the output of the API request, such as the model's completion or other relevant data, which you can then process in your application.
|
||||
## **Best Practices**
|
||||
1. 🔒 **Never hardcode keys**
|
||||
- Use `.env` files or secret managers like AWS Secrets Manager.
|
||||
2. 🔄 **Rotate keys every 90 days**
|
||||
- Especially critical for production environments.
|
||||
3. 📊 **Monitor API usage**
|
||||
- Use tools like Prometheus/Grafana for tracking.
|
||||
4. ♻️ **Retry transient errors**
|
||||
- Use the `tenacity` library for robust retry logic.
|
||||
|
||||
---
|
||||
|
||||
### 4. Testing the Setup
|
||||
|
||||
- **Accessing the GUI**: Open a web browser and navigate to `http://localhost:8080/chat/`. The GUI will now prompt you for a username and password. You can enter any username (e.g., `admin`), and for the password, enter the API key you set up in the environment variable.
|
||||
|
||||
- **Accessing the API**: Use the Python code example above to send requests to the API. Ensure the correct API key is included in the `g4f-api-key` header.
|
||||
## **Troubleshooting**
|
||||
| Issue | Solution |
|
||||
|---------------------------|-------------------------------------------|
|
||||
| **"Invalid API Key"** | 1. Verify key spelling<br>2. Regenerate key in provider dashboard |
|
||||
| **"Cookie Expired"** | 1. Re-login to provider website<br>2. Update cookie values |
|
||||
| **"Rate Limit Exceeded"** | 1. Implement rate limiting<br>2. Upgrade provider plan |
|
||||
| **"Provider Not Found"** | 1. Check provider name spelling<br>2. Verify provider compatibility |
|
||||
|
||||
---
|
||||
|
||||
### 5. Troubleshooting
|
||||
|
||||
- **GUI Access Issues**: If you're unable to access the GUI, ensure that you are using the correct API key as the password.
|
||||
- **API Access Issues**: If the API is rejecting requests, verify that the `G4F_API_KEY` environment variable is correctly set and passed to the server. You can also check the server logs for more detailed error messages.
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
By following the steps above, you will have successfully set up Basic Authentication for the G4F GUI (using any username and the API key as the password) and API key authentication for the API. This ensures that only authorized users can access both the interface and make API requests.
|
||||
|
||||
[Return to Home](/)
|
||||
**[⬆ Back to Top](#table-of-contents)** | **[Providers and Models →](providers-and-models.md)**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue