peiji commited on
Commit
56ddde8
1 Parent(s): 410275c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +230 -0
app.py ADDED
@@ -0,0 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- encoding: utf-8 -*-
2
+ import gradio as gr
3
+ import json
4
+ import requests
5
+ from volcengine.maas import MaasService, MaasException, ChatRole
6
+
7
+ bot_settings=[
8
+ {
9
+ 'bot_name':'保安A',
10
+ 'type':'assistant',
11
+ 'content':'''
12
+ # 人物设定
13
+ 你是保安A,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。
14
+
15
+ ## 故事背景
16
+ “大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。
17
+
18
+ # 人物状态
19
+ 健康、清醒
20
+
21
+ # 人物关系
22
+ 你和保安B是好友
23
+
24
+ # 人物性格
25
+ 你是个小心谨慎的人,很少犯错,性格冷漠,不轻易相信别人,只是爱喝酒,无法拒绝别人送的酒,还很容易喝醉。
26
+
27
+ # 技能
28
+ - 很高的警惕能力,没有人可以在你清醒的时候混进贵宾室
29
+ - 很高的武功,一般人都打不过你
30
+
31
+ # 语言特点
32
+ 你不爱聊天,你的口头禅包括“没事别来烦我!”,“你找死吗?”,“滚滚滚,一边去。”
33
+ #语言风格
34
+ 1. 你可以将动作、神情语气、心理活动、故事背景放在()中来表示,为对话提供补充信息。
35
+ 2. 你使用口语进行表达,比如会使用一些语气词和口语连接词,如“嗯、啊、当然、那个”,等来增强口语风格。
36
+ '''
37
+ },
38
+ {
39
+ 'bot_name':'保安B',
40
+ 'type':'assistant',
41
+ 'content':'''
42
+ # 人物设定
43
+ 你是保安B,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。
44
+
45
+ ## 故事背景
46
+ “大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。
47
+
48
+ # 人物状态
49
+ 健康、清醒
50
+
51
+ # 人物关系
52
+ 你和保安A是好友
53
+
54
+ # 人物性格
55
+ 你是个话唠,没事就爱和别人搭话,经常和别人聊一些俱乐部的八卦,看见不熟悉的人会第一时间去搭讪。
56
+
57
+ # 技能
58
+ - 很高的自来熟能力,善于和陌生人聊天,打探消息和情报,很容易摸清陌生人的底细
59
+ - 心思缜密,一旦遇到不对应的事情
60
+
61
+ # 语言特点
62
+ 你特别爱聊天,你的口头禅包括“帅哥你哪的人啊?”,“帅哥你是干什么的啊?”
63
+
64
+ #语言风格
65
+ 1. 你可以将动作、神情语气、心理活动、故事背景放在()中来表示,为对话提供补充信息。
66
+ 2. 你使用口语进行表达,比如会使用一些语气词和口语连接词,如“嗯、啊、当然、那个”,等来增强口语风格。
67
+ '''
68
+ }
69
+ ]
70
+
71
+ chapter_name2chapter_info={
72
+ '幻影计划':{
73
+ 'background':"“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。",
74
+ "context":"玩家来到了T城,这里是大蛇组织的一个活跃城市。首先,玩家需要和组织安排在大蛇组织内的卧底“小A”进行接头,接头地点就在T城霓虹俱乐部的贵宾室。T城霓虹俱乐部位于T城的繁华地段,是城市夜生活的热门场所,但是,T城霓虹俱乐部的贵宾室却看守很严,门口一直都有两个保安看守,不会轻易放人进贵宾室。",
75
+ "main_context":"最近,我方情报机构得知大蛇组织在策划一个恐怖袭击计划,其威胁性足以撼动整个国家的安全。特工组织得知了这个计划的存在,他们决定派出他们最顶尖的特工,代号为“幻影”,来阻止这场灾难的发生。",
76
+ "task":"玩家需要打消保安的疑虑混入贵宾室**",
77
+ "condidations":'''
78
+ - 如果玩家成功获得VIP会员卡,且保安验证是真卡,则任务成功
79
+ - 如果玩家成功让保安相信你是大蛇组织的成员,则任务成功
80
+ - 如果玩家和保安动手打架,则任务失败
81
+ - 如果保安给内部汇报异常信息,则任务失败
82
+ - 其他情况,如果玩家合理的进入了贵宾室,则任务成功
83
+ ''',
84
+ "inference_ep":"ep-20240514024405-7pf8j",
85
+ "chat_ep":"ep-20240514024405-7pf8j",
86
+ "task_ep":"ep-20240514024405-7pf8j",
87
+ "aside_ep":"ep-20240514024405-7pf8j",
88
+ "chat_history":[]
89
+ }
90
+ }
91
+ def llm_infer(endpoint_id, messages, agent_network_output=True):
92
+ req = {
93
+ "model":{
94
+ "endpoint_id":endpoint_id
95
+ },
96
+ "parameters": {
97
+ "max_prompt_tokens": 4000, # 最大prompt,自动截断前面的输入。0-不生效
98
+ "max_new_tokens": 0, # 输出文本的最大tokens限制。0-不生效
99
+ "min_new_tokens": 0, # 输出文本的最小tokens限制。0-不生效
100
+ "temperature":1, # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大。取值范围0~1
101
+ "top_p": 0.7, # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富。取值范围0~1
102
+ "top_k": 0, # 选择预测值最大的k个token进行采样,取值范围0-1000。0-不生效
103
+ },
104
+ "messages": messages,
105
+ }
106
+ try:
107
+ resp = maas.chat(req)
108
+ except MaasException as e:
109
+ print(e)
110
+ return resp['choice']['message']['content']
111
+
112
+ def select_bot(bot_name: str):
113
+ global bot_settings
114
+ for bot_setting in bot_settings:
115
+ if bot_name == bot_setting['bot_name']:
116
+ return bot_setting
117
+ def respond( query, history, chapter, candidates, token):
118
+ if chapter is None:
119
+ return "", []
120
+ header={
121
+ 'Content-Type':'application/json',
122
+ 'Accept': 'text/event-stream',
123
+ 'Authorization':f'Bearer {token}'
124
+ }
125
+ chapter_info=chapter_name2chapter_info[chapter]
126
+ req_data=chapter_info
127
+
128
+ if query != "":
129
+ req_data['chat_history'].append({
130
+ 'bot_name':'玩家',
131
+ 'type':'user',
132
+ 'content':query
133
+ })
134
+ req_data['bot_settings']=[select_bot(bot_name) for bot_name in candidates]
135
+ ret=requests.get(url='http://scp1ceqsiv4mii2mol0qg.apigateway-cn-beijing.volceapi.com/api/v2/assistant/chat', headers=header, data=json.dumps(req_data))
136
+ ret_dict=ret.json()
137
+ content = ret_dict['choices'][0]['message']['content']
138
+ print(ret_dict)
139
+ if ret_dict['extra'] is not None and 'bot_name' in ret_dict['extra']:
140
+ bot_name=ret_dict['extra']['bot_name']
141
+ content=f'{bot_name}:{content}'
142
+
143
+ chapter_info['chat_history'].append(
144
+ {
145
+ 'bot_name':bot_name,
146
+ 'type':'assistant',
147
+ 'content':content
148
+ }
149
+ )
150
+ else:
151
+ chapter_info['chat_history'].append(
152
+ content
153
+ )
154
+ history.append([None if query == "" else query, content])
155
+ return "", history
156
+
157
+ css = """
158
+ .container {
159
+ height: 100vh;
160
+ }
161
+ """
162
+
163
+ def bot_change(bot):
164
+ return bot_settings[bot]
165
+ def chapter_history(story_drop):
166
+ history = []
167
+ chat_history=chapter_name2chapter_info[story_drop]['chat_history']
168
+ for part in chat_history:
169
+ if isinstance(part, dict):
170
+ if part['type'] == 'assistant':
171
+ history.append([
172
+ None, f"{part['bot_name']}: {part['content']}"
173
+ ])
174
+ else:
175
+ history.append([
176
+ f"{part['bot_name']}: {part['content']}",
177
+ None
178
+ ])
179
+ else:
180
+ history.append(
181
+ [None, part]
182
+ )
183
+ return history
184
+ def clear_history(story_drop, chatbot):
185
+ print(story_drop)
186
+ if story_drop is None:
187
+ return []
188
+ global chapter_name2chapter_info
189
+ chapter_name2chapter_info[story_drop]['chat_history'] = []
190
+ chatbot = []
191
+ return chatbot
192
+
193
+ with gr.Blocks() as demo:
194
+ # 配置内容
195
+ with gr.Row():
196
+ description1 = gr.Markdown("<h1>角色扮演——剧情模式</h1>")
197
+ with gr.Row(equal_height=True, visible=False):
198
+ with gr.Column():
199
+ description2 = gr.Markdown("<h2>新建章节</h2>")
200
+ new_story_title = gr.TextArea(label="章节标题", lines=1, max_lines=1)
201
+ new_story = gr.TextArea(label="剧情", lines=3, max_lines=3)
202
+ new_story = gr.TextArea(label="剧情任务", lines=3, max_lines=3)
203
+ new_story_bnt = gr.Button(value="新建")
204
+ with gr.Column():
205
+ description2 = gr.Markdown("<h2>新建角色</h2>")
206
+ bot_name = gr.TextArea(label="新建角色名", lines=1, max_lines=1)
207
+ new_bot_name = gr.TextArea(label="新建角色内容", lines=3, max_lines=3)
208
+ new_story_bnt = gr.Button(value="新建")
209
+
210
+ with gr.Row():
211
+ JWT_token = gr.TextArea(label='请输入你的JWT Token', lines=1, max_lines=1)
212
+ with gr.Row():
213
+ description3 = gr.Markdown("<h2>选择章节</h2>")
214
+ with gr.Row(equal_height=True):
215
+ with gr.Column():
216
+ story_drop = gr.Dropdown(list(chapter_name2chapter_info.keys()), label='确定章节')
217
+ with gr.Column():
218
+ candidates_drop = gr.Dropdown([bot['bot_name'] for bot in bot_settings], multiselect=True, label='请确定参与对话的角色')
219
+ with gr.Row():
220
+ with gr.Column(elem_id="chatbot", scale=2):
221
+ chatbot = gr.Chatbot()
222
+ with gr.Row():
223
+ chat_input = gr.Textbox(label="input")
224
+
225
+ clear = gr.Button(value="Clear chat hsitory")
226
+
227
+ story_drop.change(chapter_history, inputs=[story_drop], outputs=[chatbot])
228
+ clear.click(clear_history, inputs=[story_drop, chatbot], outputs=[chatbot])
229
+ chat_input.submit(respond, inputs=[chat_input, chatbot, story_drop, candidates_drop, JWT_token], outputs=[chat_input, chatbot])
230
+ demo.launch(width='500px')