File size: 7,980 Bytes
79859e3 |
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# G4F Client API Guide
## Table of Contents
- [Introduction](#introduction)
- [Getting Started](#getting-started)
- [Switching to G4F Client](#switching-to-g4f-client)
- [Initializing the Client](#initializing-the-client)
- [Creating Chat Completions](#creating-chat-completions)
- [Configuration](#configuration)
- [Usage Examples](#usage-examples)
- [Text Completions](#text-completions)
- [Streaming Completions](#streaming-completions)
- [Image Generation](#image-generation)
- [Creating Image Variations](#creating-image-variations)
- [Advanced Usage](#advanced-usage)
- [Using a List of Providers with RetryProvider](#using-a-list-of-providers-with-retryprovider)
- [Using GeminiProVision](#using-geminiprovision)
- [Using a Vision Model](#using-a-vision-model)
- [Command-line Chat Program](#command-line-chat-program)
## Introduction
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.
## Getting Started
### Switching to G4F Client
**To begin using the G4F Client, simply update your import statement in your Python code:**
**Old Import:**
```python
from openai import OpenAI
```
**New Import:**
```python
from g4f.client import Client as OpenAI
```
The G4F Client preserves the same familiar API interface as OpenAI, ensuring a smooth transition process.
## Initializing the Client
To utilize the G4F Client, create a new instance. **Below is an example showcasing custom providers:**
```python
from g4f.client import Client
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
client = Client(
provider=OpenaiChat,
image_provider=Gemini,
# Add any other necessary parameters
)
```
## Creating Chat Completions
**Here’s an improved example of creating chat completions:**
```python
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Say this is a test"
}
]
# Add any other necessary parameters
)
```
**This example:**
- Asks a specific question `Say this is a test`
- Configures various parameters like temperature and max_tokens for more control over the output
- Disables streaming for a complete response
You can adjust these parameters based on your specific needs.
## Configuration
**You can set an `api_key` for your provider in the client and define a proxy for all outgoing requests:**
```python
from g4f.client import Client
client = Client(
api_key="your_api_key_here",
proxies="http://user:pass@host",
# Add any other necessary parameters
)
```
## Usage Examples
### Text Completions
**Generate text completions using the `ChatCompletions` endpoint:**
```python
from g4f.client import Client
client = Client()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Say this is a test"
}
]
# Add any other necessary parameters
)
print(response.choices[0].message.content)
```
### Streaming Completions
**Process responses incrementally as they are generated:**
```python
from g4f.client import Client
client = Client()
stream = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Say this is a test"
}
],
stream=True,
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content or "", end="")
```
### Image Generation
**Generate images using a specified prompt:**
```python
from g4f.client import Client
client = Client()
response = client.images.generate(
model="flux",
prompt="a white siamese cat"
# Add any other necessary parameters
)
image_url = response.data[0].url
print(f"Generated image URL: {image_url}")
```
#### Base64 Response Format
```python
from g4f.client import Client
client = Client()
response = client.images.generate(
model="flux",
prompt="a white siamese cat",
response_format="b64_json"
)
base64_text = response.data[0].b64_json
print(base64_text)
```
### Creating Image Variations
**Create variations of an existing image:**
```python
from g4f.client import Client
client = Client()
response = client.images.create_variation(
image=open("cat.jpg", "rb"),
model="bing"
# Add any other necessary parameters
)
image_url = response.data[0].url
print(f"Generated image URL: {image_url}")
```
## Advanced Usage
### Using 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
g4f.debug.version_check = False
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 GeminiProVision
```python
from g4f.client import Client
from g4f.Provider.GeminiPro import GeminiPro
client = Client(
api_key="your_api_key_here",
provider=GeminiPro
)
response = client.chat.completions.create(
model="gemini-pro-vision",
messages=[
{
"role": "user",
"content": "What are on this image?"
}
],
image=open("docs/waterfall.jpeg", "rb")
)
print(response.choices[0].message.content)
```
### Using a Vision Model
**Analyze an image and generate a description:**
```python
import g4f
import requests
from g4f.client import Client
image = requests.get("https://raw.githubusercontent.com/xtekky/gpt4free/refs/heads/main/docs/cat.jpeg", stream=True).raw
# Or: image = open("docs/cat.jpeg", "rb")
client = Client(
provider=CopilotAccount
)
response = client.chat.completions.create(
model=g4f.models.default,
messages=[
{
"role": "user",
"content": "What are on this image?"
}
],
image=image
# Add any other necessary parameters
)
print(response.choices[0].message.content)
```
## Command-line Chat Program
**Here's an example of a simple command-line chat program using the G4F Client:**
```python
import g4f
from g4f.client import Client
# Initialize the GPT client with the desired provider
client = Client()
# Initialize an empty conversation history
messages = []
while True:
# Get user input
user_input = input("You: ")
# Check if the user wants to exit the chat
if user_input.lower() == "exit":
print("Exiting chat...")
break # Exit the loop to end the conversation
# Update the conversation history with the user's message
messages.append({"role": "user", "content": user_input})
try:
# Get GPT's response
response = client.chat.completions.create(
messages=messages,
model=g4f.models.default,
)
# Extract the GPT response and print it
gpt_response = response.choices[0].message.content
print(f"Bot: {gpt_response}")
# Update the conversation history with GPT's response
messages.append({"role": "assistant", "content": gpt_response})
except Exception as e:
print(f"An error occurred: {e}")
```
This guide provides a comprehensive overview of the G4F Client API, demonstrating its versatility in handling various AI tasks, from text generation to image analysis and creation. By leveraging these features, you can build powerful and responsive applications that harness the capabilities of advanced AI models.
---
[Return to Home](/)
|