LangChain(2)提示工程 Prompt Engineering

提示一般包含如下部分:
Instructions:整体结构,模型的人设
Instructions tell the model what to do, how to use external information if provided, what to do with the query, and how to construct the output.

External information:额外提供给模型的信息
External information or context(s) act as an additional source of knowledge for the model. These can be manually inserted into the prompt, retrieved via a vector database (retrieval augmentation), or pulled in via other means (APIs, calculations, etc.).

User input or query:用户输入的问题
User input or query is typically (but not always) a query input into the system by a human user (the prompter).

Output indicator:模型应该输出什么样的结果
Output indicator marks the beginning of the to-be-generated text. If generating Python code, we may use import to indicate to the model that it must begin writing Python code (as most Python scripts begin with import).

# 提示词
prompt = """Answer the question based on the context below. 
If the question cannot be answered using the information provided answer with "I don't know". 
Context: Large Language Models (LLMs) are the latest models used in NLP. Their superior performance over smaller models has made them incredibly useful for developers building NLP enabled applications. 
These models can be accessed via Hugging Face's `transformers` library, via OpenAI using the `openai` library, and via Cohere using the `cohere` library. 
Question: Which libraries and model providers offer LLMs? 
Answer: """from langchain.llms import OpenAI# initialize the models
openai = OpenAI(
model_name="text-davinci-003",
openai_api_key="YOUR_API_KEY"
)print(openai(prompt))>>>Hugging Face's `transformers` library, OpenAI using the `openai` library, and Cohere using the `cohere` library.

提示模板 PromptTemplate

可以将提示的问题抽象为参数,其它人设、额外信息、回答方式作为常数。这样就构成一个提示模板

from langchain import PromptTemplatetemplate = """Answer the question based on the context below. If the question cannot be answered using the information provided answer with "I don't know".Context: Large Language Models (LLMs) are the latest models used in NLP. Their superior performance over smaller models has made them incredibly useful for developers building NLP enabled applications. These models can be accessed via Hugging Face's `transformers` library, via OpenAI using the `openai` library, and via Cohere using the `cohere` library. Question: {query} Answer: """# 将问题作为参数,其它人设、额外信息、回答方式作为常数
prompt_template = PromptTemplate(
input_variables=["query"],
template=template
)# 只需给模板提供问题即可
print(openai(
prompt_template.format(query="Which libraries and model providers offer LLMs?")))>>>Hugging Face's `transformers` library, OpenAI using the `openai` library, and Cohere using the `cohere` library.

FewShotPromptTemplate

模型的知识主要来自两部分,一是模型在训练过程的获取的,二是输入的额外信息

FewShotPromptTemplate通过给输入添加额外信息,使得模型拥有更多知识

# 一个完整的prompt,提供类人设,回答方式、
# 但可以拆分为几部分:人设、例子、问题、回答形式
prompt = """The following are exerpts from conversations with an AI assistant. 
The assistant is typically sarcastic and witty, producing creative and funny responses to the users questions. Here are some examples: 
User: How are you? 
AI: I can't complain but sometimes I still do. 
User: What time is it? 
AI: It's time to get a watch. 
User: What is the meaning of life? 
AI: """# 通过提高temperature 值,可以使得模型输出更随意
openai.temperature = 1.0 # increase creativity/randomness of output
# 提供给模型的知识
examples = [
{
"query": "How are you?",
"answer": "I can't complain but sometimes I still do."
}, {
"query": "What time is it?",
"answer": "It's time to get a watch."
}
]# create a example template
example_template = """ User: {query} AI: {answer} """# create a prompt example from above template
example_prompt = PromptTemplate(
input_variables=["query", "answer"],
template=example_template
)# 模型人设 the prefix is our instructions
prefix = """The following are exerpts from conversations with an AI assistant. 
The assistant is typically sarcastic and witty, producing creative and funny responses to the users questions. 
Here are some examples: """
# and the suffix our user input and output indicator
suffix = """ User: {query} AI: """# now create the few shot prompt template
few_shot_prompt_template = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix=prefix,
suffix=suffix,
input_variables=["query"],
example_separator="\n\n"
)

