jadechoghari commited on
Commit
0fb0588
1 Parent(s): d866300

Update conversation.py

Browse files
Files changed (1) hide show
  1. conversation.py +267 -16
conversation.py CHANGED
@@ -1,10 +1,14 @@
1
  import dataclasses
2
  from enum import auto, Enum
3
  from typing import List, Tuple
 
 
 
4
 
5
  VOCAB_IMAGE_W = 1000 # 224
6
  VOCAB_IMAGE_H = 1000 # 224
7
 
 
8
  class SeparatorStyle(Enum):
9
  """Different separator style."""
10
  SINGLE = auto()
@@ -12,6 +16,7 @@ class SeparatorStyle(Enum):
12
  MPT = auto()
13
  PLAIN = auto()
14
  LLAMA_2 = auto()
 
15
 
16
 
17
  @dataclasses.dataclass
@@ -27,8 +32,6 @@ class Conversation:
27
  version: str = "Unknown"
28
 
29
  skip_next: bool = False
30
- first_round: bool = True
31
-
32
 
33
  def get_prompt(self):
34
  messages = self.messages
@@ -72,7 +75,7 @@ class Conversation:
72
  else:
73
  ret += role
74
  elif self.sep_style == SeparatorStyle.LLAMA_2:
75
- wrap_sys = lambda msg: f"<<SYS>>\n{msg}\n<</SYS>>\n\n"
76
  wrap_inst = lambda msg: f"[INST] {msg} [/INST]"
77
  ret = ""
78
 
@@ -92,6 +95,16 @@ class Conversation:
92
  else:
93
  ret += ""
94
  ret = ret.lstrip(self.sep)
 
 
 
 
 
 
 
 
 
 
95
  elif self.sep_style == SeparatorStyle.PLAIN:
96
  seps = [self.sep, self.sep2]
97
  ret = self.system
