所有目标的实现都是源于对过程的深究

问题阐述

使用langchain遇到了需要在提示词里面加入json格式需求,如果是直接插入会导致识别成变量。

错误信息:

KeyError: "Input to ChatPromptTemplate is missing variables {'key1'}.  Expected: ['input', 'key1'] Received: ['input']\nNote: if you intended {key1} to be part of the string and not a variable, please escape it with double curly braces like: '{{key1}}'.\nFor troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/INVALID_PROMPT_INPUT "

不使用模版字符串的情况

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", """你是一个标准的格式输出器。请帮我输出一段用户提供键值,系统mocks生成value的json格式。格式如下:
        {key1: value1, key2:value2}
        """),
        ("human", "{input}")
    ]
)

print(prompt.invoke({"input": "username"}))

修改措施:{key1: value1, key2:value2} 修改成 {{key1: value1, key2:value2}}

使用模板字符串的情况

from langchain_core.prompts import ChatPromptTemplate

params = {
    "build_tool": "Maven",
    "framework": "Spring Boot",
    "modules": ["User", "Product"],
    "needs": ["UserService", "ProductService"]
}
# [{"type": "dir", "path": "src/main/java"}, {"type": "file", "path": "pom.xml"}]

prompt = ChatPromptTemplate.from_messages([
    ("system", f"""
      根据以下参数生成 Java 项目目录结构,输出 JSON 列表,每项为:
        {{"type": "dir"|"file", "path": "相对路径"}}
      参数:
      - build_tool: {params['build_tool']}
      - framework: {params['framework']}
      - modules: {params['modules']}
      - needs: {params['needs']}

      只输出 JSON 列表,例如:
                  [{{{{"type": "dir", "path": "src/main/java"}}}}, {{{{"type": "file", "path": "pom.xml"}}}}]

      """),
    ("human", "请输出结构。")
])
print(prompt.invoke({}))

结果措施: {{"type": "dir"|"file", "path": "相对路径"}} 修改成 {{{{"type": "dir"|"file", "path": "相对路径"}}}}

总结

不使用模板字符串,统一用"{{" “}}” 处理

使用模板字符串,统一用"{{{{" “}}}}” 处理