Skip to content

Commit

Permalink
refactor: system template as a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
umbertogriffo committed Aug 27, 2024
1 parent 1a6c3c2 commit d7cd34d
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 19 deletions.
21 changes: 11 additions & 10 deletions chatbot/bot/client/lama_cpp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
REFINED_ANSWER_CONVERSATION_AWARENESS_PROMPT_TEMPLATE,
REFINED_CTX_PROMPT_TEMPLATE,
REFINED_QUESTION_CONVERSATION_AWARENESS_PROMPT_TEMPLATE,
SYSTEM_TEMPLATE,
generate_conversation_awareness_prompt,
generate_ctx_prompt,
generate_qa_prompt,
Expand All @@ -22,7 +23,7 @@

class LamaCppClient:
"""
Class for implementing language model clients.
Class for implementing language model client.
"""

def __init__(self, model_folder: Path, model_settings: Model):
Expand Down Expand Up @@ -99,7 +100,7 @@ def generate_answer(self, prompt: str, max_new_tokens: int = 512) -> str:

output = self.llm.create_chat_completion(
messages=[
{"role": "system", "content": self.model_settings.system_template},
{"role": "system", "content": SYSTEM_TEMPLATE},
{"role": "user", "content": f"{prompt}"},
],
max_tokens=max_new_tokens,
Expand All @@ -123,7 +124,7 @@ async def async_generate_answer(self, prompt: str, max_new_tokens: int = 512) ->
"""
output = self.llm.create_chat_completion(
messages=[
{"role": "system", "content": self.model_settings.system_template},
{"role": "system", "content": SYSTEM_TEMPLATE},
{"role": "user", "content": f"{prompt}"},
],
max_tokens=max_new_tokens,
Expand Down Expand Up @@ -168,7 +169,7 @@ def start_answer_iterator_streamer(
"""
stream = self.llm.create_chat_completion(
messages=[
{"role": "system", "content": self.model_settings.system_template},
{"role": "system", "content": SYSTEM_TEMPLATE},
{"role": "user", "content": f"{prompt}"},
],
max_tokens=max_new_tokens,
Expand All @@ -192,7 +193,7 @@ async def async_start_answer_iterator_streamer(
"""
stream = self.llm.create_chat_completion(
messages=[
{"role": "system", "content": self.model_settings.system_template},
{"role": "system", "content": SYSTEM_TEMPLATE},
{"role": "user", "content": f"{prompt}"},
],
max_tokens=max_new_tokens,
Expand All @@ -217,7 +218,7 @@ def generate_qa_prompt(self, question: str) -> str:
"""
return generate_qa_prompt(
template=QA_PROMPT_TEMPLATE,
system=self.model_settings.system_template,
system=SYSTEM_TEMPLATE,
question=question,
)

Expand All @@ -234,7 +235,7 @@ def generate_ctx_prompt(self, question: str, context: str) -> str:
"""
return generate_ctx_prompt(
template=CTX_PROMPT_TEMPLATE,
system=self.model_settings.system_template,
system=SYSTEM_TEMPLATE,
question=question,
context=context,
)
Expand All @@ -253,7 +254,7 @@ def generate_refined_ctx_prompt(self, question: str, context: str, existing_answ
"""
return generate_refined_ctx_prompt(
template=REFINED_CTX_PROMPT_TEMPLATE,
system=self.model_settings.system_template,
system=SYSTEM_TEMPLATE,
question=question,
context=context,
existing_answer=existing_answer,
Expand All @@ -262,15 +263,15 @@ def generate_refined_ctx_prompt(self, question: str, context: str, existing_answ
def generate_refined_question_conversation_awareness_prompt(self, question: str, chat_history: str) -> str:
return generate_conversation_awareness_prompt(
template=REFINED_QUESTION_CONVERSATION_AWARENESS_PROMPT_TEMPLATE,
system=self.model_settings.system_template,
system=SYSTEM_TEMPLATE,
question=question,
chat_history=chat_history,
)

def generate_refined_answer_conversation_awareness_prompt(self, question: str, chat_history: str) -> str:
return generate_conversation_awareness_prompt(
template=REFINED_ANSWER_CONVERSATION_AWARENESS_PROMPT_TEMPLATE,
system=self.model_settings.system_template,
system=SYSTEM_TEMPLATE,
question=question,
chat_history=chat_history,
)
4 changes: 4 additions & 0 deletions chatbot/bot/client/prompt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# A string template for the system message.
# This template is used to define the behavior and characteristics of the assistant.
SYSTEM_TEMPLATE = """You are a helpful, respectful and honest assistant."""

# A string template with placeholders for question.
QA_PROMPT_TEMPLATE = """Answer the question below:
{question}
Expand Down
3 changes: 0 additions & 3 deletions chatbot/bot/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@
class Model(ABC):
url: str
file_name: str
clients: list[str]
config: Dict[str, Any]
config_answer: Optional[Dict[str, Any]]
type: Optional[str]
system_template: str
1 change: 0 additions & 1 deletion chatbot/bot/model/settings/llama_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ class Llama3Settings(Model):
"n_gpu_layers": 50, # The number of layers to offload to GPU, if you have GPU acceleration available
}
config_answer = {"temperature": 0.7, "stop": []}
system_template = "You are a helpful, respectful and honest assistant. "
2 changes: 0 additions & 2 deletions chatbot/bot/model/settings/openchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class OpenChat35Settings(Model):
"n_gpu_layers": 50, # The number of layers to offload to GPU, if you have GPU acceleration available
}
config_answer = {"temperature": 0.7, "stop": []}
system_template = "You are a helpful, respectful and honest assistant. "


class OpenChat36Settings(Model):
Expand All @@ -23,4 +22,3 @@ class OpenChat36Settings(Model):
"flash_attn": False, # Use flash attention.
}
config_answer = {"temperature": 0.7, "stop": []}
system_template = ""
1 change: 0 additions & 1 deletion chatbot/bot/model/settings/phi_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ class PhiThreeSettings(Model):
"n_gpu_layers": 33, # The number of layers to offload to GPU, if you have GPU acceleration available
}
config_answer = {"temperature": 0.7, "stop": []}
system_template = "You are a helpful, respectful and honest assistant. "
1 change: 0 additions & 1 deletion chatbot/bot/model/settings/stablelm_zephyr.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ class StableLMZephyrSettings(Model):
"n_gpu_layers": 35, # The number of layers to offload to GPU, if you have GPU acceleration available
}
config_answer = {"temperature": 0.7, "stop": []}
system_template = ""
1 change: 0 additions & 1 deletion chatbot/bot/model/settings/starling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ class StarlingSettings(Model):
"n_gpu_layers": 50, # The number of layers to offload to GPU, if you have GPU acceleration available
}
config_answer = {"temperature": 0.7, "stop": []}
system_template = "You are a helpful, respectful and honest assistant. "

0 comments on commit d7cd34d

Please sign in to comment.