@@ -133,7 +146,7 @@ class Conversation:
133
  result.paste(pil_img, ((height - width) // 2, 0))
134
  return result
135
  image = expand2square(image)
136
- elif image_process_mode == "Crop":
137
  pass
138
  elif image_process_mode == "Raw+Processor":
139
  pass
@@ -230,15 +243,123 @@ class Conversation:
230
  }
231
 
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
- ferret_conv_vicuna_v1_original_system = Conversation(
235
  system="A chat between a curious human and an artificial intelligence assistant. "
236
- "Assistant is able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language. "
237
- "In images, points are represented by coordinates [x, y]. The top-left corner is [0, 0]. The bottom-right corner is [width-1, height-1]. "
238
- "Increasing x moves right across the image while increasing y moves down. "
239
- "A bounding box is marked by [x1, y1, x2, y2] with the top-left and bottom-right points being [x1, y1] and [x2, y2] respectively. "
240
- f"The image size is assumed to be ({VOCAB_IMAGE_W}, {VOCAB_IMAGE_H}), i.e., width={VOCAB_IMAGE_W}, height={VOCAB_IMAGE_H}. "
241
- "Follow the instructions carefully. ",
242
  roles=("USER", "ASSISTANT"),
243
  version="v1",
244
  messages=(),
@@ -248,12 +369,68 @@ ferret_conv_vicuna_v1_original_system = Conversation(
248
  sep2="</s>",
249
  )
250
 
251
- ferret_conv_vicuna_v1 = Conversation(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  system="A chat between a human and an AI that understands visuals. "
253
  "In images, [x, y] denotes points: top-left [0, 0], bottom-right [width-1, height-1]. "
254
  "Increasing x moves right; y moves down. "
255
  f"Bounding box: [x1, y1, x2, y2]. Image size: {VOCAB_IMAGE_W}x{VOCAB_IMAGE_H}. "
256
- "Follow instructions. ",
257
  roles=("USER", "ASSISTANT"),
258
  version="v1",
259
  messages=(),
@@ -263,11 +440,85 @@ ferret_conv_vicuna_v1 = Conversation(
263
  sep2="</s>",
264
  )
265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
 
267
- default_conversation = ferret_conv_vicuna_v1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  conv_templates = {
269
- "v1": ferret_conv_vicuna_v1,
270
- "ferret_v1": ferret_conv_vicuna_v1,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  }
272
 
273
 
 
1
  import dataclasses
2
  from enum import auto, Enum
3
  from typing import List, Tuple
4
+ import base64
5
+ from io import BytesIO
6
+ from PIL import Image
7
 
8
  VOCAB_IMAGE_W = 1000 # 224
9
  VOCAB_IMAGE_H = 1000 # 224
10
 
11
+
12
  class SeparatorStyle(Enum):
13
  """Different separator style."""
14
  SINGLE = auto()
 
16
  MPT = auto()
17
  PLAIN = auto()
18
  LLAMA_2 = auto()
19
+ GEMMA = auto()
20
 
21
 
22
  @dataclasses.dataclass
 
32
  version: str = "Unknown"
33
 
34
  skip_next: bool = False
 
 
35
 
36
  def get_prompt(self):
37
  messages = self.messages
 
75
  else:
76
  ret += role
77
  elif self.sep_style == SeparatorStyle.LLAMA_2:
78
+ wrap_sys = lambda msg: f"<<SYS>>\n{msg}\n<</SYS>>\n\n" if len(msg) > 0 else msg
79
  wrap_inst = lambda msg: f"[INST] {msg} [/INST]"
80
  ret = ""
81
 
 
95
  else:
96
  ret += ""
97
  ret = ret.lstrip(self.sep)
98
+ elif self.sep_style == SeparatorStyle.GEMMA:
99
+ seps = [self.sep, self.sep2]
100
+ ret = self.system + seps[0]
101
+ for i, (role, message) in enumerate(messages):
102
+ if message:
103
+ if type(message) is tuple:
104
+ message, _, _ = message
105
+ ret += "<start_of_turn>" + role + "\n" + message + "<end_of_turn>\n" + seps[i % 2]
106
+ else:
107
+ ret += "<start_of_turn>" + role + "\n"
108
  elif self.sep_style == SeparatorStyle.PLAIN:
109
  seps = [self.sep, self.sep2]
110
  ret = self.system
 
146
  result.paste(pil_img, ((height - width) // 2, 0))
147
  return result
148
  image = expand2square(image)
149
+ elif image_process_mode in ["Default", "Crop"]:
150
  pass
151
  elif image_process_mode == "Raw+Processor":
152
  pass
 
243
  }
244
 
245
 
246
+ conv_vicuna_v0 = Conversation(
247
+ system="A chat between a curious human and an artificial intelligence assistant. "
248
+ "The assistant gives helpful, detailed, and polite answers to the human's questions.",
249
+ roles=("Human", "Assistant"),
250
+ messages=(
251
+ ("Human", "What are the key differences between renewable and non-renewable energy sources?"),
252
+ ("Assistant",
253
+ "Renewable energy sources are those that can be replenished naturally in a relatively "
254
+ "short amount of time, such as solar, wind, hydro, geothermal, and biomass. "
255
+ "Non-renewable energy sources, on the other hand, are finite and will eventually be "
256
+ "depleted, such as coal, oil, and natural gas. Here are some key differences between "
257
+ "renewable and non-renewable energy sources:\n"
258
+ "1. Availability: Renewable energy sources are virtually inexhaustible, while non-renewable "
259
+ "energy sources are finite and will eventually run out.\n"
260
+ "2. Environmental impact: Renewable energy sources have a much lower environmental impact "
261
+ "than non-renewable sources, which can lead to air and water pollution, greenhouse gas emissions, "
262
+ "and other negative effects.\n"
263
+ "3. Cost: Renewable energy sources can be more expensive to initially set up, but they typically "
264
+ "have lower operational costs than non-renewable sources.\n"
265
+ "4. Reliability: Renewable energy sources are often more reliable and can be used in more remote "
266
+ "locations than non-renewable sources.\n"
267
+ "5. Flexibility: Renewable energy sources are often more flexible and can be adapted to different "
268
+ "situations and needs, while non-renewable sources are more rigid and inflexible.\n"
269
+ "6. Sustainability: Renewable energy sources are more sustainable over the long term, while "
270
+ "non-renewable sources are not, and their depletion can lead to economic and social instability.\n")
271
+ ),
272
+ offset=2,
273
+ sep_style=SeparatorStyle.SINGLE,
274
+ sep="###",
275
+ )
276
+
277
+ conv_vicuna_v1 = Conversation(
278
+ system="A chat between a curious user and an artificial intelligence assistant. "
279
+ "The assistant gives helpful, detailed, and polite answers to the user's questions.",
280
+ roles=("USER", "ASSISTANT"),
281
+ version="v1",
282
+ messages=(),
283
+ offset=0,
284
+ sep_style=SeparatorStyle.TWO,
285
+ sep=" ",
286
+ sep2="</s>",
287
+ )
288
+
289
+ conv_llama_2 = Conversation(
290
+ 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.
291
+
292
+ 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.""",
293
+ roles=("USER", "ASSISTANT"),
294
+ version="llama_v2",
295
+ messages=(),
296
+ offset=0,
297
+ sep_style=SeparatorStyle.LLAMA_2,
298
+ sep="<s>",
299
+ sep2="</s>",
300
+ )
301
+
302
+ conv_llava_llama_2 = Conversation(
303
+ system="You are a helpful language and vision assistant. "
304
+ "You are able to understand the visual content that the user provides, "
305
+ "and assist the user with a variety of tasks using natural language.",
306
+ roles=("USER", "ASSISTANT"),
307
+ version="llama_v2",
308
+ messages=(),
309
+ offset=0,
310
+ sep_style=SeparatorStyle.LLAMA_2,
311
+ sep="<s>",
312
+ sep2="</s>",
313
+ )
314
+
315
+ conv_mpt = Conversation(
316
+ system="""<|im_start|>system
317
+ A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.""",
318
+ roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
319
+ version="mpt",
320
+ messages=(),
321
+ offset=0,
322
+ sep_style=SeparatorStyle.MPT,
323
+ sep="<|im_end|>",
324
+ )
325
+
326
+ conv_llava_plain = Conversation(
327
+ system="",
328
+ roles=("", ""),
329
+ messages=(
330
+ ),
331
+ offset=0,
332
+ sep_style=SeparatorStyle.PLAIN,
333
+ sep="\n",
334
+ )
335
+
336
+ conv_llava_v0 = Conversation(
337
+ system="A chat between a curious human and an artificial intelligence assistant. "
338
+ "The assistant gives helpful, detailed, and polite answers to the human's questions.",
339
+ roles=("Human", "Assistant"),
340
+ messages=(
341
+ ),
342
+ offset=0,
343
+ sep_style=SeparatorStyle.SINGLE,
344
+ sep="###",
345
+ )
346
+
347
+ conv_llava_v0_mmtag = Conversation(
348
+ system="A chat between a curious user and an artificial intelligence assistant. "
349
+ "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."
350
+ "The visual content will be provided with the following format: <Image>visual content</Image>.",
351
+ roles=("Human", "Assistant"),
352
+ messages=(
353
+ ),
354
+ offset=0,
355
+ sep_style=SeparatorStyle.SINGLE,
356
+ sep="###",
357
+ version="v0_mmtag",
358
+ )
359
 
360
+ conv_llava_v1 = Conversation(
361
  system="A chat between a curious human and an artificial intelligence assistant. "
362
+ "The assistant gives helpful, detailed, and polite answers to the human's questions.",
 
 
 
 
 
363
  roles=("USER", "ASSISTANT"),
364
  version="v1",
365
  messages=(),
 
369
  sep2="</s>",
370
  )
371
 
372
+ conv_llava_v1_mmtag = Conversation(
373
+ system="A chat between a curious user and an artificial intelligence assistant. "
374
+ "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."
375
+ "The visual content will be provided with the following format: <Image>visual content</Image>.",
376
+ roles=("USER", "ASSISTANT"),
377
+ messages=(),
378
+ offset=0,
379
+ sep_style=SeparatorStyle.TWO,
380
+ sep=" ",
381
+ sep2="</s>",
382
+ version="v1_mmtag",
383
+ )
384
+
385
+ conv_mistral_instruct = Conversation(
386
+ system="",
387
+ roles=("USER", "ASSISTANT"),
388
+ version="llama_v2",
389
+ messages=(),
390
+ offset=0,
391
+ sep_style=SeparatorStyle.LLAMA_2,
392
+ sep="",
393
+ sep2="</s>",
394
+ )
395
+
396
+ conv_chatml_direct = Conversation(
397
+ system="""<|im_start|>system
398
+ Answer the questions.""",
399
+ roles=("<|im_start|>user\n", "<|im_start|>assistant\n"),
400
+ version="mpt",
401
+ messages=(),
402
+ offset=0,
403
+ sep_style=SeparatorStyle.MPT,
404
+ sep="<|im_end|>",
405
+ )
406
+
407
+ conv_llava_llama_3 = Conversation(
408
+ system="<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nYou 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.",
409
+ roles=("<|start_header_id|>user<|end_header_id|>\n\n",
410
+ "<|start_header_id|>assistant<|end_header_id|>\n\n"),
411
+ version="llama3",
412
+ messages=[],
413
+ offset=0,
414
+ sep_style=SeparatorStyle.MPT,
415
+ sep="<|eot_id|>",
416
+ )
417
+
418
+ conv_ferret_llama_plain = Conversation(
419
+ system="",
420
+ roles=("", ""),
421
+ messages=(
422
+ ),
423
+ offset=0,
424
+ sep_style=SeparatorStyle.PLAIN,
425
+ sep="\n",
426
+ )
427
+
428
+ conv_ferret_vicuna_v1 = Conversation(
429
  system="A chat between a human and an AI that understands visuals. "
430
  "In images, [x, y] denotes points: top-left [0, 0], bottom-right [width-1, height-1]. "
431
  "Increasing x moves right; y moves down. "
432
  f"Bounding box: [x1, y1, x2, y2]. Image size: {VOCAB_IMAGE_W}x{VOCAB_IMAGE_H}. "
433
+ "Follow instructions.",
434
  roles=("USER", "ASSISTANT"),
435
  version="v1",
436
  messages=(),
 
440
  sep2="</s>",
441
  )
442
 
443
+ conv_ferret_llama_3 = Conversation(
444
+ system="<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\nA chat between a human and an AI that understands visuals. "
445
+ "In images, [x, y] denotes points: top-left [0, 0], bottom-right [width-1, height-1]. "
446
+ "Increasing x moves right; y moves down. "
447
+ f"Bounding box: [x1, y1, x2, y2]. Image size: {VOCAB_IMAGE_W}x{VOCAB_IMAGE_H}. "
448
+ "Follow instructions.",
449
+ roles=("<|start_header_id|>user<|end_header_id|>\n\n",
450
+ "<|start_header_id|>assistant<|end_header_id|>\n\n"),
451
+ version="llama3",
452
+ messages=[],
453
+ offset=0,
454
+ sep_style=SeparatorStyle.MPT,
455
+ sep="<|eot_id|>",
456
+ )
457
+
458
+ conv_ferret_gemma_plain = Conversation(
459
+ system="",
460
+ roles=("user", "model"),
461
+ version="gemma",
462
+ messages=(),
463
+ offset=0,
464
+ sep_style=SeparatorStyle.GEMMA,
465
+ sep="",
466
+ sep2="<eos>",
467
+ )
468
 
469
+ conv_ferret_gemma_instruct = Conversation(
470
+ system="A chat between a human and an AI that understands visuals. "
471
+ "In images, [x, y] denotes points: top-left [0, 0], bottom-right [width-1, height-1]. "
472
+ "Increasing x moves right; y moves down. "
473
+ f"Bounding box: [x1, y1, x2, y2]. Image size: {VOCAB_IMAGE_W}x{VOCAB_IMAGE_H}. "
474
+ "Follow instructions.",
475
+ roles=("user", "model"),
476
+ version="gemma",
477
+ messages=(),
478
+ offset=0,
479
+ sep_style=SeparatorStyle.GEMMA,
480
+ sep="",
481
+ sep2="<eos>",
482
+ )
483
+
484
+ conv_ferret_phi3_instruct = Conversation(
485
+ system="""<|system|>\nYou are a helpful AI assistant.""",
486
+ roles=("\n<|user|>\n", "\n<|assistant|>\n"),
487
+ version="phi3",
488
+ messages=(),
489
+ offset=0,
490
+ sep_style=SeparatorStyle.MPT,
491
+ sep="<|end|>",
492
+ )
493
+
494
+ default_conversation = conv_vicuna_v1
495
  conv_templates = {
496
+ "default": conv_vicuna_v0,
497
+ "v0": conv_vicuna_v0,
498
+ "v1": conv_vicuna_v1,
499
+ "vicuna_v1": conv_vicuna_v1,
500
+ "llama_2": conv_llama_2,
501
+ "mistral_instruct": conv_mistral_instruct,
502
+ "chatml_direct": conv_chatml_direct,
503
+ "mistral_direct": conv_chatml_direct,
504
+
505
+ "plain": conv_llava_plain,
506
+ "v0_plain": conv_llava_plain,
507
+ "llava_v0": conv_llava_v0,
508
+ "v0_mmtag": conv_llava_v0_mmtag,
509
+ "llava_v1": conv_llava_v1,
510
+ "v1_mmtag": conv_llava_v1_mmtag,
511
+ "llava_llama_2": conv_llava_llama_2,
512
+ "llava_llama_3": conv_llava_llama_3,
513
+
514
+ "ferret_llama_plain": conv_ferret_llama_plain,
515
+ "ferret_vicuna_v1": conv_ferret_vicuna_v1,
516
+ "ferret_llama_3": conv_ferret_llama_3,
517
+ "ferret_gemma_plain": conv_ferret_gemma_plain,
518
+ "ferret_gemma_instruct": conv_ferret_gemma_instruct,
519
+ "ferret_phi3_instruct": conv_ferret_phi3_instruct,
520
+
521
+ "mpt": conv_mpt,
522
  }
523
 
524