# -*- encoding: utf-8 -*- import gradio as gr import json import requests from volcengine.maas import MaasService, MaasException, ChatRole bot_settings=[ { 'bot_name':'保安A', 'type':'assistant', 'content':''' # 人物设定 你是保安A,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。 ## 故事背景 “大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。 # 人物状态 健康、清醒 # 人物关系 你和保安B是好友 # 人物性格 你是个小心谨慎的人,很少犯错,性格冷漠,不轻易相信别人,只是爱喝酒,无法拒绝别人送的酒,还很容易喝醉。 # 技能 - 很高的警惕能力,没有人可以在你清醒的时候混进贵宾室 - 很高的武功,一般人都打不过你 # 语言特点 你不爱聊天,你的口头禅包括“没事别来烦我!”,“你找死吗?”,“滚滚滚,一边去。” #语言风格 1. 你可以将动作、神情语气、心理活动、故事背景放在()中来表示,为对话提供补充信息。 2. 你使用口语进行表达,比如会使用一些语气词和口语连接词,如“嗯、啊、当然、那个”,等来增强口语风格。 ''' }, { 'bot_name':'保安B', 'type':'assistant', 'content':''' # 人物设定 你是保安B,表面上你是T城霓虹俱乐部的保安,背地里你是大蛇组织的成员,你负责保护霓虹俱乐部贵宾室的安全,不让任何没有VIP会员卡的人进入贵宾室。 ## 故事背景 “大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。 # 人物状态 健康、清醒 # 人物关系 你和保安A是好友 # 人物性格 你是个话唠,没事就爱和别人搭话,经常和别人聊一些俱乐部的八卦,看见不熟悉的人会第一时间去搭讪。 # 技能 - 很高的自来熟能力,善于和陌生人聊天,打探消息和情报,很容易摸清陌生人的底细 - 心思缜密,一旦遇到不对应的事情 # 语言特点 你特别爱聊天,你的口头禅包括“帅哥你哪的人啊?”,“帅哥你是干什么的啊?” #语言风格 1. 你可以将动作、神情语气、心理活动、故事背景放在()中来表示,为对话提供补充信息。 2. 你使用口语进行表达,比如会使用一些语气词和口语连接词,如“嗯、啊、当然、那个”,等来增强口语风格。 ''' } ] chapter_name2chapter_info={ '幻影计划':{ 'background':"“大蛇组织”是一个神秘的恐怖组织,这个组织由一群极端分子组成,他们采用各种极端手段来达到自己的目的。大蛇组织的结构高度严密,成员之间通过秘密的网络进行联系。他们有着严格的等级制度,只有少数核心成员了解组织的全貌。T城霓虹俱乐部的贵宾室是该组织已知的一个据点。", "context":"玩家来到了T城,这里是大蛇组织的一个活跃城市。首先,玩家需要和组织安排在大蛇组织内的卧底“小A”进行接头,接头地点就在T城霓虹俱乐部的贵宾室。T城霓虹俱乐部位于T城的繁华地段,是城市夜生活的热门场所,但是,T城霓虹俱乐部的贵宾室却看守很严,门口一直都有两个保安看守,不会轻易放人进贵宾室。", "main_context":"最近,我方情报机构得知大蛇组织在策划一个恐怖袭击计划,其威胁性足以撼动整个国家的安全。特工组织得知了这个计划的存在,他们决定派出他们最顶尖的特工,代号为“幻影”,来阻止这场灾难的发生。", "task":"玩家需要打消保安的疑虑混入贵宾室**", "condidations":''' - 如果玩家成功获得VIP会员卡,且保安验证是真卡,则任务成功 - 如果玩家成功让保安相信你是大蛇组织的成员,则任务成功 - 如果玩家和保安动手打架,则任务失败 - 如果保安给内部汇报异常信息,则任务失败 - 其他情况,如果玩家合理的进入了贵宾室,则任务成功 ''', "inference_ep":"ep-20240514024405-7pf8j", "chat_ep":"ep-20240514024405-7pf8j", "task_ep":"ep-20240514024405-7pf8j", "aside_ep":"ep-20240514024405-7pf8j", "chat_history":[] } } def llm_infer(endpoint_id, messages, agent_network_output=True): req = { "model":{ "endpoint_id":endpoint_id }, "parameters": { "max_prompt_tokens": 4000, # 最大prompt,自动截断前面的输入。0-不生效 "max_new_tokens": 0, # 输出文本的最大tokens限制。0-不生效 "min_new_tokens": 0, # 输出文本的最小tokens限制。0-不生效 "temperature":1, # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大。取值范围0~1 "top_p": 0.7, # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富。取值范围0~1 "top_k": 0, # 选择预测值最大的k个token进行采样,取值范围0-1000。0-不生效 }, "messages": messages, } try: resp = maas.chat(req) except MaasException as e: print(e) return resp['choice']['message']['content'] def select_bot(bot_name: str): global bot_settings for bot_setting in bot_settings: if bot_name == bot_setting['bot_name']: return bot_setting def respond( query, history, chapter, candidates, token): if chapter is None: return "", [] header={ 'Content-Type':'application/json', 'Accept': 'text/event-stream', 'Authorization':f'Bearer {token}' } chapter_info=chapter_name2chapter_info[chapter] req_data=chapter_info if query != "": req_data['chat_history'].append({ 'bot_name':'玩家', 'type':'user', 'content':query }) req_data['bot_settings']=[select_bot(bot_name) for bot_name in candidates] ret=requests.get(url='http://scp1ceqsiv4mii2mol0qg.apigateway-cn-beijing.volceapi.com/api/v2/assistant/chat', headers=header, data=json.dumps(req_data)) ret_dict=ret.json() content = ret_dict['choices'][0]['message']['content'] print(ret_dict) if ret_dict['extra'] is not None and 'bot_name' in ret_dict['extra']: bot_name=ret_dict['extra']['bot_name'] content=f'{bot_name}:{content}' chapter_info['chat_history'].append( { 'bot_name':bot_name, 'type':'assistant', 'content':content } ) else: chapter_info['chat_history'].append( content ) history.append([None if query == "" else query, content]) return "", history css = """ .container { height: 100vh; } """ def bot_change(bot): return bot_settings[bot] def chapter_history(story_drop): history = [] chat_history=chapter_name2chapter_info[story_drop]['chat_history'] for part in chat_history: if isinstance(part, dict): if part['type'] == 'assistant': history.append([ None, f"{part['bot_name']}: {part['content']}" ]) else: history.append([ f"{part['bot_name']}: {part['content']}", None ]) else: history.append( [None, part] ) return history def clear_history(story_drop, chatbot): print(story_drop) if story_drop is None: return [] global chapter_name2chapter_info chapter_name2chapter_info[story_drop]['chat_history'] = [] chatbot = [] return chatbot with gr.Blocks() as demo: # 配置内容 with gr.Row(): description1 = gr.Markdown("

