Views
No views yet
1import json
2import torch
3from transformers import (
4 AutoModelForCausalLM,
5 AutoTokenizer,
6 BitsAndBytesConfig,
7 GenerationConfig,
8)
9
10device = "cuda:0"
11repo_id = "krasserm/gba-planner-7B-v0.2"
12
13bnb_config = BitsAndBytesConfig(
14 load_in_4bit=True,
15 bnb_4bit_use_double_quant=True,
16 bnb_4bit_quant_type="nf4",
17 bnb_4bit_compute_dtype=torch.bfloat16,
18)
19
20tokenizer = AutoTokenizer.from_pretrained(repo_id)
21model = AutoModelForCausalLM.from_pretrained(
22 repo_id,
23 quantization_config=bnb_config,
24 device_map=device,
25)1prompt = """User request:
2
3```
4Get the average Rotten Tomatoes scores for DreamWorks' last 5 movies.
5```
6
7Context information:
8
9```
10Task: Find the last 5 movies released by DreamWorks.
11Result: The last five movies released by DreamWorks are "The Bad Guys" (2022), "Boss Baby: Family Business" (2021), "Trolls World Tour" (2020), "Abominable" (2019), and "How to Train Your Dragon: The Hidden World" (2019).
12
13Task: Search the internet for the Rotten Tomatoes score of "The Bad Guys" (2022)
14Result: The Rotten Tomatoes score of "The Bad Guys" (2022) is 88%.
15```
16
17Plan the next step."""1instruct_template = "[INST] {prompt} [/INST]"
2instruct_prompt = instruct_template.format(prompt=prompt)
3
4input_ids = tokenizer(instruct_prompt, return_tensors="pt", max_length=1024, truncation=True)["input_ids"]
5input_ids = input_ids.to("cuda:0")
6
7generation_config = GenerationConfig(
8 max_new_tokens=512,
9 do_sample=False,
10 eos_token_id=tokenizer.eos_token_id,
11 pad_token_id=tokenizer.pad_token_id,
12)
13
14with torch.no_grad():
15 result = model.generate(input_ids, generation_config=generation_config)
16 result = result[:, input_ids.shape[1] :]
17
18decoded = tokenizer.batch_decode(result, skip_special_tokens=True)
19decoded_dict = json.loads(decoded[0])
20print(json.dumps(decoded_dict, indent=2))1{
2 "context_information_summary": "The last five movies released by DreamWorks are \"The Bad Guys\" (2022), \"Boss Baby: Family Business\" (2021), \"Trolls World Tour\" (2020), \"Abominable\" (2019), and \"How to Train Your Dragon: The Hidden World\" (2019). The Rotten Tomatoes score of \"The Bad Guys\" (2022) is 88%.",
3 "thoughts": "Since we have the Rotten Tomatoes score for \"The Bad Guys\", the next logical step is to find the score for the next movie in the list, \"Boss Baby: Family Business\". After obtaining this score, we can proceed to find the scores for the remaining movies in the same manner.",
4 "task": "Search the internet for the Rotten Tomatoes score of \"Boss Baby: Family Business\" (2021).",
5 "selected_tool": "search_internet"
6}final_answer tool is selected, a final answer is available or can be generated from the trajectory.| Tool name | Tool description |
|---|---|
ask_user | Useful for asking user about information missing in the request. |
calculate_number | Useful for numerical tasks that result in a single number. |
create_event | Useful for adding a single entry to my calendar at given date and time. |
search_wikipedia | Useful for searching factual information in Wikipedia. |
search_internet | Useful for up-to-date information on the internet. |
send_email | Useful for sending an email to a single recipient. |
use_bash | Useful for executing commands in a Linux bash. |
final_answer | Useful for providing the final answer to a request. Must always be used in the last step. |