Added ChatGPT.add_to_chat() method to add messages that will be sent later and updated example.py
This commit is contained in:
parent
b0f6ee2336
commit
bbd0f64f54
2 changed files with 17 additions and 9 deletions
|
@ -45,15 +45,14 @@ class ChatGPT:
|
||||||
# Create string used in header
|
# Create string used in header
|
||||||
self.auth = f"Bearer {api_key}"
|
self.auth = f"Bearer {api_key}"
|
||||||
# This list will contain all prior messages
|
# This list will contain all prior messages
|
||||||
self.message_history = []
|
self._message_history = []
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|
||||||
def chat(self, message: Message) -> Message:
|
def chat(self, message: Message) -> Message:
|
||||||
# check if the message parameter is the correct type
|
'''Add a message to the message history & send it to ChatGPT. Returns the answer as a Message instance.'''
|
||||||
assert type(message) == Message, "message must be an instance of Message"
|
self.add_to_chat(message)
|
||||||
self.message_history.append(message)
|
|
||||||
# create api_input from message_history & encode it
|
# create api_input from message_history & encode it
|
||||||
api_input = [m.to_api() for m in self.message_history]
|
api_input = [m.to_api() for m in self._message_history]
|
||||||
api_input_encoded = dumps(
|
api_input_encoded = dumps(
|
||||||
{"model": self.model, "messages": api_input},
|
{"model": self.model, "messages": api_input},
|
||||||
separators=(",", ":")).encode()
|
separators=(",", ":")).encode()
|
||||||
|
@ -75,8 +74,14 @@ class ChatGPT:
|
||||||
api_output_answer["content"] = api_output_answer["content"].strip("\n")
|
api_output_answer["content"] = api_output_answer["content"].strip("\n")
|
||||||
# convert to Message object
|
# convert to Message object
|
||||||
response_message = Message.from_api(api_output_answer)
|
response_message = Message.from_api(api_output_answer)
|
||||||
self.message_history.append(response_message)
|
self._message_history.append(response_message)
|
||||||
return response_message
|
return response_message
|
||||||
|
|
||||||
|
def add_to_chat(self, message: Message) -> Message:
|
||||||
|
'''Add a message to the message history without sending it to ChatGPT'''
|
||||||
|
# check if the message parameter is the correct type
|
||||||
|
assert type(message) == Message, "message must be an instance of Message"
|
||||||
|
self._message_history.append(message)
|
||||||
|
|
||||||
def clear_message_history(self):
|
def clear_message_history(self):
|
||||||
self.message_history = []
|
self._message_history = []
|
||||||
|
|
|
@ -5,14 +5,17 @@
|
||||||
from os import environ
|
from os import environ
|
||||||
|
|
||||||
from chatgpt_pyapi import ChatGPT
|
from chatgpt_pyapi import ChatGPT
|
||||||
from chatgpt_pyapi import Models
|
|
||||||
from chatgpt_pyapi import Message
|
from chatgpt_pyapi import Message
|
||||||
|
from chatgpt_pyapi import Models
|
||||||
|
from chatgpt_pyapi import Roles
|
||||||
|
|
||||||
|
|
||||||
API_KEY = environ["OPENAI_API_KEY"]
|
API_KEY = environ["OPENAI_API_KEY"]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
cgpt = ChatGPT(API_KEY, model=Models.GPT_35_TURBO_0301)
|
cgpt = ChatGPT(API_KEY, model=Models.GPT_35_TURBO_0301)
|
||||||
|
system_in = "Please provide the following answers as cynical as possible, but still correct."
|
||||||
|
cgpt.add_to_chat(Message(system_in, role=Roles.SYSTEM))
|
||||||
user_in = "Who are you?"
|
user_in = "Who are you?"
|
||||||
print(f"USER: {user_in}")
|
print(f"USER: {user_in}")
|
||||||
print(cgpt.chat(Message(user_in)).text)
|
print(cgpt.chat(Message(user_in)).text)
|
||||||
|
@ -20,4 +23,4 @@ if __name__ == "__main__":
|
||||||
print(f"\nUSER: {user_in}")
|
print(f"\nUSER: {user_in}")
|
||||||
print(cgpt.chat(Message(user_in)).text)
|
print(cgpt.chat(Message(user_in)).text)
|
||||||
print("\nMessage History:")
|
print("\nMessage History:")
|
||||||
[print(m.to_api()) for m in cgpt.message_history]
|
[print(m.to_api()) for m in cgpt._message_history]
|
||||||
|
|
Reference in a new issue