GPT4Free

Create Provider with AI Tool

Call in your terminal the create_provider script:

python -m etc.tool.create_provider
  1. Enter your name for the new provider.
  2. Copy and paste the cURL command from your browser developer tools.
  3. Let the AI ​​create the provider for you.
  4. Customize the provider according to your needs.

Create Provider

  1. Create a new file in g4f/Provider with the name of the Provider.
  2. Implement a class that extends BaseProvider.
from __future__ import annotations

from ..typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider

class HogeService(AsyncGeneratorProvider):
    url                   = "https://chat-gpt.com"
    working               = True

    @classmethod
    async def create_async_generator(
        cls,
        model: str,
        messages: Messages,
        proxy: str = None,
        **kwargs
    ) -> AsyncResult:
        yield ""
  1. Here, you can adjust the settings, for example, if the website does support streaming, set supports_stream to True
  2. Write code to request the provider in create_async_generator and yield the response, even if it’s a one-time response, do not hesitate to look at other providers for inspiration.
  3. Add the Provider Import in g4f/Provider/__init__.py
from .HogeService import HogeService

__all__ = [
  HogeService,
]
  1. You are done !, test the provider by calling it:
import g4f

response = g4f.ChatCompletion.create(
    model='gpt-4o',
    provider=g4f.Provider.PROVIDERNAME,
    messages=[{"role": "user", "content": "test"}],
    stream=True
)

for message in response:
    print(message, flush=True, end='')

Return to Documentation