-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
prompt_template_utils.py
112 lines (93 loc) · 4.16 KB
/
prompt_template_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
This file implements prompt template for llama based models.
Modify the prompt template based on the model you select.
This seems to have significant impact on the output of the LLM.
"""
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
# this is specific to Llama-2.
system_prompt = """You are a helpful assistant, you will use the provided context to answer user questions.
Read the given context before answering questions and think step by step. If you can not answer a user question based on
the provided context, inform the user. Do not use any other information for answering user. Provide a detailed answer to the question."""
def get_prompt_template(system_prompt=system_prompt, promptTemplate_type=None, history=False):
if promptTemplate_type == "llama":
B_INST, E_INST = "[INST]", "[/INST]"
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
SYSTEM_PROMPT = B_SYS + system_prompt + E_SYS
if history:
instruction = """
Context: {history} \n {context}
User: {question}"""
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
prompt = PromptTemplate(input_variables=["history", "context", "question"], template=prompt_template)
else:
instruction = """
Context: {context}
User: {question}"""
prompt_template = B_INST + SYSTEM_PROMPT + instruction + E_INST
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
elif promptTemplate_type == "llama3":
B_INST, E_INST = "<|start_header_id|>user<|end_header_id|>", "<|eot_id|>"
B_SYS, E_SYS = "<|begin_of_text|><|start_header_id|>system<|end_header_id|> ", "<|eot_id|>"
ASSISTANT_INST = "<|start_header_id|>assistant<|end_header_id|>"
SYSTEM_PROMPT = B_SYS + system_prompt + E_SYS
if history:
instruction = """
Context: {history} \n {context}
User: {question}"""
prompt_template = SYSTEM_PROMPT + B_INST + instruction + ASSISTANT_INST
prompt = PromptTemplate(input_variables=["history", "context", "question"], template=prompt_template)
else:
instruction = """
Context: {context}
User: {question}"""
prompt_template = SYSTEM_PROMPT + B_INST + instruction + ASSISTANT_INST
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
elif promptTemplate_type == "mistral":
B_INST, E_INST = "<s>[INST] ", " [/INST]"
if history:
prompt_template = (
B_INST
+ system_prompt
+ """
Context: {history} \n {context}
User: {question}"""
+ E_INST
)
prompt = PromptTemplate(input_variables=["history", "context", "question"], template=prompt_template)
else:
prompt_template = (
B_INST
+ system_prompt
+ """
Context: {context}
User: {question}"""
+ E_INST
)
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
else:
# change this based on the model you have selected.
if history:
prompt_template = (
system_prompt
+ """
Context: {history} \n {context}
User: {question}
Answer:"""
)
prompt = PromptTemplate(input_variables=["history", "context", "question"], template=prompt_template)
else:
prompt_template = (
system_prompt
+ """
Context: {context}
User: {question}
Answer:"""
)
prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
memory = ConversationBufferMemory(input_key="question", memory_key="history")
print(f"Here is the prompt used: {prompt}")
return (
prompt,
memory,
)