2024-02-23 19:21:10 +03:00
### G4F - Client API
2024-02-13 12:45:06 +03:00
#### Introduction
2024-02-22 06:35:11 +03:00
Welcome to the G4F Client API, a cutting-edge tool for seamlessly integrating advanced AI capabilities into your Python applications. This guide is designed to facilitate your transition from using the OpenAI client to the G4F Client, offering enhanced features while maintaining compatibility with the existing OpenAI API.
2024-02-13 12:45:06 +03:00
#### Getting Started
2024-02-12 13:41:27 +03:00
2024-02-13 12:45:06 +03:00
**Switching to G4F Client:**
2024-02-12 13:41:27 +03:00
2024-02-22 06:35:11 +03:00
To begin using the G4F Client, simply update your import statement in your Python code:
2024-02-13 12:45:06 +03:00
Old Import:
2024-02-12 13:41:27 +03:00
```python
2024-02-13 12:45:06 +03:00
from openai import OpenAI
2024-02-12 13:41:27 +03:00
```
2024-02-13 12:45:06 +03:00
New Import:
2024-02-12 13:41:27 +03:00
```python
2024-02-13 13:29:41 +03:00
from g4f.client import Client as OpenAI
2024-02-12 13:41:27 +03:00
```
2024-02-22 06:35:11 +03:00
The G4F Client preserves the same familiar API interface as OpenAI, ensuring a smooth transition process.
2024-02-12 13:41:27 +03:00
2024-02-22 06:35:11 +03:00
### Initializing the Client
2024-02-12 13:41:27 +03:00
2024-02-22 06:35:11 +03:00
To utilize the G4F Client, create an new instance. Below is an example showcasing custom providers:
2024-02-12 13:41:27 +03:00
```python
from g4f.client import Client
2024-02-13 13:29:41 +03:00
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
2024-02-12 13:41:27 +03:00
client = Client(
2024-02-19 21:34:28 +03:00
provider=OpenaiChat,
2024-02-12 13:41:27 +03:00
image_provider=Gemini,
2024-02-22 06:35:11 +03:00
...
)
```
2024-02-23 13:33:38 +03:00
## Configuration
2024-02-23 19:21:10 +03:00
You can set an "api_key" for your provider in the client.
2024-02-23 13:33:38 +03:00
And you also have the option to define a proxy for all outgoing requests:
2024-02-22 06:35:11 +03:00
```python
from g4f.client import Client
client = Client(
2024-02-23 13:33:38 +03:00
api_key="...",
2024-02-22 06:35:11 +03:00
proxies="http://user:pass@host",
...
2024-02-12 13:41:27 +03:00
)
```
2024-02-13 12:45:06 +03:00
#### Usage Examples
**Text Completions:**
2024-02-12 13:41:27 +03:00
2024-02-13 12:45:06 +03:00
You can use the `ChatCompletions` endpoint to generate text completions as follows:
2024-02-12 13:41:27 +03:00
2024-02-21 19:02:54 +03:00
```python
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Say this is a test"}],
...
)
print(response.choices[0].message.content)
```
Also streaming are supported:
2024-02-12 13:41:27 +03:00
```python
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test"}],
stream=True,
2024-02-21 19:02:54 +03:00
...
2024-02-12 13:41:27 +03:00
)
for chunk in stream:
2024-02-13 12:45:06 +03:00
if chunk.choices[0].delta.content:
2024-02-23 13:33:38 +03:00
print(chunk.choices[0].delta.content or "", end="")
2024-02-12 13:41:27 +03:00
```
2024-02-13 12:45:06 +03:00
**Image Generation:**
Generate images using a specified prompt:
2024-02-12 13:41:27 +03:00
```python
response = client.images.generate(
2024-02-13 12:45:06 +03:00
model="dall-e-3",
prompt="a white siamese cat",
...
2024-02-12 13:41:27 +03:00
)
image_url = response.data[0].url
```
2024-02-13 12:45:06 +03:00
**Creating Image Variations:**
Create variations of an existing image:
2024-02-12 13:41:27 +03:00
```python
response = client.images.create_variation(
2024-02-13 12:45:06 +03:00
image=open("cat.jpg", "rb"),
model="bing",
...
2024-02-12 13:41:27 +03:00
)
image_url = response.data[0].url
```
2024-02-13 12:45:06 +03:00
Original / Variant:
2024-02-12 14:08:08 +03:00
2024-02-23 13:33:38 +03:00
[![Original Image ](/docs/cat.jpeg )](/docs/client.md) [![Variant Image ](/docs/cat.webp )](/docs/client.md)
2024-02-23 19:21:10 +03:00
#### Use a list of providers with RetryProvider
```python
from g4f.client import Client
from g4f.Provider import RetryProvider, Phind, FreeChatgpt, Liaobots
import g4f.debug
g4f.debug.logging = True
client = Client(
provider=RetryProvider([Phind, FreeChatgpt, Liaobots], shuffle=False)
)
response = client.chat.completions.create(
model="",
messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)
```
```
Using RetryProvider provider
Using Phind provider
How can I assist you today?
```
2024-02-23 13:33:38 +03:00
#### Advanced example using GeminiProVision
```python
from g4f.client import Client
from g4f.Provider.GeminiPro import GeminiPro
client = Client(
api_key="...",
provider=GeminiPro
)
response = client.chat.completions.create(
model="gemini-pro-vision",
messages=[{"role": "user", "content": "What are on this image?"}],
2024-02-23 19:21:10 +03:00
image=open("docs/waterfall.jpeg", "rb")
2024-02-23 13:33:38 +03:00
)
print(response.choices[0].message.content)
```
2024-02-23 19:21:10 +03:00
```
User: What are on this image?
```
2024-02-24 03:31:17 +03:00
![Waterfall ](/docs/waterfall.jpeg )
2024-02-23 13:33:38 +03:00
```
2024-02-23 19:21:10 +03:00
Bot: There is a waterfall in the middle of a jungle. There is a rainbow over...
2024-02-23 13:33:38 +03:00
```
2024-02-12 14:08:08 +03:00
2024-02-19 21:34:28 +03:00
[Return to Home ](/ )