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.

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

相关推荐
你那个道上的15 小时前
LangChain4j学习与实践
langchain·ai编程
衣乌安、15 小时前
如何写高效的Prompt?
人工智能·prompt
缘友一世17 小时前
LangChain【8】之工具包深度解析:从基础使用到高级实践
langchain
bytebeats19 小时前
强大的代理通信其实是 A2A + MCP + LangChain
langchain·mcp
架构师那点事儿1 天前
一文带你俯瞰大模型领域的世界
langchain·aigc·ai编程
科技资讯快报1 天前
告别Prompt依赖,容联云让智能体嵌入金融业务流
大数据·人工智能·prompt
java干货1 天前
每日Prompt:云朵猫
prompt
shadowcz0071 天前
Prompt提示工程指南#Kontext图像到图像
prompt
胡耀超1 天前
大语言模型提示词(LLM Prompt)工程系统性学习指南:从理论基础到实战应用的完整体系
人工智能·python·语言模型·自然语言处理·llm·prompt·提示词
用户84913717547162 天前
🚀5 分钟实现 Markdown 智能摘要生成器:LangChain + OpenAI 实战教程
langchain·openai