You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何同时使用Gradio ChatInterface的additional_inputs与examples?

解决Gradio ChatInterface同时使用additional_inputs、examples及自定义textbox的问题

你遇到的问题根源在于:当ChatInterface启用additional_inputs后,示例(examples)的格式需要匹配函数的参数数量;同时自定义textbox需要正确传入ChatInterface的参数中,而非单独定义后忽略。以下是修复后的完整代码:

import gradio as gr

def yes_man(message, history, input1):
    return f"{input1}" 
  
with gr.Blocks() as demo:
    # 定义额外输入框
    input1 = gr.Textbox("", label="input1")
     
    gr.ChatInterface(
        yes_man, 
        # 每个示例为列表,依次对应message、input1(history由系统自动处理)
        examples=[
            ["Hello", "good"],
            ["Am I cool?", "nice"],
            ["Are tomatoes vegetables?", "yummy"]
        ],
        additional_inputs=[input1],
        # 传入自定义textbox参数,覆盖默认输入框样式
        textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7)
    )
        
demo.launch()

关键改动说明:

  • examples格式调整:启用additional_inputs后,每个示例不能是单个字符串,必须写成包含message和所有额外输入值的列表。点击示例时,会同时填充聊天消息框和对应的额外输入框。
  • 自定义textbox正确传入:将自定义的textbox对象作为textbox参数传给ChatInterface,而非单独定义后弃用,这样就能替换默认的消息输入框样式。

修改后即可同时实现:输入input1内容后机器人返回对应值、点击示例自动填充消息和input1、自定义消息输入框样式的所有需求。

内容的提问来源于stack exchange,提问作者Alicia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 00:25:59