From bbd0f64f54cdef73f7d5b4611c96babeae4575e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20M=C3=BCller=20=28ChaoticByte=29?= Date: Thu, 9 Mar 2023 17:16:58 +0100 Subject: [PATCH] Added ChatGPT.add_to_chat() method to add messages that will be sent later and updated example.py --- chatgpt_pyapi/__init__.py | 19 ++++++++++++------- example.py | 7 +++++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/chatgpt_pyapi/__init__.py b/chatgpt_pyapi/__init__.py index ee0da21..9f80222 100644 --- a/chatgpt_pyapi/__init__.py +++ b/chatgpt_pyapi/__init__.py @@ -45,15 +45,14 @@ class ChatGPT: # Create string used in header self.auth = f"Bearer {api_key}" # This list will contain all prior messages - self.message_history = [] + self._message_history = [] self.model = model def chat(self, message: Message) -> Message: - # 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) + '''Add a message to the message history & send it to ChatGPT. Returns the answer as a Message instance.''' + self.add_to_chat(message) # 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( {"model": self.model, "messages": api_input}, separators=(",", ":")).encode() @@ -75,8 +74,14 @@ class ChatGPT: api_output_answer["content"] = api_output_answer["content"].strip("\n") # convert to Message object response_message = Message.from_api(api_output_answer) - self.message_history.append(response_message) + self._message_history.append(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): - self.message_history = [] + self._message_history = [] diff --git a/example.py b/example.py index d7f5a38..1f2592d 100755 --- a/example.py +++ b/example.py @@ -5,14 +5,17 @@ from os import environ from chatgpt_pyapi import ChatGPT -from chatgpt_pyapi import Models from chatgpt_pyapi import Message +from chatgpt_pyapi import Models +from chatgpt_pyapi import Roles API_KEY = environ["OPENAI_API_KEY"] if __name__ == "__main__": 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?" print(f"USER: {user_in}") print(cgpt.chat(Message(user_in)).text) @@ -20,4 +23,4 @@ if __name__ == "__main__": print(f"\nUSER: {user_in}") print(cgpt.chat(Message(user_in)).text) print("\nMessage History:") - [print(m.to_api()) for m in cgpt.message_history] + [print(m.to_api()) for m in cgpt._message_history]