Langchain Chat Model 和 Chat Prompt Template

0. 简介

Chat Model 不止是一个用于聊天对话的模型抽象,更重要的是提供了多角色提示能力(System,AI,Human,Function)。
Chat Prompt Template 则为开发者提供了便捷维护不同角色的提示模板与消息记录的接口。

1. 构造 ChatPromptTemplate

from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
   HumanMessagePromptTemplate,
)

import os
from dotenv import load_dotenv, find_dotenv

# 删除all_proxy环境变量
if 'all_proxy' in os.environ:
    del os.environ['all_proxy']

# 删除ALL_PROXY环境变量
if 'ALL_PROXY' in os.environ:
    del os.environ['ALL_PROXY']

_ = load_dotenv(find_dotenv())

template = (
    """You are a translation expert, proficient in various languages. \n
    Translates English to Chinese."""
)
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
print(type(system_message_prompt))
print(system_message_prompt)

human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
print(type(human_message_prompt))
print(human_message_prompt)

print("*"*40)
# 使用 System 和 Human 角色的提示模板构造 ChatPromptTemplate
chat_prompt_template = ChatPromptTemplate.from_messages(
    [system_message_prompt, human_message_prompt]
)
print(type(chat_prompt_template))
print(chat_prompt_template)

print("*"*50)
chat_prompt_prompt_value = chat_prompt_template.format_prompt(text="I love python.")
print(type(chat_prompt_prompt_value))
print(chat_prompt_prompt_value)

print("*"*60)
chat_prompt_list = chat_prompt_template.format_prompt(text="I love python.").to_messages()
print(type(chat_prompt_list))
print(chat_prompt_list)

输出:

<class 'langchain_core.prompts.chat.SystemMessagePromptTemplate'>
prompt=PromptTemplate(input_variables=[], input_types={
   }, partial_variables={
   }, template='You are a translation expert, prof
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值