Skip to content

Commit

Permalink
Merge pull request #23 from Capsize-Games/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
w4ffl35 authored Mar 31, 2023
2 parents 7796348 + 3e6de4a commit 21e7cca
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 105 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -16,6 +16,6 @@
include_package_data=True,
python_requires=">=3.10.0",
install_requires=[
"aihandler==1.8.17",
"aihandler==1.8.19",
]
)
181 changes: 78 additions & 103 deletions src/chatairunner/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
f"famous {random.choice(choices_b)}",
]


class Conversation:
model_name = "flan-t5-xl"
client: OfflineClient = None
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}<extra_id_0>Is {self.botname} still alive, yes or no? </s>")

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."
Expand All @@ -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}\n<extra_id_0>Generate a random event that could happen next. <extra_id_1>{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"]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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}<extra_id_0>Is {self.botname} still alive, yes or no? </s>")

def is_bot_alive(self) -> bool:
prompt = self.format_bot_alive_prompt()
results = self.llm_runner.generate(
Expand Down

0 comments on commit 21e7cca

Please sign in to comment.