-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetterVisionExtra.py
More file actions
114 lines (94 loc) · 2.87 KB
/
Copy pathbetterVisionExtra.py
File metadata and controls
114 lines (94 loc) · 2.87 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
from openai import OpenAI
import re
import argparse
from airsim_wrapper import AirSimWrapper
import os
import json
import time
# Set up argument parsing
parser = argparse.ArgumentParser()
parser.add_argument("--prompt", type=str, default="prompts/airsim_basic.txt")
parser.add_argument("--sysprompt", type=str, default="system_prompts/airsim_basic.txt")
args = parser.parse_args()
# Load OpenAI API Key from config
with open("config.json", "r") as f:
config = json.load(f)
print("Initializing ChatGPT...")
client = OpenAI(api_key=config["OPENAI_API_KEY"])
# Read system prompt
with open(args.sysprompt, "r") as f:
sysprompt = f.read()
chat_history = [
{
"role": "system",
"content": sysprompt
},
{
"role": "user",
"content": "move camera 10 units up"
},
{
"role": "assistant",
"content": """```python
import airsim
current_pose = aw.get_camera_pose()
new_pose = airsim.Pose(current_pose.position + airsim.Vector3r(0, 0, -10), current_pose.orientation)
aw.set_camera_pose(new_pose)
This code gets the current camera pose using get_camera_pose(), modifies the Z coordinate to move the camera 10 units up, and then sets the new camera pose using set_camera_pose()."""
}
]
def ask(prompt):
chat_history.append(
{
"role": "user",
"content": prompt,
}
)
completion = client.chat.completions.create(model="gpt-3.5-turbo",
messages=chat_history,
temperature=0)
chat_history.append(
{
"role": "assistant",
"content": completion.choices[0].message.content,
}
)
return chat_history[-1]["content"]
print(f"Done.")
code_block_regex = re.compile(r"```(.*?)```", re.DOTALL)
def extract_python_code(content):
code_blocks = code_block_regex.findall(content)
if code_blocks:
full_code = "\n".join(code_blocks)
if full_code.startswith("python"):
full_code = full_code[7:]
return full_code
else:
return None
class colors: # You may need to change color settings
RED = "\033[31m"
ENDC = "\033[m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
print(f"Initializing AirSim...")
aw = AirSimWrapper()
print(f"Done.")
with open(args.prompt, "r") as f:
prompt = f.read()
ask(prompt)
print("Welcome to the AirSim chatbot! I am ready to help you with your AirSim questions and commands.")
while True:
question = input(colors.YELLOW + "AirSim> " + colors.ENDC)
if question == "!quit" or question == "!exit":
break
if question == "!clear":
os.system("cls")
continue
response = ask(question)
print(f"\n{response}\n")
code = extract_python_code(response)
if code is not None:
print("Please wait while I run the code in AirSim...")
exec(extract_python_code(response))
print("Done!\n")