From d6b18c0f15fd1443b621e42ccae0a9c05338a156 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 1 Dec 2024 17:31:05 +0100 Subject: Use pydantic construct and `dict` instead of model_construct and model_dump --- docs/async_client.md | 34 +-------------------------------- docs/client.md | 54 ++++++---------------------------------------------- 2 files changed, 7 insertions(+), 81 deletions(-) (limited to 'docs') diff --git a/docs/async_client.md b/docs/async_client.md index 891a2356..09ceb058 100644 --- a/docs/async_client.md +++ b/docs/async_client.md @@ -2,7 +2,6 @@ # G4F - AsyncClient API Guide The G4F AsyncClient API is a powerful asynchronous interface for interacting with various AI models. This guide provides comprehensive information on how to use the API effectively, including setup, usage examples, best practices, and important considerations for optimal performance. - ## Compatibility Note The G4F AsyncClient API is designed to be compatible with the OpenAI API, making it easy for developers familiar with OpenAI's interface to transition to G4F. @@ -24,8 +23,6 @@ The G4F AsyncClient API is designed to be compatible with the OpenAI API, making - [Rate Limiting and API Usage](#rate-limiting-and-api-usage) - [Conclusion](#conclusion) - - ## Introduction The G4F AsyncClient API is an asynchronous version of the standard G4F Client API. It offers the same functionality as the synchronous API but with improved performance due to its asynchronous nature. This guide will walk you through the key features and usage of the G4F AsyncClient API. @@ -37,8 +34,6 @@ The G4F AsyncClient API is an asynchronous version of the standard G4F Client AP - **Non-Streaming Responses**: Generate complete responses in a single call. - **Image Generation and Vision Models**: Support for image-related tasks. - - ## Getting Started ### Initializing the AsyncClient **To use the G4F `AsyncClient`, create a new instance:** @@ -53,7 +48,6 @@ client = AsyncClient( ) ``` - ## Creating Chat Completions **Here’s an improved example of creating chat completions:** ```python @@ -75,7 +69,6 @@ response = await client.chat.completions.create( - Disables streaming for a complete response You can adjust these parameters based on your specific needs. - ### Configuration **Configure the `AsyncClient` with additional settings:** @@ -87,8 +80,6 @@ client = AsyncClient( ) ``` - - ## Usage Examples ### Text Completions **Generate text completions using the ChatCompletions endpoint:** @@ -114,8 +105,6 @@ async def main(): asyncio.run(main()) ``` - - ### Streaming Completions **Process responses incrementally as they are generated:** ```python @@ -143,8 +132,6 @@ async def main(): asyncio.run(main()) ``` - - ### Using a Vision Model **Analyze an image and generate a description:** ```python @@ -176,8 +163,6 @@ async def main(): asyncio.run(main()) ``` - - ### Image Generation **Generate images using a specified prompt:** ```python @@ -198,8 +183,6 @@ async def main(): asyncio.run(main()) ``` - - #### Base64 Response Format ```python import asyncio @@ -220,8 +203,6 @@ async def main(): asyncio.run(main()) ``` - - ### Concurrent Tasks with asyncio.gather **Execute multiple tasks concurrently:** ```python @@ -260,8 +241,6 @@ async def main(): asyncio.run(main()) ``` - - ## Available Models and Providers The G4F AsyncClient supports a wide range of AI models and providers, allowing you to choose the best option for your specific use case. **Here's a brief overview of the available models and providers:** @@ -274,8 +253,6 @@ The G4F AsyncClient supports a wide range of AI models and providers, allowing y - Claude (Anthropic) - And more... - - ### Providers - OpenAI - Google (for Gemini) @@ -283,7 +260,6 @@ The G4F AsyncClient supports a wide range of AI models and providers, allowing y - Microsoft Copilot - Custom providers - **To use a specific model or provider, specify it when creating the client or in the API call:** ```python client = AsyncClient(provider=g4f.Provider.OpenaiChat) @@ -302,8 +278,6 @@ response = await client.chat.completions.create( ) ``` - - ## Error Handling and Best Practices Implementing proper error handling and following best practices is crucial when working with the G4F AsyncClient API. This ensures your application remains robust and can gracefully handle various scenarios. **Here are some key practices to follow:** @@ -342,8 +316,6 @@ async def make_api_call(): pass ``` - - ## Rate Limiting and API Usage When working with the G4F AsyncClient API, it's important to implement rate limiting and monitor your API usage. This helps ensure fair usage, prevents overloading the service, and optimizes your application's performance. Here are some key strategies to consider: @@ -361,8 +333,6 @@ async def make_api_call(): pass ``` - - 2. **Monitor your API usage and implement logging:** ```python import logging @@ -378,8 +348,6 @@ async def make_api_call(): logger.error(f"API call failed: {e}") ``` - - 3. **Use caching to reduce API calls for repeated queries:** ```python from functools import lru_cache @@ -397,4 +365,4 @@ Remember to handle errors gracefully, implement rate limiting, and monitor your --- -[Return to Home](/) +[Return to Home](/) \ No newline at end of file diff --git a/docs/client.md b/docs/client.md index 5e37880c..f45d5ebe 100644 --- a/docs/client.md +++ b/docs/client.md @@ -1,6 +1,5 @@ # G4F Client API Guide - ## Table of Contents - [Introduction](#introduction) @@ -16,12 +15,9 @@ - [Creating Image Variations](#creating-image-variations) - [Advanced Usage](#advanced-usage) - [Using a List of Providers with RetryProvider](#using-a-list-of-providers-with-retryprovider) - - [Using GeminiProVision](#using-geminiprovision) - [Using a Vision Model](#using-a-vision-model) - [Command-line Chat Program](#command-line-chat-program) - - ## Introduction Welcome to the G4F Client API, a cutting-edge tool for seamlessly integrating advanced AI capabilities into your Python applications. This guide is designed to facilitate your transition from using the OpenAI client to the G4F Client, offering enhanced features while maintaining compatibility with the existing OpenAI API. @@ -34,15 +30,11 @@ Welcome to the G4F Client API, a cutting-edge tool for seamlessly integrating ad from openai import OpenAI ``` - - **New Import:** ```python from g4f.client import Client as OpenAI ``` - - The G4F Client preserves the same familiar API interface as OpenAI, ensuring a smooth transition process. ## Initializing the Client @@ -79,7 +71,6 @@ response = client.chat.completions.create( - Disables streaming for a complete response You can adjust these parameters based on your specific needs. - ## Configuration **You can set an `api_key` for your provider in the client and define a proxy for all outgoing requests:** @@ -93,8 +84,6 @@ client = Client( ) ``` - - ## Usage Examples ### Text Completions **Generate text completions using the `ChatCompletions` endpoint:** @@ -117,8 +106,6 @@ response = client.chat.completions.create( print(response.choices[0].message.content) ``` - - ### Streaming Completions **Process responses incrementally as they are generated:** ```python @@ -142,8 +129,6 @@ for chunk in stream: print(chunk.choices[0].delta.content or "", end="") ``` - - ### Image Generation **Generate images using a specified prompt:** ```python @@ -162,7 +147,6 @@ image_url = response.data[0].url print(f"Generated image URL: {image_url}") ``` - #### Base64 Response Format ```python from g4f.client import Client @@ -179,8 +163,6 @@ base64_text = response.data[0].b64_json print(base64_text) ``` - - ### Creating Image Variations **Create variations of an existing image:** ```python @@ -202,8 +184,6 @@ image_url = response.data[0].url print(f"Generated image URL: {image_url}") ``` - - ## Advanced Usage ### Using a List of Providers with RetryProvider @@ -231,47 +211,25 @@ response = client.chat.completions.create( print(response.choices[0].message.content) ``` - -### Using GeminiProVision +### Using a Vision Model +**Analyze an image and generate a description:** ```python +import g4f +import requests + from g4f.client import Client from g4f.Provider.GeminiPro import GeminiPro +# Initialize the GPT client with the desired provider and api key client = Client( api_key="your_api_key_here", provider=GeminiPro ) -response = client.chat.completions.create( - model="gemini-pro-vision", - messages=[ - { - "role": "user", - "content": "What are on this image?" - } - ], - image=open("docs/waterfall.jpeg", "rb") -) - -print(response.choices[0].message.content) -``` - - -### Using a Vision Model -**Analyze an image and generate a description:** -```python -import g4f -import requests -from g4f.client import Client - image = requests.get("https://raw.githubusercontent.com/xtekky/gpt4free/refs/heads/main/docs/cat.jpeg", stream=True).raw # Or: image = open("docs/cat.jpeg", "rb") -client = Client( - provider=CopilotAccount -) - response = client.chat.completions.create( model=g4f.models.default, messages=[ -- cgit v1.2.3