2024-10-19 20:21:14 +03:00
# G4F - Interference API Usage Guide
2024-10-25 20:33:37 +03:00
2024-10-19 20:21:14 +03:00
## Table of Contents
- [Introduction ](#introduction )
- [Running the Interference API ](#running-the-interference-api )
- [From PyPI Package ](#from-pypi-package )
- [From Repository ](#from-repository )
2024-10-25 20:33:37 +03:00
- [Using the Interference API ](#using-the-interference-api )
- [Basic Usage ](#basic-usage )
- [With OpenAI Library ](#with-openai-library )
- [With Requests Library ](#with-requests-library )
2024-10-19 20:21:14 +03:00
- [Key Points ](#key-points )
2024-10-25 20:33:37 +03:00
- [Conclusion ](#conclusion )
2024-10-19 20:21:14 +03:00
## Introduction
2024-10-29 23:03:05 +03:00
The G4F Interference API is a powerful tool that allows you to serve other OpenAI integrations using G4F (Gpt4free). It acts as a proxy, translating requests intended for the OpenAI API into requests compatible with G4F providers. This guide will walk you through the process of setting up, running, and using the Interference API effectively.
2024-10-25 20:33:37 +03:00
2024-10-19 20:21:14 +03:00
## Running the Interference API
2024-10-25 20:33:37 +03:00
**You can run the Interference API in two ways:** using the PyPI package or from the repository.
2024-10-19 20:21:14 +03:00
### From PyPI Package
2024-10-25 20:33:37 +03:00
**To run the Interference API directly from the G4F PyPI package, use the following Python code:**
2024-10-19 20:21:14 +03:00
```python
from g4f.api import run_api
run_api()
```
### From Repository
2024-10-25 20:33:37 +03:00
**If you prefer to run the Interference API from the cloned repository, you have two options:**
2024-10-19 20:21:14 +03:00
2024-10-25 20:33:37 +03:00
1. **Using the command line:**
2024-10-19 20:21:14 +03:00
```bash
g4f api
```
2024-10-25 20:33:37 +03:00
2. **Using Python:**
2024-10-19 20:21:14 +03:00
```bash
python -m g4f.api.run
```
2024-10-25 20:33:37 +03:00
**Once running, the API will be accessible at:** `http://localhost:1337/v1`
2024-11-01 16:03:45 +03:00
**(Advanced) Bind to custom port:**
```bash
python -m g4f.cli api --bind "0.0.0.0:2400"
```
2024-10-19 20:21:14 +03:00
2024-10-25 20:33:37 +03:00
## Using the Interference API
2024-10-19 20:21:14 +03:00
2024-10-25 20:33:37 +03:00
### Basic Usage
**You can interact with the Interference API using curl commands for both text and image generation:**
2024-10-19 20:21:14 +03:00
2024-10-25 20:33:37 +03:00
**For text generation:**
```bash
curl -X POST "http://localhost:1337/v1/chat/completions" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "Hello"
}
],
2024-11-12 09:44:48 +03:00
"model": "gpt-4o-mini"
2024-10-25 20:33:37 +03:00
}'
```
**For image generation:**
1. **url:**
```bash
curl -X POST "http://localhost:1337/v1/images/generate" \
-H "Content-Type: application/json" \
-d '{
"prompt": "a white siamese cat",
2024-10-28 11:43:04 +03:00
"model": "flux",
2024-10-25 20:33:37 +03:00
"response_format": "url"
}'
```
2. **b64_json**
```bash
curl -X POST "http://localhost:1337/v1/images/generate" \
-H "Content-Type: application/json" \
-d '{
"prompt": "a white siamese cat",
2024-10-28 11:43:04 +03:00
"model": "flux",
2024-10-25 20:33:37 +03:00
"response_format": "b64_json"
}'
```
### With OpenAI Library
**You can use the Interference API with the OpenAI Python library by changing the `base_url` :**
2024-10-19 20:21:14 +03:00
```python
from openai import OpenAI
client = OpenAI(
api_key="",
2024-10-25 20:33:37 +03:00
base_url="http://localhost:1337/v1"
2024-10-19 20:21:14 +03:00
)
response = client.chat.completions.create(
2024-11-12 09:44:48 +03:00
model="gpt-4o-mini",
2024-10-25 20:33:37 +03:00
messages=[{"role": "user", "content": "Write a poem about a tree"}],
2024-10-19 20:21:14 +03:00
stream=True,
)
if isinstance(response, dict):
# Not streaming
print(response.choices[0].message.content)
else:
# Streaming
for token in response:
content = token.choices[0].delta.content
if content is not None:
print(content, end="", flush=True)
2024-10-25 20:33:37 +03:00
```
2024-10-19 20:21:14 +03:00
2024-10-25 20:33:37 +03:00
### With Requests Library
2024-10-19 20:21:14 +03:00
2024-10-25 20:33:37 +03:00
**You can also send requests directly to the Interference API using the `requests` library:**
2024-10-19 20:21:14 +03:00
```python
import requests
url = "http://localhost:1337/v1/chat/completions"
2024-10-25 20:33:37 +03:00
2024-10-19 20:21:14 +03:00
body = {
2024-11-12 09:44:48 +03:00
"model": "gpt-4o-mini",
2024-10-19 20:21:14 +03:00
"stream": False,
"messages": [
{"role": "assistant", "content": "What can you do?"}
]
}
json_response = requests.post(url, json=body).json().get('choices', [])
for choice in json_response:
print(choice.get('message', {}).get('content', ''))
2024-10-25 20:33:37 +03:00
```
2024-10-19 20:21:14 +03:00
## Key Points
2024-10-25 20:33:37 +03:00
- The Interference API translates OpenAI API requests into G4F provider requests.
- It can be run from either the PyPI package or the cloned repository.
- The API supports usage with the OpenAI Python library by changing the `base_url` .
- Direct requests can be sent to the API endpoints using libraries like `requests` .
- Both text and image generation are supported.
2024-10-19 20:21:14 +03:00
2024-10-25 20:33:37 +03:00
## Conclusion
The G4F Interference API provides a seamless way to integrate G4F with existing OpenAI-based applications and tools. By following this guide, you should now be able to set up, run, and use the Interference API effectively. Whether you're using it for text generation, image creation, or as a drop-in replacement for OpenAI in your projects, the Interference API offers flexibility and power for your AI-driven applications.
2024-10-19 20:21:14 +03:00
---
[Return to Home ](/ )