-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.py
101 lines (91 loc) · 2.26 KB
/
chatgpt.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
import requests
import json
# prompt的路径
sys_prompt_path = "prompts/basic.txt"
with open(sys_prompt_path, "r", encoding="utf-8") as f:
sys_prompt = f.read()
# 自定义chat History
chat_history = [
{
"role": "user",
"content": sys_prompt
},
{
"role": "assistant",
"content": "好的,我将作为你的助手帮助你使用AirSim仿真器进行无人机操作。请告诉我你希望无人机执行的第一个任务,我会为你提供相应的Python代码并解释其作用。"
},
{
"role": "user",
"content": "起飞并移动到(10,10,5)"
},
{
"role": "assistant",
"content": """```python
# 使无人机起飞
takeoff()
```
"""
},
{
"role": "user",
"content": "起飞成功!"
},
{
"role": "assistant",
"content": """```python
# 获取当前位置
current_position = get_position()
```
"""
},
{
"role": "user",
"content": "当前位置是(0,0,0)"
},
{
"role": "assistant",
"content": """```python
# 移动到目标地点
move_to_position(10,10,5)
```
"""
},
{
"role": "user",
"content": "成功移动到(10,10,5)"
},
{
"role": "assistant",
"content": """
任务完成!
"""
}
]
def ask(prompt,history):
url = "https://apikeyplus.com/v1/chat/completions"
headers = {
'Content-Type': 'application/json',
# 填写 OpenKEY 生成的令牌/KEY,注意前面的 Bearer 要保留,并且和 KEY 中间有一个空格。
'Authorization': 'Bearer sk-vintOWhjGERJPMNZD3F5C417E8C949F991224eA2E70fE236'
}
history.append(
{
"role": "user",
"content": prompt,
}
)
data = {
"model": "gpt-3.5-turbo",
"messages": history
}
response = requests.post(url, headers=headers, json=data)
text = json.loads(response.text)['choices'][0]['message']['content']
history.append(
{
"role": "assistant",
"content": text
}
)
return text, history
if __name__ == "__main__":
print(ask("起飞并降落"))