POML 与 LangChain 集成

POML 与 LangChain 集成

本文基于官方页面(microsoft.github.io/poml/stable... POML 在 LangChain 中的集成方式、安装步骤、基础用法、两种模板模式,以及与 f-string/Jinja 等模板方法的对比。

导语/背景介绍

  • 官方说明:POML 通过 LangchainPomlTemplate 与 LangChain 无缝集成,为 LangChain 内置模板系统(Jinja2 与 f-strings)提供有力替代方案。

安装与使用方法(Installation / Basic Usage)

安装(Installation)

bash 复制代码
pip install langchain langchain-openai

基础用法(Basic Usage)

python 复制代码
from poml.integration.langchain import LangchainPomlTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

# 从文件加载 POML 模板
prompt_template = LangchainPomlTemplate.from_file("explain_code.poml")

# 或者从字符串创建模板
prompt_template = LangchainPomlTemplate.from_template(
    "<poml><task>Explain this:</task>"
    "<code inline=\"false\"><document src=\"{{ code_file }}\" parser=\"txt\" /></code></poml>"
)

# 在 LangChain chain 中使用
llm = ChatOpenAI(model="gpt-4.1")
chain = prompt_template | llm | StrOutputParser()
result = chain.invoke({"code_file": "test_sample.py"})

功能介绍:Speaker 模式与非 Speaker 模式(Speaker Mode vs Non-Speaker Mode)

python 复制代码
# Speaker 模式:返回 ChatPromptValue(结构化消息)
# 适用于需要会话结构(system/user/assistant)的场景
template = LangchainPomlTemplate.from_file("conversation.poml", speaker_mode=True)

# 非 Speaker 模式:返回 StringPromptValue(纯文本)
# 适用于需要单段文本输出的场景
template = LangchainPomlTemplate.from_file("summary.poml", speaker_mode=False)

与 f-string、Jinja 及其他模板的对比(POML vs f-string, Jinja, and Other Templates)

官方页面指出:可通过 LangchainPomlTemplate 获得 POML 的多数能力;某些 POML 特性在其他模板系统也可实现;也有部分特性可能与 LangChain 不兼容(例如工具使用)。以下示例用于对比差异与优势。

1) 模板语法与能力(Template Syntax and Capabilities)

  • Jinja2/f-string:主要是字符串插值与(Jinja 提供的)基础条件/循环。
python 复制代码
from langchain.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template(
    "Answer the question as if you are {person}, fully embodying their style, "
    "wit, personality, and habits of speech. The question is: {question}"
)
  • POML:在插值之外,支持结构化组件与逻辑流。
xml 复制代码
<poml>
  <system-msg>You are {{ person }}, answer in their unique style and personality.</system-msg>
  <human-msg>{{ question }}</human-msg>
  <div if="include_examples">
    <examples>
      <document src="{{ person }}_examples.txt" />
    </examples>
  </div>
</poml>
python 复制代码
from poml.integration.langchain import LangchainPomlTemplate
prompt_template = LangchainPomlTemplate.from_file("persona_prompt.poml")
prompt_template.invoke({
    "person": "Mark Twain",
    "question": "What is the meaning of life?",
    "include_examples": True
})

2) 富内容与文件引入(Rich Content and File Inclusion)

  • 替代方案(Jinja2/f-string):需要手工读取与处理 PDF,然后再注入模板。
python 复制代码
import PyPDF2
from langchain.prompts import PromptTemplate

def read_pdf(file_path):
    with open(file_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        text = ""
        for page in reader.pages:
            text += page.extract_text()
    return text

pdf_content = read_pdf("document.pdf")
prompt = PromptTemplate.from_template(
    "Analyze this document:\n{pdf_text}\n\nQuestion: {question}"
)
result = chain.invoke({"pdf_text": pdf_content, "question": "What are the key points?"})
  • POML 方式:无需手工处理文件,直接引用。
xml 复制代码
<poml>
  <task>Analyze this document and answer the question.</task>
  <document src="{{ pdf_path }}" />
  <human-msg>{{ question }}</human-msg>
</poml>
python 复制代码
result = chain.invoke({
    "pdf_path": "document.pdf",
    "question": "What are the key points?"
})

3) 用结构化标签组织 few-shot 示例(Structured Tags to Organize Few-shot Examples)

  • LangChain FewShotPromptTemplate:
