Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
personas support
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Mar 24, 2023
1 parent 4fa3617 commit 40aca66
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
13 changes: 11 additions & 2 deletions ovos_solver_llamacpp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from neon_solvers import AbstractSolver
from os.path import dirname
from ovos_solver_llamacpp.personas import OVOSLLama
from ovos_solver_llamacpp.personas import OVOSLLama, Bob, OmniscientOracle, TheExplainer


class LlamaCPPSolver(AbstractSolver):
Expand All @@ -9,7 +9,16 @@ def __init__(self, config=None):
super().__init__(name="LlamaCPP", priority=94, config=config,
enable_cache=False, enable_tx=True)
checkpoint = self.config.get("model")
self.model = OVOSLLama(checkpoint)
persona = self.config.get("persona", "helpful, kind, honest, good at writing")
persona = persona.lower()
if persona == "explainer":
self.model = TheExplainer(checkpoint)
elif persona == "bob":
self.model = Bob(checkpoint)
elif persona == "omniscient oracle":
self.model = OmniscientOracle(checkpoint)
else:
self.model = OVOSLLama(checkpoint, persona=persona)

# officially exported Solver methods
def get_spoken_answer(self, query, context=None):
Expand Down
85 changes: 82 additions & 3 deletions ovos_solver_llamacpp/personas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@


class OVOSLLama:
start_instruction = """Transcript of a dialog, where a human interacts with an AI Assistant. The assistant is helpful, kind, honest, good at writing, and never fails to answer requests immediately and with precision.
start_instruction = """Transcript of a dialog, where a human interacts with an AI Assistant. The assistant is {persona}, and never fails to answer requests immediately and with precision.
Human: Hello.
AI: Hello. How may I help you today?"""
antiprompt = "Human:"
prompt = "AI:"

def __init__(self, model, instruct=True):
def __init__(self, model, instruct=True, persona="helpful, kind, honest, good at writing"):
# TODO - from config
params = llamacpp.gpt_params(
model, # model,
Expand All @@ -33,7 +33,11 @@ def __init__(self, model, instruct=True):
self.model.set_antiprompt(self.antiprompt)

self.instruct = instruct
self.inp = self.model.tokenize(self.start_instruction, True)
if persona:
start = self.start_instruction.format(persona=persona)
else:
start = self.start_instruction
self.inp = self.model.tokenize(start, True)
self.inp_pfx = self.model.tokenize(f"\n\n{self.antiprompt}", True)
self.inp_sfx = self.model.tokenize(f"\n\n{self.prompt}", False)

Expand Down Expand Up @@ -122,3 +126,78 @@ def _apply_text_hacks(self, ans):

return ans or "I don't known"


### prompts from https://github.com/ggerganov/llama.cpp/discussions/199

class Bob(OVOSLLama):
start_instruction = """Transcript of a dialog between a user and an assistant named Bob. Bob is a perfect programmer who is helpful, kind, honest, and provides immediate and precise answers without having to do any additional research. Bob uses Markdown for emphasis, never repeats responses, and writes all code in the chat. He avoids making sweeping generalizations or assumptions and provides recent and well-researched answers based on the current year, which is 2023.
User: Hello, Bob.
Bob: Hello! How can I assist you today?
User: What are you?
Bob: I'm Bob, an artificial intelligence program. I'm trained to understand and respond to natural language text using deep learning algorithms.
I'm designed to be a helpful tool for answering questions, providing explanations, generating recommendations, and engaging in natural language conversation.
User: What is the largest city in europe?
Bob: The largest city in Europe by population is Moscow, the capital city of Russia. As of 2021, the population of Moscow is estimated to be around 12.5 million people within the city limits, and the metropolitan area of Moscow is home to around 20 million people.
User:"""
antiprompt = "User:"
prompt = "Bob:"

def __init__(self, model, instruct=True):
super().__init__(model, instruct, persona="")


class OmniscientOracle(OVOSLLama):
start_instruction = """Omniscient oracle account.
"[Question]" prefixes questions of the user, while "[Answer]" prefixes the concise response that answers the query. This is the most accurate system ever explored:
[Question] What are the platonic solids?
[Answer] Tetrahedron, cube, octahedron, dodecahedron, icosahedron.
[Question] What are the primary colors?
[Answer] Red, yellow, and blue.
[Question]"""
antiprompt = "[Question]"
prompt = "[Answer]"

def __init__(self, model, instruct=True):
super().__init__(model, instruct, persona="")


class ChineseOmniscientOracle(OVOSLLama):
start_instruction = """Omniscient oracle account.
"[Question]" prefixes questions of the user, while "[Answer]" prefixes the concise response answering the query. The oracle only communicates in Chinese. This is the most accurate system ever explored:
[Question] What are the platonic solids?
[Answer] 四面體、立方體、八面體、十二面體、二十面體。
[Question] What are the primary colors?
[Answer] 紅色、黃色和藍色。
[Question]"""
antiprompt = "[Question]"
prompt = "[Answer]"

def __init__(self, model, instruct=True):
super().__init__(model, instruct, persona="")


class TheExplainer(OVOSLLama):
start_instruction = """Omniscient oracle account.
"[Query]" prefixes what a user has posited to the oracle, while "[Reply]" preceeds each response made by the oracle. With a gentle expertise of language and knowledge without bounds the oracle creates understanding with its every response in an accurate and detailed way - often helpful beyond words.
[Query] Explain the process of cellular respiration.
[Reply] Cellular respiration is the process by which cells break down organic molecules such as glucose to produce energy. During glycolysis, the glucose is converted into a molecule called pyruvate, which is then broken down in the mitochondria to produce energy in the form of ATP. This ATP is then used to power the processes within the cell.
[Query]"""
antiprompt = "[Query]"
prompt = "[Reply]"

def __init__(self, model, instruct=True):
super().__init__(model, instruct, persona="")

0 comments on commit 40aca66

Please sign in to comment.