Langchain入门到实战-第四弹

Langchain入门到实战

Langchain中的提示词

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

官网地址

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

bash 复制代码
https://python.langchain.com/

Langchain概述

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

Langchain的提示词用法

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

    bash 复制代码
    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助手、人类或系统角色关联。

    bash 复制代码
    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
    bash 复制代码
    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: 完全控制格式化期间要呈现的消息

    bash 复制代码
    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
    bash 复制代码
    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
bash 复制代码
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."})

更新计划

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

相关推荐
我爱学Python!20 小时前
基于 LangChain 的自动化测试用例的生成与执行
人工智能·自然语言处理·langchain·自动化·llm·测试用例·大语言模型
贪玩懒悦2 天前
用langchain+streamlit应用RAG实现个人知识库助手搭建
人工智能·ai·语言模型·langchain·aigc
AI-智能2 天前
《开源大模型食用指南》,一杯奶茶速通大模型!新增Examples最佳实践!
人工智能·自然语言处理·langchain·开源·prompt·ai大模型
AI知识分享官5 天前
大模型增量训练--基于transformer制作一个大模型聊天机器人
人工智能·深度学习·算法·数据挖掘·langchain·机器人·transformer
weijie.zwj8 天前
LLM基础概念:Prompt
人工智能·python·langchain
玩转AI大模型8 天前
AI产品经理学习路径:从零基础到精通,从此篇开始!
人工智能·学习·语言模型·自然语言处理·langchain·transformer·产品经理
高垚淼9 天前
如何构建智能应用:深入探索Langchain的强大功能与应用潜力
人工智能·python·langchain
AI-入门9 天前
AI大模型:是走向新的巅峰还是陷入发展的僵局?
数据库·人工智能·缓存·langchain·prompt·agi
weijie.zwj9 天前
11. LCEL:LangChain Expression Language
人工智能·langchain
weijie.zwj10 天前
9. LangChain流式输出
人工智能·python·langchain