mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-09-12 08:29:30 -04:00
1.3 KiB
1.3 KiB
Client API
from g4f (beta)
Start
This new client could:
from g4f.client import Client
replaces this:
from openai import OpenAI
in your Python Code.
New client have the same API as OpenAI.
Client
Create the client with custom providers:
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:
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:
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:
response = client.images.create_variation(
image=open("cat.jpg", "rb")
model="bing",
...
)
image_url = response.data[0].url
Orginal / Variant: