-
Notifications
You must be signed in to change notification settings - Fork 0
/
ue+chat.html
244 lines (238 loc) · 10.1 KB
/
ue+chat.html
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="欢迎来到SupernovaGo的世界">
<meta name="keywords" content="个人网站">
<title>SupernovaGo</title>
<link rel="stylesheet" href="./css/bootstrap-5/css/bootstrap.min.css">
<style>
.head{
width: 60px;
height: 60px;
display: flex;
margin-top: 3%;
margin-left: 40%;
background-image: url(./resource/other/headphoto.jpg);
background-size:cover;
border-radius: 50% 50%;
background-position: 0 0;
}
.offcanvas-title{
font-size: xx-large;
color: rgb(0, 208, 255);
font-weight: bold;
font-style: italic;
}
.offcanvas-start{
background-color: #00000037;
}
.navi{
font-size: large;
color: rgb(255, 255, 255);
font-weight: bold;
}
body{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-image: url(./resource/other/background/DSC01828-14.jpg);
background-size:cover;
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
}
h3{
font-weight: bold;
margin-left: 3%;
display: flex;
}
.btn-primary{
margin-left: 6%;
font-size: larger;
}
.container:first-child{
display: flex;
margin: 0;
}
.row:first-child {
display: flex;
left: 20%;
}
.column {
display: flex;
flex-direction: column;
}
.note-head{
font-size:6rem;
display: flex;
margin-top: 3%;
margin-left: 10%;
text-shadow: 2px 2px 4px rgba(31, 169, 255, 0.807);
}
.column>h2:first-child{
margin-top: 5%;
}
</style>
</head>
<body>
<div class="container">
<div class="column">
<div class="head"></div>
<h3>SupernovaGo</h3>
<a class="btn btn-primary" data-bs-toggle="offcanvas" href="#offcanvasExample" role="button" aria-controls="offcanvasExample">
导航
</a>
</div>
<div class="row">
<div class="note-head">虚幻引擎+ChatGPT</div>
</div>
</div>
<div class="container">
<div class="column">
<h2>
实现效果(虚幻引擎运行截图)
</h2>
<hr>
<h4>
在世界场景中可以自由走动,遇到NPC可直接对话
</h4>
<img src="./resource/projects/ue+chatgpt/屏幕截图 2023-05-16 002420.jpg" alt="">
<hr>
<img src="./resource/projects/ue+chatgpt/屏幕截图 2023-05-16 002513.jpg" alt="">
<hr>
<h2>
实现方法:虚幻引擎调用ChatGPT的API
</h2>
<h4>基本原理</h4>
<ul>
<li>首先编写一份关于该角色的prompt,其中主要包含人物形象、性格特点等(俗称催眠咒语)</li>
<li>角色prompt有两种发送方式,一是利用官方提供的role中的system,作为system的文本发送过去。role一共三种:user一般是用户,assistant一般是ChatGPT,而system是让ChatGPT在对话过程中设定自己行为的文本。虽然system本身适合用来让ChatGPT扮演角色,但目前GPT3.5的system效果并不好</li>
<li>第二种是直接将prompt拼接到对话的前面,一起发送过去,这也是当前使用的方式</li>
<li>为了实现上下文对话,在蓝图中创建了多个变量,分别存储前n轮的对话,并将其随着本次的提问一起发送过去,让ChatGPT有“短期记忆”。实际上ChatGPT本身是没有记忆的,网页中能实现上下文对话同样是采样类似的方法</li>
<li>蓝图中调用API的方法和Python的一致</li>
</ul>
<hr>
<h4>
以下是Python实现ChatGPT角色扮演、多轮对话的示例代码
</h4>
<pre><code>
import openai
prompt = '你编写的提示词'
openai.api_key = "你的key"
messages = [{'role':'user','content':''},
{'role':'assistant','content':''},
{'role':'user','content':''},
{'role':'assistant','content':''}]
while True:
chat = input('chat:')
if chat == 'quit':
print('结束...')
break
else:
question = {'role':'user','content':prompt+chat}
messages.pop(0) # 第一个去掉,防止上文对话过多
messages.append(question) # 把本次的问题加入到messages中
completion = openai.ChatCompletion.create(model='gpt-3.5-turbo',messages=messages) # completion即回应
messages.pop(-1) # 把本次的对话换成没有prompt的版本,防止prompt的冗余占用大量tokens
messages.append({'role':'user','content':chat})
response = completion.choices[0].message['content'].encode().decode('UTF-8') # 回应的转换
answer = {'role':'assistant','content':response} # 把answer也做成message,加入messages
messages.pop(0)
messages.append(answer)
# message中是十六进制的字符串,想要转换成中文,只需先编码成字节流(即二进制),再解码回字符串
print(response)
</code></pre>
<hr>
<h4>
虚幻引擎中调用Openai的API原理和上面一致,只是需要用蓝图编写
</h4>
<hr>
<h2>
存在的问题
</h2>
<hr>
<ul>
<li>“催眠”的回答仍然不可控,且无法保证完全“催眠”</li>
<li>NPC无法获取周围环境信息,而如果要做到像一个真正的人类,这一点是必须的。最好的办法是使用多模态大语言模型,并在NPC中放置一个摄像机组件,将画面实时传输到语言模型中,使其能感受周围环境</li>
<li>当前NPC回答的基础是ChatGPT,相当于受控于Openai公司</li>
</ul>
<hr>
<h3>
此项目纯粹出于个人兴趣,且受限于能力,暂时无法做出一个真正的游戏来。
</h3>
</div>
</div>
<div class="offcanvas offcanvas-start" tabindex="-1" id="offcanvasExample" aria-labelledby="offcanvasExampleLabel">
<div class="offcanvas-header">
<h5 class="offcanvas-title" id="offcanvasExampleLabel">侧边栏导航</h5>
<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close"></button>
</div>
<div class="offcanvas-body">
<div class="navi">
Hello!
</div>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
笔记本
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="course.html">计算机和经济统计</a></li>
<li><a class="dropdown-item" href="skills.html">课外拓展</a></li>
<li><a class="dropdown-item" href="essay.html">论文解析</a></li>
<li><a class="dropdown-item" href="xmind.html">思维导图</a></li>
</ul>
</div>
<hr>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
“心之回忆”计划
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="plan-2023.html">2023</a></li>
<li><a class="dropdown-item" href="plan-2022.html">2022</a></li>
<li><a class="dropdown-item" href="#">2021(暂不可用)</a></li>
<li><a class="dropdown-item" href="#">2020(暂不可用)</a></li>
</ul>
</div>
<hr>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
项目与实践
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="https://github.com/SupernovaGo/Elaina-LoRA-for-Llama3-8B-it/tree/main">用小说数据微调Llama3-8B模型</a></li>
<li><a class="dropdown-item" href="ue+chat.html">UE5+ChatGPT</a></li>
<li><a class="dropdown-item" href="sd.html">AI绘图</a></li>
<li><a class="dropdown-item" href="spider.html">爬虫</a></li>
<li><a class="dropdown-item" href="blender.html">Blender</a></li>
<li><a class="dropdown-item" href="AIvill.html">复现AI小镇实验</a></li>
<li><a class="dropdown-item" href="P+C.html">Python&C++联合编程</a></li>
</ul>
</div>
<hr>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
小说
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="./resource/novel/黑洞旅行者.pdf">黑洞旅行者</a></li>
<li><a class="dropdown-item" href="#">银河追光(暂不可用)</a></li>
</ul>
</div>
<hr>
<div class="dropdown mt-3">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown">
关于本网站(必看)
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="README.md">说明(必看)</a></li>
</ul>
</div>
</div>
</div>
<script src="./css/bootstrap-5/js/bootstrap.bundle.js"></script>
</body>
</html>