MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / chat

Function chat

SwissArmyTransformer/examples/chatglm2/chat_sat.py:8–32  ·  view source on GitHub ↗
(model, tokenizer, 
        max_length: int = 256, top_p=0.7, top_k=0, temperature=0.95)

Source from the content-addressed store, hash-verified

6from sat.generation.sampling_strategies import BaseStrategy
7
8def chat(model, tokenizer,
9 max_length: int = 256, top_p=0.7, top_k=0, temperature=0.95):
10 prompt = "[Round 0]\n\n问:你好\n\n答:"
11 inputs = tokenizer([prompt], return_tensors="pt").to(model.parameters().__next__().device)['input_ids'][0]
12 seq = torch.cat(
13 [inputs, torch.tensor([-1]*(max_length-len(inputs)), device=inputs.device)], dim=0
14 )
15 strategy = BaseStrategy(temperature=temperature, top_p=top_p, top_k=top_k, end_tokens=[tokenizer.eos_token_id])
16
17 filling_stream = stream_filling_sequence(
18 model, seq,
19 batch_size=1,
20 strategy=strategy
21 )
22 offset = 0
23 for tokens, mems in filling_stream:
24 tmp_response = tokenizer.decode(tokens[0])
25 if tmp_response[-1] != "�":
26 print(tmp_response[offset:], end='')
27 offset = len(tmp_response)
28 print()
29 output = strategy.finalize(tokens, mems)[0]
30
31 response = tokenizer.decode(output[0])
32 print("final:", response)
33
34if __name__ == "__main__":
35 import argparse

Callers 1

chat_sat.pyFile · 0.70

Calls 7

finalizeMethod · 0.95
BaseStrategyClass · 0.90
stream_filling_sequenceFunction · 0.90
toMethod · 0.80
parametersMethod · 0.80
printFunction · 0.50
decodeMethod · 0.45

Tested by

no test coverage detected