-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from BESSER-PEARL/dev
v1.5.0
- Loading branch information
Showing
42 changed files
with
1,546 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from besser.bot.core.processors.processor import Processor | ||
from besser.bot.nlp.llm.llm import LLM | ||
from besser.bot.core.bot import Bot | ||
from besser.bot.core.session import Session | ||
from besser.bot.nlp.nlp_engine import NLPEngine | ||
|
||
|
||
class UserAdaptationProcessor(Processor): | ||
"""The UserAdaptationProcessor takes into account the user's profile and adapts the bot's responses to fit the | ||
profile. The goal is to increase the user experience. | ||
This processor leverages LLMs to adapt the messages given a user profile. For static profiles, an adaptation will be | ||
done once. If the profile changes, then an adapation will be triggered again. | ||
Args: | ||
bot (Bot): The bot the processor belongs to | ||
llm_name (str): the name of the LLM to use. | ||
context (str): additional context to improve the adaptation. should include information about the bot itself | ||
and the task it should accomplish | ||
Attributes: | ||
bot (Bot): The bot the processor belongs to | ||
_llm_name (str): the name of the LLM to use. | ||
_context (str): additional context to improve the adaptation. should include information about the bot itself | ||
and the task it should accomplish | ||
_user_model (dict): dictionary containing the user models | ||
""" | ||
def __init__(self, bot: 'Bot', llm_name: str, context: str = None): | ||
super().__init__(bot=bot, bot_messages=True) | ||
self._llm_name: str = llm_name | ||
self._nlp_engine: 'NLPEngine' = bot.nlp_engine | ||
self._user_model: dict = {} | ||
if context: | ||
self._context = context | ||
else: | ||
self._context = "You are a chatbot." | ||
|
||
# add capability to improve/change prompt of context | ||
def process(self, session: 'Session', message: str) -> str: | ||
"""Method to process a message and adapt its content based on a given user model. | ||
The stored user model will be fetched and sent as part of the context. | ||
Args: | ||
session (Session): the current session | ||
message (str): the message to be processed | ||
Returns: | ||
str: the processed message | ||
""" | ||
llm: LLM = self._nlp_engine._llms[self._llm_name] | ||
user_context = f"{self._context}\n\ | ||
You are capable of adapting your predefined answers based on a given user profile.\ | ||
Your goal is to increase the user experience by adapting the messages based on the different attributes of the user\ | ||
profile as best as possible and take all the attributes into account.\ | ||
You are free to adapt the messages in any way you like.\ | ||
The user should relate more. This is the user's profile\n \ | ||
{str(self._user_model[session.id])}" | ||
prompt = f"You need to adapt this message: {message}\n Only respond with the adapated message!" | ||
llm_response: str = llm.predict(prompt, session=session, system_message=user_context) | ||
return llm_response | ||
|
||
def add_user_model(self, session: 'Session', user_model: dict) -> None: | ||
"""Method to store the user model internally. | ||
The user model shall be stored internally. | ||
Args: | ||
session (Session): the current session | ||
user_model (dict): the user model of a given user | ||
""" | ||
self._user_model[session.id] = user_model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.