参考:
https://www.pinecone.io/learn/series/langchain/langchain-prompt-templates/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/2901.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux之Ubuntu系列(六)用户管理 终端命令 which 查看执行命令所在的位置

提示 /etc/passwd 是用于保存用户信息的文件 可以用cat 命令查看 cat /etc/passwd/usr/bin/passwd 是用于修改用户密码的 程序 ,是程序 程序 , which 命令 可以查看执行命令所在的位置 # 输出 /bin/ls which ls # 输出 /usr/sbin/useradd which useradd…

话费电费中控搭建,api接口h5,公众号,小程序app

话费充值接口文档 接口版本:1.0 ―、引言 文档概述 本文档提供话费充值接口规范说明,提供一整套的完整的接入示例(http 接口)供商户参 考,可以帮助商户开发人员快速完成接口开发与联调,实现与话费充值系统的交易互联。 公司官网…

【算法第六天7.19】反转字符串,反转字符串||,剑指 Offer 05. 替换空格,反转字符串的单词, 左旋转字符串

链接:力扣344-反转字符串 链接:力扣541-反转字符串|| 链接:剑指 Offer 05. 替换空格 链接:力扣151- 反转字符串的单词 链接:剑指 Offer 58 - II. 左旋转字符串 链接:力扣344-反转字符串 思路&#xff…

git clone 或者是vscode clone 时遇到the remote end hung up unexpectedly

fatal: the remote end hung up unexpectedly fatal: early EOF fatal: index-pack failed使用git clone总是报错 查看原因有三种可能:要么是缓存不够,要么是网络不行,要么墙的原因。 如果是网络不行,可以配置git的最低速度和最…

C#基础--线程之Task

C#基础–线程之Task Task 是.NetFramework3.0出现的,Task里面的线程是来自于线程池 一、Task 1. 开启一个线程 Task 构造函数 传入一个无参数的 Action 委托作为参数 Task task = new Task(() =>{this.DoSomethingLong("btntask_Click1");}); task.Start();Run…

局部响应归一化LRN(Local Response Normalization)

LRN(Local Response Normalization)是一种局部响应归一化的技术,在深度学习中常用于增强模型的泛化能力和对光照、对比度等变化的鲁棒性。LRN主要用于激活函数后的归一化过程,它对局部神经元响应进行归一化,使得响应较…

C/C++ 使用 define 实现运行时函数是在哪个文件哪个函数被调用

1. 原始代码 // demo2.h #include <iostream>void testFunc(int num) {std::cout << num << std::endl; }//main.cc #include "demo2.h"void func1() { }void func2() {testFunc(24); }int main() {func1();func2();return 0; }我现在需要知道 te…

re学习(19)[ACTF新生赛2020]easyre1(UPX脱壳)

文章链接&#xff1a;BUUCTF在线评测 参考视频&#xff1a;B站 【新手教程三】小Z带你学习什么是ESP定律和什么是堆栈平衡 &#xff1f; - 『脱壳破解区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn 题解&#xff1a; 工具脱壳 key"*F\"N,\"…

MYSQL数据库-数据库的学习

MYSQL数据库-数据库的学习 MYSQL数据库一、数据库的基本概念二、常见的数据库三、MySQL数据库四、SQL操作五、SQL高级操作六、SQL函数&#xff08;SQL 拥有很多可用于计数和计算的内建函数) MYSQL数据库 一、数据库的基本概念 1、数据库的英文单词:DataBase 简称 : DB 2、什么…

C#基础--进程和线程的认识

C#基础–进程和线程的认识 一、基础概念 1. 什么是进程? 进程并不是物理的东西,是虚拟出来的,是一种概念。当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源。而一个进程又是由多个线程所组成的。是一种计算机概念,是程序在运…

【矩阵的创建与基本运算】——matlab基础

