Views
No views yet
<GPT4> token (i.e., token_id=32000).
1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model = AutoModelForCausalLM.from_pretrained("leeroo/LeerooDedicated-Math-7b", trust_remote_code=True)
4tokenizer = AutoTokenizer.from_pretrained("leeroo/LeerooDedicated-Math-7b")
5device = model.device
6
7# the following question is answered by the leeroo expert
8question = "Natalia sold clips to 48 of her friends in April,and then she sold half as many clips in May.How many clips did Natalia sell altogether in April and May?"
9encodeds = tokenizer([question], return_tensors="pt")
10model_inputs = encodeds['input_ids'].to(device)
11generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=False)
12decoded = tokenizer.batch_decode(generated_ids)
13print(decoded[0])
14# Natalia sold 48 clips in April.\nIn May, she sold half as many clips as in April,
15# so she sold 48/2 = 24 clips.\nAltogether, Natalia sold 48 + 24 = 72 clips in April and May.\n#### 72\nThe answer is: 72</s>
16
17# sends the following question to GPT4
18question = "James loves to go swimming and has to swim across a 20-mile lake. He can swim at a pace of 2 miles per hour. He swims 60% of the distance. After that, he stops on an island and rests for half as long as the swimming time. He then finishes the remaining distance while going half the speed. How long did it take him to get across the lake?"
19encodeds = tokenizer([question], return_tensors="pt")
20model_inputs = encodeds['input_ids'].to(device)
21generated_ids = model.generate(model_inputs, max_new_tokens=100, do_sample=False)
22decoded = tokenizer.batch_decode(generated_ids)
23print(decoded[0])
24# <GPT4></s><GPT4> token is generated:1from openai import OpenAI
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model = AutoModelForCausalLM.from_pretrained("leeroo/LeerooDedicated-Math-7b", trust_remote_code=True)
5tokenizer = AutoTokenizer.from_pretrained("leeroo/LeerooDedicated-Math-7b")
6openai_client = OpenAI(
7 api_key= "OPENAI_API_KEY",
8 base_url= "https://api.openai.com/v1"
9)
10
11def generate(prompt, tokenizer, model, openai_client, max_new_tokens=100, verbose=True):
12 inputs = tokenizer(prompt, return_tensors="pt")
13 inputs = {k:v.to(model.device) for k,v in inputs.items()}
14 gen_tokens = model.generate( **inputs , max_new_tokens=max_new_tokens, do_sample=False, pad_token_id= tokenizer.pad_token_id)
15 if gen_tokens[0, inputs['input_ids'].shape[1]] != tokenizer.unk_token_id:
16 if verbose: print("\033[94mGenerating using MetaMath7b.\033[0m")
17 gen_text = tokenizer.decode(
18 gen_tokens[0, inputs['input_ids'].shape[1]:].tolist() )
19 else:
20 if verbose: print("\033[94mGenerating using gpt4.\033[0m")
21 gen_text = openai_client.completions.create(
22 model = "gpt-4-1106-preview", # NOTE you can use any bigger mode here having performance similar to gpt4
23 prompt = prompt,
24 max_tokens = max_new_tokens,
25 temperature = 0.0
26 ).choices[0].text
27 return gen_text
28
29# the following question is answered by the leeroo expert
30prompt = "Question: Natalia sold clips to 48 of her friends in April,and then she sold half as many clips in May.How many clips did Natalia sell altogether in April and May?\nAnswer:"
31generation = generate(prompt, tokenizer, model, openai_client, max_new_tokens=500)
32print(generation)
33#> Generating using MetaMath7b.
34# Natalia sold 48 clips in April.\nIn May, she sold half as many clips as in April,
35# so she sold 48/2 = 24 clips.\nAltogether, Natalia sold 48 + 24 = 72 clips in April and May.\n#### 72\nThe answer is: 72</s>
36
37# sends the following question to GPT4
38prompt = "James loves to go swimming and has to swim across a 40-mile lake. He can swim at a pace of 2 miles per hour. He swims 60% of the distance. After that, he stops on an island and rests for half as long as the swimming time. He then finishes the remaining distance while going half the speed. How many hours did it take him to get across the lake?"
39generation = generate(prompt, tokenizer, model, openai_client, max_new_tokens=500)
40print(generation)
41#> Generating using gpt4.
42# He swam 40*.6=24 miles
43# So he swam for 24/2=12 hours
44# He rested for 12/2=6 hours
45# He had 40-24=16 miles left to swim
46# He swam at 2/2=1 mile per hour
47# So he swam for 16/1=16 hours
48# So in total, it took him 12+6+16=34 hours
49# 34@misc{mohammadshahi2024leeroo,
title={Leeroo Orchestrator: Elevating LLMs Performance Through Model Integration},
author={Alireza Mohammadshahi and Ali Shaikh and Majid Yazdani},
year={2024},
eprint={2401.13979},
archivePrefix={arXiv},
primaryClass={cs.CL}
}