mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-12-06 10:40:43 -08:00
Improve async client readme, Fix print styling, Add image api example
This commit is contained in:
parent
5115cf0e23
commit
77a1f500a8
3 changed files with 51 additions and 4 deletions
|
|
@ -9,15 +9,39 @@ Designed to maintain compatibility with the existing OpenAI API, the G4F AsyncCl
|
||||||
The G4F AsyncClient API offers several key features:
|
The G4F AsyncClient API offers several key features:
|
||||||
|
|
||||||
- **Custom Providers:** The G4F Client API allows you to use custom providers. This feature enhances the flexibility of the API, enabling it to cater to a wide range of use cases.
|
- **Custom Providers:** The G4F Client API allows you to use custom providers. This feature enhances the flexibility of the API, enabling it to cater to a wide range of use cases.
|
||||||
|
|
||||||
- **ChatCompletion Interface:** The G4F package provides an interface for interacting with chat models through the ChatCompletion class. This class provides methods for creating both streaming and non-streaming responses.
|
- **ChatCompletion Interface:** The G4F package provides an interface for interacting with chat models through the ChatCompletion class. This class provides methods for creating both streaming and non-streaming responses.
|
||||||
|
|
||||||
- **Streaming Responses:** The ChatCompletion.create method can return a response iteratively as and when they are received if the stream parameter is set to True.
|
- **Streaming Responses:** The ChatCompletion.create method can return a response iteratively as and when they are received if the stream parameter is set to True.
|
||||||
|
|
||||||
- **Non-Streaming Responses:** The ChatCompletion.create method can also generate non-streaming responses.
|
- **Non-Streaming Responses:** The ChatCompletion.create method can also generate non-streaming responses.
|
||||||
|
- **Image Generation and Vision Models:** The G4F Client API also supports image generation and vision models, expanding its utility beyond text-based interactions.
|
||||||
|
|
||||||
- **Image Generation and Vision Models:** The G4F Client API also supports image generation and vision models, expanding its utility beyond text-based interactions.
|
## Initializing the Client
|
||||||
|
|
||||||
|
To utilize the G4F AsyncClient, create a new instance. Below is an example showcasing custom providers:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from g4f.client import AsyncClient
|
||||||
|
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
|
||||||
|
|
||||||
|
client = AsyncClient(
|
||||||
|
provider=OpenaiChat,
|
||||||
|
image_provider=Gemini,
|
||||||
|
...
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
You can set an "api_key" for your provider in the client. You also have the option to define a proxy for all outgoing requests:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from g4f.client import AsyncClient
|
||||||
|
|
||||||
|
client = AsyncClient(
|
||||||
|
api_key="...",
|
||||||
|
proxies="http://user:pass@host",
|
||||||
|
...
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
## Using AsyncClient
|
## Using AsyncClient
|
||||||
|
|
||||||
|
|
@ -62,6 +86,17 @@ response = await client.images.generate(
|
||||||
image_url = response.data[0].url
|
image_url = response.data[0].url
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Base64 as the response format
|
||||||
|
|
||||||
|
```python
|
||||||
|
response = await client.images.generate(
|
||||||
|
prompt="a cool cat",
|
||||||
|
response_format="b64_json"
|
||||||
|
)
|
||||||
|
|
||||||
|
base64_text = response.data[0].b64_json
|
||||||
|
```
|
||||||
|
|
||||||
### Example usage with asyncio.gather
|
### Example usage with asyncio.gather
|
||||||
|
|
||||||
Start two tasks at the same time:
|
Start two tasks at the same time:
|
||||||
|
|
|
||||||
9
etc/examples/image_api.py
Normal file
9
etc/examples/image_api.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import requests
|
||||||
|
url = "http://localhost:1337/v1/images/generations"
|
||||||
|
body = {
|
||||||
|
"prompt": "heaven for dogs",
|
||||||
|
"provider": "OpenaiAccount",
|
||||||
|
"response_format": "b64_json",
|
||||||
|
}
|
||||||
|
data = requests.post(url, json=body, stream=True).json()
|
||||||
|
print(data)
|
||||||
|
|
@ -1143,4 +1143,7 @@ a:-webkit-any-link {
|
||||||
.message .user {
|
.message .user {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
.message.regenerate {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue