GPT实战系列-LangChain的Prompt提示模版构建

GPT实战系列-LangChain的Prompt提示模版构建

LangChain

GPT实战系列-LangChain如何构建基通义千问的多工具链

GPT实战系列-构建多参数的自定义LangChain工具

GPT实战系列-通过Basetool构建自定义LangChain工具方法

GPT实战系列-一种构建LangChain自定义Tool工具的简单方法

GPT实战系列-搭建LangChain流程简单应用

GPT实战系列-简单聊聊LangChain搭建本地知识库准备

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-简单聊聊LangChain

大模型查询工具助手之股票免费查询接口

Prompt模版是用于生成语言模型提示的预定义模版。

模板可能包括说明、小样本示例,和特定的上下文和问题(适合于特定的任务)。

LangChain提供创建和使用提示模板的工具,其实也没有做太多的工作,就是字符串格式化操作差不多。模版与模型无关,使其适应在不同的语言模型中重复使用。

通常,语言模型的输入,通常是字符串或聊天消息列表。

Prompt模版

用于为字符串提示创建模板。PromptTemplate

默认情况下,PromptTemplate使用 Python 的 用于模板的 str.format 语法,一种字符替换的格式。

python 复制代码
from langchain.prompts import PromptTemplate

prompt_template = PromptTemplate.from_template(
    "Tell me a {adjective} joke about {content}."
)
prompt_template.format(adjective="funny", content="chickens")
text 复制代码
'Tell me a funny joke about chickens.'

该模板支持任意数量的变量,包括无变量:

python 复制代码
from langchain.prompts import PromptTemplate

prompt_template = PromptTemplate.from_template("Tell me a joke")
prompt_template.format()
text 复制代码
'Tell me a joke'

因此,您可以创建任意的自定义提示模板,以任何方式设置提示的格式。

聊天对话模版ChatPromptTemplate

通常,大语言模型(LLM)的应用模型是聊天模型,它的提示是聊天消息列表。

每条聊天消息都与内容相关联,并且其他 参数调用 。例如,在 OpenAI 聊天完成中 API,聊天 消息可以与 AI 助手、人类或系统相关联 角色。

创建一个聊天提示模板,如下所示:

python 复制代码
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?")

ChatPromptTemplate.from_messages 就是接受各种消息输入。

例如,除了使用 (type, content),则可以传入 or 的实例。MessagePromptTemplate``BaseMessage

python 复制代码
from langchain.prompts import HumanMessagePromptTemplate
from langchain_core.messages import SystemMessage
from langchain_openai import ChatOpenAI

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}"),
    ]
)
messages = chat_template.format_messages(text="I don't like eating tasty things")
print(messages)
text 复制代码
[SystemMessage(content="You are a helpful assistant that re-writes the user's text to sound more upbeat."), HumanMessage(content="I don't like eating tasty things")]

其实就是做了简单的封装,提供一些灵活性,来构建您的 聊天提示。

LangChain是一个Python框架,可以使用LLMs构建应用程序。它与各种模块连接,使与LLM和提示管理,一切变得简单。

觉得有用 收藏 收藏 收藏

点个赞 点个赞 点个赞

End

GPT专栏文章:

GPT实战系列-实战Qwen通义千问在Cuda 12+24G部署方案_通义千问 ptuning-CSDN博客

GPT实战系列-ChatGLM3本地部署CUDA11+1080Ti+显卡24G实战方案

GPT实战系列-Baichuan2本地化部署实战方案

GPT实战系列-让CodeGeeX2帮你写代码和注释_codegeex 中文-CSDN博客

GPT实战系列-ChatGLM3管理工具的API接口_chatglm3 api文档-CSDN博客

GPT实战系列-大话LLM大模型训练-CSDN博客

GPT实战系列-LangChain + ChatGLM3构建天气查询助手

GPT实战系列-大模型为我所用之借用ChatGLM3构建查询助手

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(二)

GPT实战系列-P-Tuning本地化训练ChatGLM2等LLM模型,到底做了什么?(一)

GPT实战系列-ChatGLM2模型的微调训练参数解读

GPT实战系列-如何用自己数据微调ChatGLM2模型训练

GPT实战系列-ChatGLM2部署Ubuntu+Cuda11+显存24G实战方案

GPT实战系列-Baichuan2等大模型的计算精度与量化

GPT实战系列-GPT训练的Pretraining,SFT,Reward Modeling,RLHF

GPT实战系列-探究GPT等大模型的文本生成-CSDN博客

相关推荐
玲小珑3 小时前
LangChain.js 完全开发手册(七)RAG(检索增强生成)架构设计与实现
前端·langchain·ai编程
虫无涯4 小时前
LangChain中的Prompt模板如何使用?
服务器·langchain·prompt
今天也要学习吖12 小时前
谷歌nano banana官方Prompt模板发布,解锁六大图像生成风格
人工智能·学习·ai·prompt·nano banana·谷歌ai
ChinaRainbowSea14 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
zzywxc78716 小时前
AI在金融、医疗、教育、制造业等领域的落地案例
人工智能·机器学习·金融·prompt·流程图
AIGC小火龙果1 天前
OpenAI的开源王牌:gpt-oss上手指南与深度解析
人工智能·经验分享·gpt·搜索引擎·aigc·ai编程
精灵vector1 天前
LLMCompiler:基于LangGraph的并行化Agent架构高效实现
人工智能·python·langchain
*星星之火*1 天前
【GPT入门】第66 课 llamaIndex调用远程llm模型与embedding模型的方法
gpt·embedding
golang学习记1 天前
Claude Code 平替:OpenAI发布 Codex CLI ,GPT-5 国内直接使用
gpt
z千鑫1 天前
【OpenAI】性价比极高的轻量级多模态模型GPT-4.1-mini介绍 + API KEY的使用教程!
人工智能·gpt·ai·语言模型·chatgpt