-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
115 lines (92 loc) · 2.97 KB
/
migrate.py
File metadata and controls
115 lines (92 loc) · 2.97 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
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 10 units up"
},
{
"role": "assistant",
"content": """```python
aw.fly_to([aw.get_drone_position()[0], aw.get_drone_position()[1], aw.get_drone_position()[2]+10])
```
This code uses the `fly_to()` function to move the drone to a new position that is 10 units up from the current position. It does this by getting the current position of the drone using `get_drone_position()` and then creating a new list with the same X and Y coordinates, but with the Z coordinate increased by 10. The drone will then fly to this new position using `fly_to()`."""
}
]
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")