Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new provider, enhance functionality, and update docs #2304

Merged
merged 32 commits into from
Oct 30, 2024

Conversation

kqlio67
Copy link
Contributor

@kqlio67 kqlio67 commented Oct 26, 2024

# New provider added

  • g4f/Provider/GizAI.py
    • Text generation: gemini-flash, gemini-pro, gpt-4o-mini, gpt-4o, claude-3.5-sonnet, claude-3-haiku, llama-3.1-70b, llama-3.1-8b, mistral-large
    • Image generation: sdxl, sd-1.5, sd-3.5, flux-schnell

G4F update

(docs/providers-and-models.md): add GizAI provider with multiple models

  • Introduce a new AI provider, GizAI, with support for various text and image models.
    • Text Models: gemini-flash, gemini-pro, gpt-4o-mini, gpt-4o, claude-3.5-sonnet, claude-3-haiku, llama-3.1-70b, llama-3.1-8b, mistral-large
    • Image Models: sd-1.5, sd-3.5, flux-schnell

This addition provides users with more options for text generation and image creation tasks.
BREAKING CHANGE: GizAI provider includes new models and capabilities.


(g4f/gui/server/api.py): enhance image handling and directory management

  • Enhance image handling functionalities and ensure image directory exists
    • Add ensure_images_dir function to create image directory if it doesn't exist
    • Modify serve_images and _copy_images methods to use ensure_images_dir
    • Remove unnecessary imports to clean up code
    • Update typing annotations to improve type safety

This ensures that the directory for storing images is always present and improves code readability and maintainability.


(g4f/gui/server/website.py): add redirect for /images/ endpoint

  • Add a new route '/images/' to the website routes configuration
    • The new route redirects to the home page similar to other routes
    • Supports both GET and POST methods

This change ensures consistency in route management and better handles potential future expansion for the '/images/' endpoint.


(g4f/gui/client/index.html): enhance model/provider options

  • Enhanced model selection options by adding new models such as gpt-4o, gpt-4o-mini, llama-3.1-405b, mixtral-8x7b, gemini-flash, claude-3-haiku, and claude-3.5-sonnet.
  • Enhanced provider selection options by adding new providers such as DeepInfraChat, ReplicateHome, Blackbox, HuggingChat, DDG, and Pizzagpt.

These changes improve the user interface by expanding the available options for models and providers.


# G4F improvements

(g4f/api/init.py): refactor API structure and improve async handling

  • Updated create_app to accept an optional g4f_api_key parameter.
  • Refactored ChatCompletionsForm to ChatCompletionsConfig and ImagesGenerateForm to ImageGenerationConfig.
  • Improved async handling for chat_completions and generate_image endpoints.
  • Added type hints for better readability and maintainability.
  • Updated routes to include /v1/images/generate.
  • Enhanced error handling and logging.
  • Introduced global list_ignored_providers and related setter method.

Breaking change: Some API endpoints and request/response formats have been modified.
#2222 (comment)


(g4f/models.py): add new provider GizAI and update best providers
Add GizAI as a new provider for various models and update best provider lists
- Add GizAI to the imports and update best provider lists for several models
- Update default model's best providers
- Adjust gpt-4o, gpt-4o-mini, gpt-4, o1-mini, llama-3.1-8b, llama-3.1-70b, mistral-large, claude-3-haiku, claude-3.5-sonnet, gemini-pro, gemini-flash, sd-1.5, sd-3.5, and flux-schnell to include GizAI

Breaking change: Best provider logic updated to include new provider


# Updating documentation and improving G4F

(README.md): update README with badges and links

  • Add new badges for commits, issues, and license at the top for improved visibility

  • Enhance author attribution with centered formatting

  • Correct various internal links within the Table of Contents

  • Fix broken link to the Interference API documentation

  • Adjust formatting for better readability and consistency throughout the file

  • Correct typo in the "Latest version" note and update internal links

    • Fix "Lastet" to "Latest" in the version note section
    • Update "Installation Guide for Windows (.exe)" link in the What's New section
    • Update link formats for internal references and external URLs

(docs/async_client.md): update guide compatibility and improved chat completions example

  • Introduce new section for creating chat completions with an improved example
  • Update table of contents to include new section

