Added Bing's Chatbot, extended API and example, updated README

This commit is contained in:
Julian Müller (ChaoticByte) 2023-03-13 10:44:01 +01:00
parent 308bf0730f
commit c8361abe28
4 changed files with 110 additions and 17 deletions

View file

@ -3,26 +3,39 @@
# Copyright (c) 2023 Julian Müller (ChaoticByte)
from os import environ
from sys import argv
from chatgpt_pyapi.openai import ChatGPT, Message, Models, Roles
# Read the API key from a environment variable
API_KEY = environ["OPENAI_API_KEY"]
example_system_message = "Please provide the following answers as cynical as possible, but still correct."
example_questions = [
"Who are you?",
"Could you please elaborate?"
]
if __name__ == "__main__":
# Create ChatGPT API instance
cgpt = ChatGPT(API_KEY, model=Models.GPT_35_TURBO_0301)
# Provide a system message that will influence the answers
system_in = "Please provide the following answers as cynical as possible, but still correct."
# Add the message to the history, but don't send it yet
cgpt.add_to_chat(Message(system_in, role=Roles.SYSTEM))
bing = False
if len(argv) > 1:
bing = argv[1] == "bing"
if bing:
from chatgpt_pyapi.bing import ChatGPT, Message, ConversationStyle
# Read the path to the cookies file from a environment variable
BING_COOKIES_FILE = environ["BING_COOKIES_FILE"]
# Create ChatGPT API instance (with creative answers)
cgpt = ChatGPT(BING_COOKIES_FILE, ConversationStyle.creative)
cgpt.chat(Message(example_system_message))
else:
from chatgpt_pyapi.openai import ChatGPT, Message, Models, Roles
# Read the API key from a environment variable
API_KEY = environ["OPENAI_API_KEY"]
# Create ChatGPT API instance
cgpt = ChatGPT(API_KEY, model=Models.GPT_35_TURBO_0301)
# Provide a system message that will influence the answers
sys_msg = Message(example_system_message, role=Roles.SYSTEM)
# Add the message to the history, but don't send it yet
cgpt.add_to_chat(sys_msg)
# Have a little chat :D
user_in = "Who are you?"
print(f"USER: {user_in}")
print(cgpt.chat(Message(user_in)).text)
user_in = "Could you please elaborate?"
print(f"\nUSER: {user_in}")
print(cgpt.chat(Message(user_in)).text)
for q in example_questions:
print(f"\nUSER: {q}")
print(cgpt.chat(Message(q)).text)
# Print out message history
print("\nMessage History:")
[print(m.to_api()) for m in cgpt._message_history]
[print(str(m)) for m in cgpt._message_history]