返回提交历史
Modified
docs/client.md
+40
-0
XFEstudio/gpt4free
Aktualisieren von client.md
7d6c770b
代码差异
1 个文件
+40
-0
@@ -163,4 +163,44 @@ User: What are on this image?
163
163
Bot: There is a waterfall in the middle of a jungle. There is a rainbow over...
164
164
```
165
165
166
Advanced example: A command-line program
167
```python
168
import g4f
169
from g4f.client import Client
170
171
# Initialize the GPT client with the desired provider
172
client = Client(provider=g4f.Provider.Bing)
173
174
# Initialize an empty conversation history
175
messages = []
176
177
while True:
178
# Get user input
179
user_input = input("You: ")
180
181
# Check if the user wants to exit the chat
182
if user_input.lower() == "exit":
183
print("Exiting chat...")
184
break # Exit the loop to end the conversation
185
186
# Update the conversation history with the user's message
187
messages.append({"role": "user", "content": user_input})
188
189
try:
190
# Get GPT's response
191
response = client.chat.completions.create(
192
messages=messages,
193
model=g4f.models.default,
194
)
195
196
# Extract the GPT response and print it
197
gpt_response = response.choices[0].message.content
198
print(f"Bot: {gpt_response}")
199
200
# Update the conversation history with GPT's response
201
messages.append({"role": "assistant", "content": gpt_response})
202
except Exception as e:
203
print(f"An error occurred: {e}")
204
```
205
166
206
[Return to Home](/)