LangChain使用Prompt02

1.设置提示

python 复制代码
from langchain.prompts import ChatPromptTemplate
prompt_template = ChatPromptTemplate.from_messages(
    [
        ("system", "你是一位专业的翻译,能够将{input_language}翻译成{output_language},并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本,不要有任何其它内容。"),
        ("human", "文本:{text}\n语言风格:{style}"),
    ]
)

2.输出提示模板

python 复制代码
prompt_template.input_variables

['input_language', 'output_language', 'style', 'text']

3.提示中输入值

python 复制代码
prompt_value = prompt_template.invoke({"input_language": "英语", "output_language": "汉语", 
                                       "text":"I'm so hungry I could eat a horse", "style": "文言文"})
prompt_value

ChatPromptValue(messages=[SystemMessage(content='你是一位专业的翻译,能够将英语翻译成汉语,并且输出文本会根据用户要求的任何语言风格进行调整。请只输出翻译后的文本,不要有任何其它内容。'), HumanMessage(content="文本:I'm so hungry I could eat a horse\n语言风格:文言文")])

4.取出提示

python 复制代码
prompt_value.messages

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

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

5.输入模型

python 复制代码
model = ChatOpenAI(model="gpt-3.5-turbo",base_url="https://api.chatanywhere.tech/v1")
response = model.invoke(prompt_value)

6.获得结果

python 复制代码
response

AIMessage(content='吾今飢極,如欲食馬也。', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 17, 'prompt_tokens': 109, 'total_tokens': 126, 'completion_tokens_details': {'reasoning_tokens': 0}}, 'model_name': 'gpt-3.5-turbo-0125', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-9d752bd9-468e-4fe1-9ff9-e379561c957b-0', usage_metadata={'input_tokens': 109, 'output_tokens': 17, 'total_tokens': 126})

7.取出结果

python 复制代码
response.content

'吾今飢極,如欲食馬也。'

8.多行示例

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": "口语"
    }
]

10.输入模型

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

输出:

吾今饥饱难忍,实有食马之心也。

I am sorry for what thou hast done.

Oggi tempo fantastico

お前本当にイライラするな

相关推荐
MickeyCV16 分钟前
Nginx学习笔记:常用命令&端口占用报错解决&Nginx核心配置文件解读
前端·nginx
祈澈菇凉33 分钟前
webpack和grunt以及gulp有什么不同?
前端·webpack·gulp
zy01010140 分钟前
HTML列表,表格和表单
前端·html
初辰ge43 分钟前
【p-camera-h5】 一款开箱即用的H5相机插件,支持拍照、录像、动态水印与样式高度定制化。
前端·相机
HugeYLH1 小时前
解决npm问题:错误的代理设置
前端·npm·node.js
六个点2 小时前
DNS与获取页面白屏时间
前端·面试·dns
道不尽世间的沧桑2 小时前
第9篇:插槽(Slots)的使用
前端·javascript·vue.js
bin91532 小时前
DeepSeek 助力 Vue 开发:打造丝滑的滑块(Slider)
前端·javascript·vue.js·前端框架·ecmascript·deepseek
uhakadotcom2 小时前
最新发布的Tailwind CSS v4.0提供了什么新能力?
前端
GISer_Jing2 小时前
Node.js中如何修改全局变量的几种方式
前端·javascript·node.js