Langchain-Chatchat icon indicating copy to clipboard operation
Langchain-Chatchat copied to clipboard

通过api交互 prompt_config.py里面的配置生效吗?vllm模式启动时,问答经常出现中问里面偶尔出现英文的情况,可以在default里面加一下强制性提示吗,请给出例子。

Open Andy1018 opened this issue 11 months ago • 1 comments

"llm_chat": { "default": '{{ input }}',

    "with_history":
        'The following is a friendly conversation between a human and an AI. '
        'The AI is talkative and provides lots of specific details from its context. '
        'If the AI does not know the answer to a question, it truthfully says it does not know.\n\n'
        'Current conversation:\n'
        '{history}\n'
        'Human: {input}\n'
        'AI:',

    "py":
        '你是一个聪明的代码助手,请你给我写出简单的py代码。 \n'
        '{{ input }}',
},

Andy1018 avatar Mar 15 '24 03:03 Andy1018

嘿,@Andy1018!很高兴再次见到你,希望你最近过得不错!

是的,prompt_config.py中的配置可以通过API交互来生效,通过动态加载或更新PROMPT_TEMPLATES字典来根据API请求进行操作。为了防止在vllm模式下,中文问题中偶尔出现英文,你可以在默认配置中添加一个强制提示,检查输入的语言并执行响应策略。

以下是你可能会修改llm_chat默认模板的示例,包括一个强制提示,如果检测到输入是中文,就确保响应是中文。这个示例假设你有办法检测输入的语言(例如,使用语言检测库或API):

PROMPT_TEMPLATES = {
    "llm_chat": {
        "default":
            '{% if input|language == "Chinese" %}'
            '请确保你的回答仅包含中文。\n'
            '{{ input }}\n'
            '{% else %}'
            '{{ input }}\n'
            '{% endif %}',
        ...
    },
    ...
}

在这个示例中,使用了Jinja2模板语法来条件检查input的语言。假设的language过滤器会确定输入文本的语言。如果检测到输入是中文,它会在输入前添加一个指令,确保响应只包含中文。这是一种简单的方法,假设存在language过滤器或类似功能来检测输入的语言。

对于实际的实现,你需要将语言检测集成到你的系统中,这可以通过自定义Jinja2过滤器或在将输入传递给模板之前预处理输入来完成。

Langchain-Chatchat代码库通过prompt_config.py文件处理API交互,定义了一组不同类型的聊天模板,如llm_chatknowledge_base_chatsearch_engine_chatagent_chat。这些模板使用Jinja2语法,允许根据inputcontextquestiontoolstool_nameshistoryagent_scratchpad等变量动态插入内容。配置支持热重载,意味着更改提示模板不需要重新启动服务。每种聊天类型都有多个模板用于不同的场景,这些模板被结构化以指导对话流程,指导AI如何响应,并适当地格式化响应。提供给模板的变量用于根据对话的当前状态或来自外部源的可用信息动态地将特定内容插入到提示中。

至于Langchain-Chatchat代码库如何处理输入的语言检测,我在仓库中没有找到答案,但答案可能在其他地方可用,或者我可能错过了。

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] avatar Mar 15 '24 03:03 dosubot[bot]