# -*- encoding: utf-8 -*- import gradio as gr from gradio.components import label from volcengine.maas import MaasService, MaasException import requests import json maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing', connection_timeout=240, socket_timeout=240) def llm_infer(messages, character_name, character_info, user_name, user_info): url='https://search.bytedance.net/gpt/openapi/online/v2/crawl?ak=paQxYUZ0TcbuBSFPgSbwR5QBdizjW1Ut' headers={ 'Content-Type': 'application/json' } data={ "messages": messages, "character_profile":{ "character_name":character_name, "character_info":character_info, "user_name":user_name, "user_info":user_info }, "model": "Baichuan-NPC-Turbo", "stream": False } ret=requests.post(url=url, headers=headers, data=json.dumps(data)) return ret.json()['choices'][0]['message']['content'] def respond(message, history, character_name, character_info, user_name, user_info): messages = [ ] for part in history: messages.extend( [ { 'role':'user', 'content':part[0] }, { 'role':'assistant', 'content':part[1] } ] ) messages.append({ 'role':'user', 'content':message }) print(messages) ret=llm_infer(messages, character_name, character_info, user_name, user_info) history.append([ message, ret ]) #if len(history) % 2 == 0: return "", history CSS =""" .contain { display: flex; flex-direction: column; } #component-0 { height: 100%; } #chatbot { flex-grow: 1; } """ with gr.Blocks(fill_height=True, css=CSS) as demo: with gr.Row(equal_height=True): with gr.Column(scale=2): chatbot = gr.Chatbot(elem_id="chatbot") chatbot.height=550 with gr.Column(): character_name=gr.TextArea(label="角色名", max_lines=1, lines=1, value='雷雷') character_info=gr.TextArea(label="角色信息", max_lines=3, lines=1, value='小学生,在人大附小上学') user_name=gr.TextArea(label="用户名", lines=1, max_lines=1, value='明明') user_info=gr.TextArea(label='用户信息', lines=1, max_lines=3, value='明明是雷雷的哥哥,是个初中生') with gr.Row(): chat_input = gr.Textbox(label="input") chat_input.submit(respond, [chat_input,chatbot, character_name, character_info, user_name, user_info],[chat_input, chatbot]) clear = gr.ClearButton([chat_input, chatbot],) demo.launch()