微信扫码
添加专属顾问
我要投稿
from langchain import PromptTemplatetemplate = "客户问题:{question}\n请提供详细的解答:"prompt = PromptTemplate.from_template(template)input_variables = {"question": "如何重置我的密码?"}print(prompt.format(input_variables))
from langchain import PromptTemplateprompt_template = PromptTemplate(input_variables=["topic"],template="请您分享对{topic}的看法。")print(prompt_template.format(topic="新产品功能"))
from jinja2 import Templatetemplate = Template("请撰写一篇关于{{ topic }}的文章,重点讨论{{ aspect }}。")prompt = template.render(topic="人工智能", aspect="未来发展")print(prompt)
from langchain import ChatPromptTemplatechat_prompt = ChatPromptTemplate.from_messages([{"role": "system", "content": "你是一名帮助用户的助手。"},{"role": "user", "content": "你能告诉我今天的天气吗?"}])messages = chat_prompt.format_messages()print(messages)
from langchain import FewShotPromptTemplateexamples = [{"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, abstractmethodfrom typing import Dict, Listclass 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+中大型企业
2026-01-29
自建一个 Agent 很难吗?一语道破,万语难明
2026-01-28
全球首个Skills Vibe Agents,AtomStorm技术揭秘:我是怎么用Context Engineering让Agent不"变傻"的
2026-01-22
Deepagents落地场景来了:用openwork实现专属办公小管家
2026-01-05
快速上手:LangChain + AgentRun 浏览器沙箱极简集成指南
2026-01-05
为什么大模型企业都在强调可以连续工作XX小时的Agent和模型?长时运行Agent解析(Long-Running Agents)
2025-12-29
单agent落幕,双agent才能解决复杂问题!附LangGraph+Milvus实操
2025-12-28
为什么说LangGraph是企业级AI智能体的「终极答案」?
2025-12-26
跟我学LangChain:提示词模板,PromptTemplate包装器,工程化管理你的提示词
2025-11-06
2025-11-05
2025-12-21
2025-12-21
2025-11-25
2025-11-08
2025-12-08
2025-11-18
2025-11-07
2025-11-25
2025-11-03
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21