summaryrefslogtreecommitdiffstats
path: root/docs/interference-api.md
blob: 617df9cdc5f574258a496166ba380d89285325ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# G4F - Interference API Usage Guide
  

## Table of Contents
   - [Introduction](#introduction)
   - [Running the Interference API](#running-the-interference-api)
   - [From PyPI Package](#from-pypi-package)
   - [From Repository](#from-repository)
   - [Using the Interference API](#using-the-interference-api)
   - [Basic Usage](#basic-usage)
   - [With OpenAI Library](#with-openai-library)
   - [With Requests Library](#with-requests-library)
   - [Key Points](#key-points)
   - [Conclusion](#conclusion)
  

## Introduction
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 and Anthropic 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.
  

## Running the Interference API
**You can run the Interference API in two ways:** using the PyPI package or from the repository.
  

### From PyPI Package
**To run the Interference API directly from the G4F PyPI package, use the following Python code:**

```python
from g4f.api import run_api

run_api()
```

  
### From Repository
**If you prefer to run the Interference API from the cloned repository, you have two options:**

1. **Using the command line:**
```bash
g4f api
```

2. **Using Python:**
```bash
python -m g4f.api.run
```

**Once running, the API will be accessible at:** `http://localhost:1337/v1`
  

## Using the Interference API

### Basic Usage
**You can interact with the Interference API using curl commands for both text and image generation:**

**For text generation:**
```bash
curl -X POST "http://localhost:1337/v1/chat/completions" \
     -H "Content-Type: application/json" \
     -d '{
           "messages": [
             {
               "role": "user",
               "content": "Hello"
             }
           ],
           "model": "gpt-3.5-turbo"
         }'
```

**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",
           "model": "dall-e-3",
           "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",
           "model": "dall-e-3",
           "response_format": "b64_json"
         }'
```


### With OpenAI Library

**You can use the Interference API with the OpenAI Python library by changing the `base_url`:**
```python
from openai import OpenAI

client = OpenAI(
    api_key="",
    base_url="http://localhost:1337/v1"
)

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Write a poem about a tree"}],
    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)

```
  

### With Requests Library

**You can also send requests directly to the Interference API using the `requests` library:**
```python
import requests

url = "http://localhost:1337/v1/chat/completions"

body = {
    "model": "gpt-3.5-turbo",
    "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', ''))

```

## Key Points
   - 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.

  
## 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.
 

---

[Return to Home](/)