当前位置: 首页 > article >正文

langchain 提示词(一) 字符提示词和聊天提示词

1,简介

‌LangChain中的提示词(Prompts)‌是用户与模型交互的方式,即模型的输入,通过提示词可以让模型返回用户期望的内容。提示词在LLM(Large Language Models,大语言模型)领域中扮演着关键角色,它不仅是模型接收信息的入口,也是模型生成输出的起点。
提示词也是用于生成语言模型提示词的预定义脚本。语言模型能够接受的提示词是字符串或聊天消息列表。
提示词中提供了两个提示词模版:
PromptTemplate:字符提示词模版,模版支持任意数量的变量,包括无边量。
ChatPromptTemplate:聊天提示词模版,聊天模版的提示是一个聊天消息列表。每条聊天消息都是有角色和内容组成。

2,代码示例

字符提示词PromptTemplate

from langchain.prompts import PromptTemplate

#有变量
prompt_template = PromptTemplate.from_template(
    "给我讲一个关于{content}的{adjective}笑话。"
)
rep = prompt_template.format(adjective="有意思的", content="猫")
print(rep)
#给我讲一个关于猫的有意思的笑话。

#无变量
prompt_template = PromptTemplate.from_template(
    "给我讲个笑话"
)
rep = prompt_template.format()
print(rep)
#给我讲个笑话

聊天提示词ChatPromptTemplate

from langchain_core.prompts import ChatPromptTemplate

chat_template = ChatPromptTemplate.from_messages(
    [
        ("system", "你是一个乐于助人的人工智能机器人。你的名字是{name}"),
        ("human", "你好你最近怎么样?"),
        ("ai", "我很好,谢谢!"),
        ("human", "{user_imput}"),
    ]
)
messages = chat_template.format_messages(name="王老师", user_imput="你叫什么名字?")
print(messages)

#[SystemMessage(content='你是一个乐于助人的人工智能机器人。你的名字是王老师'), HumanMessage(content='你好你最近怎么样?'), AIMessage(content='我很好,谢谢!'), HumanMessage(content='你叫什么名字?')]

#ChatPromptTemplate.from_messages方法接受各种消息表示形式,并且是一种使用你想要的消息格式化聊天模型输入的便捷方法。
from langchain_core.messages import SystemMessage
from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate

chat_template = ChatPromptTemplate.from_messages(
    [
        SystemMessage(
            content=(
                "你是一位有用的助手,可以重写用户的文本,使其听起来更乐观。"
            )
        ),
        HumanMessagePromptTemplate.from_template("{text}"),
    ]
)
messages = chat_template.format_messages(text="我不喜欢吃好的东西")
print(messages)
#[SystemMessage(content='你是一位有用的助手,可以重写用户的文本,使其听起来更乐观。'), HumanMessage(content='我不喜欢吃好的东西')]

消息占位符MessagesPlaceholder
消息占位符通过字面意思,就是占为作用,比如,现在要对之前说过的聊天对话进行总结。之前有多少对话目前不太确定,没有统计。那么这种情况就可以用消息占位符,先进行占位,在将之前的聊天记录取出,在进行总结。

from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)

human_prompt = "用 {word_count} 字总结我们到今为止的对话。"
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)

chat_prompt = ChatPromptTemplate.from_messages([MessagesPlaceholder(variable_name="conversation"),human_message_template])

from langchain_core.messages import AIMessage,HumanMessage

human_message = HumanMessage(content="学习编程的最佳方法是什么?")
ai_message = AIMessage(
    content="""\
    1. 选择编程语言:决定你想要学习的编程语言。
    2. 从基础开始:熟悉变量、数据类型和控制结构等基本编程概念。
    3. 练习、练习、再练习:学习编程的最好方法是通过实践检验\
    """
)

rep = chat_prompt.format_prompt(
    conversation=[human_message,ai_message], word_count="10"
).to_messages()
print(rep)
#[HumanMessage(content='学习编程的最佳方法是什么?'), AIMessage(content='    1. 选择编程语言:决定你想要学习的编程语言。\n    2. 从基础开始:熟悉变量、数据类型和控制结构等基本编程概念。\n    3. 练习、练习、再练习:学习编程的最好方法是通过实践检验    '), HumanMessage(content='用 10 字总结我们到今为止的对话。')]


http://www.kler.cn/news/318963.html

相关文章:

  • 类似QQ聊天功能的Java程序
  • Linux —— Socket编程(一)
  • 叉车防撞报警系统解决方案:提高仓库、保障员工的安全性
  • 零基础学Axios
  • 每天学习一个技术栈 ——【Celery】篇(1)
  • C++20-协程
  • 短视频矩阵管理系统贴牌 源码开发
  • 数据库某字段要保存中文时,怎样确定长度(以Oracle为例)
  • 神经网络(四):UNet语义分割网络
  • 走向管理岗,必须懂这13个人才管理铁律
  • 详解机器学习经典模型(原理及应用)——岭回归
  • 一场大模型面试,三个小时,被撞飞了
  • MODELS 2024震撼续章:科技与可持续性的未来交响曲
  • MES系统如何提升制造企业的运营效率和灵活性
  • [6]Opengl ES预览摄像头
  • 徐州网站建设的最新趋势与技术
  • 无人机之编程基础原理
  • 接口测试Postman关联,断言,前置,参数化用法
  • 《AI设计类工具系列之三——Magic Design》
  • 程序设计中,day 与 date 这2个单词的区别
  • Unity的Text组件中实现输入内容的渐变色效果
  • Scanner流程控制语句
  • 【kafka-03】springboot整合kafka以及核心参数详解
  • 代码随想录Day17 图论-2
  • PyCharm 的安装和配置
  • 《机器学习》周志华-CH8(集成学习)
  • yolov8环境安装
  • apache paimon简介(官翻)
  • 【第十三章:Sentosa_DSML社区版-机器学习之聚类】
  • 共享单车轨迹数据分析:以厦门市共享单车数据为例(六)