返回提交历史
Modified
docs/interference-api.md
+79
-27
XFEstudio/gpt4free
docs(docs/interference-api.md): update Interference API usage guide
17384a11
代码差异
1 个文件
+79
-27
@@ -1,23 +1,30 @@
1
2
1
# G4F - Interference API Usage Guide
3
2
4
3
5
4
## Table of Contents
6
5
- [Introduction](#introduction)
7
6
- [Running the Interference API](#running-the-interference-api)
8
7
- [From PyPI Package](#from-pypi-package)
9
8
- [From Repository](#from-repository)
10
- [Usage with OpenAI Library](#usage-with-openai-library)
11
- [Usage with Requests Library](#usage-with-requests-library)
9
- [Using the Interference API](#using-the-interference-api)
10
- [Basic Usage](#basic-usage)
11
- [With OpenAI Library](#with-openai-library)
12
- [With Requests Library](#with-requests-library)
12
13
- [Key Points](#key-points)
14
- [Conclusion](#conclusion)
15
13
16
14
17
## Introduction
15
The Interference API allows you to serve other OpenAI integrations with G4F. It acts as a proxy, translating requests to the OpenAI API into requests to the G4F providers.
18
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.
19
16
20
17
21
## Running the Interference API
22
**You can run the Interference API in two ways:** using the PyPI package or from the repository.
23
18
24
19
25
### From PyPI Package
20
**You can run the Interference API directly from the G4F PyPI package:**
26
**To run the Interference API directly from the G4F PyPI package, use the following Python code:**
27
21
28
```python
22
29
from g4f.api import run_api
23
30
@@ -25,37 +32,80 @@ run_api()
25
32
```
26
33
27
34
28
29
35
### From Repository
30
Alternatively, you can run the Interference API from the cloned repository.
36
**If you prefer to run the Interference API from the cloned repository, you have two options:**
31
37
32
**Run the server with:**
38
1. **Using the command line:**
33
39
```bash
34
40
g4f api
35
41
```
36
or
42
43
2. **Using Python:**
37
44
```bash
38
45
python -m g4f.api.run
39
46
```
40
47
48
**Once running, the API will be accessible at:** `http://localhost:1337/v1`
41
49
42
50
43
## Usage with OpenAI Library
51
## Using the Interference API
44
52
45
53
### Basic Usage
54
**You can interact with the Interference API using curl commands for both text and image generation:**
46
55
56
**For text generation:**
57
```bash
58
curl -X POST "http://localhost:1337/v1/chat/completions" \
59
-H "Content-Type: application/json" \
60
-d '{
61
"messages": [
62
{
63
"role": "user",
64
"content": "Hello"
65
}
66
],
67
"model": "gpt-3.5-turbo"
68
}'
69
```
70
71
**For image generation:**
72
1. **url:**
73
```bash
74
curl -X POST "http://localhost:1337/v1/images/generate" \
75
-H "Content-Type: application/json" \
76
-d '{
77
"prompt": "a white siamese cat",
78
"model": "dall-e-3",
79
"response_format": "url"
80
}'
81
```
82
83
2. **b64_json**
84
```bash
85
curl -X POST "http://localhost:1337/v1/images/generate" \
86
-H "Content-Type: application/json" \
87
-d '{
88
"prompt": "a white siamese cat",
89
"model": "dall-e-3",
90
"response_format": "b64_json"
91
}'
92
```
93
94
95
### With OpenAI Library
96
97
**You can use the Interference API with the OpenAI Python library by changing the `base_url`:**
47
98
```python
48
99
from openai import OpenAI
49
100
50
101
client = OpenAI(
51
102
api_key="",
52
# Change the API base URL to the local interference API
53
base_url="http://localhost:1337/v1"
103
base_url="http://localhost:1337/v1"
54
104
)
55
105
56
106
response = client.chat.completions.create(
57
107
model="gpt-3.5-turbo",
58
messages=[{"role": "user", "content": "write a poem about a tree"}],
108
messages=[{"role": "user", "content": "Write a poem about a tree"}],
59
109
stream=True,
60
110
)
61
111
@@ -68,20 +118,20 @@ else:
68
118
content = token.choices[0].delta.content
69
119
if content is not None:
70
120
print(content, end="", flush=True)
71
```
72
121
122
```
73
123
74
124
75
## Usage with Requests Library
76
You can also send requests directly to the Interference API using the requests library.
125
### With Requests Library
77
126
78
**Send a POST request to `/v1/chat/completions` with the request body containing the model and other parameters:**
127
**You can also send requests directly to the Interference API using the `requests` library:**
79
128
```python
80
129
import requests
81
130
82
131
url = "http://localhost:1337/v1/chat/completions"
132
83
133
body = {
84
"model": "gpt-3.5-turbo",
134
"model": "gpt-3.5-turbo",
85
135
"stream": False,
86
136
"messages": [
87
137
{"role": "assistant", "content": "What can you do?"}
@@ -92,18 +142,20 @@ json_response = requests.post(url, json=body).json().get('choices', [])
92
142
93
143
for choice in json_response:
94
144
print(choice.get('message', {}).get('content', ''))
95
```
96
145
97
146
```
98
147
99
148
## Key Points
100
- The Interference API translates OpenAI API requests into G4F provider requests
101
- You can run it from the PyPI package or the cloned repository
102
- It supports usage with the OpenAI Python library by changing the `base_url`
103
- Direct requests can be sent to the API endpoints using libraries like `requests`
149
- The Interference API translates OpenAI API requests into G4F provider requests.
150
- It can be run from either the PyPI package or the cloned repository.
151
- The API supports usage with the OpenAI Python library by changing the `base_url`.
152
- Direct requests can be sent to the API endpoints using libraries like `requests`.
153
- Both text and image generation are supported.
104
154
105
155
106
**_The Interference API allows easy integration of G4F with existing OpenAI-based applications and tools._**
156
## Conclusion
157
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.
158
107
159
108
160
---
109
161