微信扫码
添加专属顾问
我要投稿
from langchain import PromptTemplate
template = "客户问题:{question}\n请提供详细的解答:"
prompt = PromptTemplate.from_template(template)
input_variables = {"question": "如何重置我的密码?"}
print(prompt.format(input_variables))
from langchain import PromptTemplate
prompt_template = PromptTemplate(
input_variables=["topic"],
template="请您分享对{topic}的看法。"
)
print(prompt_template.format(topic="新产品功能"))
from jinja2 import Template
template = Template("请撰写一篇关于{{ topic }}的文章,重点讨论{{ aspect }}。")
prompt = template.render(topic="人工智能", aspect="未来发展")
print(prompt)
from langchain import ChatPromptTemplate
chat_prompt = ChatPromptTemplate.from_messages([
{"role": "system", "content": "你是一名帮助用户的助手。"},
{"role": "user", "content": "你能告诉我今天的天气吗?"}
])
messages = chat_prompt.format_messages()
print(messages)
from langchain import FewShotPromptTemplate
examples = [
{"input": "你好", "output": "你好,有什么可以帮你的吗?"},
{"input": "今天的天气怎么样?", "output": "今天的天气很不错,晴朗无云。"}
]
few_shot_prompt = FewShotPromptTemplate.from_examples(
examples=examples,
prefix="这是一些对话示例:",
suffix="请基于上面的示例生成你的回答:",
input_variables=["input"]
)
print(few_shot_prompt.format(input="我想知道最新的新闻"))
from abc import ABC, abstractmethod
from typing import Dict, List
class BaseExampleSelector(ABC):
"""用于选择包含在提示中的示例的接口。"""
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""根据输入选择要使用的示例。"""
pass
def parser_prompt():
# 创建一个输出解析器,用于处理带逗号分隔的列表输出
output_parser = CommaSeparatedListOutputParser()
# 获取格式化指令,该指令告诉模型如何格式化其输出
format_instructions = output_parser.get_format_instructions()
# 创建一个提示模板,它会基于给定的模板和变量来生成提示
prompt = PromptTemplate(
template="List five {subject}.\n{format_instructions}",# 模板内容
input_variables=["subject"],# 输入变量
partial_variables={"format_instructions": format_instructions}# 预定义的变量,这里我们传入格式化指令
)
_input = prompt.format(subject="ice cream flavors")
print(_input)
output = chat_llm(_input)
print(output)
def parser_date():
output_parser = DatetimeOutputParser()
template = """Answer the users question:
{question}
{format_instructions}"""
prompt = PromptTemplate.from_template(
template,
partial_variables={"format_instructions": output_parser.get_format_instructions()},
)
chain = LLMChain(prompt=prompt, llm=chat_llm)
output = chain.run("around when was bitcoin founded?")#比特币大约是何时创建的
print(output)
print(output_parser.parse(output))
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-07-29
LangGraph v0.6 重磅发布!全新 Context API 让智能体开发更简单
2025-07-25
LangChain---Memory:为AI应用赋予“记忆”
2025-07-23
Langchain学习教程一(Hello World)
2025-07-23
Langchain学习教程二(提示词模板和 LCEL 语法)
2025-07-23
Langchain 教程三(FewShotChatMessagePromptTemplate)
2025-07-23
Langchain 教程四(OutputParser)
2025-07-23
Langchain教程五(LCEL 上篇)
2025-07-23
Langchain教程六(LCEL下篇)
2025-05-06
2025-06-05
2025-05-28
2025-05-08
2025-05-28
2025-05-19
2025-06-26
2025-07-14
2025-05-19
2025-05-14
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21
2025-05-19
2025-05-08