XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

Add async client docs

8321dca1
Heiner Lohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +97 -1
Modified README.md +2 -1
@@ -188,7 +188,8 @@ image_url = response.data[0].url
188 188
189 189 **Full Documentation for Python API**
190 190
191 - New Client API like the OpenAI Python library: [/docs/client](/docs/client.md)
191 - New AsyncClient API from G4F: [/docs/async_client](/docs/async_client.md)
192 - Client API like the OpenAI Python library: [/docs/client](/docs/async_client.md)
192 193 - Legacy API with python modules: [/docs/legacy](/docs/legacy.md)
193 194
194 195 #### Web UI
Added docs/async_client.md +95 -0
@@ -0,0 +1,95 @@
1 # How to Use the G4F AsyncClient API
2
3 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.
4
5 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.
6
7 ## Key Features
8
9 The G4F AsyncClient API offers several key features:
10
11 - **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.
12
13 - **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.
14
15 - **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.
16
17 - **Non-Streaming Responses:** The ChatCompletion.create method can also generate non-streaming responses.
18
19 - **Image Generation and Vision Models:** The G4F Client API also supports image generation and vision models, expanding its utility beyond text-based interactions.
20
21
22 ## Using AsyncClient
23
24 ### Text Completions:
25
26 You can use the ChatCompletions endpoint to generate text completions as follows:
27
28 ```python
29 response = await client.chat.completions.create(
30 model="gpt-3.5-turbo",
31 messages=[{"role": "user", "content": "Say this is a test"}],
32 ...
33 )
34 print(response.choices[0].message.content)
35 ```
36
37 Streaming completions are also supported:
38
39 ```python
40 stream = client.chat.completions.create(
41 model="gpt-4",
42 messages=[{"role": "user", "content": "Say this is a test"}],
43 stream=True,
44 ...
45 )
46 async for chunk in stream:
47 if chunk.choices[0].delta.content:
48 print(chunk.choices[0].delta.content or "", end="")
49 ```
50
51 ### Image Generation:
52
53 You can generate images using a specified prompt:
54
55 ```python
56 response = await client.images.generate(
57 model="dall-e-3",
58 prompt="a white siamese cat",
59 ...
60 )
61
62 image_url = response.data[0].url
63 ```
64
65 ### Example usage with asyncio.gather
66
67 Start two tasks at the same time:
68
69 ```python
70 import asyncio
71
72 from g4f.client import AsyncClient
73 from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
74
75 async def main():
76 client = AsyncClient(
77 provider=OpenaiChat,
78 image_provider=Gemini,
79 # other parameters...
80 )
81
82 task1 = client.chat.completions.create(
83 model="gpt-3.5-turbo",
84 messages=[{"role": "user", "content": "Say this is a test"}],
85 )
86 task2 = client.images.generate(
87 model="dall-e-3",
88 prompt="a white siamese cat",
89 )
90 responses = await asyncio.gather(task1, task2)
91
92 print(responses)
93
94 asyncio.run(main())
95 ```