0

0

聊聊如何设计一个优秀的提示应用程序?

王林

王林

发布时间:2023-04-24 18:49:09

|

1297人浏览过

|

来源于51CTO.COM

转载

在本指南中,我们将介绍一些高级和有趣的方法,我们可以使用提示工程来执行有用和更高级的任务。

请注意,此部分正在大量开发中。 主题:

  • 生成数据
  • 程序辅助语言模型
  • Python 笔记本

一、生成数据

LLM 具有强大的文本生成能力。使用有效的提示策略可以引导模型产生更好、更一致和更符合事实的响应。LLM 对于生成对运行各种实验有用的数据也特别有用。例如,我们可以使用它为情感分类器生成快速样本,如下所示:

迅速的:

Produce 10 exemplars for sentiment analysis. Examples are categorized as either positive or negative. Produce 2 negative examples and 8 positive examples. Use this format for the examples:
Q: 
A: 

输出:

Q: 我刚得到了有史以来最好的消息!
A: 积极的

Q: 我们刚刚在工作中得到了加薪!
A: 积极的

Q: 我为今天所取得的成就感到非常自豪。
A: 积极的

Q: 我度过了有史以来最好的一天!
A: 积极的

Q: 我非常期待周末的到来。
A: 积极的

Q: 我刚收到了有史以来最好的礼物!
A: 积极的

Q: 我现在非常开心。
A: 积极的

Q: 我很幸运有这么一支了不起的家庭。
A: 积极的

Q: 外面的天气很阴暗。
A: 消极的

Q: 我刚得到了一些可怕的消息。
A: 消极的

这非常有用。我们将此示例用于指南另一部分中的不同测试。

二、PAL(程序辅助语言模型)

Gao 等人 (2022)提出了一种使用 LLM 阅读自然语言问题并生成程序作为中间推理步骤的方法。创造的程序辅助语言模型 (PAL) 与思维链提示的不同之处在于,它不是使用自由格式的文本来获得解决方案,而是将解决方案步骤卸载到编程运行时,例如 Python 解释器。


我们一起聊聊提示应用程序


让我们看一个使用 LangChain 和 OpenAI GPT-3 的例子。我们有兴趣开发一个简单的应用程序,该应用程序能够解释所提出的问题并通过利用 Python 解释器提供答案。

具体来说,我们有兴趣创建一个函数,允许使用 LLM 来回答需要理解日期的问题。我们将为 LLM 提供提示,其中包括从此处采用的一些范例。

这些是我们需要的导入:

import openai
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from langchain.llms import OpenAI
from dotenv import load_dotenv

让我们首先配置一些东西:

load_dotenv()

API configuration

openai.api_key = os.getenv("OPENAI_API_KEY")

for LangChain

os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")

设置模型实例:

llm = OpenAI(model_name='text-davinci-003', temperature=0)

设置提示+问题:

question = "Today is 27 February 2023. I was born exactly 25 years ago. What is the date I was born in MM/DD/YYYY?"

