From 775a0c43a0856f57dbd847a73b9d20b7cddb5063 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 24 Feb 2024 01:31:17 +0100 Subject: Add help me coding guide Add MissingAuthError in GeminiPro --- README.md | 97 +++++++++---------------------------- docs/client.md | 3 +- docs/guides/create_provider.md | 62 ++++++++++++++++++++++++ docs/guides/help_me.md | 106 +++++++++++++++++++++++++++++++++++++++++ docs/guides/phone.jpeg | Bin 13405 -> 0 bytes docs/guides/phone.md | 4 +- g4f/Provider/GeminiPro.py | 19 ++++---- g4f/__init__.py | 2 +- 8 files changed, 205 insertions(+), 88 deletions(-) create mode 100644 docs/guides/create_provider.md create mode 100644 docs/guides/help_me.md delete mode 100644 docs/guides/phone.jpeg diff --git a/README.md b/README.md index 17aaaeb4..4966620c 100644 --- a/README.md +++ b/README.md @@ -19,18 +19,20 @@ pip install -U g4f docker pull hlohaus789/g4f ``` -## πŸ†• What's New πŸš€ -- How do I use my smartphoneπŸ“± to run g4f? [/docs/guides/phone](/docs/guides/phone.md) +## πŸ†• What's New +- Guide: How do I use my smartphoneπŸ“±to run g4f? + - [/docs/guides/phone](/docs/guides/phone.md) +- New: How can AI help me πŸ’with writing code? + - [/docs/guides/help_me](/docs/guides/help_me.md) - Join our Telegram Channel: [t.me/g4f_channel](https://telegram.me/g4f_channel) - Join our Discord Group: [discord.gg/XfybzPXPH5](https://discord.gg/XfybzPXPH5) -## Site Takedown +##πŸ”» Site Takedown Is your site on this repository and you want to take it down ? email takedown@g4f.ai with proof it is yours and it will be removed as fast as possible. - to prevent reproduction please secure your api ; ) -## Feedback +##πŸš€ Feedback and Todo You can always leave some feedback here: https://forms.gle/FeWV9RLEedfdkmFN6 -## To do As per the survey, here is a list of improvements to come - [x] update the repository to include the new openai library syntax (ex: `Openai()` class) | completed, use `g4f.client.Client` - [ ] golang implementation @@ -51,13 +53,13 @@ As per the survey, here is a list of improvements to come - [Quick start](#quick-start) + [Use python](#use-python) - [Prerequisites](#prerequisites) - - [Install using PyPI package:](#install-using-pypi-package-) - - [Install from source:](#install-from-source-) - - [Install using Docker:](#install-using-docker-) + - [Install using PyPI package:](#install-using-pypi-package) + - [Install from source:](#install-from-source) + - [Install using Docker:](#install-using-docker) - [πŸ’‘ Usage](#-usage) - * [The Web UI](#the-web-ui) * [Text Generation](#text-generation) * [Image Generation](#text-generation) + * [Web UI](#web-ui) * [Interference API](#interference-api) * [Configuration](#configuration) - [πŸš€ Providers and Models](#-providers-and-models) @@ -67,8 +69,8 @@ As per the survey, here is a list of improvements to come * [Models](#models) - [πŸ”— Related GPT4Free Projects](#-related-gpt4free-projects) - [🀝 Contribute](#-contribute) - + [Create Provider with AI Tool](#create-provider-with-ai-tool) - + [Create Provider](#create-provider) + + [How do i create a new Provider?](#guide-how-do-i-create-a-new-provider) + + [How can AI help me with writing code?](#guide-how-can-ai-help-me-with-writing-code) - [πŸ™Œ Contributors](#-contributors) - [©️ Copyright](#-copyright) - [⭐ Star History](#-star-history) @@ -158,15 +160,13 @@ response = client.images.generate( image_url = response.data[0].url ``` -**Result:** [![Image with cat](/docs/cat.jpeg)](/docs/client.md) -**See also:** - -- Documentation for the new Client API: [/docs/client](/docs/client.md) -- Documentation for the leagcy API: [/docs/leagcy](/docs/leagcy.md) +**Full Documentation for Python API** +- New Client API like the OpenAI Python library: [/docs/client](/docs/client.md) +- Leagcy API with python modules: [/docs/leagcy](/docs/leagcy.md) #### Web UI @@ -425,72 +425,19 @@ set G4F_PROXY=http://host:port ## 🀝 Contribute -#### Create Provider with AI Tool - -Call in your terminal the `create_provider.py` script: -```bash -python etc/tool/create_provider.py -``` -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. Check out the current [list of potential providers](https://github.com/zukixa/cool-ai-stuff#ai-chat-websites), or find your own provider source! -2. Create a new file in [g4f/Provider](./g4f/Provider) with the name of the Provider -3. Implement a class that extends [BaseProvider](./g4f/Provider/base_provider.py). - -```py -from __future__ import annotations - -from ..typing import AsyncResult, Messages -from .base_provider import AsyncGeneratorProvider - -class HogeService(AsyncGeneratorProvider): - url = "https://chat-gpt.com" - working = True - supports_gpt_35_turbo = True - - @classmethod - async def create_async_generator( - cls, - model: str, - messages: Messages, - proxy: str = None, - **kwargs - ) -> AsyncResult: - yield "" -``` - -4. Here, you can adjust the settings, for example, if the website does support streaming, set `supports_stream` to `True`... -5. 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 -6. Add the Provider Name in [`g4f/Provider/__init__.py`](./g4f/Provider/__init__.py) - -```py -from .HogeService import HogeService +We welcome contributions from the community. Whether you're adding new providers or features, or simply fixing typos and making small improvements, your input is valued. Creating a pull request is all it takes – our co-pilot will handle the code review process. Once all changes have been addressed, we'll merge the pull request into the main branch and release the updates at a later time. -__all__ = [ - HogeService, -] -``` +###### Guide: How do i create a new Provider? -7. You are done !, test the provider by calling it: + - Read: [/docs/guides/create_provider](/docs/guides/create_provider.md) -```py -import g4f +###### Guide: How can AI help me with writing code? -response = g4f.ChatCompletion.create(model='gpt-3.5-turbo', provider=g4f.Provider.PROVIDERNAME, - messages=[{"role": "user", "content": "test"}], stream=g4f.Provider.PROVIDERNAME.supports_stream) - -for message in response: - print(message, flush=True, end='') -``` + - Read: [/docs/guides/help_me](/docs/guides/help_me.md) ## πŸ™Œ Contributors -A list of the contributors is available [here](https://github.com/xtekky/gpt4free/graphs/contributors) +A list of all contributors is available [here](https://github.com/xtekky/gpt4free/graphs/contributors) The [`Vercel.py`](https://github.com/xtekky/gpt4free/blob/main/g4f/Provider/Vercel.py) file contains code from [vercel-llm-api](https://github.com/ading2210/vercel-llm-api) by [@ading2210](https://github.com/ading2210), which is licensed under the [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.txt) Top 1 Contributor: [@hlohaus](https://github.com/hlohaus) diff --git a/docs/client.md b/docs/client.md index fe02dc92..31440027 100644 --- a/docs/client.md +++ b/docs/client.md @@ -154,10 +154,11 @@ response = client.chat.completions.create( ) print(response.choices[0].message.content) ``` -![Waterfall](/docs/waterfall.jpeg) ``` User: What are on this image? ``` +![Waterfall](/docs/waterfall.jpeg) + ``` Bot: There is a waterfall in the middle of a jungle. There is a rainbow over... ``` diff --git a/docs/guides/create_provider.md b/docs/guides/create_provider.md new file mode 100644 index 00000000..e2e1ab6a --- /dev/null +++ b/docs/guides/create_provider.md @@ -0,0 +1,62 @@ +#### Create Provider with AI Tool + +Call in your terminal the `create_provider` script: +```bash +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. Check out the current [list of potential providers](https://github.com/zukixa/cool-ai-stuff#ai-chat-websites), or find your own provider source! +2. Create a new file in [g4f/Provider](/g4f/Provider) with the name of the Provider +3. Implement a class that extends [BaseProvider](/g4f/providers/base_provider.py). + +```py +from __future__ import annotations + +from ..typing import AsyncResult, Messages +from .base_provider import AsyncGeneratorProvider + +class HogeService(AsyncGeneratorProvider): + url = "https://chat-gpt.com" + working = True + supports_gpt_35_turbo = True + + @classmethod + async def create_async_generator( + cls, + model: str, + messages: Messages, + proxy: str = None, + **kwargs + ) -> AsyncResult: + yield "" +``` + +4. Here, you can adjust the settings, for example, if the website does support streaming, set `supports_stream` to `True`... +5. 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 +6. Add the Provider Import in [`g4f/Provider/__init__.py`](./g4f/Provider/__init__.py) + +```py +from .HogeService import HogeService + +__all__ = [ + HogeService, +] +``` + +7. You are done !, test the provider by calling it: + +```py +import g4f + +response = g4f.ChatCompletion.create(model='gpt-3.5-turbo', provider=g4f.Provider.PROVIDERNAME, + messages=[{"role": "user", "content": "test"}], stream=g4f.Provider.PROVIDERNAME.supports_stream) + +for message in response: + print(message, flush=True, end='') +``` \ No newline at end of file diff --git a/docs/guides/help_me.md b/docs/guides/help_me.md new file mode 100644 index 00000000..1a56a74a --- /dev/null +++ b/docs/guides/help_me.md @@ -0,0 +1,106 @@ +### Guide: How can AI help me with writing code? + +πŸ€– Ever dreamt of writing code at lightning speed, effortlessly crafting clean, bug-free functionalities? Welcome to the age of AI-powered coding, where your imagination merges seamlessly with the machine's precision. This guide unveils 4 powerful ways AI can become your secret weapon in the coding world, saving you time, resources, and frustration. 😎 + +- Language Agnostic? No Problem! Rewrite Code with Ease +- Improving Written Code: Polish Your Code Like a Pro +- Demystifying the Unreadable: Deobfuscating Unreadable Code +- Custom Functions: Your AI-Powered Genie in a Bottle + +So you can streamline your coding process, improve code quality, and enhance productivity. + +#### 1. Rewriting Code from One Language to Another +You can leverage AI to translate code from one programming language to another. For instance, you can easily convert JavaScript code to Python using the following approach: + +**Prompt:** +``` +Rewrite this JavaScript code to Python code. Use the aiohttp package: +``` +```javascript +async function fetch_data(url) { + const response = await fetch(url); + const data = await response.json(); + return data; +} +``` + +**AI Response:** +```python +import aiohttp + +async def fetch_data(url): + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + data = await response.json() + return data +``` + +#### 2. Improving Written Code +You can ask the AI to enhance your code by adding comments, docstrings, and type hints for better readability and maintainability. + +**Example Usage:** +``` +Improve this Python code, add Google docstrings and type hints. +Responses with the complete code and don't use placeholders. +Keep the comments: +``` +```python +def calculate_area(radius): + # Calculate the area of a circle + return 3.14 * radius ** 2 +``` + +**AI Response:** +```python +def calculate_area(radius: float) -> float: + """Calculate the area of a circle. + + Args: + radius (float): The radius of the circle. + + Returns: + float: The area of the circle. + """ + return 3.14 * radius ** 2 +``` + +#### 3. Deobfuscating Unreadable Code +AI can help in making obfuscated or unreadable code more understandable and maintainable. + +**Example Usage:** +``` +Deobfuscate this JavaScript code. Improve the readability: +``` + +```javascript +(function() { + var a = 'Hello', b = 'World'; + console.log(a + ' ' + b); +})(); +``` + +**AI Response:** +```javascript +(function() { + var greeting = 'Hello'; + var subject = 'World'; + console.log(greeting + ' ' + subject); +})(); +``` + +#### 4. Writing Custom Functions +You can also ask the AI to generate custom functions or help you with specific coding issues. + +**Example Usage:** +``` +Write a function that returns the sum of two numbers. +``` + +**AI Response:** +```python +def add_numbers(a, b): + """Add two numbers together.""" + return a + b +``` + +These are just a few ways AI can revolutionize your coding experience. As AI technology continues to evolve, the possibilities are endless. So, embrace the future, unlock the power of AI, and watch your coding potential soar! πŸ‘·β€β™‚οΈ \ No newline at end of file diff --git a/docs/guides/phone.jpeg b/docs/guides/phone.jpeg deleted file mode 100644 index 0cc57b14..00000000 Binary files a/docs/guides/phone.jpeg and /dev/null differ diff --git a/docs/guides/phone.md b/docs/guides/phone.md index 2d3ec032..60a09e9d 100644 --- a/docs/guides/phone.md +++ b/docs/guides/phone.md @@ -21,9 +21,9 @@ Running Python applications on your smartphone is possible with specialized apps - In the app settings for Pydroid, disable power-saving mode and ensure that the option to pause when not in use is also disabled. This ensures uninterrupted operation of your Python scripts. 4. **Install Required Packages:** - - Open Pip within the Pydroid app and install the necessary packages by executing the following commands: + - Open Pip within the Pydroid app and install this necessary packages: ``` - pip install g4f flask pillow beautifulsoup4 + g4f flask pillow beautifulsoup4 ``` 5. **Create a New Python Script:** diff --git a/g4f/Provider/GeminiPro.py b/g4f/Provider/GeminiPro.py index b296f253..e1738dc8 100644 --- a/g4f/Provider/GeminiPro.py +++ b/g4f/Provider/GeminiPro.py @@ -7,7 +7,7 @@ from aiohttp import ClientSession from ..typing import AsyncResult, Messages, ImageType from .base_provider import AsyncGeneratorProvider, ProviderModelMixin from ..image import to_bytes, is_accepted_format - +from ..errors import MissingAuthError class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): url = "https://ai.google.dev" @@ -29,7 +29,8 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): ) -> AsyncResult: model = "gemini-pro-vision" if not model and image else model model = cls.get_model(model) - api_key = api_key if api_key else kwargs.get("access_token") + if not api_key: + raise MissingAuthError('Missing "api_key" for auth') headers = { "Content-Type": "application/json", } @@ -53,13 +54,13 @@ class GeminiPro(AsyncGeneratorProvider, ProviderModelMixin): }) data = { "contents": contents, - # "generationConfig": { - # "stopSequences": kwargs.get("stop"), - # "temperature": kwargs.get("temperature"), - # "maxOutputTokens": kwargs.get("max_tokens"), - # "topP": kwargs.get("top_p"), - # "topK": kwargs.get("top_k"), - # } + "generationConfig": { + "stopSequences": kwargs.get("stop"), + "temperature": kwargs.get("temperature"), + "maxOutputTokens": kwargs.get("max_tokens"), + "topP": kwargs.get("top_p"), + "topK": kwargs.get("top_k"), + } } async with session.post(url, params={"key": api_key}, json=data, proxy=proxy) as response: if not response.ok: diff --git a/g4f/__init__.py b/g4f/__init__.py index 6716c727..5df942ae 100644 --- a/g4f/__init__.py +++ b/g4f/__init__.py @@ -42,7 +42,7 @@ def get_model_and_provider(model : Union[Model, str], if debug.version_check: debug.version_check = False version.utils.check_version() - + if isinstance(provider, str): if " " in provider: provider_list = [ProviderUtils.convert[p] for p in provider.split() if p in ProviderUtils.convert] -- cgit v1.2.3 From cbfa5ee7e9a5e224a032f19dfc770f6208d14b29 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 24 Feb 2024 02:07:01 +0100 Subject: Fix some typos --- README.md | 8 +-- docs/guides/phone.md | 2 +- docs/leagcy.md | 200 --------------------------------------------------- docs/legacy.md | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+), 205 deletions(-) delete mode 100644 docs/leagcy.md create mode 100644 docs/legacy.md diff --git a/README.md b/README.md index 4966620c..59ad89f3 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,10 @@ docker pull hlohaus789/g4f - Join our Telegram Channel: [t.me/g4f_channel](https://telegram.me/g4f_channel) - Join our Discord Group: [discord.gg/XfybzPXPH5](https://discord.gg/XfybzPXPH5) -##πŸ”» Site Takedown +## πŸ”» Site Takedown Is your site on this repository and you want to take it down ? email takedown@g4f.ai with proof it is yours and it will be removed as fast as possible. - to prevent reproduction please secure your api ; ) -##πŸš€ Feedback and Todo +## πŸš€ Feedback and Todo You can always leave some feedback here: https://forms.gle/FeWV9RLEedfdkmFN6 As per the survey, here is a list of improvements to come @@ -58,7 +58,7 @@ As per the survey, here is a list of improvements to come - [Install using Docker:](#install-using-docker) - [πŸ’‘ Usage](#-usage) * [Text Generation](#text-generation) - * [Image Generation](#text-generation) + * [Image Generation](#image-generation) * [Web UI](#web-ui) * [Interference API](#interference-api) * [Configuration](#configuration) @@ -166,7 +166,7 @@ image_url = response.data[0].url **Full Documentation for Python API** - New Client API like the OpenAI Python library: [/docs/client](/docs/client.md) -- Leagcy API with python modules: [/docs/leagcy](/docs/leagcy.md) +- Legacy API with python modules: [/docs/legacy](/docs/legacy.md) #### Web UI diff --git a/docs/guides/phone.md b/docs/guides/phone.md index 60a09e9d..ae9865cd 100644 --- a/docs/guides/phone.md +++ b/docs/guides/phone.md @@ -21,7 +21,7 @@ Running Python applications on your smartphone is possible with specialized apps - In the app settings for Pydroid, disable power-saving mode and ensure that the option to pause when not in use is also disabled. This ensures uninterrupted operation of your Python scripts. 4. **Install Required Packages:** - - Open Pip within the Pydroid app and install this necessary packages: + - Open Pip within the Pydroid app and install these necessary packages: ``` g4f flask pillow beautifulsoup4 ``` diff --git a/docs/leagcy.md b/docs/leagcy.md deleted file mode 100644 index e8808381..00000000 --- a/docs/leagcy.md +++ /dev/null @@ -1,200 +0,0 @@ -### G4F - Leagcy API - -#### ChatCompletion - -```python -import g4f - -g4f.debug.logging = True # Enable debug logging -g4f.debug.version_check = False # Disable automatic version checking -print(g4f.Provider.Bing.params) # Print supported args for Bing - -# Using automatic a provider for the given model -## Streamed completion -response = g4f.ChatCompletion.create( - model="gpt-3.5-turbo", - messages=[{"role": "user", "content": "Hello"}], - stream=True, -) -for message in response: - print(message, flush=True, end='') - -## Normal response -response = g4f.ChatCompletion.create( - model=g4f.models.gpt_4, - messages=[{"role": "user", "content": "Hello"}], -) # Alternative model setting - -print(response) -``` - -##### Completion - -```python -import g4f - -allowed_models = [ - 'code-davinci-002', - 'text-ada-001', - 'text-babbage-001', - 'text-curie-001', - 'text-davinci-002', - 'text-davinci-003' -] - -response = g4f.Completion.create( - model='text-davinci-003', - prompt='say this is a test' -) - -print(response) -``` - -##### Providers - -```python -import g4f - -# Print all available providers -print([ - provider.__name__ - for provider in g4f.Provider.__providers__ - if provider.working -]) - -# Execute with a specific provider -response = g4f.ChatCompletion.create( - model="gpt-3.5-turbo", - provider=g4f.Provider.Aichat, - messages=[{"role": "user", "content": "Hello"}], - stream=True, -) -for message in response: - print(message) -``` - - -##### Image Upload & Generation - -Image upload and generation are supported by three main providers: - -- **Bing & Other GPT-4 Providers:** Utilizes Microsoft's Image Creator. -- **Google Gemini:** Available for free accounts with IP addresses outside Europe. -- **OpenaiChat with GPT-4:** Accessible for users with a Plus subscription. - -```python -import g4f - -# Setting up the request for image creation -response = g4f.ChatCompletion.create( - model=g4f.models.default, # Using the default model - provider=g4f.Provider.Gemini, # Specifying the provider as Gemini - messages=[{"role": "user", "content": "Create an image like this"}], - image=open("images/g4f.png", "rb"), # Image input can be a data URI, bytes, PIL Image, or IO object - image_name="g4f.png" # Optional: specifying the filename -) - -# Displaying the response -print(response) - -from g4f.image import ImageResponse - -# Get image links from response -for chunk in g4f.ChatCompletion.create( - model=g4f.models.default, # Using the default model - provider=g4f.Provider.OpenaiChat, # Specifying the provider as OpenaiChat - messages=[{"role": "user", "content": "Create images with dogs"}], - access_token="...", # Need a access token from a plus user - stream=True, - ignore_stream=True -): - if isinstance(chunk, ImageResponse): - print(chunk.images) # Print generated image links - print(chunk.alt) # Print used prompt for image generation -``` - -##### Using Browser - -Some providers using a browser to bypass the bot protection. They using the selenium webdriver to control the browser. The browser settings and the login data are saved in a custom directory. If the headless mode is enabled, the browser windows are loaded invisibly. For performance reasons, it is recommended to reuse the browser instances and close them yourself at the end: - -```python -import g4f -from undetected_chromedriver import Chrome, ChromeOptions -from g4f.Provider import ( - Bard, - Poe, - AItianhuSpace, - MyShell, - PerplexityAi, -) - -options = ChromeOptions() -options.add_argument("--incognito"); -webdriver = Chrome(options=options, headless=True) -for idx in range(10): - response = g4f.ChatCompletion.create( - model=g4f.models.default, - provider=g4f.Provider.MyShell, - messages=[{"role": "user", "content": "Suggest me a name."}], - webdriver=webdriver - ) - print(f"{idx}:", response) -webdriver.quit() -``` - -##### Async Support - -To enhance speed and overall performance, execute providers asynchronously. The total execution time will be determined by the duration of the slowest provider's execution. - -```python -import g4f -import asyncio - -_providers = [ - g4f.Provider.Aichat, - g4f.Provider.ChatBase, - g4f.Provider.Bing, - g4f.Provider.GptGo, - g4f.Provider.You, - g4f.Provider.Yqcloud, -] - -async def run_provider(provider: g4f.Provider.BaseProvider): - try: - response = await g4f.ChatCompletion.create_async( - model=g4f.models.default, - messages=[{"role": "user", "content": "Hello"}], - provider=provider, - ) - print(f"{provider.__name__}:", response) - except Exception as e: - print(f"{provider.__name__}:", e) - -async def run_all(): - calls = [ - run_provider(provider) for provider in _providers - ] - await asyncio.gather(*calls) - -asyncio.run(run_all()) -``` - -##### Proxy and Timeout Support - -All providers support specifying a proxy and increasing timeout in the create functions. - -```python -import g4f - -response = g4f.ChatCompletion.create( - model=g4f.models.default, - messages=[{"role": "user", "content": "Hello"}], - proxy="http://host:port", - # or socks5://user:pass@host:port - timeout=120, # in secs -) - -print(f"Result:", response) -``` - -[Return to Home](/) \ No newline at end of file diff --git a/docs/legacy.md b/docs/legacy.md new file mode 100644 index 00000000..d5cd5a36 --- /dev/null +++ b/docs/legacy.md @@ -0,0 +1,200 @@ +### G4F - Legacy API + +#### ChatCompletion + +```python +import g4f + +g4f.debug.logging = True # Enable debug logging +g4f.debug.version_check = False # Disable automatic version checking +print(g4f.Provider.Bing.params) # Print supported args for Bing + +# Using automatic a provider for the given model +## Streamed completion +response = g4f.ChatCompletion.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": "Hello"}], + stream=True, +) +for message in response: + print(message, flush=True, end='') + +## Normal response +response = g4f.ChatCompletion.create( + model=g4f.models.gpt_4, + messages=[{"role": "user", "content": "Hello"}], +) # Alternative model setting + +print(response) +``` + +##### Completion + +```python +import g4f + +allowed_models = [ + 'code-davinci-002', + 'text-ada-001', + 'text-babbage-001', + 'text-curie-001', + 'text-davinci-002', + 'text-davinci-003' +] + +response = g4f.Completion.create( + model='text-davinci-003', + prompt='say this is a test' +) + +print(response) +``` + +##### Providers + +```python +import g4f + +# Print all available providers +print([ + provider.__name__ + for provider in g4f.Provider.__providers__ + if provider.working +]) + +# Execute with a specific provider +response = g4f.ChatCompletion.create( + model="gpt-3.5-turbo", + provider=g4f.Provider.Aichat, + messages=[{"role": "user", "content": "Hello"}], + stream=True, +) +for message in response: + print(message) +``` + + +##### Image Upload & Generation + +Image upload and generation are supported by three main providers: + +- **Bing & Other GPT-4 Providers:** Utilizes Microsoft's Image Creator. +- **Google Gemini:** Available for free accounts with IP addresses outside Europe. +- **OpenaiChat with GPT-4:** Accessible for users with a Plus subscription. + +```python +import g4f + +# Setting up the request for image creation +response = g4f.ChatCompletion.create( + model=g4f.models.default, # Using the default model + provider=g4f.Provider.Gemini, # Specifying the provider as Gemini + messages=[{"role": "user", "content": "Create an image like this"}], + image=open("images/g4f.png", "rb"), # Image input can be a data URI, bytes, PIL Image, or IO object + image_name="g4f.png" # Optional: specifying the filename +) + +# Displaying the response +print(response) + +from g4f.image import ImageResponse + +# Get image links from response +for chunk in g4f.ChatCompletion.create( + model=g4f.models.default, # Using the default model + provider=g4f.Provider.OpenaiChat, # Specifying the provider as OpenaiChat + messages=[{"role": "user", "content": "Create images with dogs"}], + access_token="...", # Need a access token from a plus user + stream=True, + ignore_stream=True +): + if isinstance(chunk, ImageResponse): + print(chunk.images) # Print generated image links + print(chunk.alt) # Print used prompt for image generation +``` + +##### Using Browser + +Some providers using a browser to bypass the bot protection. They using the selenium webdriver to control the browser. The browser settings and the login data are saved in a custom directory. If the headless mode is enabled, the browser windows are loaded invisibly. For performance reasons, it is recommended to reuse the browser instances and close them yourself at the end: + +```python +import g4f +from undetected_chromedriver import Chrome, ChromeOptions +from g4f.Provider import ( + Bard, + Poe, + AItianhuSpace, + MyShell, + PerplexityAi, +) + +options = ChromeOptions() +options.add_argument("--incognito"); +webdriver = Chrome(options=options, headless=True) +for idx in range(10): + response = g4f.ChatCompletion.create( + model=g4f.models.default, + provider=g4f.Provider.MyShell, + messages=[{"role": "user", "content": "Suggest me a name."}], + webdriver=webdriver + ) + print(f"{idx}:", response) +webdriver.quit() +``` + +##### Async Support + +To enhance speed and overall performance, execute providers asynchronously. The total execution time will be determined by the duration of the slowest provider's execution. + +```python +import g4f +import asyncio + +_providers = [ + g4f.Provider.Aichat, + g4f.Provider.ChatBase, + g4f.Provider.Bing, + g4f.Provider.GptGo, + g4f.Provider.You, + g4f.Provider.Yqcloud, +] + +async def run_provider(provider: g4f.Provider.BaseProvider): + try: + response = await g4f.ChatCompletion.create_async( + model=g4f.models.default, + messages=[{"role": "user", "content": "Hello"}], + provider=provider, + ) + print(f"{provider.__name__}:", response) + except Exception as e: + print(f"{provider.__name__}:", e) + +async def run_all(): + calls = [ + run_provider(provider) for provider in _providers + ] + await asyncio.gather(*calls) + +asyncio.run(run_all()) +``` + +##### Proxy and Timeout Support + +All providers support specifying a proxy and increasing timeout in the create functions. + +```python +import g4f + +response = g4f.ChatCompletion.create( + model=g4f.models.default, + messages=[{"role": "user", "content": "Hello"}], + proxy="http://host:port", + # or socks5://user:pass@host:port + timeout=120, # in secs +) + +print(f"Result:", response) +``` + +[Return to Home](/) \ No newline at end of file -- cgit v1.2.3