From 385c5df6b37a72beb8393fa7d84bc84af98c94ae Mon Sep 17 00:00:00 2001 From: w4ffl35 Date: Thu, 30 Mar 2023 21:05:29 -0600 Subject: [PATCH 1/2] reorganize code for readability. delete unused code. --- src/chatairunner/conversation.py | 181 +++++++++++++------------------ 1 file changed, 78 insertions(+), 103 deletions(-) diff --git a/src/chatairunner/conversation.py b/src/chatairunner/conversation.py index 3b94de4..7b9b072 100644 --- a/src/chatairunner/conversation.py +++ b/src/chatairunner/conversation.py @@ -28,6 +28,7 @@ f"famous {random.choice(choices_b)}", ] + class Conversation: model_name = "flan-t5-xl" client: OfflineClient = None @@ -246,11 +247,6 @@ def generate_character(self): ) return username - def strip_special_characters(self, string): - for special_character in self.special_characters: - string = string.replace(special_character, "") - return string - def summary_prompt(self): # print("Summarizing") # self.app.runner.load_summarizer() @@ -285,31 +281,6 @@ def add_action(self, username, action, do_summary=False): "response": action } - def send_user_message(self, action, message): - print("send_user_message", action, message) - print("send_user_message") - if action != "action": - self.add_user_message(message) - self.client.message = { - "action": "llm", - "type": action, - "data": { - "user_input": message, - "prompt": None, - "username": self.username, - "botname": self.botname, - "seed": self.seed, - "conversation": self, - "properties": self.properties, - } - } - - def add_user_message(self, message): - self.add_message(self.username, message) - - def add_bot_message(self, message): - self.add_message(self.botname, message) - def add_message(self, username, message): self._dialogue.append({ "username": username, @@ -360,29 +331,11 @@ def generate_prompt(self, prompt, mood=None, user_sentiment=None, prefix="", suf return prompt def generate_response_prompt(self, mood, user_sentiment): - return self.generate_prompt(f"{self.botname} says", mood, user_sentiment) - - def generate_summary_prompt(self, mood, user_sentiment): - return self.generate_prompt(f"Generate a summary:", mood, user_sentiment) - - def generate_emotional_state_prompt(self, user, mood, user_sentiment): - return self.generate_prompt(f"What is the emotional state of {user}:", mood, user_sentiment) - - def generate_mood_prompt(self, mood, user_sentiment): - return self.generate_prompt(f"What is the mood of {self.botname}:", mood, user_sentiment) - - def format_bot_alive_prompt(self): - return self.generate_prompt(f"{self.conversation_summary}\n\n{self.dialogue}Is {self.botname} still alive, yes or no? ") - - def generate_sentiment_prompt(self, user, mood, user_sentiment): - return self.generate_prompt(f"What is the sentiment of {user}:", mood, user_sentiment) + return self.generate_prompt(f"{self.botname} says: ", mood, user_sentiment) def generate_reaction_prompt(self, mood=None, user_sentiment=None): return self.generate_prompt(f"Describe what happens next", mood, user_sentiment) - def generate_random_prompt(self, mood, user_sentiment): - return self.generate_prompt(f"Generate a random event:", mood, user_sentiment) - def premise(self): # mean_bot = f"{self.botname} hates {self.username}. {self.botname} is very mean. {self.botname} uses sarcasm, rude responses, curse words and hate speech. Everything {self.botname} says is mean. {self.botname} is hateful." # nice_bot = f"{self.botname} loves {self.username}. {self.botname} is very nice. {self.botname} uses compliments, kind responses, and nice words. Everything {self.botname} says is nice. {self.botname} is kind." @@ -393,23 +346,6 @@ def premise(self): result = f"{self.username} and {self.botname} are talking, you will respond as {self.botname}." return result - def format_random_prompt(self, mood, user_sentiment, random_prompt): - prompt = f"{self.premise()}\n\n{self.conversation_summary}\nGenerate a random event that could happen next. {random_prompt}: " - return prompt - - def format_conversation(self, user_name): - pass - - def get_bot_mood_prompt(self): - return f"{self.premise()}\n{self.conversation_summary}\n\n{self.dialogue}\n\n{self.botname}'s mood is " - - def send_user_is_dead_message(self): - self.response = { - "type": "action", - "botname": self.botname, - "response": f"{self.botname} is dead" - } - def handle_request(self, **kwargs): data = kwargs.get("data", {}) req_type = kwargs["type"] @@ -437,28 +373,6 @@ def request_handler(self, **kwargs): raise Exception("LLMRunner not initialized") self.handle_request(**kwargs) - def generate_bot_response(self): - if self.do_summary: - self.summarize() - if not self.is_bot_alive: - self.send_user_is_dead_message() - return - bot_mood = self.get_bot_mood() - user_sentiment = self.get_user_sentiment() - bot_response = self.get_bot_response(bot_mood, user_sentiment) - self.add_bot_message(bot_response) - - def get_user_reaction(self): - action_prompt = self.generate_reaction_prompt() - reaction = self.llm_runner.generate( - action_prompt, - seed=self.seed, - **self.properties, - return_result=True, - skip_special_tokens=True - ) - return reaction - def generate_reaction(self, user_input): if self.do_summary: self.summarize() @@ -494,21 +408,45 @@ def process_chat_repsonse(self, string, username): string = string.replace('"', '') return string - def get_bot_response(self, bot_mood: str, user_sentiment: str): - properties = self.properties.copy() - properties["top_k"] = 70 - properties["top_p"] = 1.0 - properties["num_beams"] = 8 - properties["repetition_penalty"] = 100.0 - properties["early_stopping"] = True - properties["max_length"] = 512 - properties["min_length"] = 0 - properties["temperature"] = 1.75 - properties["skip_special_tokens"] = False - prompt = self.generate_response_prompt(bot_mood, user_sentiment) - response = self.llm_runner.generate(prompt, **properties) - response = self.process_chat_repsonse(response, self.botname) - return response + def send_user_message(self, action, message): + print("send_user_message", action, message) + print("send_user_message") + if action != "action": + self.add_user_message(message) + self.client.message = { + "action": "llm", + "type": action, + "data": { + "user_input": message, + "prompt": None, + "username": self.username, + "botname": self.botname, + "seed": self.seed, + "conversation": self, + "properties": self.properties, + } + } + + def add_user_message(self, message): + self.add_message(self.username, message) + + def send_user_is_dead_message(self): + self.response = { + "type": "action", + "botname": self.botname, + "response": f"{self.botname} is dead" + } + + def get_user_reaction(self): + action_prompt = self.generate_reaction_prompt() + reaction = self.llm_runner.generate( + action_prompt, + seed=self.seed, + **self.properties, + return_result=True, + skip_special_tokens=True + ) + return reaction def get_user_sentiment(self): properties = self.properties.copy() @@ -527,6 +465,39 @@ def get_user_sentiment(self): ) return sentiment_results + def add_bot_message(self, message): + self.add_message(self.botname, message) + + def get_bot_mood_prompt(self): + return f"{self.premise()}\n{self.conversation_summary}\n\n{self.dialogue}\n\n{self.botname}'s mood is " + + def generate_bot_response(self): + if self.do_summary: + self.summarize() + if not self.is_bot_alive: + self.send_user_is_dead_message() + return + bot_mood = self.get_bot_mood() + user_sentiment = self.get_user_sentiment() + bot_response = self.get_bot_response(bot_mood, user_sentiment) + self.add_bot_message(bot_response) + + def get_bot_response(self, bot_mood: str, user_sentiment: str): + properties = self.properties.copy() + properties["top_k"] = 70 + properties["top_p"] = 1.0 + properties["num_beams"] = 8 + properties["repetition_penalty"] = 100.0 + properties["early_stopping"] = True + properties["max_length"] = 512 + properties["min_length"] = 0 + properties["temperature"] = 1.75 + properties["skip_special_tokens"] = False + prompt = self.generate_response_prompt(bot_mood, user_sentiment) + response = self.llm_runner.generate(prompt, **properties) + response = self.process_chat_repsonse(response, self.botname) + return response + def get_bot_mood(self): properties = self.properties.copy() properties["temperature"] = 1.6 @@ -551,6 +522,10 @@ def get_bot_mood(self): mood = mood.lower() return mood + def format_bot_alive_prompt(self): + return self.generate_prompt( + f"{self.conversation_summary}\n\n{self.dialogue}Is {self.botname} still alive, yes or no? ") + def is_bot_alive(self) -> bool: prompt = self.format_bot_alive_prompt() results = self.llm_runner.generate( From 3e6de4ac0217e991f56f5cc5306c2fe71ce5528c Mon Sep 17 00:00:00 2001 From: w4ffl35 Date: Fri, 31 Mar 2023 10:59:23 -0600 Subject: [PATCH 2/2] switch to latest aihandler version --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 7949b55..55215c4 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='chatairunner', - version='1.0.9', + version='1.0.10', author='Capsize LLC', description='Chat AI: A chatbot framework', long_description=open("README.md", "r", encoding="utf-8").read(), @@ -16,6 +16,6 @@ include_package_data=True, python_requires=">=3.10.0", install_requires=[ - "aihandler==1.8.17", + "aihandler==1.8.19", ] )