微信扫码
添加专属顾问
我要投稿
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-06-13
【LangChain 团队重磅实测报告】多智能体架构揭秘:谁才是性能之王?
2025-06-13
大模型_百炼:MCP让我焦虑
2025-06-11
前端学AI之LangChain.js入门教程:实现智能对话机器人
2025-06-08
Langfuse:重新定义LLM应用开发与运维的可观测性
2025-06-08
Langgraph实战--自定义embeding
2025-06-07
为 AI Agent 铺路:深度解析下一代应用的核心基建 LangGraph
2025-06-05
智能体框架怎么选?LangChain、Dify、CrewAI、AutoGen五大框架横向对比
2025-06-04
吴恩达对谈LangChain创始人:企业构建Agen系统的核心认知!
2025-03-20
2025-03-17
2025-05-08
2025-04-18
2025-03-22
2025-05-06
2025-03-23
2025-04-13
2025-05-28
2025-03-22
2025-06-13
2025-05-21
2025-05-19
2025-05-08
2025-05-06
2025-04-22
2025-04-18
2025-03-22