A class to represent a turn in a conversation.
| 228 | |
| 229 | @dataclass |
| 230 | class Turn: |
| 231 | """ |
| 232 | A class to represent a turn in a conversation. |
| 233 | """ |
| 234 | |
| 235 | id: int |
| 236 | prompt: str |
| 237 | response: str |
| 238 | message: list |
| 239 | images: list[str] = None |
| 240 | input_tokens: int = 0 |
| 241 | output_tokens: int = 0 |
| 242 | embedding: Tensor = None |
| 243 | |
| 244 | def to_dict(self): |
| 245 | return {k: v for k, v in asdict(self).items() if k != "embedding"} |
| 246 | |
| 247 | def calc_token(self): |
| 248 | """ |
| 249 | Calculate the number of tokens for the turn. |
| 250 | """ |
| 251 | if self.images is not None: |
| 252 | self.input_tokens += calc_image_tokens(self.images) |
| 253 | self.input_tokens += len(ENCODING.encode(self.prompt)) |
| 254 | self.output_tokens = len(ENCODING.encode(self.response)) |
| 255 | |
| 256 | def __eq__(self, other): |
| 257 | return self is other |
| 258 | |
| 259 | |
| 260 | class Role: |