Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added persona #1

Merged
merged 3 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions neon_llm_core/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ def llm_model_name(self) -> str:
def _system_prompt(self) -> str:
pass

def ask(self, message: str, chat_history: List[List[str]]) -> str:
def ask(self, message: str, chat_history: List[List[str]], persona: dict) -> str:
""" Generates llm response based on user message and (user, llm) chat history """
prompt = self._assemble_prompt(message, chat_history)
prompt = self._assemble_prompt(message, chat_history, persona)
llm_text_output = self._call_model(prompt)
return llm_text_output

@abstractmethod
def get_sorted_answer_indexes(self, question: str, answers: List[str]) -> List[int]:
def get_sorted_answer_indexes(self, question: str, answers: List[str], persona: dict) -> List[int]:
"""
Creates sorted list of answer indexes with respect to order provided in :param answers
Results should be sorted from best to worst
Expand All @@ -87,7 +87,7 @@ def _call_model(self, prompt: str) -> str:
pass

@abstractmethod
def _assemble_prompt(self, message: str, chat_history: List[List[str]]):
def _assemble_prompt(self, message: str, chat_history: List[List[str]], persona: dict):
"""
Assembles prompt engineering logic

Expand Down
16 changes: 10 additions & 6 deletions neon_llm_core/rmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ def handle_request(self, body: dict):

query = body["query"]
history = body["history"]
persona = body.get("persona",{})

try:
response = self.model.ask(message=query, chat_history=history)
response = self.model.ask(message=query, chat_history=history, persona=persona)
except ValueError as err:
LOG.error(f'ValueError={err}')
response = 'Sorry, but I cannot respond to your message at the moment, please try again later'
Expand All @@ -131,12 +132,13 @@ def handle_score_request(self, body: dict):

query = body["query"]
responses = body["responses"]
persona = body.get("persona",{})

if not responses:
sorted_answer_indexes = []
else:
try:
sorted_answer_indexes = self.model.get_sorted_answer_indexes(question=query, answers=responses)
sorted_answer_indexes = self.model.get_sorted_answer_indexes(question=query, answers=responses, persona=persona)
except ValueError as err:
LOG.error(f'ValueError={err}')
sorted_answer_indexes = []
Expand All @@ -159,17 +161,19 @@ def handle_opinion_request(self, body: dict):

query = body["query"]
options = body["options"]
persona = body.get("persona",{})
responses = list(options.values())

if not responses:
opinion = "Sorry, but I got no options to choose from."
else:
try:
sorted_answer_indexes = self.model.get_sorted_answer_indexes(question=query, answers=responses)
sorted_answer_indexes = self.model.get_sorted_answer_indexes(question=query, answers=responses, persona=persona)
best_respondent_nick, best_response = list(options.items())[sorted_answer_indexes[0]]
opinion = self._ask_model_for_opinion(respondent_nick=best_respondent_nick,
question=query,
answer=best_response)
answer=best_response,
persona=persona)
except ValueError as err:
LOG.error(f'ValueError={err}')
opinion = "Sorry, but I experienced an issue trying to make up an opinion on this topic"
Expand All @@ -183,11 +187,11 @@ def handle_opinion_request(self, body: dict):
queue=routing_key)
LOG.info(f"Handled ask request for message_id={message_id}")

def _ask_model_for_opinion(self, respondent_nick: str, question: str, answer: str) -> str:
def _ask_model_for_opinion(self, respondent_nick: str, question: str, answer: str, persona: dict) -> str:
prompt = self.compose_opinion_prompt(respondent_nick=respondent_nick,
question=question,
answer=answer)
opinion = self.model.ask(message=prompt, chat_history=[])
opinion = self.model.ask(message=prompt, chat_history=[], persona=persona)
LOG.info(f'Received LLM opinion={opinion}, prompt={prompt}')
return opinion

Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

__version__ = "0.0.6"
__version__ = "0.1.0"
Loading