summaryrefslogtreecommitdiffstats
path: root/docs/client.md
blob: 1201a83d73ae852351404d392112d7548d3aa344 (plain) (blame)
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
### 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(
  image=open('cat.jpg')
  model="bing",
  ...
)

image_url = response.data[0].url
```

[to Home](/docs/client.md)