当前位置: 首页 > news >正文

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

文章目录

    • 一. 提示词怎么写
    • 二. 完整代码
    • 三. 基于事实的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.lryc.cn/news/360987.html

相关文章:

  • JAVA和爬虫,那个值得学习
  • Vue.js 与 TypeScript(1) :项目配置、props标注类型、emits标注类型
  • 【考试100】安全员B证《建设工程安全生产技术》单选题
  • linux进阶的一些操作以及知识点------习题集(实践)
  • 提莫攻击 ---- 模拟算法
  • SpringBootWeb 篇-深入了解 Spring 异常处理、事务管理和配置文件参数配置化、yml 配置文件
  • 重学java 55. 集合 Set接口
  • spring项目修改时间格式
  • 每周统计-20240531
  • 【工具】探索 DOU:每用户数据使用量
  • JVM之垃圾判断的详细解析
  • 07- Redis 中的 HyperLogLog 数据类型和应用场景
  • jenkins应用2-freestyle-job
  • K210视觉识别模块学习笔记1:第一个串口程序_程序烧录与开机启动
  • [数据集][目标检测]脑溢血检测数据集VOC+YOLO格式767张2类别
  • 如何借VR之手,让展厅互动更精彩?
  • 微信小程序如何使用地图
  • 力扣 287. 寻找重复数
  • 怎样清理Mac存储空间 苹果电脑内存不够用怎么办 苹果电脑内存满了怎么清理
  • 网络遗忘权的实现方法
  • 【Python内功心法】:深挖内置函数,释放语言潜能
  • JS-09-es6常用知识1
  • SpringBoot 基础之自动配置
  • Oracle dblink 发现Network 等待事件的分析 enq: KO - fast object checkpoint
  • SpringMVC:向三大域对象存数据
  • 如何用python做一个用户登录界面——浔川python社
  • Python知识点9---推导式
  • 用C++做一个跑酷游戏
  • 基于字典树可视化 COCA20000 词汇
  • TypeScript 中的命名空间