python 复制代码
from langchain.prompts import FewShotPromptTemplate, PromptTemplate
examples = [
    {"input": "2+2", "output": "4"},
    {"input": "3*3", "output": "9"}
]
example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}\nOutput: {output}"
)
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="Solve these math problems:",
    suffix="Input: {input}\nOutput:",
    input_variables=["input"]
)
  • POML:
xml 复制代码
<poml>
  <task>Solve these math problems:</task>
  <example for="ex in examples">
    <example-input>{{ ex.input }}</example-input>
    <example-output>{{ ex.output }}</example-output>
  </example>
  <human-msg>{{ input }}</human-msg>
</poml>
  • 直接得到(Immediately gives you):
json 复制代码
[
  { "speaker": "system", "content": "# Task\n\nSolve these math problems:" },
  { "speaker": "human", "content": "2+2" },
  { "speaker": "ai", "content": "4" },
  { "speaker": "human", "content": "3*3" },
  { "speaker": "ai", "content": "9" },
  { "speaker": "human", "content": "5-4" }
]

4) 对话格式支持(Support for Conversational Formats)

  • 替代方案:需要多段独立 prompt 或复杂的字符串拼接来管理会话。
python 复制代码
system_prompt = "You are a helpful assistant."
user_prompt = "What is {{ language }}?"
assistant_response = "{{ language }} is a programming language..."
followup = "Can you give an example of {{ language }}?"
# 手工管理会话流
from langchain.prompts import PromptTemplate
template_user = PromptTemplate.from_template(user_prompt)
template_assistant = PromptTemplate.from_template(assistant_response)
template_followup = PromptTemplate.from_template(followup)
template_messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": template_user},
    {"role": "ai", "content": template_assistant},
    {"role": "user", "content": template_followup},
]
  • POML:一个文件描述整个 AI 会话。
xml 复制代码
<poml>
  <system-msg>You are a helpful assistant.</system-msg>
  <human-msg>What is {{ language }}?</human-msg>
  <ai-msg>{{ language }} is a high-level, interpreted programming language...</ai-msg>
  <human-msg>Can you give an example of {{ language }}?</human-msg>
</poml>

使用场景

  • 官方页面未提供具体使用场景示例。

优势亮点(基于页面表述)

  • 与 LangChain 的无缝集成,LangchainPomlTemplate 可直接用于链式调用。
  • 可作为 Jinja2/f-string 的替代:除插值外,支持结构化组件与逻辑流。
  • 富内容与文件引入:例如直接引用文档(如 PDF),无需手工解析。
  • 结构化标签便于组织 few-shot 示例,并直接得到结构化会话消息。
  • 支持对话格式:可在同一 POML 文件中表达完整会话。
  • 注意:页面指出部分 POML 特性可能与 LangChain 不兼容(例如工具使用)。

未来集成(Future Integrations)

  • LangSmith:面向 LangChain 的调试与监控平台
  • Langfuse:用于 LLM 可观测性与分析

总结/展望

  • 本页展示了 POML 在 LangChain 中的集成方式:安装、基础用法、两种模板模式,以及与其他模板方法的对比示例。
  • 如需完整细节与最新示例,请参阅官方页面:
相关推荐
在未来等你1 天前
AI Agent设计模式 Day 5:Reflexion模式:自我反思与持续改进
设计模式·llm·react·ai agent·plan-and-execute
智泊AI1 天前
为什么需要垂直领域的SFT微调?垂直领域SFT微调怎么做?
llm
大千AI助手1 天前
PPT: Pre-trained Prompt Tuning - 预训练提示调优详解
人工智能·神经网络·llm·prompt·ppt·大千ai助手·预训练提示调优
大模型教程1 天前
用Unsloth微调一个老中医垂直领域大模型
程序员·llm·agent
AI大模型2 天前
全网最细,Qwen3大模型极致微调与推理实战:Unsloth一站式教程
程序员·llm·agent
AI大模型2 天前
Unsloth 的全微调之路丨从 Adapter 到 Full Fine-tuning
程序员·llm·agent
菠菠萝宝2 天前
【AI应用探索】-7- LLaMA-Factory微调模型
人工智能·深度学习·大模型·llm·nlp·attention·llama
数据智能老司机2 天前
Spring AI 实战——提交用于生成的提示词
spring·llm·ai编程
数据智能老司机2 天前
Spring AI 实战——评估生成结果
spring·llm·ai编程
大模型教程2 天前
从 RAG 到 CAG:AI 正在超越“检索”,学会“融会贯通”!
程序员·llm·agent