G4F, also known as GPT4free is a python package which enables users to prompt language or image generation models for free directly inside a python script without the need to pay for APIs like OpenAI. So what’s the draw-back? Well, there really isn’t one. G4F is just a big list of free GPT providers, like ChatGPT, You, Copilot, etc. When you send a prompt, it will go to a free provider and simply return the answer.
Traditional g4f client
Although the plain g4f client will work most of the time, nothing comes free in life. If you follow the code from the official Github repository,
from g4f.client import Client
client = Client()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What's 1+1?"}],
)
… you may get invalid responses, as there is no guarantee that a provider will always work. Because of this, you must constantly search for new providers or update g4f.
G4f Client with SmartG4F
If you want to avoid all this manual work, you can use a package called SmartG4F to select a list of providers which you can validate yourself:
import g4f
from smartg4f import get_provider
client = Client(
provider=get_provider(),
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What's 1+1?"}],
)
# get_provider() returns working providers as RetryProvider object
It’s as easy as that. SmartG4F will go trough all g4f providers, and return a list with working providers. Moreover, it validates them by checking if the response contains at least one character. Still, if you want a better validation algorithm, you can specify your own:
import g4f
from smartg4f import get_provider
from datetime import datetime
# Custom prompt
prompt = "Return today's date in the format %d-%m-%y. Do not add any other text besides the date, such as a comment!"
# Custom validation algorithm
def validate(response):
try:
if not type(param) == str:
if not type(param.choices[0].message.content) == str:
return False
response = param.choices[0].message.content
if response != datetime.today().strftime('%d-%m-%y'):
return False
return True
except:
return False
client = Client(
provider=get_provider(prompt=prompt, validation=validate),
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What's 1+1?"}],
)
In this script, the user wants to get only the providers that are able to return today’s date in a day-month-year format, without any other text/comment. First the user specifies a custom prompt, and a function used for validating the response. The user then sets the prompt and validation parameters inside get_provider(). After the plugin has tested every provider, the updated client object will contain all providers that were validated by your function.
You can find more about SmartG4F here: PyPi or Github. Want to find out more about vector databases? Click here!