NeMo
Safetensors
llama
mylesgoose commited on
Commit
9df56ac
·
verified ·
1 Parent(s): 5480a2f

Upload conversation.py

Browse files
Files changed (1) hide show
  1. vision/conversation.py +605 -0
vision/conversation.py ADDED
@@ -0,0 +1,605 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dataclasses
2
+ from enum import auto, Enum
3
+ from typing import List, Any, Dict, Union, Tuple
4
+ import re
5
+ import base64
6
+ from io import BytesIO
7
+ from PIL import Image
8
+ from transformers import AutoTokenizer
9
+ import os
10
+ from datetime import date
11
+ class SeparatorStyle(Enum):
12
+ """Different separator style."""
13
+
14
+ SINGLE = auto()
15
+ TWO = auto()
16
+ MPT = auto()
17
+ PLAIN = auto()
18
+ CHATML = auto()
19
+ LLAMA_2 = auto()
20
+ LLAMA_3 = auto()
21
+ LLAMA_3_1 = auto()
22
+ QWEN = auto()
23
+ GEMMA = auto()
24
+
25
+
26
+ @dataclasses.dataclass
27
+ class Conversation:
28
+ """A class that keeps all conversation history."""
29
+
30
+ system: str
31
+ roles: List[str]
32
+ messages: List[List[str]]
33
+ offset: int
34
+ sep_style: SeparatorStyle = SeparatorStyle.SINGLE
35
+ sep: str = "###"
36
+ sep2: str = None
37
+ version: str = "Unknown"
38
+ tokenizer_id: str = ""
39
+ tokenizer: Any = None
40
+ stop_str: Union[str, List[str]] = None
41
+ stop_token_ids: List[int] = None
42
+ skip_next: bool = False
43
+ date_string: str = date.today().strftime("%d %b %Y")
44
+ eos_token_id: List[int] = None
45
+
46
+ def get_prompt(self):
47
+ messages = self.messages
48
+ if len(messages) > 0 and type(messages[0][1]) is tuple:
49
+ messages = self.messages.copy()
50
+ init_role, init_msg = messages[0].copy()
51
+ init_msg = init_msg[0]
52
+ if "mmtag" in self.version:
53
+ init_msg = init_msg.replace("<image>", "").strip()
54
+ messages[0] = (init_role, init_msg)
55
+ messages.insert(0, (self.roles[0], "<Image><image></Image>"))
56
+ messages.insert(1, (self.roles[1], "Received."))
57
+ elif not init_msg.startswith("<image>"):
58
+ init_msg = init_msg.replace("<image>", "").strip()
59
+ messages[0] = (init_role, "<image>\n" + init_msg)
60
+ else:
61
+ messages[0] = (init_role, init_msg)
62
+
63
+ if self.sep_style == SeparatorStyle.SINGLE:
64
+ ret = self.system + self.sep
65
+ for role, message in messages:
66
+ if message:
67
+ if type(message) is tuple:
68
+ message, _, _ = message
69
+ ret += role + ": " + message + self.sep
70
+ else:
71
+ ret += role + ":"
72
+
73
+ elif self.sep_style == SeparatorStyle.TWO:
74
+ seps = [self.sep, self.sep2]
75
+ ret = self.system + seps[0]
76
+ for i, (role, message) in enumerate(messages):
77
+ if message:
78
+ if type(message) is tuple:
79
+ message, _, _ = message
80
+ ret += role + ": " + message + seps[i % 2]
81
+ else:
82
+ ret += role + ":"
83
+
84
+ elif self.sep_style == SeparatorStyle.CHATML:
85
+ ret = "" if self.system == "" else self.system + self.sep + "\n"
86
+ for role, message in messages:
87
+ if message:
88
+ if type(message) is tuple:
89
+ message, images, _ = message
90
+ message = "<image>" * len(images) + message
91
+ ret += role + "\n" + message + self.sep + "\n"
92
+ else:
93
+ ret += role + "\n"
94
+ return ret
95
+
96
+ elif self.sep_style == SeparatorStyle.LLAMA_3:
97
+ if self.tokenizer is None:
98
+ raise ValueError("Llama 3 tokenizer is not available. Make sure you have the necessary permissions.")
99
+ chat_template_messages = [{"role": "system", "content": self.system}]
100
+ for role, message in messages:
101
+ if message:
102
+ if type(message) is tuple:
103
+ message, images = message
104
+ message = "<image>" * len(images) + message
105
+ chat_template_messages.append({"role": role, "content": message})
106
+
107
+ #print(chat_template_messages)
108
+ processed_output = self.tokenizer.apply_chat_template(chat_template_messages, tokenize=False, date_string=self.date_string, add_generation_prompt=True)
109
+ #print(processed_output)
110
+ return processed_output
111
+ elif self.sep_style == SeparatorStyle.LLAMA_3_1:
112
+ if self.tokenizer is None:
113
+ raise ValueError("Llama 3 tokenizer is not available. Make sure you have the necessary permissions.")
114
+ chat_template_messages = [{"role": "system", "content": self.system}]
115
+ for role, message in messages:
116
+ if message:
117
+ if type(message) is tuple:
118
+ message, images = message
119
+ message = "<image>" * len(images) + message
120
+ chat_template_messages.append({"role": role, "content": message})
121
+ #print(chat_template_messages)
122
+ processed_output = self.tokenizer.apply_chat_template(chat_template_messages, tokenize=False, date_string=self.date_string, add_generation_prompt=True)
123
+ #print(processed_output)
124
+ return processed_output
125
+
126
+ elif self.sep_style == SeparatorStyle.MPT:
127
+ ret = self.system + self.sep
128
+ for role, message in messages:
129
+ if message:
130
+ if type(message) is tuple:
131
+ message, _, _ = message
132
+ ret += role + message + self.sep
133
+ else:
134
+ ret += role
135
+
136
+ elif self.sep_style == SeparatorStyle.GEMMA:
137
+ ret = ""
138
+ for i, (role, message) in enumerate(messages):
139
+ assert role == self.roles[i % 2], "Conversation should alternate user/assistant/user/assistant/..."
140
+ if message:
141
+ if type(message) is tuple:
142
+ message, _, _ = message
143
+ ret += role + message + self.sep
144
+ else:
145
+ ret += role
146
+
147
+ elif self.sep_style == SeparatorStyle.LLAMA_2:
148
+ wrap_sys = lambda msg: f"<<SYS>>\n{msg}\n<</SYS>>\n\n" if len(msg) > 0 else msg
149
+ wrap_inst = lambda msg: f"[INST] {msg} [/INST]"
150
+ ret = ""
151
+
152
+ for i, (role, message) in enumerate(messages):
153
+ if i == 0:
154
+ assert message, "first message should not be none"
155
+ assert role == self.roles[0], "first message should come from user"
156
+ if message:
157
+ if type(message) is tuple:
158
+ message, _, _ = message
159
+ if i == 0:
160
+ message = wrap_sys(self.system) + message
161
+ if i % 2 == 0:
162
+ message = wrap_inst(message)
163
+ ret += self.sep + message
164
+ else:
165
+ ret += " " + message + " " + self.sep2
166
+ else:
167
+ ret += ""
168
+ ret = ret.lstrip(self.sep)
169
+
170
+ elif self.sep_style == SeparatorStyle.PLAIN:
171
+ seps = [self.sep, self.sep2]
172
+ ret = self.system
173
+ for i, (role, message) in enumerate(messages):
174
+ if message:
175
+ if type(message) is tuple:
176
+ message, _, _ = message
177
+ ret += message + seps[i % 2]
178
+ else:
179
+ ret += ""
180
+ else:
181
+ raise ValueError(f"Invalid style: {self.sep_style}")
182
+
183
+ return ret
184
+
185
+ def append_message(self, role, message):
186
+ self.messages.append([role, message])
187
+
188
+ def process_image(self, image, image_process_mode, return_pil=False, image_format="PNG"):
189
+ if image_process_mode == "Pad":
190
+
191
+ def expand2square(pil_img, background_color=(122, 116, 104)):
192
+ width, height = pil_img.size
193
+ if width == height:
194
+ return pil_img
195
+ elif width > height:
196
+ result = Image.new(pil_img.mode, (width, width), background_color)
197
+ result.paste(pil_img, (0, (width - height) // 2))
198
+ return result
199
+ else:
200
+ result = Image.new(pil_img.mode, (height, height), background_color)
201
+ result.paste(pil_img, ((height - width) // 2, 0))
202
+ return result
203
+
204
+ image = expand2square(image)
205
+ elif image_process_mode in ["Default", "Crop"]:
206
+ pass
207
+ elif image_process_mode == "Resize":
208
+ image = image.resize((336, 336))
209
+ else:
210
+ raise ValueError(f"Invalid image_process_mode: {image_process_mode}")
211
+
212
+ if type(image) is not Image.Image:
213
+ image = Image.open(image).convert("RGB")
214
+
215
+ max_hw, min_hw = max(image.size), min(image.size)
216
+ aspect_ratio = max_hw / min_hw
217
+ max_len, min_len = 672, 448
218
+ shortest_edge = int(min(max_len / aspect_ratio, min_len, min_hw))
219
+ longest_edge = int(shortest_edge * aspect_ratio)
220
+ W, H = image.size
221
+ if H > W:
222
+ H, W = longest_edge, shortest_edge
223
+ else:
224
+ H, W = shortest_edge, longest_edge
225
+ image = image.resize((W, H))
226
+ if return_pil:
227
+ return image
228
+ else:
229
+ buffered = BytesIO()
230
+ image.save(buffered, format=image_format)
231
+ img_b64_str = base64.b64encode(buffered.getvalue()).decode()
232
+ return img_b64_str
233
+
234
+ def get_images(self, return_pil=False, return_path=False):
235
+ images = []
236
+ for i, (role, msg) in enumerate(self.messages[self.offset :]):
237
+ if i % 2 == 0:
238
+ if type(msg) is tuple:
239
+ msg, image, image_process_mode = msg
240
+ if type(image) != list:
241
+ image = [image]
242
+ for img in image:
243
+ if not return_path and self.is_image_file(img):
244
+ img = self.process_image(img, image_process_mode, return_pil=return_pil)
245
+ else:
246
+ images.append(img)
247
+ return images
248
+
249
+ def is_image_file(self, filename):
250
+ image_extensions = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".webp"]
251
+ return any(filename.lower().endswith(ext) for ext in image_extensions)
252
+
253
+ def is_video_file(self, filename):
254
+ video_extensions = [".mp4", ".mov", ".avi", ".mkv", ".wmv", ".flv", ".mpeg", ".mpg"]
255
+ return any(filename.lower().endswith(ext) for ext in video_extensions)
256
+
257
+ def to_gradio_chatbot(self):
258
+ ret = []
259
+ for i, (role, msg) in enumerate(self.messages[self.offset :]):
260
+ if i % 2 == 0:
261
+ if type(msg) is tuple:
262
+ msg, image, image_process_mode = msg
263
+ if type(image) != list:
264
+ image = [image]
265
+ if len(image) == 1:
266
+ msg = "<image>\n" + msg.replace("<image>", "").strip()
267
+ else:
268
+ msg = re.sub(r"(<image>)\n(?=<image>)", r"\1 ", msg)
269
+
270
+ img_str_list = []
271
+ for img in image:
272
+ if self.is_image_file(img):
273
+ img_b64_str = self.process_image(img, "Default", return_pil=False, image_format="JPEG")
274
+ img_str = f'<img src="data:image/jpeg;base64,{img_b64_str}" style="max-width: 256px; max-height: 256px; width: auto; height: auto; object-fit: contain;"/>'
275
+ img_str_list.append(img_str)
276
+ elif self.is_video_file(img):
277
+ ret.append(((img,), None))
278
+
279
+ msg = msg.strip()
280
+ img_place_holder = ""
281
+ for img_str in img_str_list:
282
+ img_place_holder += f"{img_str}\n\n"
283
+
284
+ if len(img_str_list) > 0:
285
+ msg = f"{img_place_holder}\n\n{msg}"
286
+
287
+ if len(msg) > 0:
288
+ ret.append([msg, None])
289
+ else:
290
+ ret.append([msg, None])
291
+ else:
292
+ ret[-1][-1] = msg
293
+ return ret
294
+
295
+ def copy(self):
296
+ return Conversation(system=self.system, roles=self.roles, messages=[[x, y] for x, y in self.messages], offset=self.offset, sep_style=self.sep_style, sep=self.sep, sep2=self.sep2, version=self.version)
297
+
298
+ def dict(self):
299
+ if len(self.get_images()) > 0:
300
+ return {
301
+ "system": self.system,
302
+ "roles": self.roles,
303
+ "messages": [[x, y[0] if type(y) is tuple else y] for x, y in self.messages],
304
+ "offset": self.offset,
305
+ "sep": self.sep,
306
+ "sep2": self.sep2,
307
+ }
308
+ return {
309
+ "system": self.system,
310
+ "roles": self.roles,
311
+ "messages": self.messages,
312
+ "offset": self.offset,
313
+ "sep": self.sep,
314
+ "sep2": self.sep2,
315
+ }
316
+
317
+
318
+ conv_vicuna_v0 = Conversation(
319
+ system="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.",
320
+ roles=("Human", "Assistant"),
321
+ messages=[
322
+ ["Human", "What are the key differences between renewable and non-renewable energy sources?"],
323
+ [
324
+ "Assistant",
325
+ "Renewable energy sources are those that can be replenished naturally in a relatively "
326
+ "short amount of time, such as solar, wind, hydro, geothermal, and biomass. "
327
+ "Non-renewable energy sources, on the other hand, are finite and will eventually be "
328
+ "depleted, such as coal, oil, and natural gas. Here are some key differences between "
329
+ "renewable and non-renewable energy sources:\n"
330
+ "1. Availability: Renewable energy sources are virtually inexhaustible, while non-renewable "
331
+ "energy sources are finite and will eventually run out.\n"
332
+ "2. Environmental impact: Renewable energy sources have a much lower environmental impact "
333
+ "than non-renewable sources, which can lead to air and water pollution, greenhouse gas emissions, "
334
+ "and other negative effects.\n"
335
+ "3. Cost: Renewable energy sources can be more expensive to initially set up, but they typically "
336
+ "have lower operational costs than non-renewable sources.\n"
337
+ "4. Reliability: Renewable energy sources are often more reliable and can be used in more remote "
338
+ "locations than non-renewable sources.\n"
339
+ "5. Flexibility: Renewable energy sources are often more flexible and can be adapted to different "
340
+ "situations and needs, while non-renewable sources are more rigid and inflexible.\n"
341
+ "6. Sustainability: Renewable energy sources are more sustainable over the long term, while "
342
+ "non-renewable sources are not, and their depletion can lead to economic and social instability.\n",
343
+ ],
344
+ ],
345
+ offset=2,
346
+ sep_style=SeparatorStyle.SINGLE,
347
+ sep="###",
348
+ )
349
+
350
+ conv_vicuna_v1 = Conversation(
351
+ system="A chat between a curious user and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the user's questions.",
352
+ roles=("USER", "ASSISTANT"),
353
+ version="v1",
354
+ messages=[],
355
+ offset=0,
356
+ sep_style=SeparatorStyle.TWO,
357
+ sep=" ",
358
+ sep2="</s>",
359
+ )
360
+
361
+ conv_llama_2 = Conversation(
362
+ system="""You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
363
+
364
+ If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.""",
365
+ roles=("USER", "ASSISTANT"),
366
+ version="llama_v2",
367
+ messages=[],
368
+ offset=0,
369
+ sep_style=SeparatorStyle.LLAMA_2,
370
+ sep="<s>",
371
+ sep2="</s>",
372
+ )
373
+
374
+ conv_llava_llama_2 = Conversation(
375
+ system="You are a helpful language and vision assistant. " "You are able to understand the visual content that the user provides, " "and assist the user with a variety of tasks using natural language.",
376
+ roles=("USER", "ASSISTANT"),
377
+ version="llama_v2",
378
+ messages=[],
379
+ offset=0,
380
+ sep_style=SeparatorStyle.LLAMA_2,
381
+ sep="<s>",
382
+ sep2="</s>",
383
+ )
384
+
385
+ def safe_load_tokenizer(tokenizer_id):
386
+ try:
387
+ return AutoTokenizer.from_pretrained(tokenizer_id)
388
+ except Exception:
389
+ return None
390
+
391
+ conv_llava_llama_3 = Conversation(
392
+ system="You are a helpful language and vision assistant. " "You are able to understand the visual content that the user provides, " "and assist the user with a variety of tasks using natural language. You are a world-class AI system, capable of complex reasoning and reflection. Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags. If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags.",
393
+ roles=("user", "assistant"),
394
+ version="llama_3",
395
+ messages=[],
396
+ offset=0,
397
+ sep="<|eot_id|>",
398
+ sep_style=SeparatorStyle.LLAMA_3,
399
+ tokenizer_id="mylesgoose/Meta-Llama-3.1-8B-Instruct-goose-abliterated-reflection",
400
+ tokenizer=safe_load_tokenizer("mylesgoose/Meta-Llama-3.1-8B-Instruct-goose-abliterated-reflection"),
401
+ stop_token_ids=[128009]
402
+ )
403
+
404
+ conv_llava_llama_3_1 = Conversation(
405
+ system="You are a helpful language and vision AI." "You are able to understand the visual content that the user provides, and you may assist the user with a variety of tasks, using natural language. You are a world-class AI system, capable of complex reasoning and reflection. Reason through the query inside <thinking> tags, and then provide your final response inside <output> tags. If you detect that you made a mistake in your reasoning at any point, correct yourself inside <reflection> tags.",
406
+ roles=("user", "assistant"),
407
+ version="llama_3_1",
408
+ messages=[],
409
+ offset=0,
410
+ sep="<|eot_id|>",
411
+ sep_style=SeparatorStyle.LLAMA_3_1,
412
+ tokenizer_id="nvidia/Llama-3.1-Minitron-4B-Width-Base",
413
+ tokenizer=safe_load_tokenizer("nvidia/Llama-3.1-Minitron-4B-Width-Base"),
414
+ stop_token_ids=[128009, 128008, 128001],
415
+ date_string=date.today().strftime("%d %b %Y")
416
+ )
417
+
418
+ conv_mistral_instruct = Conversation(
419
+ system="",
420
+ roles=("USER", "ASSISTANT"),
421
+ version="llama_v2",
422
+ messages=[],
423
+ offset=0,
424
+ sep_style=SeparatorStyle.LLAMA_2,
425
+ sep="",
426
+ sep2="</s>",
427
+ )
428
+
429
+ conv_llava_llama_2_simple = Conversation(
430
+ system="Answer the questions about the visual content that the user provides.",
431
+ roles=("USER", "ASSISTANT"),
432
+ version="llama_v2",
433
+ messages=[],
434
+ offset=0,
435
+ sep_style=SeparatorStyle.LLAMA_2,
436
+ sep="<s>",
437
+ sep2="</s>",
438
+ )
439
+
440
+ conv_llava_llama_2_mmtag = Conversation(
441
+ system="Answer the questions about the visual content that the user provides." "The visual content will be provided with the following format: <Image>visual content</Image>.",
442
+ roles=("USER", "ASSISTANT"),
443
+ version="llama_v2_mmtag",
444
+ messages=[],
445
+ offset=0,
446
+ sep_style=SeparatorStyle.LLAMA_2,
447
+ sep="<s>",
448
+ sep2="</s>",
449
+ )
450
+
451
+ conv_mpt = Conversation(
452
+ system="""<|im_start|>system
453
+ A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.""",
454
+ roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
455
+ version="mpt",
456
+ messages=[],
457
+ offset=0,
458
+ sep_style=SeparatorStyle.MPT,
459
+ sep="<|im_end|>",
460
+ )
461
+
462
+ conv_qwen = Conversation(
463
+ system="""<|im_start|>system
464
+ You are a helpful assistant.""",
465
+ roles=("<|im_start|>user", "<|im_start|>assistant"),
466
+ version="qwen",
467
+ messages=[],
468
+ offset=0,
469
+ sep_style=SeparatorStyle.CHATML,
470
+ sep="<|im_end|>",
471
+ )
472
+
473
+ conv_gemma_instruct = Conversation(system="", roles=("<start_of_turn>user\n", "<start_of_turn>model\n"), version="gemma", messages=[], offset=0, sep_style=SeparatorStyle.GEMMA, sep="<end_of_turn>\n")
474
+
475
+ conv_llava_plain = Conversation(
476
+ system="",
477
+ roles=("", ""),
478
+ messages=[],
479
+ offset=0,
480
+ sep_style=SeparatorStyle.PLAIN,
481
+ sep="\n",
482
+ )
483
+
484
+ conv_llava_v0 = Conversation(
485
+ system="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.",
486
+ roles=("Human", "Assistant"),
487
+ messages=[],
488
+ offset=0,
489
+ sep_style=SeparatorStyle.SINGLE,
490
+ sep="###",
491
+ )
492
+
493
+ conv_llava_v0_mmtag = Conversation(
494
+ system="A chat between a curious user and an artificial intelligence assistant. "
495
+ "The assistant is able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language."
496
+ "The visual content will be provided with the following format: <Image>visual content</Image>.",
497
+ roles=("Human", "Assistant"),
498
+ messages=[],
499
+ offset=0,
500
+ sep_style=SeparatorStyle.SINGLE,
501
+ sep="###",
502
+ version="v0_mmtag",
503
+ )
504
+
505
+ conv_llava_v1 = Conversation(
506
+ system="A chat between a curious human and an artificial intelligence assistant. " "The assistant gives helpful, detailed, and polite answers to the human's questions.",
507
+ roles=("USER", "ASSISTANT"),
508
+ version="v1",
509
+ messages=[],
510
+ offset=0,
511
+ sep_style=SeparatorStyle.TWO,
512
+ sep=" ",
513
+ sep2="</s>",
514
+ )
515
+
516
+ conv_llava_v1_mmtag = Conversation(
517
+ system="A chat between a curious user and an artificial intelligence assistant. "
518
+ "The assistant is able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language."
519
+ "The visual content will be provided with the following format: <Image>visual content</Image>.",
520
+ roles=("USER", "ASSISTANT"),
521
+ messages=[],
522
+ offset=0,
523
+ sep_style=SeparatorStyle.TWO,
524
+ sep=" ",
525
+ sep2="</s>",
526
+ version="v1_mmtag",
527
+ )
528
+
529
+ conv_mistral_orca = Conversation(
530
+ system="""<|im_start|>system
531
+ You are MistralOrca, a large language model trained by Alignment Lab AI. Write out your reasoning step-by-step to be sure you get the right answers!""",
532
+ roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
533
+ version="mpt",
534
+ messages=[],
535
+ offset=0,
536
+ sep_style=SeparatorStyle.MPT,
537
+ sep="<|im_end|>",
538
+ )
539
+
540
+ conv_mistral_zephyr = Conversation(
541
+ system="""<|system|>
542
+ You are a helpful AI assistant.""",
543
+ roles=("<|user|>\n", "<|assistant|>\n"),
544
+ version="mpt",
545
+ messages=[],
546
+ offset=0,
547
+ sep_style=SeparatorStyle.MPT,
548
+ sep="</s>",
549
+ )
550
+
551
+ conv_mistral_direct = Conversation(
552
+ system="""<|im_start|>system
553
+ Answer the questions.""",
554
+ roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
555
+ version="mpt",
556
+ messages=[],
557
+ offset=0,
558
+ sep_style=SeparatorStyle.MPT,
559
+ sep="<|im_end|>",
560
+ )
561
+
562
+ conv_chatml_direct = Conversation(
563
+ system="""<|im_start|>system
564
+ Answer the questions.""",
565
+ roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
566
+ version="mpt",
567
+ messages=[],
568
+ offset=0,
569
+ sep_style=SeparatorStyle.MPT,
570
+ sep="<|im_end|>",
571
+ )
572
+
573
+ default_conversation = conv_vicuna_v0
574
+ conv_templates = {
575
+ "default": conv_vicuna_v0,
576
+ "v0": conv_vicuna_v0,
577
+ "v1": conv_vicuna_v1,
578
+ "vicuna_v1": conv_vicuna_v1,
579
+ "llama_2": conv_llama_2,
580
+ "mistral_instruct": conv_mistral_instruct,
581
+ "mistral_orca": conv_mistral_orca,
582
+ "mistral_zephyr": conv_mistral_zephyr,
583
+ "mistral_direct": conv_mistral_direct,
584
+ "plain": conv_llava_plain,
585
+ "v0_plain": conv_llava_plain,
586
+ "chatml_direct": conv_chatml_direct,
587
+ "llava_v0": conv_llava_v0,
588
+ "llava_v0_mmtag": conv_llava_v0_mmtag,
589
+ "llava_v1": conv_llava_v1,
590
+ "llava_v1_mmtag": conv_llava_v1_mmtag,
591
+ "llava_llama_2": conv_llava_llama_2,
592
+ "llava_llama_3": conv_llava_llama_3,
593
+ "llava_llama_3_1": conv_llava_llama_3_1,
594
+ "llava_llama_2_simple": conv_llava_llama_2_simple,
595
+ "llava_llama_2_mmtag": conv_llava_llama_2_mmtag,
596
+ "llava_mistral_instruct": conv_mistral_instruct,
597
+ "mpt": conv_mpt,
598
+ "qwen_1_5": conv_qwen,
599
+ "qwen_2": conv_qwen,
600
+ "gemma_instruct": conv_gemma_instruct,
601
+ }
602
+
603
+
604
+ if __name__ == "__main__":
605
+ print(default_conversation.get_prompt())