diff --git a/webui/modules/agents/langchain_agent.py b/webui/modules/agents/langchain_agent.py index 6dd2a75e..7bef5855 100644 --- a/webui/modules/agents/langchain_agent.py +++ b/webui/modules/agents/langchain_agent.py @@ -143,6 +143,7 @@ def cfg_chat_openai(): widgets['temperature'] = gr.Slider(label='Temperature', minimum=0., maximum=1., step=0.1, value=0.7, info=i18n('temperature')) widgets['extra_stop'] = gr.Textbox(label='Extra stop words', info=i18n('extra_stop')) widgets['meta_prompt'] = gr.Textbox(label='Meta prompt', info=i18n('meta_prompt'), value='Respond to the human as helpfully and accurately as possible.', lines=5) + widgets['greeting'] = gr.Textbox(label='Greeting', info=i18n('greeting'), value=None, lines=5) return widgets @@ -150,9 +151,12 @@ def langchain_style_history(history) -> ChatMessageHistory: memory = ChatMessageHistory() for row in history['internal']: response = '' + if row[0]: + memory.add_user_message(row[0]) for step in row[1]: if isinstance(step, msg.ToolInput): - response += f'Thought: {step.thought or ""}\n' + if step.thought: + response += f'Thought: {step.thought}\n' args = json.dumps({k: v['content'] for k, v in step.args.items()}, ensure_ascii=False) tool_str = f'{{\n "action": "{step.name}",\n "action_input": {args}\n}}' response += 'Action:\n```\n' + tool_str + '\n```\n' @@ -166,7 +170,6 @@ def langchain_style_history(history) -> ChatMessageHistory: response += f'Thought: {step.thought or ""}\n' tool_str = f'{{\n "action": "Final Answer",\n "action_input": "{step.text}"\n}}' response += 'Action:\n```\n' + tool_str + '\n```\n' - memory.add_user_message(row[0]) memory.add_ai_message(response) return memory diff --git a/webui/modules/chat.py b/webui/modules/chat.py index ea98736f..6639f261 100644 --- a/webui/modules/chat.py +++ b/webui/modules/chat.py @@ -135,7 +135,7 @@ def redraw_html(history): def modify_last_message(history): - if len(history['visible']) > 0 and history['internal'][-1][0] != '<|BEGIN-VISIBLE-CHAT|>': + if len(history['visible']) > 0 and history['visible'][-1][0] != '<|BEGIN-VISIBLE-CHAT|>': last = history['internal'].pop() history['visible'].pop() else: @@ -147,8 +147,14 @@ def modify_last_message(history): def start_new_chat(): history = {'internal': [], 'visible': []} - unique_id = datetime.now().strftime('%Y%m%d-%H-%M-%S') - save_history(history, unique_id) + from .message_schema import Answer + from .settings import get_agent_settings + if shared.agent_name: + agent_settings = get_agent_settings(shared.agent_name) + greeting = agent_settings.get('greeting', None) + if greeting: + history['internal'] += [['', [Answer(text=greeting)]]] + history['visible'] += [['<|BEGIN-VISIBLE-CHAT|>', greeting]] return history diff --git a/webui/modules/html_generator.py b/webui/modules/html_generator.py index 03efc1b3..0846ea7d 100644 --- a/webui/modules/html_generator.py +++ b/webui/modules/html_generator.py @@ -38,6 +38,8 @@ def replace_blockquote(m): def convert_to_markdown(string): + if string == '<|BEGIN-VISIBLE-CHAT|>': + return '' # Blockquote string = re.sub(r'(^|[\n])>', r'\1>', string) diff --git a/webui/modules/i18n.yml b/webui/modules/i18n.yml index 3803aab1..d0415992 100644 --- a/webui/modules/i18n.yml +++ b/webui/modules/i18n.yml @@ -81,6 +81,9 @@ langchain_agent: meta_prompt: en: "The extra meta prompt to the agent." zh: "额外的系统提示词" + greeting: + en: "If set, use the message as the first generated message when start a new chat." + zh: "如果设置,在开始新聊天时,使用此消息作为生成的第一条消息。" # agents/lagent_agent.py lagent_agent: diff --git a/webui/modules/ui_chat.py b/webui/modules/ui_chat.py index c8736639..685afb2f 100644 --- a/webui/modules/ui_chat.py +++ b/webui/modules/ui_chat.py @@ -95,7 +95,7 @@ def create_event_handlers(): .then(chat.redraw_html, gradio('history'), gradio('display')) shared.gradio['Start new chat']\ - .click(lambda: {'internal': [], 'visible': []}, None, gradio('history'))\ + .click(chat.start_new_chat, None, gradio('history'))\ .then(chat.redraw_html, gradio('history'), gradio('display'))\ .then(lambda: gr.update(value=None), None, gradio('unique_id')) diff --git a/webui/modules/utils.py b/webui/modules/utils.py index f9d08ec7..08ad8d0e 100644 --- a/webui/modules/utils.py +++ b/webui/modules/utils.py @@ -29,12 +29,13 @@ def parse_inputs(toolmeta: ToolMeta, args: Union[str, tuple, dict]) -> Mapping[s parsed_args = {} for k, v in args.items(): - p = params[k] - if p.type is ImageIO: + if k not in params: + parsed_args[k] = dict(type='text', content=v) + elif params[k].type is ImageIO: parsed_args[k] = dict(type='image', content=v) - elif p.type is AudioIO: + elif params[k].type is AudioIO: parsed_args[k] = dict(type='audio', content=v) - elif p.type is File: + elif params[k].type is File: parsed_args[k] = dict(type='file', content=v) else: parsed_args[k] = dict(type='text', content=v)