-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (55 loc) · 1.76 KB
/
main.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
import re
import os
import io
import sys
from apis import *
from chatgpt import ask,chat_history
code_block_regex = re.compile(r"```(.*?)```", re.DOTALL)
def extract_python_code(content):
# 提取出chatgpt回复中的代码
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
def execute_instruction(instruction):
# 使用 io.StringIO 捕获执行指令时的输出
stdout_backup = sys.stdout
sys.stdout = io.StringIO()
try:
exec(instruction)
finally:
# 将捕获的输出取出并恢复原来的输出流
output = sys.stdout.getvalue()
sys.stdout = stdout_backup
return output
def main():
print("欢迎使用Airsim Chatbot!请告诉我接下来的指令")
while True:
question = input("AirSim> ")
if question == "!quit" or question == "!exit":
break
if question == "!clear":
os.system("cls")
continue
print("等待chatgpt响应中...")
response,his = ask(question,chat_history)
print(f"\n{response}\n")
while "完成" not in response:
code = extract_python_code(response)
if code is not None:
print("正在执行指令中...")
output = execute_instruction(code)
print("指令执行完成!\n")
his.append(
{
"role": "assistant",
"content": output
}
)
response, his = ask(question, his)
if __name__ == "__main__":
main()