g4f advanced usage
g4f
to generate Completions.
tldr: we import Client
from g4f.client
and create a Client
to initialise g4f
.
We then use the client.chat.completions.create
method to generate completions. We can also stream the completion, by setting stream=True
.
completions.create(...)
functiong4f
. The create
function has various additional parameters that can be used to customise the completions generated.
create(...)
additional parameters{ "type": "json_object" }
enables JSON mode, which guarantees the message the model generates is valid JSON.Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message.None
.None
.False
.False
.temperature
, they are not guaranteed to work, and depend on how much customisation a certain Provider exposes in his Api.g4f
g4f
can be a bit buggy sometimes and cause you some trouble. You can create your own providers list and pass it with RetryProvider
to Client
to make g4f
more stable.
RetryProvider
will try to use the first provider in the list, if it fails, it will try the next one, and so on. Ensuring a higher chance of success.
If you wish to use a single provider with retry use it like so: