Views
No views yet
SYSTEM: Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation.
USER: How is insulin synthesized?
ASSISTANT:1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch, json
3# model path
4model_path = "NurtureAI/SynthIA-7B-v2.0-16k"
5output_file_path = "./SynthIA-7B-v2.0-conversations.jsonl"
6device_map = {"": "cuda"}
7model = AutoModelForCausalLM.from_pretrained(
8 model_path,
9 torch_dtype=torch.float16,
10 device_map=device_map,
11 load_in_8bit=False,
12 trust_remote_code=True,
13)
14# tokenizer
15tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
16
17def generate_text(instruction):
18 tokens = tokenizer.encode(instruction)
19 tokens = torch.LongTensor(tokens).unsqueeze(0)
20 tokens = tokens.to("cuda")
21 instance = {
22 "input_ids": tokens,
23 "top_p": 1.0,
24 "temperature": 0.75,
25 "generate_len": 1024,
26 "top_k": 50,
27 }
28 length = len(tokens[0])
29 with torch.no_grad():
30 rest = model.generate(
31 input_ids=tokens,
32 max_length=length + instance["generate_len"],
33 use_cache=True,
34 do_sample=True,
35 top_p=instance["top_p"],
36 temperature=instance["temperature"],
37 top_k=instance["top_k"],
38 num_return_sequences=1,
39 )
40 output = rest[0][length:]
41 string = tokenizer.decode(output, skip_special_tokens=True)
42 answer = string.split("USER:")[0].strip()
43 return f"{answer}"
44
45
46conversation = f"SYSTEM: Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation."
47
48
49while True:
50 user_input = input("You: ")
51 llm_prompt = f"{conversation} \nUSER: {user_input} \nASSISTANT: "
52 answer = generate_text(llm_prompt)
53 print(answer)
54 conversation = f"{llm_prompt}{answer}"
55 json_data = {"prompt": user_input, "answer": answer}
56
57 ## Save your conversation
58 with open(output_file_path, "a") as output_file:
59 output_file.write(json.dumps(json_data) + "\n")
60