diff options
author | kqlio67 <kqlio67@users.noreply.github.com> | 2024-10-19 19:21:14 +0200 |
---|---|---|
committer | kqlio67 <kqlio67@users.noreply.github.com> | 2024-10-19 19:21:14 +0200 |
commit | d0b80e2bab8c6b31feeb750f12b94d8da7d66468 (patch) | |
tree | 56914cd4451ceb81e0f1b605ebdebe94b37b36bd /docs/client.md | |
parent | Updated (docs/providers-and-models.md) (diff) | |
download | gpt4free-d0b80e2bab8c6b31feeb750f12b94d8da7d66468.tar gpt4free-d0b80e2bab8c6b31feeb750f12b94d8da7d66468.tar.gz gpt4free-d0b80e2bab8c6b31feeb750f12b94d8da7d66468.tar.bz2 gpt4free-d0b80e2bab8c6b31feeb750f12b94d8da7d66468.tar.lz gpt4free-d0b80e2bab8c6b31feeb750f12b94d8da7d66468.tar.xz gpt4free-d0b80e2bab8c6b31feeb750f12b94d8da7d66468.tar.zst gpt4free-d0b80e2bab8c6b31feeb750f12b94d8da7d66468.zip |
Diffstat (limited to '')
-rw-r--r-- | docs/client.md | 182 |
1 files changed, 115 insertions, 67 deletions
diff --git a/docs/client.md b/docs/client.md index e95c510d..08445402 100644 --- a/docs/client.md +++ b/docs/client.md @@ -1,32 +1,51 @@ -### G4F - Client API - -#### Introduction - +# G4F Client API Guide + + +## Table of Contents + - [Introduction](#introduction) + - [Getting Started](#getting-started) + - [Switching to G4F Client](#switching-to-g4f-client) + - [Initializing the Client](#initializing-the-client) + - [Configuration](#configuration) + - [Usage Examples](#usage-examples) + - [Text Completions](#text-completions) + - [Streaming Completions](#streaming-completions) + - [Image Generation](#image-generation) + - [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. -#### Getting Started - -**Switching to G4F Client:** +## Getting Started +### Switching to G4F Client +**To begin using the G4F Client, simply update your import statement in your Python code:** -To begin using the G4F Client, simply update your import statement in your Python code: - -Old Import: +**Old Import:** ```python from openai import OpenAI ``` -New Import: + + +**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 - -To utilize the G4F Client, create an new instance. Below is an example showcasing custom providers: +The G4F Client preserves the same familiar API interface as OpenAI, ensuring a smooth transition process. +## Initializing the Client +To utilize the G4F Client, create a new instance. **Below is an example showcasing custom providers:** ```python from g4f.client import Client from g4f.Provider import BingCreateImages, OpenaiChat, Gemini @@ -37,49 +56,61 @@ client = Client( # Add any other necessary parameters ) ``` + ## Configuration - -You can set an "api_key" for your provider in the client. -And you also have the option to define a proxy for all outgoing requests: - +**You can set an `api_key` for your provider in the client and define a proxy for all outgoing requests:** ```python from g4f.client import Client client = Client( - api_key="...", + api_key="your_api_key_here", proxies="http://user:pass@host", # Add any other necessary parameters ) ``` -#### Usage Examples - -**Text Completions:** - -You can use the `ChatCompletions` endpoint to generate text completions as follows: + +## Usage Examples +### Text Completions +**Generate text completions using the `ChatCompletions` endpoint:** ```python from g4f.client import Client client = Client() + response = client.chat.completions.create( model="gpt-3.5-turbo", - messages=[{"role": "user", "content": "Say this is a test"}], + messages=[ + { + "role": "user", + "content": "Say this is a test" + } + ] # Add any other necessary parameters ) + print(response.choices[0].message.content) ``` -Also streaming are supported: + +### Streaming Completions +**Process responses incrementally as they are generated:** ```python from g4f.client import Client client = Client() + stream = client.chat.completions.create( model="gpt-4", - messages=[{"role": "user", "content": "Say this is a test"}], + messages=[ + { + "role": "user", + "content": "Say this is a test" + } + ], stream=True, ) @@ -88,101 +119,104 @@ for chunk in stream: print(chunk.choices[0].delta.content or "", end="") ``` -**Image Generation:** - -Generate images using a specified prompt: + +### Image Generation +**Generate images using a specified prompt:** ```python from g4f.client import Client client = Client() + response = client.images.generate( model="dall-e-3", - prompt="a white siamese cat", + prompt="a white siamese cat" # Add any other necessary parameters ) image_url = response.data[0].url + print(f"Generated image URL: {image_url}") ``` -**Creating Image Variations:** - -Create variations of an existing image: + +### Creating Image Variations +**Create variations of an existing image:** ```python from g4f.client import Client client = Client() + response = client.images.create_variation( image=open("cat.jpg", "rb"), - model="bing", + model="bing" # Add any other necessary parameters ) image_url = response.data[0].url + print(f"Generated image URL: {image_url}") ``` -Original / Variant: -[![Original Image](/docs/cat.jpeg)](/docs/client.md) [![Variant Image](/docs/cat.webp)](/docs/client.md) + -#### Use a list of providers with RetryProvider +## Advanced Usage +### Using a List of Providers with RetryProvider ```python from g4f.client import Client from g4f.Provider import RetryProvider, Phind, FreeChatgpt, Liaobots - import g4f.debug + g4f.debug.logging = True g4f.debug.version_check = False client = Client( provider=RetryProvider([Phind, FreeChatgpt, Liaobots], shuffle=False) ) + response = client.chat.completions.create( model="", - messages=[{"role": "user", "content": "Hello"}], + messages=[ + { + "role": "user", + "content": "Hello" + } + ] ) -print(response.choices[0].message.content) -``` -``` -Using RetryProvider provider -Using Phind provider -How can I assist you today? +print(response.choices[0].message.content) ``` -#### Advanced example using GeminiProVision - + +### Using GeminiProVision ```python from g4f.client import Client from g4f.Provider.GeminiPro import GeminiPro client = Client( - api_key="...", + 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?"}], + messages=[ + { + "role": "user", + "content": "What are on this image?" + } + ], image=open("docs/waterfall.jpeg", "rb") ) -print(response.choices[0].message.content) -``` +print(response.choices[0].message.content) ``` -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... -``` - -### Example: Using a Vision Model -The following code snippet demonstrates how to use a vision model to analyze an image and generate a description based on the content of the image. This example shows how to fetch an image, send it to the model, and then process the response. + +### Using a Vision Model +**Analyze an image and generate a description:** ```python import g4f import requests @@ -192,17 +226,26 @@ image = requests.get("https://raw.githubusercontent.com/xtekky/gpt4free/refs/hea # Or: image = open("docs/cat.jpeg", "rb") client = Client() + response = client.chat.completions.create( model=g4f.models.default, - messages=[{"role": "user", "content": "What are on this image?"}], + messages=[ + { + "role": "user", + "content": "What are on this image?" + } + ], provider=g4f.Provider.Bing, - image=image, + image=image # Add any other necessary parameters ) + print(response.choices[0].message.content) ``` -#### Advanced example: A command-line program + +## Command-line Chat Program +**Here's an example of a simple command-line chat program using the G4F Client:** ```python import g4f from g4f.client import Client @@ -216,7 +259,7 @@ messages = [] while True: # Get user input user_input = input("You: ") - + # Check if the user wants to exit the chat if user_input.lower() == "exit": print("Exiting chat...") @@ -238,8 +281,13 @@ while True: # Update the conversation history with GPT's response messages.append({"role": "assistant", "content": gpt_response}) + except Exception as e: print(f"An error occurred: {e}") ``` + +This guide provides a comprehensive overview of the G4F Client API, demonstrating its versatility in handling various AI tasks, from text generation to image analysis and creation. By leveraging these features, you can build powerful and responsive applications that harness the capabilities of advanced AI models. + +--- [Return to Home](/) |