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: What is the relationship between Earth's atmosphere, magnetic field and gravity?
ASSISTANT:critic and the regenerator. 1import torch, json
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5def load_model(model_path):
6 model = AutoModelForCausalLM.from_pretrained(
7 model_path,
8 torch_dtype=torch.float16,
9 device_map="cuda",
10 load_in_4bit=False,
11 trust_remote_code=True,
12 )
13 return model
14
15def load_tokenizer(model_path):
16 tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
17 return tokenizer
18
19def generate_text(instruction, adapter):
20 # Select our required LoRA adapter
21 adapter_model.set_adapter(adapter)
22
23 tokens = base_tokenizer.encode(instruction)
24 tokens = torch.LongTensor(tokens).unsqueeze(0)
25 tokens = tokens.to("cuda")
26
27 instance = {
28 "input_ids": tokens,
29 "top_p": 1.0,
30 "temperature": 0.75,
31 "generate_len": 1024,
32 "top_k": 50,
33 }
34
35 length = len(tokens[0])
36 with torch.no_grad():
37 rest = adapter_model.generate(
38 input_ids=tokens,
39 max_length=length + instance["generate_len"],
40 use_cache=True,
41 do_sample=True,
42 top_p=instance["top_p"],
43 temperature=instance["temperature"],
44 top_k=instance["top_k"],
45 num_return_sequences=1,
46 pad_token_id=base_tokenizer.eos_token_id,
47 )
48 output = rest[0][length:]
49 string = base_tokenizer.decode(output, skip_special_tokens=True)
50 return f"{string}"
51
52# Load our base Mistral 7B model and tokenizer
53base_model = load_model("mistralai/Mistral-7B-v0.1")
54base_tokenizer = load_tokenizer("mistralai/Mistral-7B-v0.1")
55
56# Load in our three different LoRA adapters for the actor, critic and regenerator
57adapter_model = PeftModel.from_pretrained(base_model, "rhysjones/HelixNet-LMoE-Actor", "actor")
58adapter_model.load_adapter("rhysjones/HelixNet-LMoE-Critic", adapter_name="critic")
59adapter_model.load_adapter("rhysjones/HelixNet-LMoE-Regenerator", adapter_name="regenerator")
60
61system_prompt = "You are HelixNet. 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."
62
63while True:
64 user_input = input("You: ")
65
66 prompt_actor = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nASSISTANT: "
67 actor_response = generate_text(prompt_actor, "actor")
68 print(f"ACTOR: {actor_response}\n\n")
69
70 prompt_critic = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nRESPONSE: {actor_response} \nCRITIQUE:"
71 critic_response = generate_text(prompt_critic, "critic")
72 print(f"CRITIQUE: {critic_response}\n\n")
73
74 prompt_regenerator = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nRESPONSE: {actor_response} \nCRITIQUE: {critic_response} \nREGENERATOR:"
75 regenerator_response = generate_text(prompt_regenerator, "regenerator")
76 print(f"REGENERATION: {regenerator_response}")
77actor, a critic, and a regenerator. The actor LLM produces an initial response to a given system-context and a question. The critic then takes in as input, a tuple of (system-context, question, response) and provides a critique based on the provided answer to the given system-context and the question. Its job is not to criticize, but to provide an intelligent critique so that the answer can be modified/regenerated to address the question better. Finally, the regenerator takes in a tuple of (system-context, question, response, critique) and regenerates the answer.