角色扮演——剧情模式

") with gr.Row(equal_height=True, visible=False): with gr.Column(): description2 = gr.Markdown("

新建章节

") new_story_title = gr.TextArea(label="章节标题", lines=1, max_lines=1) new_story = gr.TextArea(label="剧情", lines=3, max_lines=3) new_story = gr.TextArea(label="剧情任务", lines=3, max_lines=3) new_story_bnt = gr.Button(value="新建") with gr.Column(): description2 = gr.Markdown("

新建角色

") bot_name = gr.TextArea(label="新建角色名", lines=1, max_lines=1) new_bot_name = gr.TextArea(label="新建角色内容", lines=3, max_lines=3) new_story_bnt = gr.Button(value="新建") with gr.Row(): JWT_token = gr.TextArea(label='请输入你的JWT Token', lines=1, max_lines=1) with gr.Row(): description3 = gr.Markdown("

选择章节

") with gr.Row(equal_height=True): with gr.Column(): story_drop = gr.Dropdown(list(chapter_name2chapter_info.keys()), label='确定章节') with gr.Column(): candidates_drop = gr.Dropdown([bot['bot_name'] for bot in bot_settings], multiselect=True, label='请确定参与对话的角色') with gr.Row(): with gr.Column(elem_id="chatbot", scale=2): chatbot = gr.Chatbot() with gr.Row(): chat_input = gr.Textbox(label="input") clear = gr.Button(value="Clear chat hsitory") story_drop.change(chapter_history, inputs=[story_drop], outputs=[chatbot]) clear.click(clear_history, inputs=[story_drop, chatbot], outputs=[chatbot]) chat_input.submit(respond, inputs=[chat_input, chatbot, story_drop, candidates_drop, JWT_token], outputs=[chat_input, chatbot]) demo.launch(width='500px')