Views
No views yet

1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3from peft import PeftModel
4
5base_id = "google/gemma-7b"
6peft_id_7b_qatar = "gcw-ai/gemma-scappy-qatar-adapter"
7
8bnb_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_quant_type="nf4",
11 bnb_4bit_compute_dtype=torch.bfloat16
12)
13
14base_model = AutoModelForCausalLM.from_pretrained(base_id, quantization_config=bnb_config, device_map={"":0})
15tokenizer = AutoTokenizer.from_pretrained(base_id)
16
17model = PeftModel.from_pretrained(base_model, peft_id_7b_qatar, adapter_name="qatar")
18
19instruction = f"""Write a function to add the given list to the given tuples.
20Evaluate the following test cases with print.
21add_lists([5, 6, 7], (9, 10)) == (9, 10, 5, 6, 7)
22add_lists([6, 7, 8], (10, 11)) == (10, 11, 6, 7, 8)"""
23
24answer_txt = """Here is the implementation of the requested function:
25
26```python
27def add_lists(list1, tuples):
28 return tuples + list1
29```
30
31Explanation:
32- The function `add_lists` takes two arguments, `list1` which is a list, and `tuples` which is a tuple.
33- The function concatenates the `tuples` and `list1` using the `+` operator, which returns a new tuple with the elements of both the tuple and the list combined.
34- The function returns the concatenated tuple.
35
36Test cases:
37
38```python
39print(add_lists([5, 6, 7], (9, 10))) # Output: (9, 10, 5, 6, 7)
40print(add_lists([6, 7, 8], (10, 11))) # Output: (10, 11, 6, 7, 8)
41```"""
42
43execution_result = """Error: TypeError: can only concatenate tuple (not "list") to tuple"""
44
45
46qatar_prompt = f"@@@ Instruction:{instruction}\n\n@@@ Output:{answer_txt}\n\n@@@ Execution Result:{execution_result}\n\n@@@ Thought:"
47
48inputs = tokenizer(qatar_prompt, return_tensors="pt").to("cuda:0")
49
50outputs = model.generate(**inputs, max_new_tokens=1000)
51print(tokenizer.decode(outputs[0], skip_special_tokens=True))@@@ Instruction:Write a function to add the given list to the given tuples.
Evaluate the following test cases with print.
add_lists([5, 6, 7], (9, 10)) == (9, 10, 5, 6, 7)
add_lists([6, 7, 8], (10, 11)) == (10, 11, 6, 7, 8)
@@@ Output:Here is the implementation of the requested function:
```python
def add_lists(list1, tuples):
return tuples + list1
```
Explanation:
- The function `add_lists` takes two arguments, `list1` which is a list, and `tuples` which is a tuple.
- The function concatenates the `tuples` and `list1` using the `+` operator, which returns a new tuple with the elements of both the tuple and the list combined.
- The function returns the concatenated tuple.
Test cases:
```python
print(add_lists([5, 6, 7], (9, 10))) # Output: (9, 10, 5, 6, 7)
print(add_lists([6, 7, 8], (10, 11))) # Output: (10, 11, 6, 7, 8)
```
@@@ Execution Result:Error: TypeError: can only concatenate tuple (not "list") to tuple
@@@ Thought:The provided answer contains a function that attempts to concatenate a list and a tuple, which is not possible in Python. The error in the execution result is due to the attempt to concatenate a list and a tuple, which is not supported in Python. The correct approach would be to convert the list to a tuple before concatenation.
@@@ Action:fail
@@@ Revised Answer:To add a list to a tuple, you need to convert the list to a tuple first. Here is the corrected implementation of the function:
```python
def add_lists(list1, tuples):
return tuples + tuple(list1)
```
Explanation:
- The function `add_lists` takes two arguments, `list1` which is a list, and `tuples` which is a tuple.
- The function converts the `list1` to a tuple using the `tuple()` function.
- The function concatenates the `tuples` and the converted `list1` using the `+` operator, which returns a new tuple with the elements of both the tuple and the list combined.
- The function returns the concatenated tuple.
Test cases:
```python
print(add_lists([5, 6, 7], (9, 10))) # Output: (9, 10, 5, 6, 7)
print(add_lists([6, 7, 8], (10, 11))) # Output: (10, 11, 6, 7, 8)
```