|
| 1 | +from dataclasses import dataclass |
| 2 | +from enum import Enum |
| 3 | +from typing import List, Optional |
| 4 | + |
| 5 | +from prompts.templates import template |
| 6 | + |
| 7 | + |
| 8 | +class Role(Enum): |
| 9 | + system = "system" |
| 10 | + user = "user" |
| 11 | + assistant = "assistant" |
| 12 | + |
| 13 | + |
| 14 | +@dataclass |
| 15 | +class Message: |
| 16 | + role: Role |
| 17 | + content: str |
| 18 | + |
| 19 | + |
| 20 | +class Chat: |
| 21 | + history: List[Message] |
| 22 | + |
| 23 | + def __init__(self, model_name: str, system_msg: Optional[str] = None): |
| 24 | + from transformers import AutoTokenizer |
| 25 | + |
| 26 | + # This is annoying, we need to handle those ourselves. |
| 27 | + self.tokenizer = AutoTokenizer.from_pretrained(model_name) |
| 28 | + self.history = [] |
| 29 | + if system_msg is not None: |
| 30 | + self.history.append(Message(Role.system, system_msg)) |
| 31 | + |
| 32 | + def __str__(self): |
| 33 | + """Render the prompt that corresponds to the chat history in the format |
| 34 | + that `model_name` expects. |
| 35 | +
|
| 36 | + In order to be compatible with any library we choose to append the |
| 37 | + token that corresponds to the beginning of the assistant's response |
| 38 | + when the last message is from a `user`. |
| 39 | +
|
| 40 | + How is not adding this token useful anyway? |
| 41 | +
|
| 42 | + This needs to be properly documented. |
| 43 | +
|
| 44 | + I think correctness, i.e. alternation between user and assistant, should |
| 45 | + be checked after filtering the history. |
| 46 | +
|
| 47 | + """ |
| 48 | + history = self.filter() |
| 49 | + if not self._is_history_valid(history): |
| 50 | + raise ValueError("History not valid") |
| 51 | + |
| 52 | + prompt = chat_template[self.model_name](history) |
| 53 | + |
| 54 | + # translate this to format expected by huggingface |
| 55 | + # use tokenizer.apply_chat_template(chat, tokenizer=False) |
| 56 | + |
| 57 | + return prompt |
| 58 | + |
| 59 | + def filter(self): |
| 60 | + """Filter the messages before building the prompt. |
| 61 | +
|
| 62 | + The `Chat` class should be subclassed by users who want to filter |
| 63 | + messages before building the prompt, and override this method. This |
| 64 | + can for instance use a RAG step. |
| 65 | +
|
| 66 | + (Document) |
| 67 | +
|
| 68 | + """ |
| 69 | + return self.history |
| 70 | + |
| 71 | + def __getitem__(self, index: int): |
| 72 | + return self.history[index] |
| 73 | + |
| 74 | + def __getattribute__(self, role: str): |
| 75 | + """Returns all messages for the role `role`""" |
| 76 | + return [message for message in self.history if message.role == role] |
| 77 | + |
| 78 | + def user(self, msg: str): |
| 79 | + """Add a new user message.""" |
| 80 | + self.history.append(Message(Role.user, msg)) |
| 81 | + |
| 82 | + def assistant(self, msg: str): |
| 83 | + """Add a new assistant message.""" |
| 84 | + |
| 85 | + self.history.append(Message(Role.assistant, msg)) |
| 86 | + |
| 87 | + |
| 88 | +@template |
| 89 | +def chat_template(messages): |
| 90 | + """ |
| 91 | + {% for message in messages %} |
| 92 | + {%- if loop.index == 0 %} |
| 93 | + {%- if message.role == 'system' %} |
| 94 | + {{- message.content + bos }}\n |
| 95 | + {%- else %} |
| 96 | + {{- bos + user.begin + message.content + user.end }} |
| 97 | + {%- endif %} |
| 98 | + {%- else %} |
| 99 | + {%- if message.role == 'user' %} |
| 100 | + \n{{- user.begin + message.content + user.end }} |
| 101 | + {%- else %} |
| 102 | + \n{{- assistant.begin + message.content + assistant.end }} |
| 103 | + {%- endif %} |
| 104 | + {%- endif %} |
| 105 | + {% endfor %} |
| 106 | + {%- if messages[-1].role == 'user'} |
| 107 | + \n{{ assistant.begin }} |
| 108 | + {% endif %}""" |
0 commit comments