Basic Usage
Learn how to use g4f in your projects
g4f
is very similar to the OpenAI Python Sdk, the g4f completion syntax was made to work exactly like the sdk with some ClassNames being different.The Client
Class
As of the new v1
library of the OpenAi sdk, g4f is initialised with the g4f.client.Client
class.
The Client
class accepts several arguments, you can set a default provider there. More on the Provider
structure of g4f
further down.
Client(...)
Class Arguments
The provider to use for the client. If not specified, the default provider will be used.
The image provider provider to use for the client. If not specified, the default provider will be used.
Create completions
Once initialised, you can use the client
object to create chat completions. Here again, client.chat.completions.create
works like the OpenAI sdk, with the same parameters.
create(...)
basic Params
(For more advanced params and usage and examples, refer to the Full Documentation)
A list of messages to use for the chat completion. Each message should be a dictionary with the keys role
and content
.
It represents the conversation between a user and an assistant.
The provider to use for the chat completion. Imported from g4f.Provider.ProviderName
. Different working providers are outlined in the g4f README.
If set, the completion will be streamed, i.e. Tokens will be sent sequentially as soon as generated.
create
will return a generator.
Stream completions
You may want to use g4f
with streaming Response, you can do this with stream=True
in the client.chat.completions.create
method.
If you dont want the words to start at a newline every time:
Full g4f example:
For more advanced usage and examples, refer to the Full Documentation.
Was this page helpful?