Aktualisieren von client.md

pull/1792/head
H Lohaus 3 months ago committed by GitHub
parent 9d23ada968
commit 7d6c770bbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -163,4 +163,44 @@ User: What are on this image?
Bot: There is a waterfall in the middle of a jungle. There is a rainbow over...
```
Advanced example: A command-line program
```python
import g4f
from g4f.client import Client
# Initialize the GPT client with the desired provider
client = Client(provider=g4f.Provider.Bing)
# Initialize an empty conversation history
messages = []
while True:
# Get user input
user_input = input("You: ")
# Check if the user wants to exit the chat
if user_input.lower() == "exit":
print("Exiting chat...")
break # Exit the loop to end the conversation
# Update the conversation history with the user's message
messages.append({"role": "user", "content": user_input})
try:
# Get GPT's response
response = client.chat.completions.create(
messages=messages,
model=g4f.models.default,
)
# Extract the GPT response and print it
gpt_response = response.choices[0].message.content
print(f"Bot: {gpt_response}")
# Update the conversation history with GPT's response
messages.append({"role": "assistant", "content": gpt_response})
except Exception as e:
print(f"An error occurred: {e}")
```
[Return to Home](/)
Loading…
Cancel
Save