2024-02-12 10:41:27 +00:00
|
|
|
### Client API
|
|
|
|
##### from g4f (beta)
|
|
|
|
|
|
|
|
#### Start
|
|
|
|
This new client could:
|
|
|
|
|
|
|
|
```python
|
|
|
|
from g4f.client import Client
|
|
|
|
```
|
|
|
|
replaces this:
|
|
|
|
|
|
|
|
```python
|
|
|
|
from openai import OpenAI
|
|
|
|
```
|
|
|
|
in your Python Code.
|
|
|
|
|
|
|
|
New client have the same API as OpenAI.
|
|
|
|
|
|
|
|
#### Client
|
|
|
|
|
|
|
|
Create the client with custom providers:
|
|
|
|
|
|
|
|
```python
|
|
|
|
from g4f.client import Client
|
|
|
|
from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
|
|
|
|
|
|
|
|
client = Client(
|
|
|
|
provider=OpenaiChat,
|
|
|
|
image_provider=Gemini,
|
|
|
|
proxies=None
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Examples
|
|
|
|
|
|
|
|
Use the ChatCompletions:
|
|
|
|
|
|
|
|
```python
|
|
|
|
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 is not None:
|
|
|
|
print(chunk.choices[0].delta.content, end="")
|
|
|
|
```
|
|
|
|
|
|
|
|
Or use it for creating a image:
|
|
|
|
```python
|
|
|
|
response = client.images.generate(
|
|
|
|
model="dall-e-3",
|
|
|
|
prompt="a white siamese cat",
|
|
|
|
...
|
|
|
|
)
|
|
|
|
|
|
|
|
image_url = response.data[0].url
|
|
|
|
```
|
|
|
|
|
|
|
|
Also this works with the client:
|
|
|
|
```python
|
|
|
|
response = client.images.create_variation(
|
2024-02-12 11:11:22 +00:00
|
|
|
image=open("cat.jpg", "rb")
|
2024-02-12 10:41:27 +00:00
|
|
|
model="bing",
|
|
|
|
...
|
|
|
|
)
|
|
|
|
|
|
|
|
image_url = response.data[0].url
|
|
|
|
```
|
|
|
|
|
2024-02-12 11:10:37 +00:00
|
|
|
Orginal / Variant:
|
2024-02-12 11:08:08 +00:00
|
|
|
|
2024-02-12 11:10:37 +00:00
|
|
|
[![Image with cat](/docs/cat.jpeg)](/docs/client.md)
|
2024-02-12 11:08:08 +00:00
|
|
|
[![Image with cat](/docs/cat.webp)](/docs/client.md)
|
|
|
|
|
2024-02-12 10:41:27 +00:00
|
|
|
[to Home](/docs/client.md)
|