From 7d6c770bbe73262947613eaff6c4eb440c5e887b Mon Sep 17 00:00:00 2001 From: H Lohaus Date: Sat, 6 Apr 2024 03:15:04 +0200 Subject: Aktualisieren von client.md --- docs/client.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/client.md b/docs/client.md index 31440027..2d0608b6 100644 --- a/docs/client.md +++ b/docs/client.md @@ -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](/) \ No newline at end of file -- cgit v1.2.3