MCPcopy
hub / github.com/Tele-AI/Telechat / History

Class History

models/12B_4bit/generation_utils.py:7–64  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5
6
7class History:
8
9 def __init__(self, tokenizer, history):
10 '''
11 init from a list of dict
12 '''
13 # use deque to meet some special situation
14 self.input_history = deque()
15 self.tokenizer = tokenizer
16 if history:
17 self._transfer_from_list(history)
18
19 def _transfer_from_list(self, history):
20 for message in history:
21 content = message.get("content")
22 # the token result may not be equal to the result model gen
23 message.update(self.tokenizer(content))
24 self.input_history.append(message)
25
26 def append(self, message):
27 content = message.get("content")
28 if "input_ids" not in message or "attention_mask" not in message:
29 message.update(self.tokenizer(content))
30 self.input_history.append(message)
31
32 def append_left(self, message):
33 content = message.get("content")
34 if "input_ids" not in message or "attention_mask" not in message:
35 message.update(self.tokenizer(content))
36 self.input_history.appendleft(message)
37
38 def pop(self):
39 x = self.input_history.pop()
40 return x
41
42 def pop_left(self):
43 x = self.pop_left()
44 return x
45
46 def update(self, message):
47 self.input_history.pop()
48 self.append(message)
49
50 def __len__(self):
51 return self.input_history.__len__()
52
53 def __str__(self):
54 return self.input_history.__str__()
55
56 def __copy__(self):
57 new_instance = type(self)(self.tokenizer, [])
58 new_instance.input_history = copy.copy(self.input_history)
59 return new_instance
60
61 def __deepcopy__(self, memodict={}):
62 new_instance = type(self)(self.tokenizer, [])
63 new_instance.input_history = copy.deepcopy(self.input_history)
64 return new_instance

Callers 1

chatMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected