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

Function chat

SwissArmyTransformer/examples/llama/split_model.py:10–42  ·  view source on GitHub ↗

from https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/app.py system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, d

(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

8import os
9
10def chat(model, tokenizer,
11 max_length: int = 256, num_beams=1, top_p=0.7, top_k=0, temperature=0.95):
12 """from https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/app.py
13 system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
14 input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
15 for interaction in chatbot:
16 input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "
17
18 input_prompt = input_prompt + str(message) + " [/INST] "
19 """
20 prompt = "[INST] <<SYS>>\n\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\n<</SYS>>\n\n What is the capital of China? [/INST] "
21 inputs = tokenizer([prompt], return_tensors="pt", add_special_tokens=False).to(model.parameters().__next__().device)['input_ids'][0]
22 seq = torch.cat(
23 [inputs, torch.tensor([-1]*(max_length-len(inputs)), device=inputs.device)], dim=0
24 )
25 strategy = BaseStrategy(temperature=temperature, top_p=top_p, top_k=0, end_tokens=[tokenizer.eos_token_id])
26 strategy = BeamSearchStrategy(temperature=temperature, top_p=top_p, top_k=0, end_tokens=[tokenizer.eos_token_id], num_beams=num_beams, consider_end=True)
27
28 output = filling_sequence(
29 model, seq,
30 batch_size=1,
31 strategy=strategy
32 )[0] # drop memory
33
34 # ---------------
35 # port from inference_glm.py, more general than chat mode
36 # clip -1s and fill back generated things into seq
37 output_list = list(output)
38
39 response = tokenizer.decode(output_list[0])
40 if get_model_parallel_rank() == 0:
41 with open(f"{torch.distributed.get_rank()}.txt", 'w') as f:
42 f.write(response)
43
44if __name__ == "__main__":
45 import argparse

Callers 1

split_model.pyFile · 0.70

Calls 7

BaseStrategyClass · 0.90
BeamSearchStrategyClass · 0.90
filling_sequenceFunction · 0.90
get_model_parallel_rankFunction · 0.90
toMethod · 0.80
parametersMethod · 0.80
decodeMethod · 0.45

Tested by

no test coverage detected