File size: 6,704 Bytes
52925b5
6e73cd3
 
 
087de2e
6e73cd3
087de2e
 
2c6c5df
6e73cd3
 
 
 
 
 
 
 
 
 
52925b5
6e73cd3
 
 
 
 
 
 
 
 
52925b5
6e73cd3
 
714d948
6e73cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b54758d
 
6e73cd3
 
 
 
 
 
 
b54758d
6e73cd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714d948
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from transformers import LlamaForCausalLM, LlamaTokenizer, BitsAndBytesConfig, GenerationConfig
from utils import setup_device
import torch
import tqdm
import os

model_name = os.environ.get('LLM_MODEL')

model_path = "models/CRYSTAL-instruct" if model_name == None else model_name

device = setup_device()

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.float32 if device == "cpu" else torch.bfloat16
)

model = LlamaForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float32 if device == "cpu" else torch.bfloat16,
    device_map="auto",
    trust_remote_code=True,
    low_cpu_mem_usage=True,
    offload_folder="offloads",
    quantization_config=bnb_config if str(device) != "cpu" else None,
)

tokenizer = LlamaTokenizer.from_pretrained(
    model_name,
    trust_remote_code=True,
    use_fast=True,
)

PROMPT = '''### Instruction:
{}
### Input:
{}
### Response:'''

if tokenizer.pad_token_id is None:
    tokenizer.pad_token = tokenizer.eos_token

tokenizer.padding_side = "left"
tokenizer = tokenizer
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.eval()


def evaluate(
    prompt='',
    temperature=0.4,
    top_p=0.65,
    top_k=35,
    repetition_penalty=1.1,
    max_new_tokens=512,
    stream_output=False,
    **kwargs,
):
    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs["input_ids"].to(device)
    generation_config = GenerationConfig(
        temperature=temperature,
        top_p=top_p,
        top_k=top_k,
        repetition_penalty=repetition_penalty,
        **kwargs,
    )

    with torch.no_grad():
        generation_output = model.generate(
            input_ids=input_ids,
            generation_config=generation_config,
            return_dict_in_generate=True,
            output_scores=True,
            max_new_tokens=max_new_tokens,
            eos_token_id=tokenizer.eos_token_id,
            pad_token_id=tokenizer.pad_token_id,
        )
    s = generation_output.sequences[0]
    output = tokenizer.decode(s, skip_special_tokens=True)
    yield output.split("### Response:")[-1].strip()


def run_instruction(
    instructions,
    inputs,
    temperature=0.4,
    top_p=0.65,
    top_k=35,
    repetition_penalty=1.1,
    max_new_tokens=512,
    stream_output=False,
):
    now_prompt = PROMPT.format(instructions+'\n', inputs)

    response = evaluate(
        now_prompt, temperature, top_p, top_k, repetition_penalty, max_new_tokens, stream_output, do_sample=True
    )

    if stream_output:
        response = tqdm.tqdm(response, unit='token')

    for i in response:
        print(i)
        response = i

    return response


def search_keyword(prompt):
    instructions = """Prompt:Time: Fri, 23 August 2023 2:30PM\nWeather: 73F\nHow many friends have I told you about?
Search Keyword:Friends
Prompt:Time: Thu, 27 September 2023 3:41PM\nWeather: 62F\nWhat was our very first conversation
Chat Index:0
Prompt:Time: Tue, 21 September 2023 2:30PM\nWeather: 67F\nWhat was the last thing I said to you
Chat Index:-1
Prompt:Time: Sun, 31 October 2023 7:33AM\nWeather: 59F\nWhat was the last thing I said to you before that
Chat Index:-2
Prompt:Time: Sat, 30 October 2023 8:21PM\nWeather: 65F\nDid I ever tell you about my math class?
Search Keyword:math
Prompt:Time: Mon, 13 November 2023 4:52PM\nWeather: 55F\nWhat was my 7th grade English teacher's name?
Search Keyword:English
Prompt:Time: Wed, 15 May 2023 6:19PM\nWeather: 80F\nWhere did I say my wallet was?
Search Keyword:Wallet
Prompt:Time: Fri, 24 June 2023 1:52PM\nWeather: 92F\nWhat did Alex tell you?
Search Keyword:Alex
Prompt:Time: Sat, 19 July 2023 2:44PM\nWeather: 91F\nWhat was my first conversation today
Search Keyword:24 June"""
    answer = ''.join(run_instruction(
        instructions,
        "Prompt:"+prompt+"\n",
        temperature=0.5,
        top_p=0.5,
        top_k=200,
        repetition_penalty=1.1,
        max_new_tokens=256,
        stream_output=False,
    ))
    return answer


def identify_objects_from_text(prompt):
    instructions = """Input:The object that flies in the air from this picture is a toy helicopter
Output:Toy helicopter
Input:For the robot to be able to achieve the task, the robot needs to look for a white shirt
Output:White shirt
Input:To complete the task, the robot needs to remove the fruits from the wooden basket.
Output:fruits, wooden basket
Input:To clean up your desk, you need to gather and organize the various items scattered around it. This includes the laptop, cell phone, scissors, pens, and other objects. By putting these items back in their designated spaces or containers, you can create a more organized and clutter-free workspace.
Output:Laptop, cell phone, scissors, pens, containers
Input:The tree with a colorful sky background is the one to be looking for.
Output:Tree"""
    answer = ''.join(run_instruction(
        instructions,
        prompt,
        temperature=0.5,
        top_p=0.5,
        top_k=200,
        repetition_penalty=1.1,
        max_new_tokens=256,
        stream_output=False,
    ))
    return answer


def robotix(prompt):
    instructions = """#Get me some water
objects = [['water: 57%', (781, 592)]]
robot.target((781, 592))
object_distance = distance()
if object_distance > 10:
    robot.go("forward", object_distance, track="water")
robot.grab()
if object_distance > 10:
    robot.go("back", object_distance)
robot.release("here")
### Input:
#Stand by the table
objects = [['table: 81%', (1489, 1173)], ['table: 75%', (1971, 1293)]]
### Response:
robot.target((1489, 1173))
if distance() > 10:
    robot.go(forward, distance())
### Input:
#Put the apples in the basket
objects = [['basket: 77%', (89, 112)], ['apples: 72%', (222, 182)]]
### Response:
robot.target((281, 189))
if distance() > 10:
    robot.go("forward", distance(), track="apples")
robot.grab()
robot.target(robot.find("basket"))
robot.release(distance())
### Input:
#Go to the sofa
objects=[['sofa: 81%', (1060, 931)]]
### Response:
robot.target((1060, 931))
if distance() > 10:
    robot.go("forward", distance())
### Input:
#Go to that person over there and then come back
objects=[['person: 85%', (331, 354)]]
### Response:
robot.target((331, 354))
object_distance = distance()
if object_distance > 10:
    robot.go("forward", object_distance)
    robot.go("backward", object_distance)"""

    answer = ''.join(run_instruction(
        instructions,
        prompt,
        temperature=0.2,
        top_p=0.5,
        top_k=300,
        repetition_penalty=1.1,
        max_new_tokens=256,
        stream_output=False,
    ))
    return answer