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

Function chat

SwissArmyTransformer/examples/llama/chat_sat.py:8–30  ·  view source on GitHub ↗
(model, tokenizer, 
        max_length: int = 256, num_beams=1, 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, BeamSearchStrategy
7
8def chat(model, tokenizer,
9 max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95):
10 prompt = "The capital of China is"
11 inputs = tokenizer([prompt], return_tensors="pt", add_special_tokens=False).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=0, end_tokens=[tokenizer.eos_token_id])
16 strategy = BeamSearchStrategy(temperature=temperature, top_p=top_p, top_k=0, end_tokens=[tokenizer.eos_token_id], num_beams=num_beams, consider_end=True)
17
18 output = filling_sequence(
19 model, seq,
20 batch_size=1,
21 strategy=strategy
22 )[0] # drop memory
23
24 # ---------------
25 # port from inference_glm.py, more general than chat mode
26 # clip -1s and fill back generated things into seq
27 output_list = list(output)
28
29 response = tokenizer.decode(output_list[0])
30 print(response)
31
32if __name__ == "__main__":
33 import argparse

Callers 1

chat_sat.pyFile · 0.70

Calls 7

BaseStrategyClass · 0.90
BeamSearchStrategyClass · 0.90
filling_sequenceFunction · 0.90
toMethod · 0.80
parametersMethod · 0.80
printFunction · 0.50
decodeMethod · 0.45

Tested by

no test coverage detected