@ -16,7 +16,7 @@ The G4F AsyncClient API offers several key features:
## Initializing the Client
To utilize the G4F AsyncClient, create a new instance. Below is an example showcasing custom providers:
To utilize the G4F `AsyncClient`, you need to create a new instance. Below is an example showcasing how to initialize the client with custom providers:
```python
from g4f.client import AsyncClient
@ -29,25 +29,32 @@ client = AsyncClient(
)
```
In this example:
- `provider` specifies the primary provider for generating text completions.
- `image_provider` specifies the provider for image-related functionalities.
## 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:
You can configure the `AsyncClient` with additional settings, such as an API key for your provider and a proxy for all outgoing requests:
```python
from g4f.client import AsyncClient
client = AsyncClient(
api_key="...",
api_key="your_api_key_here",
proxies="http://user:pass@host",
...
)
```
- `api_key`: Your API key for the provider.
- `proxies`: The proxy configuration for routing requests.
## Using AsyncClient
### Text Completions:
### Text Completions
You can use the ChatCompletions endpoint to generate text completions as follows:
You can use the `ChatCompletions` endpoint to generate text completions. Here’s how you can do it:
The `AsyncClient` also supports streaming completions. This allows you to process the response incrementally as it is generated:
```python
stream = client.chat.completions.create(
@ -72,6 +81,33 @@ async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
```
In this example:
- `stream=True` enables streaming of the response.
### Example: Using a Vision Model
The following code snippet demonstrates how to use a vision model to analyze an image and generate a description based on the content of the image. This example shows how to fetch an image, send it to the model, and then process the response.