Advanced Usage
g4f advanced usage
In Basic Usage we already discussed, how to use 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
.
The completions.create(...)
function
Lets now dive into more advanced usage of g4f
. The create
function has various additional parameters that can be used to customise the completions generated.
create(...)
additional parameters
An object specifying the format that the model must output. Compatible with advanced llm’s.
Setting to { "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.
The maximum number of tokens that can be generated in the chat completion. The total length of input tokens and generated tokens is limited by the model’s context length.
Up to 4 sequences where the API will stop generating further tokens.
The API key to use for the request. Defaults to None
.
A list of strings to ignore in the response. Defaults to None
.
Whether to ignore the working status of the provider. Defaults to False
.
Whether to ignore the stream status of the provider. Defaults to False
.
temperature
, they are not guaranteed to work, and depend on how much customisation a certain Provider exposes in his Api.Additional keyword arguments to pass to the API request.
Provider rotating in 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:
This works well if there is a very good provider that fails sometimes.
Was this page helpful?