目录索引 创建矩阵&#xff1a;zeros()&#xff1a;ones&#xff1a;eye()&#xff1a;magic()&#xff1a;引号创建序列矩阵&#xff1a;linspace()&#xff1a; 加减与数乘&#xff1a;其他运算&#xff1a;指数运算&#xff1a;*exp()&#xff1a;* 点式运算&#xff1a; 创…

Fiddler抓包app(方便后端定位app调用的是那个接口?参数为何?)

一、抓http请求的包 1、设置Fiddler允许远程连接 选择Tools->Options 选择Connections选项卡&#xff0c;选中允许远程连接&#xff0c;如图所示 2、手机与电脑连接相同的wlan网络 3、手机wlan设置手动代理&#xff0c; 1&#xff09;手动代理的主机名设为电脑ip&…

centos 7升级gcc到10.5.0

目录 1、安装gcc 1.1、查看是否含有gcc及gcc版本 1.2、快速安装gcc 2、升级gcc 2.1、下载gcc源码包并解压缩 2.2、下载编译依赖项 2.3、新建gcc-bulid目录&#xff08;与gcc-10.5.0同级&#xff09;并进入该目录中 2.4、生成Makefile文件 2.5、开始编译 2.6、安装 2…

北京小厂一面30问

小厂一面30问 Redis除了存储数据外还能实现什么功能&#xff1f; 除了存储数据外&#xff0c;Redis还可以实现数据的缓存、分布式锁、消息队列、计数器等功能。Redis还支持事务、Lua脚本、发布/订阅等高级功能。 HashMap的底层原理是什么&#xff1f; HashMap是通过数组和链表…

【统计函数3】——excel常见函数

相关数据资料来源于网易 函数一览&#xff1a; rank、rand、randbetween、floor、int rank函数: 求某单元格在某区域内的排名 RANK(数值,引用区域,降序0/升序1)范围多指定&#xff1a; 分开的范围之间可用逗号隔开&#xff0c;最后再用一个小括号括起来。F4可以快速锁定行和列。…

【测试开发】测试用例的设计方法

目录 一. 测试用例的基本要素 二. 测试用例的设计方法 1. 测试用例设计的万能公式 水杯测试用例 2. 基于需求的设计方法 邮箱注册测试用例 3. 等价类方法 有效等价类和无效等价类 等价类思想设计测试用例步骤 4. 边界值方法 边界值思想设计测试用例步骤 5. 判定表方法…

Linux resin自动启动设置

1、添加启动脚本 在 /etc/init.d 添加启动脚本 vi resin #!/bin/sh #chkconfig:345 99 10 #description:auto start resin #/etc/init.d/resin export export JAVA_HOME/usr/weaver/jdk1.8.0_151 export CLASSPATH$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib exp…

Promise

Promise 什么是Promise Promise是一种异步编程的解决方案&#xff0c;可以避免回调地狱&#xff0c;使得异步操作更加简单、清晰、灵活。 为什么使用Promise function request(cb) {// 模拟网络请求let flag Math.random() < 0.5 ? true : falsesetTimeout(() > {c…

利用脚本将代码部署到测试环境(.sh)

1.将上次的部署文件备份名为xxx_bak 2.首次需要输入环境密码 3.并生成一个zip的压缩包,方便部署到线上环境 4.在终端 输入./update.sh 则可执行 文件名update.sh,放在package.json文件同级 # example: 1. ./update.sh 2. 输入服务器密码 【 如果不想每次都输入密码可以先生…

《零基础入门学习Python》第046讲:魔法方法:描述符(Property的原理)

0. 请写下这一节课你学习到的内容&#xff1a;格式不限&#xff0c;回忆并复述是加强记忆的好方式&#xff01; 常言道&#xff1a;“无规矩不成方圆”&#xff0c;讲的是万事万物的发展都要在一定的规则下去运行&#xff0c;只有遵循一定的协议去做&#xff0c;事情才能够按照…