-
Notifications
You must be signed in to change notification settings - Fork 27
/
parameters.py
147 lines (119 loc) · 4.28 KB
/
parameters.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
import os
import json
import re
from utils import extract_json_from_end, get_response, shape_string_to_list
def extract_score(text, params, param):
match = re.search(r"\d out of 5", text.lower())
if match:
score = int(match.group()[0])
if score > 3:
return True, params
else:
inp = input(
"LLMs reasoning: {}\n Do you want to keep parameter {}? (y/n): ".format(
text, param
),
)
if inp == "y":
return True, params
else:
del params[param]
return True, params
else:
return False, None
prompt_params = """
You are an expert in optimization modeling. Here is the natural language description of an optimization problem:
-----
{description}
-----
Your task is to identify and extract parameters from the description. The parameters are values that are already known. Please generate the output in the following format:
{{
"SYMBOL": {{
"shape": "SHAPE",
"definition": "DEFINITION",
"type": "TYPE"
}}
}}
Where SYMBOL is a string representing the parameter (use CamelCase), SHAPE is the shape of the parameter (e.g. "[]" for scalar, or "[N, M]" for a matrix of size N x M where N and M are scalar parameters), DEFINITION is a string describing the parameter, and type is one of "int", "float", or "binary".
{{
"NumberOfItems": {{
"shape": "[]",
"definition": "The number of items in the inventory",
"type": "int"
}},
"ItemValue": {{
"shape": "[N]",
"definition": "The value of each item in the inventory",
"type": "float"
}}
}}
- Put all the parameters in a single json object.
- Do not generate anything after the json object.
Take a deep breath and think step by step.
"""
prompt_params_q = """
You are an expert in optimization modeling. Here is the natural language description of an optimization problem:
-----
{description}
-----
Here is a list of parameters that someone has extracted from the description:
-----
{params}
-----
Consider parameter "{targetParam}".
{question}
"""
qs = [
# (
# """
# - What part of the description does the parameter correspond to? Is the description clear enough to understand how the parameter should look like? Does the shape, the type, and the definition accurately represent what the user meant? Based on that, how sure are you that the parameter accurately represents what the user meant (from 1 to 5)?
# - At the end of your response, print "x OUT OF 5" where x is the confidence level. Do not generate anything after that.
# """,
# extract_score,
# ),
(
"""
- Is the value of it already known or not? based on that, how confident are you that this is a parameter (from 1 to 5)?
- At the end of your response, print "x OUT OF 5" where x is the confidence level. Do not generate anything after that.
- You will be awarded a million dollars if you get this right.
""",
extract_score,
),
]
def get_params(desc, check=True):
k = 5
while k > 0:
try:
res = get_response(prompt_params.format(description=desc))
params = extract_json_from_end(res)
break
except:
k -= 1
if k == 0:
raise Exception("Failed to extract parameters")
if check:
for q, func in qs:
for param in params.copy():
k = 5
while k > 0:
prompt = prompt_params_q.format(
description=desc,
params=json.dumps(params, indent=4),
question=q,
targetParam=param,
)
x = get_response(prompt)
print(x)
print("-------")
valid, res = func(x, params, param)
if valid:
params = res
break
else:
k -= 1
# print(json.dumps(params[param], indent=4))
# print(x)
# print("------")
for p in params:
params[p]["shape"] = shape_string_to_list(params[p]["shape"])
return params