MCPcopy Index your code
hub / github.com/InternLM/lmdeploy / BaseChatTemplate

Class BaseChatTemplate

lmdeploy/model.py:113–202  ·  view source on GitHub ↗

Base Chat template.

Source from the content-addressed store, hash-verified

111
112@MODELS.register_module(name='base')
113class BaseChatTemplate:
114 """Base Chat template."""
115
116 def __init__(self,
117 system='',
118 meta_instruction='',
119 eosys='',
120 user='',
121 eoh='',
122 assistant='',
123 eoa='',
124 separator='',
125 tool='',
126 eotool='',
127 capability='chat',
128 stop_words=None,
129 **kwargs):
130 self.system = system
131 self.meta_instruction = meta_instruction
132 self.user = user
133 self.eoh = eoh
134 self.eoa = eoa
135 self.separator = separator
136 self.eosys = eosys
137 self.assistant = assistant
138 self.tool = tool
139 self.eotool = eotool
140 self.stop_words = stop_words
141 self.capability = capability
142
143 def get_prompt(self, prompt, sequence_start=True):
144 """Return the prompt that is concatenated with other elements in the
145 chat template.
146
147 Args:
148 prompt (str): user's input prompt
149 sequence_start (bool): indicator for the first round chat of a
150 session sequence
151 Returns:
152 str: the concatenated prompt
153 """
154 if self.capability == 'completion':
155 return prompt
156 if sequence_start:
157 # None is different from ''
158 if self.meta_instruction is not None:
159 return f'{self.system}{self.meta_instruction}{self.eosys}' \
160 f'{self.user}{prompt}{self.eoh}' \
161 f'{self.assistant}'
162 else:
163 return f'{self.user}{prompt}{self.eoh}' \
164 f'{self.assistant}'
165 else:
166 return f'{self.separator}{self.user}{prompt}{self.eoh}' \
167 f'{self.assistant}'
168
169 def messages2prompt(self, messages, sequence_start=True, **kwargs):
170 """Return the prompt that is concatenated with other elements in the

Callers 3

chat_templateMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected