mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-12-27 21:21:41 +03:00
Aktualisieren von client.md
This commit is contained in:
parent
9d23ada968
commit
7d6c770bbe
@ -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…
Reference in New Issue
Block a user