diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-04-09 17:45:42 +0200 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-04-09 17:45:42 +0200 |
commit | 8321dca121e72624ab7735fd5cb34d40ead5436e (patch) | |
tree | 4ddd69ad2209bb53a23e82948c015ea514ca0ac8 | |
parent | Add text to speech module (diff) | |
download | gpt4free-8321dca121e72624ab7735fd5cb34d40ead5436e.tar gpt4free-8321dca121e72624ab7735fd5cb34d40ead5436e.tar.gz gpt4free-8321dca121e72624ab7735fd5cb34d40ead5436e.tar.bz2 gpt4free-8321dca121e72624ab7735fd5cb34d40ead5436e.tar.lz gpt4free-8321dca121e72624ab7735fd5cb34d40ead5436e.tar.xz gpt4free-8321dca121e72624ab7735fd5cb34d40ead5436e.tar.zst gpt4free-8321dca121e72624ab7735fd5cb34d40ead5436e.zip |
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | docs/async_client.md | 95 |
2 files changed, 97 insertions, 1 deletions
@@ -188,7 +188,8 @@ image_url = response.data[0].url **Full Documentation for Python API** -- New Client API like the OpenAI Python library: [/docs/client](/docs/client.md) +- New AsyncClient API from G4F: [/docs/async_client](/docs/async_client.md) +- Client API like the OpenAI Python library: [/docs/client](/docs/async_client.md) - Legacy API with python modules: [/docs/legacy](/docs/legacy.md) #### Web UI diff --git a/docs/async_client.md b/docs/async_client.md new file mode 100644 index 00000000..4827d11b --- /dev/null +++ b/docs/async_client.md @@ -0,0 +1,95 @@ +# How to Use the G4F AsyncClient API + +The AsyncClient API is the asynchronous counterpart to the standard G4F Client API. It offers the same functionality as the synchronous API, but with the added benefit of improved performance due to its asynchronous nature. + +Designed to maintain compatibility with the existing OpenAI API, the G4F AsyncClient API ensures a seamless transition for users already familiar with the OpenAI client. + +## Key Features + +The G4F AsyncClient API offers several key features: + +- **Custom Providers:** The G4F Client API allows you to use custom providers. This feature enhances the flexibility of the API, enabling it to cater to a wide range of use cases. + +- **ChatCompletion Interface:** The G4F package provides an interface for interacting with chat models through the ChatCompletion class. This class provides methods for creating both streaming and non-streaming responses. + +- **Streaming Responses:** The ChatCompletion.create method can return a response iteratively as and when they are received if the stream parameter is set to True. + +- **Non-Streaming Responses:** The ChatCompletion.create method can also generate non-streaming responses. + +- **Image Generation and Vision Models:** The G4F Client API also supports image generation and vision models, expanding its utility beyond text-based interactions. + + +## Using AsyncClient + +### Text Completions: + +You can use the ChatCompletions endpoint to generate text completions as follows: + +```python +response = await client.chat.completions.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": "Say this is a test"}], + ... +) +print(response.choices[0].message.content) +``` + +Streaming completions are also supported: + +```python +stream = client.chat.completions.create( + model="gpt-4", + messages=[{"role": "user", "content": "Say this is a test"}], + stream=True, + ... +) +async for chunk in stream: + if chunk.choices[0].delta.content: + print(chunk.choices[0].delta.content or "", end="") +``` + +### Image Generation: + +You can generate images using a specified prompt: + +```python +response = await client.images.generate( + model="dall-e-3", + prompt="a white siamese cat", + ... +) + +image_url = response.data[0].url +``` + +### Example usage with asyncio.gather + +Start two tasks at the same time: + +```python +import asyncio + +from g4f.client import AsyncClient +from g4f.Provider import BingCreateImages, OpenaiChat, Gemini + +async def main(): + client = AsyncClient( + provider=OpenaiChat, + image_provider=Gemini, + # other parameters... + ) + + task1 = client.chat.completions.create( + model="gpt-3.5-turbo", + messages=[{"role": "user", "content": "Say this is a test"}], + ) + task2 = client.images.generate( + model="dall-e-3", + prompt="a white siamese cat", + ) + responses = await asyncio.gather(task1, task2) + + print(responses) + +asyncio.run(main()) +```
\ No newline at end of file |