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

XFEstudio/gpt4free

refactor(docs): Update AsyncClient API documentation to reflect changes in API usage and add asyncio examples

f45c072d
kqlio67 <kqlio67@users.noreply.github.com>
提交于

代码差异

1 个文件 +85 -65
Modified docs/async_client.md +85 -65
@@ -26,7 +26,7 @@ from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
26 26 client = AsyncClient(
27 27 provider=OpenaiChat,
28 28 image_provider=Gemini,
29 ...
29 # Add any other necessary parameters
30 30 )
31 31 ```
32 32
@@ -44,7 +44,7 @@ from g4f.client import AsyncClient
44 44 client = AsyncClient(
45 45 api_key="your_api_key_here",
46 46 proxies="http://user:pass@host",
47 ...
47 # Add any other necessary parameters
48 48 )
49 49 ```
50 50
@@ -59,18 +59,20 @@ You can use the `ChatCompletions` endpoint to generate text completions. Here’
59 59
60 60 ```python
61 61 import asyncio
62 from g4f.client import AsyncClient
62
63 from g4f.client import Client
63 64
64 65 async def main():
65 client = AsyncClient()
66 response = await client.chat.completions.create(
67 [{"role": "user", "content": "say this is a test"}],
68 model="gpt-3.5-turbo"
66 client = Client()
67 response = await client.chat.completions.async_create(
68 model="gpt-3.5-turbo",
69 messages=[{"role": "user", "content": "say this is a test"}],
70 # Add any other necessary parameters
69 71 )
70
71 72 print(response.choices[0].message.content)
72 73
73 74 asyncio.run(main())
75
74 76 ```
75 77
76 78 ### Streaming Completions
@@ -79,19 +81,23 @@ The `AsyncClient` also supports streaming completions. This allows you to proces
79 81
80 82 ```python
81 83 import asyncio
82 from g4f.client import AsyncClient
84
85 from g4f.client import Client
83 86
84 87 async def main():
85 client = AsyncClient()
86 async for chunk in await client.chat.completions.create(
87 [{"role": "user", "content": "say this is a test"}],
88 client = Client()
89 stream = await client.chat.completions.async_create(
88 90 model="gpt-4",
91 messages=[{"role": "user", "content": "say this is a test"}],
89 92 stream=True,
90 ):
91 print(chunk.choices[0].delta.content or "", end="")
92 print()
93 # Add any other necessary parameters
94 )
95 async for chunk in stream:
96 if chunk.choices[0].delta.content:
97 print(chunk.choices[0].delta.content or "", end="")
93 98
94 99 asyncio.run(main())
100
95 101 ```
96 102
97 103 In this example:
@@ -102,23 +108,29 @@ In this example:
102 108 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.
103 109
104 110 ```python
111 import g4f
105 112 import requests
113 import asyncio
114
106 115 from g4f.client import Client
107 from g4f.Provider import Bing
108 116
109 client = AsyncClient(
110 provider=Bing
111 )
117 image = requests.get("https://raw.githubusercontent.com/xtekky/gpt4free/refs/heads/main/docs/cat.jpeg", stream=True).raw
118 # Or: image = open("docs/cat.jpeg", "rb")
112 119
113 image = requests.get("https://my_website/image.jpg", stream=True).raw
114 # Or: image = open("local_path/image.jpg", "rb")
115 120
116 response = client.chat.completions.create(
117 "",
118 messages=[{"role": "user", "content": "what is in this picture?"}],
119 image=image
120 )
121 print(response.choices[0].message.content)
121 async def main():
122 client = Client()
123 response = await client.chat.completions.async_create(
124 model=g4f.models.default,
125 provider=g4f.Provider.Bing,
126 messages=[{"role": "user", "content": "What are on this image?"}],
127 image=image
128 # Add any other necessary parameters
129 )
130 print(response.choices[0].message.content)
131
132 asyncio.run(main())
133
122 134 ```
123 135
124 136 ### Image Generation:
@@ -127,32 +139,40 @@ You can generate images using a specified prompt:
127 139
128 140 ```python
129 141 import asyncio
130 from g4f.client import AsyncClient
142 from g4f.client import Client
131 143
132 144 async def main():
133 client = AsyncClient(image_provider='')
134 response = await client.images.generate(
135 prompt="a white siamese cat"
136 model="flux",
137 #n=1,
138 #size="1024x1024"
139 # ...
145 client = Client()
146 response = await client.images.async_generate(
147 prompt="a white siamese cat",
148 model="dall-e-3",
149 # Add any other necessary parameters
140 150 )
141 151 image_url = response.data[0].url
142 print(image_url)
152 print(f"Generated image URL: {image_url}")
143 153
144 154 asyncio.run(main())
155
145 156 ```
146 157
147 158 #### Base64 as the response format
148 159
149 160 ```python
150 response = await client.images.generate(
151 prompt="a cool cat",
152 response_format="b64_json"
153 )
161 import asyncio
162 from g4f.client import Client
154 163
155 base64_text = response.data[0].b64_json
164 async def main():
165 client = Client()
166 response = await client.images.async_generate(
167 prompt="a white siamese cat",
168 model="dall-e-3",
169 response_format="b64_json"
170 # Add any other necessary parameters
171 )
172 base64_text = response.data[0].b64_json
173 print(base64_text)
174
175 asyncio.run(main())
156 176 ```
157 177
158 178 ### Example usage with asyncio.gather
@@ -161,34 +181,34 @@ Start two tasks at the same time:
161 181
162 182 ```python
163 183 import asyncio
164 import g4f
165 from g4f.client import AsyncClient
184
185 from g4f.client import Client
166 186
167 187 async def main():
168 client = AsyncClient(
169 provider=OpenaiChat,
170 image_provider=BingCreateImages,
188 client = Client()
189
190 task1 = client.chat.completions.async_create(
191 model="gpt-3.5-turbo",
192 messages=[{"role": "user", "content": "Say this is a test"}],
193 )
194 task2 = client.images.generate(
195 model="dall-e-3",
196 prompt="a white siamese cat",
171 197 )
172 198
173 # Task for text completion
174 async def text_task():
175 response = await client.chat.completions.create(
176 [{"role": "user", "content": "Say this is a test"}],
177 model="gpt-3.5-turbo",
178 )
179 print(response.choices[0].message.content)
180 print()
181
182 # Task for image generation
183 async def image_task():
184 response = await client.images.generate(
185 "a white siamese cat",
186 model="flux",
187 )
188 print(f"Image generated: {response.data[0].url}")
189
190 # Execute both tasks asynchronously
191 await asyncio.gather(text_task(), image_task())
199 responses = await asyncio.gather(task1, task2)
200
201 chat_response, image_response = responses
202
203 print("Chat Response:")
204 print(chat_response.choices[0].message.content)
205
206 print("\nImage Response:")
207 image_url = image_response.data[0].url
208 print(image_url)
192 209
193 210 asyncio.run(main())
211
194 212 ```
213
214 [Return to Home](/)