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

Function chat

SwissArmyTransformer/examples/chatglm3/chat_sat.py:31–52  ·  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

29 return content, history
30
31def chat(model, tokenizer,
32 max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95):
33 query = "你好"
34 history = []
35 inputs = tokenizer.build_chat_input(query, history=history, role="user")['input_ids'].to(model.parameters().__next__().device)[0]
36 seq = torch.cat(
37 [inputs, torch.tensor([-1]*(max_length-len(inputs)), device=inputs.device)], dim=0
38 )
39 strategy = BaseStrategy(temperature=temperature, top_p=top_p, top_k=0, end_tokens=[tokenizer.eos_token_id, tokenizer.get_command("<|user|>"), tokenizer.get_command("<|observation|>")])
40
41 output = filling_sequence(
42 model, seq,
43 batch_size=1,
44 strategy=strategy
45 )[0] # drop memory
46 output_list = output.tolist()[0][len(inputs):-1]
47
48 response = tokenizer.decode(output_list)
49 history.append({"role": "user", "content": query})
50 response, history = process_response(response, history)
51 print(response)
52 print(history)
53
54if __name__ == "__main__":
55 import argparse

Callers 1

chat_sat.pyFile · 0.70

Calls 9

BaseStrategyClass · 0.90
filling_sequenceFunction · 0.90
toMethod · 0.80
parametersMethod · 0.80
appendMethod · 0.80
process_responseFunction · 0.70
printFunction · 0.50
get_commandMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected