Views
No views yet
SYSTEM: <ANY SYSTEM CONTEXT>
USER:
ASSISTANT: 
1import torch, json
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_path = "migtissera/Tess-2.0-Yi-34B-200K"
5output_file_path = "./conversations.jsonl"
6
7model = AutoModelForCausalLM.from_pretrained(
8 model_path,
9 torch_dtype=torch.float16,
10 device_map="auto",
11 load_in_8bit=False,
12 trust_remote_code=True,
13)
14
15tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
16
17
18def generate_text(instruction):
19 tokens = tokenizer.encode(instruction)
20 tokens = torch.LongTensor(tokens).unsqueeze(0)
21 tokens = tokens.to("cuda")
22
23 instance = {
24 "input_ids": tokens,
25 "top_p": 1.0,
26 "temperature": 0.5,
27 "generate_len": 1024,
28 "top_k": 50,
29 }
30
31 length = len(tokens[0])
32 with torch.no_grad():
33 rest = model.generate(
34 input_ids=tokens,
35 max_length=length + instance["generate_len"],
36 use_cache=True,
37 do_sample=True,
38 top_p=instance["top_p"],
39 temperature=instance["temperature"],
40 top_k=instance["top_k"],
41 num_return_sequences=1,
42 )
43 output = rest[0][length:]
44 string = tokenizer.decode(output, skip_special_tokens=True)
45 answer = string.split("USER:")[0].strip()
46 return f"{answer}"
47
48
49conversation = f"SYSTEM: Answer the question thoughtfully and intelligently. Always answer without hesitation."
50
51
52while True:
53 user_input = input("You: ")
54 llm_prompt = f"{conversation} \nUSER: {user_input} \nASSISTANT: "
55 answer = generate_text(llm_prompt)
56 print(answer)
57 conversation = f"{llm_prompt}{answer}"
58 json_data = {"prompt": user_input, "answer": answer}
59
60 ## Save your conversation
61 with open(output_file_path, "a") as output_file:
62 output_file.write(json.dumps(json_data) + "\n")
63