【大模型应用开发极简入门】构建新闻稿生成器:提示词的使用与基于事实的提示词

本文主要是介绍【大模型应用开发极简入门】构建新闻稿生成器:提示词的使用与基于事实的提示词,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一. 提示词怎么写
    • 二. 完整代码
    • 三. 基于事实的prompt

GPT-4和ChatGPT等LLM专用于生成文本。我们可以使用GPT-4和ChatGPT在各种场景中生成文本,举例如下。

  • 电子邮件
  • 合同或正式文档
  • 创意写作
  • 逐步行动计划
  • 头脑风暴
  • 广告
  • 职位描述

对于本项目,我们将创建一个工具,它可以根据一系列事实生成新闻稿。我们可以根据目标媒体和受众选择新闻稿的篇幅、语调和风格。

一. 提示词怎么写

这里主要描述prompt(提示词)的构建逻辑,因为大模型可以根据prompt的规定生成符合要求的文档。

  1. 给AI模型分配一个角色,并尽可能精确地描述任务。如下给AI模型分配的角色是记者助手:
prompt_role = "You are an assistant for journalists. \Your task is to write articles, based on the FACTS that are \given to you. \You should respect the instructions: the TONE, the LENGTH, \and the STYLE"
  1. 其他规定
  • prompt_role:角色的描述,以便大模型能够按照角色回答
  • FACTS:基于给定的事实数据来回答
  • TONE:回答风格:这里是informal
  • LENGTH:回答的单词数
  • STYLE:生成的文本格式:这里是blogpost
# 拼装messages,规定了prompt的格式:  
# prompt_role:角色的描述,以便大模型能够按照角色回答  
# FACTS:基于给定的事实数据来回答  
# TONE:回答风格:这里是informal  
# LENGTH:回答的单词数  
# STYLE:生成的文本格式:这里是blogpost  
def assist_journalist(  facts: List[str], tone: str, length_words: int, style: str  
):  facts = ", ".join(facts)  prompt = f"{prompt_role} \  FACTS: {facts} \  TONE: {tone} \  LENGTH: {length_words} words \  STYLE: {style}"  return ask_chatgpt([{"role": "user", "content": prompt}])

 

二. 完整代码

import os  import openai  
from typing import List  openai.api_key = os.getenv('OPENAI_API_KEY')  # 调用openai api  
def ask_chatgpt(messages):  response = openai.ChatCompletion.create(  model="gpt-3.5-turbo", messages=messages  )  return response["choices"][0]["message"]["content"]  # prompt_role描述  
prompt_role = "You are an assistant for journalists. \  Your task is to write articles, based on the FACTS that are \  given to you. \  You should respect the instructions: the TONE, the LENGTH, \  and the STYLE"  # 拼装messages,规定了prompt的格式:  
# prompt_role:角色的描述,以便大模型能够按照角色回答  
# FACTS:基于给定的事实数据来回答  
# TONE:回答风格:这里是informal  
# LENGTH:回答的单词数  
# STYLE:生成的文本格式:这里是blogpost  
def assist_journalist(  facts: List[str], tone: str, length_words: int, style: str  
):  facts = ", ".join(facts)  prompt = f"{prompt_role} \  FACTS: {facts} \  TONE: {tone} \  LENGTH: {length_words} words \  STYLE: {style}"  return ask_chatgpt([{"role": "user", "content": prompt}])  print(  assist_journalist(  ["The sky is blue", "The grass is green"], "informal", \  100, "blogpost"  )  
)

 

输出如下


"Hey, everyone! Did you know that the sky is blue and the grass is green?
I mean, it's something we see every day and probably take for granted,
but it's still pretty amazing if you think about it! The sky appears
blue to us because of something called Rayleigh scattering – basically,
the molecules in the Earth's atmosphere scatter sunlight in all different
directions. Blue light has a shorter wavelength, so it gets scattered
more than the other colors in the spectrum. That's why the sky looks
blue most of the time! As for the grass being green... that's due to
chlorophyll, the pigment that helps plants capture sunlight to make
their food. Chlorophyll absorbs red and blue light, but reflects
green light, which is why we see plants as green.It's pretty cool how science explains these things we take for granted,
don't you think? Next time you're outside, take a moment to appreciate
the color palette around you!"

 

三. 基于事实的prompt

通过明确facts数据,让GPT基于事实来回答。

print(assist_journalist(# 这里让facts=["A book on ChatGPT has been published last week","The title is Developing Apps with GPT-4 and ChatGPT","The publisher is O'Reilly.",],tone="excited",length_words=50,style="news flash",)
)

结果如下:

Exciting news for tech enthusiasts! O'Reilly has just published a
new book on ChatGPT called "Developing Apps with GPT-4 and ChatGPT".
Get ready to delve into the world of artificial intelligence and learn
how to develop apps using the latest technology. Don't miss out on this
opportunity to sharpen your skills!

 

这篇关于【大模型应用开发极简入门】构建新闻稿生成器:提示词的使用与基于事实的提示词的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1025763

相关文章

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Java中的Lambda表达式及其应用小结

《Java中的Lambda表达式及其应用小结》Java中的Lambda表达式是一项极具创新性的特性,它使得Java代码更加简洁和高效,尤其是在集合操作和并行处理方面,:本文主要介绍Java中的La... 目录前言1. 什么是Lambda表达式?2. Lambda表达式的基本语法例子1:最简单的Lambda表

使用Python实现图像LBP特征提取的操作方法

《使用Python实现图像LBP特征提取的操作方法》LBP特征叫做局部二值模式,常用于纹理特征提取,并在纹理分类中具有较强的区分能力,本文给大家介绍了如何使用Python实现图像LBP特征提取的操作方... 目录一、LBP特征介绍二、LBP特征描述三、一些改进版本的LBP1.圆形LBP算子2.旋转不变的LB

Maven的使用和配置国内源的保姆级教程

《Maven的使用和配置国内源的保姆级教程》Maven是⼀个项目管理工具,基于POM(ProjectObjectModel,项目对象模型)的概念,Maven可以通过一小段描述信息来管理项目的构建,报告... 目录1. 什么是Maven?2.创建⼀个Maven项目3.Maven 核心功能4.使用Maven H

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

Java使用SLF4J记录不同级别日志的示例详解

《Java使用SLF4J记录不同级别日志的示例详解》SLF4J是一个简单的日志门面,它允许在运行时选择不同的日志实现,这篇文章主要为大家详细介绍了如何使用SLF4J记录不同级别日志,感兴趣的可以了解下... 目录一、SLF4J简介二、添加依赖三、配置Logback四、记录不同级别的日志五、总结一、SLF4J

使用Python实现一个优雅的异步定时器

《使用Python实现一个优雅的异步定时器》在Python中实现定时器功能是一个常见需求,尤其是在需要周期性执行任务的场景下,本文给大家介绍了基于asyncio和threading模块,可扩展的异步定... 目录需求背景代码1. 单例事件循环的实现2. 事件循环的运行与关闭3. 定时器核心逻辑4. 启动与停