DATE_UNDERSTANDING_PROMPT = """
# Q: 2015 is coming in 36 hours. What is the date one week from today in MM/DD/YYYY?

# If 2015 is coming in 36 hours, then today is 36 hours before.

today = datetime(2015, 1, 1) - relativedelta(hours=36)

# One week from today,

one_week_from_today = today + relativedelta(weeks=1)

# The answer formatted with %m/%d/%Y is

one_week_from_today.strftime('%m/%d/%Y')

# Q: The first day of 2019 is a Tuesday, and today is the first Monday of 2019. What is the date today in MM/DD/YYYY?

# If the first day of 2019 is a Tuesday, and today is the first Monday of 2019, then today is 6 days later.

today = datetime(2019, 1, 1) + relativedelta(days=6)

# The answer formatted with %m/%d/%Y is

today.strftime('%m/%d/%Y')

# Q: The concert was scheduled to be on 06/01/1943, but was delayed by one day to today. What is the date 10 days ago in MM/DD/YYYY?

# If the concert was scheduled to be on 06/01/1943, but was delayed by one day to today, then today is one day later.

today = datetime(1943, 6, 1) + relativedelta(days=1)

# 10 days ago,

ten_days_ago = today - relativedelta(days=10)

# The answer formatted with %m/%d/%Y is

ten_days_ago.strftime('%m/%d/%Y')

# Q: It is 4/19/1969 today. What is the date 24 hours later in MM/DD/YYYY?

# It is 4/19/1969 today.

today = datetime(1969, 4, 19)

# 24 hours later,

later = today + relativedelta(hours=24)

# The answer formatted with %m/%d/%Y is

today.strftime('%m/%d/%Y')
# Q: Jane thought today is 3/11/2002, but today is in fact Mar 12, which is 1 day later. What is the date 24 hours later in MM/DD/YYYY?

# If Jane thought today is 3/11/2002, but today is in fact Mar 12, then today is 3/1/2002.

today = datetime(2002, 3, 12)

# 24 hours later,

later = today + relativedelta(hours=24)

# The answer formatted with %m/%d/%Y is

later.strftime('%m/%d/%Y')

# Q: Jane was born on the last day of Feburary in 2001. Today is her 16-year-old birthday. What is the date yesterday in MM/DD/YYYY?

# If Jane was born on the last day of Feburary in 2001 and today is her 16-year-old birthday, then today is 16 years later.

today = datetime(2001, 2, 28) + relativedelta(years=16)

# Yesterday,

yesterday = today - relativedelta(days=1)

# The answer formatted with %m/%d/%Y is

yesterday.strftime('%m/%d/%Y')

# Q: {question}

""".strip() + 'n'
llm_out = llm(DATE_UNDERSTANDING_PROMPT.format(question=question))
print(llm_out)
exec(llm_out)
print(born)

这将输出以下内容:02/27/1998

相关专题

更多
python开发工具
python开发工具

php中文网为大家提供各种python开发工具,好的开发工具,可帮助开发者攻克编程学习中的基础障碍,理解每一行源代码在程序执行时在计算机中的过程。php中文网还为大家带来python相关课程以及相关文章等内容,供大家免费下载使用。

709

2023.06.15

python打包成可执行文件
python打包成可执行文件

本专题为大家带来python打包成可执行文件相关的文章,大家可以免费的下载体验。

625

2023.07.20

python能做什么
python能做什么

python能做的有:可用于开发基于控制台的应用程序、多媒体部分开发、用于开发基于Web的应用程序、使用python处理数据、系统编程等等。本专题为大家提供python相关的各种文章、以及下载和课程。

737

2023.07.25

format在python中的用法
format在python中的用法

Python中的format是一种字符串格式化方法,用于将变量或值插入到字符串中的占位符位置。通过format方法,我们可以动态地构建字符串,使其包含不同值。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

616

2023.07.31

python教程
python教程

Python已成为一门网红语言,即使是在非编程开发者当中,也掀起了一股学习的热潮。本专题为大家带来python教程的相关文章,大家可以免费体验学习。

1235

2023.08.03

python环境变量的配置
python环境变量的配置

Python是一种流行的编程语言,被广泛用于软件开发、数据分析和科学计算等领域。在安装Python之后,我们需要配置环境变量,以便在任何位置都能够访问Python的可执行文件。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

547

2023.08.04

python eval
python eval

eval函数是Python中一个非常强大的函数,它可以将字符串作为Python代码进行执行,实现动态编程的效果。然而,由于其潜在的安全风险和性能问题,需要谨慎使用。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

573

2023.08.04

scratch和python区别
scratch和python区别

scratch和python的区别:1、scratch是一种专为初学者设计的图形化编程语言,python是一种文本编程语言;2、scratch使用的是基于积木的编程语法,python采用更加传统的文本编程语法等等。本专题为大家提供scratch和python相关的文章、下载、课程内容,供大家免费下载体验。

696

2023.08.11

ip地址修改教程大全
ip地址修改教程大全

本专题整合了ip地址修改教程大全,阅读下面的文章自行寻找合适的解决教程。

27

2025.12.26

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
ECMAScript6 / ES6---十天技能课堂
ECMAScript6 / ES6---十天技能课堂

共25课时 | 1.9万人学习

进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号