Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,8 @@ 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',
|
@@ -70,6 +71,7 @@ bot_settings=[
|
|
70 |
|
71 |
chapter_name2chapter_info={
|
72 |
'幻影计划':{
|
|
|
73 |
'background':"“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。",
|
74 |
"context":"玩家来到了T城,这里是大蛇组织的一个活跃城市。首先,玩家需要和组织安排在大蛇组织内的卧底“小A”进行接头,接头地点就在T城霓虹俱乐部的贵宾室。T城霓虹俱乐部位于T城的繁华地段,是城市夜生活的热门场所,但是,T城霓虹俱乐部的贵宾室却看守很严,门口一直都有两个保安看守,不会轻易放人进贵宾室。",
|
75 |
"main_context":"最近,我方情报机构得知大蛇组织在策划一个恐怖袭击计划,其威胁性足以撼动整个国家的安全。特工组织得知了这个计划的存在,他们决定派出他们最顶尖的特工,代号为“幻影”,来阻止这场灾难的发生。",
|
@@ -85,74 +87,91 @@ chapter_name2chapter_info={
|
|
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 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
content=f'{bot_name}:{content}'
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
)
|
154 |
-
|
155 |
-
return "", history
|
156 |
|
157 |
css = """
|
158 |
.container {
|
@@ -162,25 +181,7 @@ css = """
|
|
162 |
|
163 |
def bot_change(bot):
|
164 |
return bot_settings[bot]
|
165 |
-
|
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:
|
@@ -194,7 +195,7 @@ with gr.Blocks() as demo:
|
|
194 |
# 配置内容
|
195 |
with gr.Row():
|
196 |
description1 = gr.Markdown("<h1>角色扮演——剧情模式</h1>")
|
197 |
-
with gr.Row(equal_height=True, visible=
|
198 |
with gr.Column():
|
199 |
description2 = gr.Markdown("<h2>新建章节</h2>")
|
200 |
new_story_title = gr.TextArea(label="章节标题", lines=1, max_lines=1)
|
@@ -224,7 +225,7 @@ with gr.Blocks() as demo:
|
|
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(
|
|
|
3 |
import json
|
4 |
import requests
|
5 |
from volcengine.maas import MaasService, MaasException, ChatRole
|
6 |
+
from collections import defaultdict
|
7 |
+
from copy import deepcopy
|
8 |
bot_settings=[
|
9 |
{
|
10 |
'bot_name':'保安A',
|
|
|
71 |
|
72 |
chapter_name2chapter_info={
|
73 |
'幻影计划':{
|
74 |
+
"stream":True,
|
75 |
'background':"“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。",
|
76 |
"context":"玩家来到了T城,这里是大蛇组织的一个活跃城市。首先,玩家需要和组织安排在大蛇组织内的卧底“小A”进行接头,接头地点就在T城霓虹俱乐部的贵宾室。T城霓虹俱乐部位于T城的繁华地段,是城市夜生活的热门场所,但是,T城霓虹俱乐部的贵宾室却看守很严,门口一直都有两个保安看守,不会轻易放人进贵宾室。",
|
77 |
"main_context":"最近,我方情报机构得知大蛇组织在策划一个恐怖袭击计划,其威胁性足以撼动整个国家的安全。特工组织得知了这个计划的存在,他们决定派出他们最顶尖的特工,代号为“幻影”,来阻止这场灾难的发生。",
|
|
|
87 |
"chat_ep":"ep-20240514024405-7pf8j",
|
88 |
"task_ep":"ep-20240514024405-7pf8j",
|
89 |
"aside_ep":"ep-20240514024405-7pf8j",
|
|
|
90 |
}
|
91 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
def select_bot(bot_name: str):
|
94 |
global bot_settings
|
95 |
for bot_setting in bot_settings:
|
96 |
if bot_name == bot_setting['bot_name']:
|
97 |
return bot_setting
|
98 |
+
def make_chat_history(history):
|
99 |
+
chat_history = []
|
100 |
+
for part in history:
|
101 |
+
user, assistant = part
|
102 |
+
_type = 'user' if user is not None else 'assistant'
|
103 |
+
split=user.find(': ')
|
104 |
+
bot_name = user[:split] if user is not None else assistant[:split]
|
105 |
+
content=user[split:] if user is not None else assistant[split:]
|
106 |
+
chat_history.append({
|
107 |
+
'bot_name':bot_name,
|
108 |
+
'type':_type,
|
109 |
+
'content':content
|
110 |
+
})
|
111 |
+
|
112 |
+
return chat_history
|
113 |
def respond( query, history, chapter, candidates, token):
|
114 |
+
chat_history=make_chat_history(history)
|
115 |
if chapter is None:
|
116 |
return "", []
|
117 |
+
|
118 |
header={
|
119 |
'Content-Type':'application/json',
|
120 |
'Accept': 'text/event-stream',
|
121 |
'Authorization':f'Bearer {token}'
|
122 |
}
|
123 |
+
chapter_info=deepcopy(chapter_name2chapter_info[chapter])
|
124 |
req_data=chapter_info
|
125 |
+
req_data['chat_history']=chat_history
|
126 |
if query != "":
|
127 |
req_data['chat_history'].append({
|
128 |
'bot_name':'玩家',
|
129 |
'type':'user',
|
130 |
'content':query
|
131 |
})
|
132 |
+
history.append([query, None])
|
133 |
+
yield "", history
|
134 |
+
|
135 |
req_data['bot_settings']=[select_bot(bot_name) for bot_name in candidates]
|
136 |
+
# 在线
|
137 |
+
ret=requests.get(url='http://scp1ceqsiv4mii2mol0qg.apigateway-cn-beijing.volceapi.com/api/v2/assistant/chat', headers=header, data=json.dumps(req_data), stream=True)
|
138 |
+
# 离线
|
139 |
+
#ret=requests.get(url='http://0.0.0.0:8081/api/v2/assistant/chat', headers=header, data=json.dumps(req_data), stream=True)
|
140 |
+
bot_contents = defaultdict(str)
|
141 |
+
content = ""
|
|
|
142 |
|
143 |
+
bot_name=''
|
144 |
+
for line in ret.iter_content(decode_unicode=True):
|
145 |
+
if content.endswith('\r\n\r\n'):
|
146 |
+
data=eval(content.strip('data:'))
|
147 |
+
try:
|
148 |
+
_bot_name=data['extra']['bot_name']
|
149 |
+
content_tmp=data['choices'][0]['message']['content']
|
150 |
+
except Exception as e:
|
151 |
+
break
|
152 |
+
if _bot_name!= bot_name:
|
153 |
+
bot_name=_bot_name
|
154 |
+
history.append([
|
155 |
+
None, f"{bot_name}:"
|
156 |
+
])
|
157 |
+
print(f'\n\n{bot_name}: ', end='', flush=True)
|
158 |
+
print(content_tmp, end='', flush=True)
|
159 |
+
history[-1][-1] +=content_tmp
|
160 |
+
yield "", history
|
161 |
+
bot_contents[bot_name]+=content_tmp
|
162 |
+
content=""
|
163 |
+
content+=line
|
164 |
+
|
165 |
+
for k, v in bot_contents.items(): ## 叠加历史对话
|
166 |
+
req_data['chat_history'].append({
|
167 |
+
'bot_name':k,
|
168 |
+
'type':'assistant',
|
169 |
+
'content':v
|
170 |
+
})
|
171 |
+
history.append(
|
172 |
+
[None, f"{k}: {v}"]
|
173 |
)
|
174 |
+
|
|
|
175 |
|
176 |
css = """
|
177 |
.container {
|
|
|
181 |
|
182 |
def bot_change(bot):
|
183 |
return bot_settings[bot]
|
184 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
def clear_history(story_drop, chatbot):
|
186 |
print(story_drop)
|
187 |
if story_drop is None:
|
|
|
195 |
# 配置内容
|
196 |
with gr.Row():
|
197 |
description1 = gr.Markdown("<h1>角色扮演——剧情模式</h1>")
|
198 |
+
with gr.Row(equal_height=True, visible=True):
|
199 |
with gr.Column():
|
200 |
description2 = gr.Markdown("<h2>新建章节</h2>")
|
201 |
new_story_title = gr.TextArea(label="章节标题", lines=1, max_lines=1)
|
|
|
225 |
|
226 |
clear = gr.Button(value="Clear chat hsitory")
|
227 |
|
228 |
+
#story_drop.change(chapter_history, inputs=[story_drop], outputs=[chatbot])
|
229 |
clear.click(clear_history, inputs=[story_drop, chatbot], outputs=[chatbot])
|
230 |
chat_input.submit(respond, inputs=[chat_input, chatbot, story_drop, candidates_drop, JWT_token], outputs=[chat_input, chatbot])
|
231 |
+
demo.launch()
|