Langchain入门到实战-第四弹

news/2024/9/23 7:34:13/

Langchain入门到实战

  • Langchain中的提示词
    • 官网地址
    • Langchain概述
    • Langchain的提示词用法
    • 更新计划

Langchain中的提示词

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

官网地址

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

https://python.langchain.com/

Langchain概述

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

Langchain的提示词用法

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

    from langchain_core.prompts import PromptTemplateprompt_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 ChatPromptTemplatechat_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 ChatMessagePromptTemplateprompt = "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, HumanMessagehuman_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."})

在这里插入图片描述

更新计划

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


http://www.ppmy.cn/news/1431644.html

相关文章

什么是SSRF攻击?该如何防御SSRF攻击?

随着网络安全形式日益严峻,各式各样的攻击频繁发生。当前,应用程序为了给用户提供更多更方便的功能,从另一个URL获取数据的场景越来越多,因此出现了一种安全漏洞攻击-SSRF。并且,由于云服务和体系结构的复杂性&#xf…

Java单例模式的五种实现方式 懒汉式 饿汉式 双重校验锁 静态变量 静态内部类 枚举实现单例模式等

1、什么是单例模式? Java单例模式是一种设计模式,用于确保一个类只有一个实例,并提供全局访问点以获取该实例。它通常用于需要共享资源或控制某些共享状态的情况下。 2、实现方式 懒汉式:在类加载的时候就创建对象,…

c++模拟实现list——详讲双链表--链表

在C语言中我们已经模拟实现了list,现在对比c看看二者的区别 双链表————详讲 个人博客主页: 个人主页 个人码云 码云代码 文章目录 目录 文章目录 ​编辑 前言 一、list是什么? 二、list的使用 三、模拟实现list和搭建list的结构 1.节点结…

Qt 集成VTK

Qt hello | 专注于Qt的技术分享平台 一,需求集成VTK环境到Qt中 二,步骤 下载vtk源码,并使用cmake 结合vs2019编辑成dll库。GitHub - Kitware/VTK: Mirror of Visualization Toolkit repositoryQt工程引入,库比较多,…

java锁常识

AQS框架 AQS(AbstractQueuedSynchronizer)是 Java 中用于构建锁和同步器的基础框架。它提供了一种实现同步器的方式,使得开发者可以基于 AQS 构建各种类型的同步工具,如独占锁、共享锁、信号量等。 AQS 主要基于 FIFO 队列&…

Java 基础:工厂方法模式

工厂方法模式(Factory Method Pattern)是一种创建型设计模式,它提供了一个创建对象的通用接口,但将实际创建逻辑推迟到子类中实现。这种模式允许客户端使用抽象接口来创建特定类型的对象,而无需了解具体的实现细节。以…

【后端】python深度学习的详细指引

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、什么是深度学习二、python深度学习的详细指引1. 确定学习目标和项目2. 学习基础知识3. 选择合适的深度学习框架4. 学习深度学习理论5. 实践项目6. 深入学习…

05_Qt资源文件添加

Qt资源文件添加 Qt 资源系统是一个跨平台的资源机制,用于将程序运行时所需要的资源以二进制的形式存储于可执行文件内部。如果你的程序需要加载特定的资源(图标、文本翻译等),那么,将其放置在资源文件中,就…