summaryrefslogtreecommitdiffstats
path: root/docs
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-04-06 03:15:04 +0200
committerGitHub <noreply@github.com>2024-04-06 03:15:04 +0200
commit7d6c770bbe73262947613eaff6c4eb440c5e887b (patch)
tree88ade0a159e2266d95461941a882e43c2329e69d /docs
parentMerge pull request #1790 from hlohaus/free (diff)
downloadgpt4free-7d6c770bbe73262947613eaff6c4eb440c5e887b.tar
gpt4free-7d6c770bbe73262947613eaff6c4eb440c5e887b.tar.gz
gpt4free-7d6c770bbe73262947613eaff6c4eb440c5e887b.tar.bz2
gpt4free-7d6c770bbe73262947613eaff6c4eb440c5e887b.tar.lz
gpt4free-7d6c770bbe73262947613eaff6c4eb440c5e887b.tar.xz
gpt4free-7d6c770bbe73262947613eaff6c4eb440c5e887b.tar.zst
gpt4free-7d6c770bbe73262947613eaff6c4eb440c5e887b.zip
Diffstat (limited to '')
-rw-r--r--docs/client.md40
1 files changed, 40 insertions, 0 deletions
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