Skip to content

Commit

Permalink
Support greeting in Langchain webui.
Browse files Browse the repository at this point in the history
  • Loading branch information
mzr1996 committed Feb 6, 2024
1 parent 92ce6b6 commit 08a1c08
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
7 changes: 5 additions & 2 deletions webui/modules/agents/langchain_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,20 @@ 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


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'
Expand All @@ -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

Expand Down
12 changes: 9 additions & 3 deletions webui/modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions webui/modules/html_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])&gt;', r'\1>', string)
Expand Down
3 changes: 3 additions & 0 deletions webui/modules/i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion webui/modules/ui_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand Down
9 changes: 5 additions & 4 deletions webui/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 08a1c08

Please sign in to comment.