-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathllm_planner.py
336 lines (286 loc) · 10.2 KB
/
llm_planner.py
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import abc
from openai import OpenAI
import torch
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
PreTrainedTokenizer,
PreTrainedModel,
)
from vllm import LLM, RequestOutput, SamplingParams
from vllm.lora.request import LoRARequest
class PlanningProblem:
def __init__(
self,
natural_language: str,
domain: str,
problem: str,
):
"""Initializes a new PlanningProblem.
Args:
natural_language (str): The natural language description of the
problem to be solved.
domain (str): A string representation of the domain of the problem.
problem (str): A string representation of the ground truth PDDL.
"""
self.natural_language = natural_language
self.domain = domain
self.problem = problem
def apply_template(
self,
domain_prompt: str = "",
problem_prompt: str = "",
include_answer: bool = True,
) -> list[dict[str, str]]:
"""Apply problem template to the problem.
Args:
domain_prompt (str, optional): How to prompt the domain. Defaults to "".
problem_prompt (str, optional): How to prompt the problem. Defaults to "".
include_answer (bool, optional): Whether to include the answer. Defaults to True.
Returns:
list[dict[str, str]]: Problem prompt.
"""
return [
{
"role": "user",
"content": f"{problem_prompt} {self.natural_language} "
+ f"{domain_prompt}\n{self.domain}\n",
},
] + (
[
{
"role": "assistant",
"content": " " + self.problem,
},
]
if include_answer
else []
)
class Planner(abc.ABC):
@abc.abstractmethod
def plan_chat(
self,
messages: list[list[dict[str, str]]],
device=None,
max_new_tokens: int = 8_000,
**kwargs,
) -> list[str]:
"""Passes messages to a model for completion.
Args:
messages (list[list[dict[str, str]]]): A list of messages to be
passed to the model.
device (optional): The device to run the model on. Defaults to None.
max_new_tokens (int): The maximum number of tokens to generate.
Defaults to 8_000.
Returns:
list[str]: The message completion.
"""
pass
class HFPlanner:
"""A class for planning using Huggingface transformers."""
def __init__(
self,
model_name: str | None = None,
tokenizer_name: str | None = None,
tokenizer: PreTrainedTokenizer | None = None,
model: PreTrainedModel | None = None,
**kwargs,
):
"""Initializes a new HFPlanner.
Args:
model_name (str): The name of the model to be used.
tokenizer_name (str, optional): The name of the tokenizer to be used.
Defaults to None, in which case the model_name is used.
kwargs: Additional keyword arguments to be passed to the model.
"""
if model is not None and tokenizer is not None:
self.model = model
self.tokenizer = tokenizer
else:
self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_name or model_name)
self.model = AutoModelForCausalLM.from_pretrained(model_name, **kwargs)
def plan(self, prompt: str, device=None, **kwargs) -> str:
"""Passes the prompt to the model for completion, without applying a
chat template.
Args:
prompt (str): The prompt to be passed to the model.
device (optional): The device to run the model on. Defaults to None.
Returns:
str: The message completion.
"""
encoded = self.tokenizer.encode(prompt, return_tensors="pt")
if device is not None:
encoded = encoded.to(device)
generate_config = {
"max_new_tokens": 4000,
"temperature": 0.0,
"do_sample": False,
}
generate_config.update(kwargs)
generated_ids = self.model.generate(encoded, **generate_config)
decoded = self.tokenizer.decode(generated_ids)
return decoded[0]
def plan_chat(
self,
messages: list[dict[str, str]],
device=None,
**kwargs,
) -> list[str]:
"""Passes messages to the model for completion, applying a chat template.
Args:
messages (list[dict[str, str]]): A list of messages to be passed to
the model.
device (optional): The device to run the model on. Defaults to None.
kwargs: Additional keyword arguments to be passed to the model.
Returns:
str: The message completion.
"""
encoded = self.tokenizer.apply_chat_template(
messages,
return_tensors="pt",
padding=True,
add_generation_prompt=True,
)
if device is not None:
encoded = encoded.to(device)
generate_config = { # default generate config
"max_new_tokens": 4000,
"do_sample": False,
}
generate_config.update(kwargs)
with torch.no_grad():
generated_ids = self.model.generate(encoded, **generate_config)
decoded = []
for g, e in zip(generated_ids, encoded):
decoded.append(
self.tokenizer.decode(
g[len(e) :],
skip_special_tokens=True,
)
)
return decoded
class VLLMPlanner(Planner):
"""A class for planning using VLLM models."""
def __init__(self, model_name: str, lora: str | None = None, **kwargs):
"""Initializes a new VLLMPlanner.
Args:
model_name (str): The name of the model to be used.
kwargs: Additional keyword arguments to be passed to the model.
"""
self.lora = LoRARequest(lora, 1, lora) if lora else None
self.model = LLM(model_name, enable_lora=bool(lora), **kwargs)
self.tokenizer = self.model.get_tokenizer()
def plan_chat(
self,
messages: list[list[dict[str, str]]],
device=None,
max_new_tokens: int = 8_000,
**kwargs,
) -> list[str]:
"""Passes messages to the model for completion.
Args:
messages (list[dict[str, str]]): A list of messages to be passed to
the model.
Returns:
list[str]: The message completions.
"""
encoded = self.tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=False,
)
params = SamplingParams(
max_tokens=max_new_tokens,
temperature=kwargs.get("temperature", 0.0),
top_p=kwargs.get("top_p", 1.0),
top_k=kwargs.get("top_k", -1),
min_p=kwargs.get("min_p", 0.0),
)
outputs: list[RequestOutput] = self.model.generate(
encoded,
params,
use_tqdm=False,
lora_request=self.lora,
)
return [output.outputs[0].text for output in outputs]
class OpenAIPlanner:
"""A class for planning using OpenAI models."""
def __init__(self, model_name: str, **kwargs):
"""Initializes a new OpenAIPlanner.
Args:
model_name (str): The name of the model to be used.
kwargs: Additional keyword arguments to be passed to the OpenAI
client.
"""
self.client = OpenAI(**kwargs)
self.model_name = model_name
self.is_o1 = model_name.startswith("o1")
def _plan_chat(
self,
messages: list[dict[str, str]],
max_new_tokens: int | None = None,
device=None,
**kwargs,
) -> str:
"""Passes messages to the model for completion.
Args:
messages (list[dict[str, str]]): A list of messages to be passed to
the model.
device (optional): The device to run the model on (ignored for OpenAI).
Returns:
str: The message completion.
"""
if self.is_o1:
return (
self.client.chat.completions.create(
model=self.model_name,
messages=messages,
frequency_penalty=kwargs.get("frequency_penalty", None),
max_completion_tokens=max_new_tokens,
n=1,
presence_penalty=kwargs.get("presence_penalty", None),
temperature=kwargs.get("temperature", 0.0),
top_p=kwargs.get("top_p", None),
)
.choices[0]
.message.content
)
else:
return (
self.client.chat.completions.create(
model=self.model_name,
messages=messages,
frequency_penalty=kwargs.get("frequency_penalty", None),
max_tokens=max_new_tokens,
n=1,
presence_penalty=kwargs.get("presence_penalty", None),
temperature=kwargs.get("temperature", 0.0),
top_p=kwargs.get("top_p", None),
)
.choices[0]
.message.content
)
def plan_chat(
self,
messages: list[list[dict[str, str]]],
max_new_tokens: int | None = None,
device=None,
**kwargs,
) -> list[str]:
"""Passes messages to the model for completion.
Args:
messages (list[list[dict[str, str]]]): A list of messages to be
passed to the model.
device (optional): The device to run the model on (ignored for OpenAI).
Returns:
list[str]: The message completions.
"""
return [
self._plan_chat(
message,
max_new_tokens=max_new_tokens,
device=device,
**kwargs,
)
for message in messages
]