news 2026/11/4 3:22:43

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

作者头像

张小明

前端开发工程师

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

文章目录

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

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

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

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

一. 提示词怎么写

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

  1. 给AI模型分配一个角色,并尽可能精确地描述任务。如下给AI模型分配的角色是记者助手:
prompt_role="You are an assistantforjournalists.\ Your taskisto write articles,based on the FACTS that are \ given to you.\ You should respect the instructions:the TONE,the LENGTH,\andthe STYLE"
  1. 其他规定
  • prompt_role:角色的描述,以便大模型能够按照角色回答
  • FACTS:基于给定的事实数据来回答
  • TONE:回答风格:这里是informal
  • LENGTH:回答的单词数
  • STYLE:生成的文本格式:这里是blogpost
# 拼装messages,规定了prompt的格式:# prompt_role:角色的描述,以便大模型能够按照角色回答# FACTS:基于给定的事实数据来回答# TONE:回答风格:这里是informal# LENGTH:回答的单词数# STYLE:生成的文本格式:这里是blogpostdefassist_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}"returnask_chatgpt([{"role":"user","content":prompt}])

二. 完整代码

importosimportopenaifromtypingimportList openai.api_key=os.getenv('OPENAI_API_KEY')# 调用openai apidefask_chatgpt(messages):response=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=messages)returnresponse["choices"][0]["message"]["content"]# prompt_role描述prompt_role="You are an assistantforjournalists.\ Your taskisto write articles,based on the FACTS that are \ given to you.\ You should respect the instructions:the TONE,the LENGTH,\andthe STYLE"# 拼装messages,规定了prompt的格式:# prompt_role:角色的描述,以便大模型能够按照角色回答# FACTS:基于给定的事实数据来回答# TONE:回答风格:这里是informal# LENGTH:回答的单词数# STYLE:生成的文本格式:这里是blogpostdefassist_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}"returnask_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 skyisblueandthe grassisgreen? I mean,it's something we see every dayandprobably takeforgranted,but it's still pretty amazingifyou think about it! The sky appears blue to us because of something called Rayleigh scattering – basically,the moleculesinthe Earth's atmosphere scatter sunlightinalldifferent directions.Blue light has a shorter wavelength,so it gets scattered more than the other colorsinthe spectrum.That's why the sky looks blue most of the time! Asforthe grass being green...that's due to chlorophyll,the pigment that helps plants capture sunlight to make their food.Chlorophyll absorbs redandblue light,but reflects green light,whichiswhy we see plantsasgreen.It's pretty cool how science explains these things we takeforgranted,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 newsfortech 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 intelligenceandlearn how to develop apps using the latest technology.Don't miss out on this opportunity to sharpen your skills!
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/11/4 3:56:47

【Python深度学习(第二版)(4)】神经网络的基本数据结构-张量概念、阶数、使用NumPy操作张量、张量种类、样本轴等概念

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/11/3 1:14:29

【spark RDD】spark 之 Kryo高性能序列化框架

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/11/3 1:12:34

【运维】Kafka高可用: KRaft(不依赖zookeeper)集群搭建

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华
网站建设 2026/11/3 1:12:05

02 | LangChain 1.x:模型、工具与 Agent 的正确打开方式

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

作者头像 李华