LangChain中使用Prompt01

1.引入提示模板

python 复制代码
from langchain.prompts import (
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

2.设置系统提示

python 复制代码
system_template_text="你是一位专业的翻译,能够将{input_language}翻译成{output_language},并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本,不要有任何其它内容。"
system_prompt_template = SystemMessagePromptTemplate.from_template(system_template_text)

3.输出系统提示

python 复制代码
system_prompt_template

SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input_language', 'output_language'], template='你是一位专业的翻译,能够将{input_language}翻译成{output_language},并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本,不要有任何其它内容。'))

4.输出系统提示参数值

python 复制代码
system_prompt_template.input_variables

输出:['input_language', 'output_language']

5.设置用户提示

python 复制代码
human_template_text="文本:{text}\n语言风格:{style}"
human_prompt_template = HumanMessagePromptTemplate.from_template(human_template_text)

6.输出用户提示

python 复制代码
human_prompt_template.input_variables

输出:['style', 'text']

7.为系统提示输入值并输出

python 复制代码
system_prompt = system_prompt_template.format(input_language="英语", output_language="汉语")
system_prompt

输出:SystemMessage(content='你是一位专业的翻译,能够将英语翻译成汉语,并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本,不要有任何其它内容。')

8.为用户提示设置值并输出

python 复制代码
human_prompt = human_prompt_template.format(text="I'm so hungry I could eat a horse", style="文言文")
human_prompt

输出:HumanMessage(content="文本:I'm so hungry I could eat a horse\n语言风格:文言文")

9.将提示输入模型

from langchain_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-3.5-turbo",base_url="https://api.chatanywhere.tech/v1")

response = model.invoke([

system_prompt,

human_prompt

])

10.输出结果

python 复制代码
print(response.content)

输出:吾今飢甚,可食馬矣。

11.多个示例输入

python 复制代码
input_variables = [
    {
        "input_language": "英语",
        "output_language": "汉语",
        "text": "I'm so hungry I could eat a horse",
        "style": "文言文"
    },
    {
        "input_language": "法语",
        "output_language": "英语",
        "text": "Je suis désolé pour ce que tu as fait",
        "style": "古英语"
    },
    {
        "input_language": "俄语",
        "output_language": "意大利语",
        "text": "Сегодня отличная погода",
        "style": "网络用语"
    },
    {
        "input_language": "韩语",
        "output_language": "日语",
        "text": "너 정말 짜증나",
        "style": "口语"
    }
]

12.输出

python 复制代码
for input in input_variables:
    response = model.invoke([
        system_prompt_template.format(input_language=input["input_language"], output_language=input["output_language"]), 
        human_prompt_template.format(text=input["text"], style=input["style"])])
    print(response.content)

输出:

吾今飢甚,可食馬也。

I am sorry for what thou hast done

Oggi il tempo è fantastico.

お前、マジでイライラするな。

相关推荐
MoSTChillax1 小时前
用 Figma Make 提示词生成可交互应用原型:从入门到避坑
prompt·figma·原型设计·产品原型·figma make
小芳矶2 小时前
【langchain框架—组合链】利用组合链完成客服优先等级的划分
langchain
有点笨的蛋3 小时前
LangChain 入门与实践:从 LLM 调用到 AI 工作流的工程化思维
前端·langchain
工藤学编程3 小时前
AI Ping 赋能:基于 GLM-4.7(免费!)+ LangChain + Redis 打造智能AI聊天助手
人工智能·redis·langchain
semantist@语校4 小时前
第五十七篇|东京银星日本语学校的数据建模:高密度城市中的学习节律、制度边界与 Prompt 接口设计
大数据·数据库·人工智能·学习·百度·prompt·知识图谱
啊吧怪不啊吧4 小时前
新品限免|国产大模型工程化实战:GLM-4.7与MiniMax M2.1 免费选型对比
人工智能·机器学习·langchain
闻道且行之5 小时前
NLP 部署实操:Langchain-Chatchat 完整部署教程与踩坑记录
人工智能·自然语言处理·langchain
MoSTChillax5 小时前
Figma Make 提示词设计:从布局、组件、交互到风格
prompt·figma·原型设计·figma make
xhxxx17 小时前
别再让 AI 自由发挥了!用 LangChain + Zod 强制它输出合法 JSON
前端·langchain·llm
San30.20 小时前
从零到一:开启 LangChain 的 AI 工程化之旅
人工智能·langchain·node.js