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

XFEstudio/gpt4free

refactor(docs/client.): Update G4F Client API documentation to reflect changes in API usage and add examples

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

代码差异

1 个文件 +48 -8
Modified docs/client.md +48 -8
@@ -1,3 +1,4 @@
1
1 2 ### G4F - Client API
2 3
3 4 #### Introduction
@@ -33,7 +34,7 @@ from g4f.Provider import BingCreateImages, OpenaiChat, Gemini
33 34 client = Client(
34 35 provider=OpenaiChat,
35 36 image_provider=Gemini,
36 ...
37 # Add any other necessary parameters
37 38 )
38 39 ```
39 40
@@ -48,7 +49,7 @@ from g4f.client import Client
48 49 client = Client(
49 50 api_key="...",
50 51 proxies="http://user:pass@host",
51 ...
52 # Add any other necessary parameters
52 53 )
53 54 ```
54 55
@@ -59,10 +60,13 @@ client = Client(
59 60 You can use the `ChatCompletions` endpoint to generate text completions as follows:
60 61
61 62 ```python
63 from g4f.client import Client
64 client = Client()
65
62 66 response = client.chat.completions.create(
63 67 model="gpt-3.5-turbo",
64 68 messages=[{"role": "user", "content": "Say this is a test"}],
65 ...
69 # Add any other necessary parameters
66 70 )
67 71 print(response.choices[0].message.content)
68 72 ```
@@ -70,12 +74,16 @@ print(response.choices[0].message.content)
70 74 Also streaming are supported:
71 75
72 76 ```python
77 from g4f.client import Client
78
79 client = Client()
80
73 81 stream = client.chat.completions.create(
74 82 model="gpt-4",
75 83 messages=[{"role": "user", "content": "Say this is a test"}],
76 84 stream=True,
77 ...
78 85 )
86
79 87 for chunk in stream:
80 88 if chunk.choices[0].delta.content:
81 89 print(chunk.choices[0].delta.content or "", end="")
@@ -86,13 +94,17 @@ for chunk in stream:
86 94 Generate images using a specified prompt:
87 95
88 96 ```python
97 from g4f.client import Client
98
99 client = Client()
89 100 response = client.images.generate(
90 101 model="dall-e-3",
91 102 prompt="a white siamese cat",
92 ...
103 # Add any other necessary parameters
93 104 )
94 105
95 106 image_url = response.data[0].url
107 print(f"Generated image URL: {image_url}")
96 108 ```
97 109
98 110 **Creating Image Variations:**
@@ -100,13 +112,17 @@ image_url = response.data[0].url
100 112 Create variations of an existing image:
101 113
102 114 ```python
115 from g4f.client import Client
116
117 client = Client()
103 118 response = client.images.create_variation(
104 119 image=open("cat.jpg", "rb"),
105 120 model="bing",
106 ...
121 # Add any other necessary parameters
107 122 )
108 123
109 124 image_url = response.data[0].url
125 print(f"Generated image URL: {image_url}")
110 126 ```
111 127 Original / Variant:
112 128
@@ -120,6 +136,7 @@ from g4f.Provider import RetryProvider, Phind, FreeChatgpt, Liaobots
120 136
121 137 import g4f.debug
122 138 g4f.debug.logging = True
139 g4f.debug.version_check = False
123 140
124 141 client = Client(
125 142 provider=RetryProvider([Phind, FreeChatgpt, Liaobots], shuffle=False)
@@ -163,13 +180,36 @@ User: What are on this image?
163 180 Bot: There is a waterfall in the middle of a jungle. There is a rainbow over...
164 181 ```
165 182
183 ### Example: Using a Vision Model
184
185 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.
186
187 ```python
188 import g4f
189 import requests
190 from g4f.client import Client
191
192 image = requests.get("https://raw.githubusercontent.com/xtekky/gpt4free/refs/heads/main/docs/cat.jpeg", stream=True).raw
193 # Or: image = open("docs/cat.jpeg", "rb")
194
195 client = Client()
196 response = client.chat.completions.create(
197 model=g4f.models.default,
198 messages=[{"role": "user", "content": "What are on this image?"}],
199 provider=g4f.Provider.Bing,
200 image=image,
201 # Add any other necessary parameters
202 )
203 print(response.choices[0].message.content)
204 ```
205
166 206 #### Advanced example: A command-line program
167 207 ```python
168 208 import g4f
169 209 from g4f.client import Client
170 210
171 211 # Initialize the GPT client with the desired provider
172 client = Client(provider=g4f.Provider.Bing)
212 client = Client()
173 213
174 214 # Initialize an empty conversation history
175 215 messages = []
@@ -203,4 +243,4 @@ while True:
203 243 print(f"An error occurred: {e}")
204 244 ```
205 245
206 [Return to Home](/)
246 [Return to Home](/)