Langchain入门到实战-第四弹

本文介绍了Langchain,一个用于利用大型语言模型构建应用的框架,涉及PromptTemplate、ChatPromptTemplate等工具,展示了如何创建和处理提示模板,以及其在实际场景中的用法和更新计划。
摘要由CSDN通过智能技术生成

Langchain中的提示词

  • 语言模型提示模板是预定义的生成语言模型提示的方法。
  • 模板可能包括指令、少样本示例、特定任务的上下文和问题。
  • LangChain 提供了创建和处理提示模板的工具。
  • LangChain 致力于创建模型不可知的模板,以便轻松地跨不同的语言模型重用现有的模板。
  • 通常,语言模型expects 提示要么是字符串,要么是聊天消息列表。

官网地址

声明: 由于操作系统, 版本更新等原因, 文章所列内容不一定100%复现, 还要以官方信息为准

https://python.langchain.com/

Langchain概述

LangChain是一个用于开发由大型语言模型(LLM)驱动的应用程序的框架。

Langchain的提示词用法

  • PromptTemplate: 默认情况下,PromptTemplate 使用 Python 的 str.format 语法进行模板化。

    from langchain_core.prompts import PromptTemplate
    
    prompt_template = PromptTemplate.from_template(
        "Tell me a {adjective} joke about {content}."
    )
    prompt_template.format(adjective="funny", content="chickens")
    

在这里插入图片描述

  • ChatPromptTemplate: 聊天模型的提示是一个聊天消息列表。每个聊天消息都与内容关联,并有一个额外的参数称为角色。例如,在OpenAI聊天完成API中,一个聊天消息可以与AI助手、人类或系统角色关联。

    from langchain_core.prompts import ChatPromptTemplate
    
    chat_template = ChatPromptTemplate.from_messages(
        [
            ("system", "You are a helpful AI bot. Your name is {name}."),
            ("human", "Hello, how are you doing?"),
            ("ai", "I'm doing well, thanks!"),
            ("human", "{user_input}"),
        ]
    )
    
    messages = chat_template.format_messages(name="Bob", user_input="What is your name?")
    messages
    

在这里插入图片描述

  • Message Prompts: 提供了不同类型的MessagePromptTemplate
    • AIMessagePromptTemplate
    • SystemMessagePromptTemplate
    • HumanMessagePromptTemplate
    • 任意指定角色的PromptTemplate
    from langchain_core.prompts import ChatMessagePromptTemplate
    
    prompt = "May the {subject} be with you"
    
    chat_message_prompt = ChatMessagePromptTemplate.from_template(
        role="Jedi", template=prompt
    )
    chat_message_prompt.format(subject="force")
    

在这里插入图片描述

  • MessagesPlaceholder: 完全控制格式化期间要呈现的消息

    from langchain_core.prompts import (
        ChatPromptTemplate,
        HumanMessagePromptTemplate,
        MessagesPlaceholder,
    )
    
    human_prompt = "Summarize our conversation so far in {word_count} words."
    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="What is the best way to learn programming?")
    ai_message = AIMessage(
        content="""\
    1. Choose a programming language: Decide on a programming language that you want to learn.
    
    2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.
    
    3. Practice, practice, practice: The best way to learn programming is through hands-on experience\
    """
    )
    
    chat_prompt.format_prompt(
        conversation=[human_message, ai_message], word_count="10"
    ).to_messages()
    

在这里插入图片描述

  • LCEL
    • PromptTemplate 入参是字典, 返回值是StringPromptValue
    • ChatPromptTemplate 入参是字典, 返回值是ChatPromptValue
    from langchain_core.prompts import PromptTemplate
    prompt_template = PromptTemplate.from_template(
        "Tell me a {adjective} joke about {content}."
    )
    
    prompt_val = prompt_template.invoke({"adjective": "funny", "content": "chickens"})
    prompt_val
    

在这里插入图片描述

from langchain_core.messages import SystemMessage
from langchain_core.prompts import ChatPromptTemplate
chat_template = ChatPromptTemplate.from_messages(
    [
        SystemMessage(
            content=(
                "You are a helpful assistant that re-writes the user's text to "
                "sound more upbeat."
            )
        ),
        HumanMessagePromptTemplate.from_template("{text}"),
    ]
)

chat_val = chat_template.invoke({"text": "i dont like eating tasty things."})

在这里插入图片描述

更新计划

欲知后事如何, 请听下回分解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值