You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gpt4free/docs/client.md

93 lines
2.0 KiB
Markdown

5 months ago
### G4F Client API Documentation (Beta Version)
#### Introduction
The G4F Client API introduces a new way to integrate advanced AI functionalities into your Python applications. This guide will help you transition from using the OpenAI client to the new G4F Client, offering compatibility with the existing OpenAI API alongside additional features.
#### Getting Started
5 months ago
**Switching to G4F Client:**
5 months ago
Replace the OpenAI client import statement in your Python code as follows:
Old Import:
```python
5 months ago
from openai import OpenAI
```
5 months ago
New Import:
```python
5 months ago
from g4f.client import Client
```
5 months ago
The G4F Client maintains the same API interface as OpenAI, ensuring a seamless transition.
5 months ago
#### Initializing the Client
5 months ago
To use the G4F Client, create an instance with customized providers:
```python
from g4f.client import Client
5 months ago
from g4f.providers import BingCreateImages, OpenaiChat, Gemini
client = Client(
5 months ago
text_provider=OpenaiChat,
image_provider=Gemini,
proxies=None
)
```
5 months ago
#### Usage Examples
**Text Completions:**
5 months ago
You can use the `ChatCompletions` endpoint to generate text completions as follows:
```python
stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Say this is a test"}],
stream=True,
)
for chunk in stream:
5 months ago
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
```
5 months ago
**Image Generation:**
Generate images using a specified prompt:
```python
response = client.images.generate(
5 months ago
model="dall-e-3",
prompt="a white siamese cat",
...
)
image_url = response.data[0].url
```
5 months ago
**Creating Image Variations:**
Create variations of an existing image:
```python
response = client.images.create_variation(
5 months ago
image=open("cat.jpg", "rb"),
model="bing",
...
)
image_url = response.data[0].url
```
5 months ago
#### Visual Examples
Original / Variant:
5 months ago
[![Original Image](/docs/cat.jpeg)](/docs/client.md)
[![Variant Image](/docs/cat.webp)](/docs/client.md)
5 months ago
[Return to Documentation Home](/)
```