This update enhances the guide by providing clearer instructions for developers familiar with the OpenAI interfaces, making the transition to G4F smoother. The improved example helps developers understand how to create chat completions more effectively.


(docs/async_client.md): update G4F async client API guide

  • Update the G4F async client API guide with changes to the image generation model. Replace dall-e-3 with flux in image generation examples. ((Add new provider, enhance functionality, and update docs #2304 (comment)))
    • Update image generation prompt to use model flux
    • Modify Base64 response format example to use model flux
    • Update concurrent tasks example to use model flux for image generation

This update ensures the guide reflects the current image generation model.


(docs/client.md): update G4F Client API guide

  • Update the G4F Client API guide to include new sections and examples:
    • Added section Creating Chat Completions
    • Minor text and format adjustments for clarity and completeness

(docs/client.md): add base64 response format for image generation

This enhancement improves the documentation by providing users with additional methods for handling image data.


(docs/interference-api.md): update Interference API usage guide

  • Enhance the Interference API usage guide with detailed instructions and examples for better clarity and usability.
    • Added sections for "Using the Interference API" and "Basic Usage"
    • Introduced example curl commands for text and image generation
    • Updated existing examples with improved formatting and explanations
    • Included a "Conclusion" section to summarize the guide's content
    • Defined how to run the API from both PyPI and the repository more clearly

These updates aim to provide a more comprehensive and user-friendly guide for integrating G4F with OpenAI-based applications.

(docs/interference-api.md): update image generation model in usage guide

This update ensures the documentation aligns with the latest supported models.


Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review for Pull Request: Add new provider, enhance functionality, and update docs

Summary

Thank you for your contributions to the project! This pull request adds a new provider (GizAI), enhances functionalities related to system prompts, and improves the documentation significantly. These changes enhance both the internal structure of the project and user interaction.

Highlights

  • New Provider: Introduced GizAI with a variety of models for text and image generation. This is a great addition and expands the range of capabilities available to users.
  • System Prompt Enhancement: The addition of system prompt support in the Completions class is valuable. It follows best practices and aligns with other modern API interfaces.
  • Image Handling Improvements: The enhancements for image handling in the GUI are user-friendly and improve maintainability of the image features.
  • Documentation Updates: Comprehensive updates to the README and various documentation files improve clarity for users. This ensures that they can easily understand how to use new functionalities like the system prompts and new models.

Detailed Comments

  1. Code Quality: The new provider implementation in GizAI.py follows good coding standards, maintaining readability and structure.
  2. Performance Considerations: The async handling appears effective, leveraging non-blocking calls appropriately. The decision to manage images in a dedicated directory helps in organization.
  3. UI Improvements: The changes in the GUI for image handling make it consistent and enhance the user experience significantly. Adding controls for image navigation and display is commendable.
  4. Documentation: The updates to the various documentation files, including the client API and guides, provide clear examples and enhance understanding. The corrections in the README concerning links and formatting also improve navigation.

Suggestions

  • Unit Tests: If not already included, it would be good to see unit tests for the new provider and new features added to ensure they work as expected under different conditions.
  • Error Handling: It might be beneficial to add more robust error handling in the image handling features, especially for scenarios where images fail to load.

Overall, this is an impressive contribution that significantly enhances the project. Thank you once again for your hard work! Keep it up!

@@ -10,7 +10,7 @@
class AI365VIP(AsyncGeneratorProvider, ProviderModelMixin):
url = "https://chat.ai365vip.com"
api_endpoint = "/api/chat"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The 'working' attribute was changed from True to False. It's best to provide a clear reason or explanation for this change for better understanding and maintenance.

messages: Messages,
proxy: str = None,
**kwargs
) -> AsyncResult:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider providing more descriptive variable names instead of using general kwargs in the method signature.

# Image generation
prompt = messages[-1]["content"]
data = {
"model": model,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure input parameters like width, height, steps, output_format, etc., are properly validated for valid values to prevent potential errors.

# Chat completion
data = {
"model": model,
"input": {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding input validation for messages structure to ensure it aligns with the expected format.

@@ -14,36 +14,38 @@
from fastapi.encoders import jsonable_encoder
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Union, Optional
from typing import Union, Optional, Iterator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing the unused import Iterator if it is not used elsewhere in the code.

@@ -1,5 +1,6 @@
import json
from flask import request, Flask
import os
from flask import request, Flask, jsonify, send_from_directory

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure consistent formatting of import statements.

@@ -54,6 +55,10 @@
'/images/<path:name>': {
'function': self.serve_images,
'methods': ['GET']
},
'/images': {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring to describe the purpose of the get_images function.

@@ -54,6 +55,10 @@
'/images/<path:name>': {
'function': self.serve_images,
'methods': ['GET']
},
'/images': {
'function': self.get_images,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding type hints for better code documentation and understanding.

},
'/images': {
'function': self.get_images,
'methods': ['GET']

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure alignment of method attributes for clarity.

@@ -110,4 +115,19 @@
Returns:
str: A JSON formatted string.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider providing more detailed description in the docstring for clarity.

@TheFirstNoob
Copy link

TheFirstNoob commented Oct 26, 2024

@kqlio67 Hello. Glad to see you with new changes :)
I want to attach my list of small bugs and possible improvements for better use of the library.

  1. Nexra provider after the update gives an error like Provider <class 'g4f.Provider.nexra.NexraMidjourney.NexraMidjourney'> does not support image generation and this is done for all subtypes of providers regardless of which parameter to call, generate or async_generate.

I will also assume that after this problem all flux models began to generate images of poor quality, but here the problem is not with the provider, but with flux itself.

Also for NexraGPT4o the answer occurs: General (unknown error) response

  1. An unknown bug occurs for random text generation providers. String out of index. I can't reproduce this bug as a permanent one, but sometimes it happens when I run tests as if the response comes earlier than it was received from the site. Perhaps it is possible to add some delays in receiving the response by 0.1?

  2. Add the variable ability to use a single Size parameter to configure the image size. For flux, Size is used. For SDXL and similar, the Width and Height dimensions are used. I think you can combine them and just enter the size, for example, 16x9 (Aspect Ratio).

  3. I would like to get a little more information about how to use cloud scraper?
    Some providers use it, and some do not. Is this a way to bypass captcha or region blocking? Is it possible to simply make a cloud scraper for all providers and add a variable like cloud = true/false.

  4. Update docs for image model. We didnt have dalle-3 at the moment. Replace for most any stable and popular image model.

@UX0l0l
Copy link

UX0l0l commented Oct 28, 2024

Also don't forget about the o1 issue with amigochat in #2289

@foxfire52
Copy link
Contributor

foxfire52 commented Oct 29, 2024

After previous async update, code might need an AsyncClient definition for legacy script compatibility. And appropriate deprecation warning.
Issue #2308

Also is this line still correct?
https://github.com/kqlio67/gpt4free/blob/0aad039ac8554c4a011501e5e232a1237e69eacb/docs/async_client.md?plain=1#L286-L287

@xtekky
Copy link
Owner

xtekky commented Oct 30, 2024

We definitely need to reimplement AsyncClient for legacy purposes, @kqlio67 would you mind adding it before i merge this pull request ?

@xtekky xtekky merged commit 1c8061a into xtekky:main Oct 30, 2024
1 check passed
@kqlio67
Copy link
Contributor Author

kqlio67 commented Oct 30, 2024

We definitely need to reimplement AsyncClient for legacy purposes, @kqlio67 would you mind adding it before i merge this pull request ?

Hi @xtekky!

I'd like to get your input on reimplementing AsyncClient. Before proceeding, I'd appreciate your guidance on the preferred approach:

  1. Should I implement separate Sync and Async clients
  2. Or maintain both sync/async support in the Client class while restoring AsyncClient as a legacy wrapper (with current improvements)?

I'll include the previous AsyncClient documentation as well. I noticed the PR was already merged - I'll create a new one with these changes once you confirm the direction.

Looking forward to your feedback!

@kqlio67
Copy link
Contributor Author

kqlio67 commented Oct 30, 2024

Also don't forget about the o1 issue with amigochat in #2289

Hi @UX0l0l! Thanks for bringing this up. Regarding amigochat, I'm not sure if we'll keep this provider due to ongoing Cloudflare-related issues. It might be temporarily disabled until we find a stable solution.

As for the o1 models, I'm actively looking for providers that support these models. I'll definitely add new providers with o1 support once I find reliable options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants