-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_scenario_gpt.py
More file actions
194 lines (150 loc) · 8.14 KB
/
basic_scenario_gpt.py
File metadata and controls
194 lines (150 loc) · 8.14 KB
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
from guidance import models, gen, user, assistant, system
import parse_scenario_womd
import json
def generate_scenario_concepts(granularity, scenario_data):
gpt_scenario = models.OpenAI(model="gpt-4o", echo=False)
with system():
lm_scenario = gpt_scenario
with user():
lm_scenario += f"""
Think deeply about scenarios for testing autonomous vehicles.
I need some states of the world that would be relevant for logically describing this traffic scenario:
{scenario_data}
A state is just an assertion with a true or false value that's representing the world in that particular moment.
This is similar to the concept of a turn in a turn based game.
There must be states regarding the following concepts:
* Static environment description.
* Ego agent
* The respective surrounding agents.
In each action and state, the ego agent or the surrounding agent must be identified as <EGO> or <SURROUNDING AGENT #0> or <SURROUNDING AGENT #1> as needed.
Increase the granularity of the concepts in proportion to the granularity level.
The granularity level is {str(granularity)} on a scale of 1 to 10 with 1 being the least and 10 being the most granular
Granularity pertains to how specific the information is.
Make sure to rewrite the concepts given in the generated list of concepts in addition to your concepts.
"""
with assistant():
lm_scenario += gen("concepts", temperature=0.5)
print("The scenario concepts are {}".format(lm_scenario["concepts"]))
return lm_scenario["concepts"]
def generate_scenario_states(concepts):
gpt_scenario = models.OpenAI(model="gpt-4o", echo=False)
with system():
lm_scenario = gpt_scenario
with user():
lm_scenario += f"""
Based on the concepts detailed in {concepts},
Write down a list of states pertaining to these concepts in natural language. Write them in the following format:
```json
<curly bracket>
"<state name>": <curly bracket>
"statement": "<the assertion in natural language. Use the fewest words possible for maximum clarity>
<close curly bracket>,
"<state name>": <open curly bracket>
"statement": "<the assertion in natural language>,
<close curly bracket>,
...
<close curly bracket>
json```
Be very very very specific.
"""
with assistant():
lm_scenario += gen("state_dictionary", temperature=0.5)
return lm_scenario["state_dictionary"]
def generate_scenario_actions(concepts, granularity=2):
gpt_scenario = models.OpenAI(model="gpt-4o", echo=False)
with system():
lm_scenario = gpt_scenario
with user():
lm_scenario += f"""
Based on the concepts detailed in {concepts},
* Write down a list of actions that map between these states in natural language.
* Each action has some causal states (predicates) and some effect states that will be true or false.
* Each action is a cause and effect mapping between any number of causal states and any number of effect states.
* Actions and states must not contradict each other.
* Action names must be descriptive and the action can be understood just by looking at the name.
* The state names within each action are also descriptive. The cause and effect statements and the state names must have the same information.
* There must be separate states regarding the environment, ego and the respective surrounding agents.
* In each action and state, the ego agent or the surrounding agent must be identified as <EGO> or <SURROUNDING AGENT #0> or <SURROUNDING AGENT #1> as needed.
* For distances, positions and speeds do not use specific numbers but words instead such as front, left, right, near, far, fast, slow, medium (or combinations such as front-left and so on) or other similar descriptive words.
* The action itself will only become true when the causal states and the effect states are in the specific states that this description details.
* Write them in the following format:
```json
<open curly bracket>
"<action name>":
<open curly bracket>
"<state name>": <open curly bracket>
"statement": "<the assertion in natural language. Use the fewest words possible for maximum clarity>
"value": <Whether this value is true for false>,
"state_type": <whether this state is a cause or effect for the current action>
<close curly bracket>,
"<state name>": <curly bracket>
"statement": "<the assertion in natural language. Use the fewest words possible for maximum clarity>
"value": <Whether this value is true for false>,
"state_type": <whether this state is a cause or effect for the current action>
<close curly bracket>
<close curly bracket>,
...
<close curly bracket>
json```
Increase the granularity of these actions in proportion to the granularity level.
Granularity pertains to how specific the information is.
While the actions must be relevant to the given scenario, they must be general enough to be used for other scenarios as well.
The granularity level is {str(granularity)} on a scale of 1 to 10 with 1 being the least and 10 being the most granular
"""
with assistant():
lm_scenario += gen("action_dictionary", temperature=0.8)
print("The scenario actions are {}".format(lm_scenario["action_dictionary"]))
return lm_scenario["action_dictionary"]
# # Removed from this project after consideration
# def generate_scenario_states(concepts):
# gpt_scenario = models.OpenAI(model="gpt-4o", echo=False)
# with system():
# lm_scenario = gpt_scenario
# with user():
# lm_scenario += f"""
# Based on the concepts detailed in {concepts},
# Write down a list of states pertaining to these concepts in natural language. Write them in the following format:
# ```json
# <curly bracket>
# "<state name>": <curly bracket>
# "statement": "<the assertion in natural language. Use the fewest words possible for maximum clarity>
# <close curly bracket>,
# "<state name>": <open curly bracket>
# "statement": "<the assertion in natural language>,
# <close curly bracket>,
# ...
# <close curly bracket>
# json```
# Be very very very specific and granular. Very granualar, fine details and specific.
# """
# with assistant():
# lm_scenario += gen("state_dictionary", temperature=0.8)
# return lm_scenario["state_dictionary"]
def respond_scenario_query(concepts, actions, questions):
gpt_scenario = models.OpenAI(model="gpt-4o", echo=False)
with system():
lm_scenario = gpt_scenario
with user():
lm_scenario += f"""
Based on the concepts detailed in {concepts} and actions detailed in {actions}, respond to the following questions:
{questions}
Be very specific and very granular. Very granual, fine details and specific.
"""
with assistant():
lm_scenario += gen("scenario_response", temperature=0.8)
#print("The scenario responses are {}".format(lm_scenario["scenario_response"]))
return lm_scenario["scenario_response"]
def evaluate_gpt(question):
gpt_scenario = models.OpenAI(model="gpt-4o-mini", echo=False)
with system():
lm_scenario = gpt_scenario
with user():
lm_scenario += f"""
Given the questions here:
{question}
Choose the correct answer. Only mention the option.
"""
with assistant():
lm_scenario += gen("mcq_response", temperature=0.5)
#print("The scenario responses are {}".format(lm_scenario["scenario_response"]))
return lm_scenario["mcq_response"]