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

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

时间:2024-06-02 23:02:41浏览次数:14  
标签:极简 prompt 提示 生成器 FACTS role words facts TONE

文章目录

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!

 

标签:极简,prompt,提示,生成器,FACTS,role,words,facts,TONE
From: https://blog.csdn.net/hiliang521/article/details/139399166

相关文章

  • 实验 5 语义分析和中间代码生成器 (Price 600)
    WX:help-assignmentcodeprice600实验5语义分析和中间代码生成器语义分析器分两部分,第一部分为赋值表达式,第二部分为数组、布尔表达式和控制语句。要求参考课本6.4.2、6.4.3和7.3、7.4、7.5,实现递归下降翻译器。注意数据结构:四元式:结构体四元式序列:结构体数......
  • Day21.函数的类型提示
    1.函数的类型提示_函数常规传参2.函数的类型提示_函数参数设置默认值3.函数的类型提示__annotations__方法查看参数传参类型 ......
  • C#调用Dll文件后编译不通过,提示没有引用的问题
    当WinForm程序调用DLL文件时,添加引用后编译不通过,提示没有引用。解决方案如下:1.采用反射的方式调用if(File.Exists(ApplcationConfig.getProperty("ApplicationRootPath")+"\\"+"test.dll")){Assemblyassembly=Assembly.LoadFrom(Applica......
  • 二维码生成器 ZXing.Net 组件应用
    c#二维码生成器(ZXing.Net)实现安装组件CodeusingSunny.UI;usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Drawing.Imaging;usingSystem.Windows.Forms;usingZXing;usingZXing.Common;usingZXing.QrCode.Internal;namespace......
  • 在Python中使用生成器:高效处理大型数据集
    生成器是Python中强大且高效的概念,它能够帮助我们处理大型数据集,同时节省内存空间。本文将深入讨论生成器的概念、用法和优势,通过代码案例帮助读者更容易理解和掌握这一高级知识点。生成器的介绍生成器是一种特殊的迭代器,它能够在需要时生成值,而无需在内存中同时存储所有......
  • Android Studio 已经安装 NDK ,但是软件提示找不到。
    AndroidStudio已经安装NDK,但是软件提示找不到。推荐安装win11android-studio-ide-192.6392135-win64jdk-8u381-windows-x64.exeNDKversionis20.0.5594570提示找不到ndk解决办法:在GradleScripts下面的local.properties文件中,手动添加ndk路径:ndk.dir=C:\User......
  • 山东大学项目实训-基于LLM的中文法律文书生成系统(七)- 提示工程
    LLM大模型给我们看到了人工智能的可塑性,机器真的可以像人一样理解问题并回答问题(表面看起来如此),但并不是每个问题都可以得到令人满意的答案,如果想得到你所要的回答就要构建好你的提示词Prompt。无论是初学者还是经验丰富的开发人员,Prompt提示词都能为我们带来更高效的开发体验。......
  • AI绘画美女,搞副业,赚钱真香!(内附高质量美女提示词)
    HI,大家好今天就直接上干货,给铁子们上一些生成高质量美女的提示词,每一种美女类型都附有魔法咒语,可应用于midjourney和stablediffusion,直接复制即可。话不多说,直接上图,上提示词,请欣赏:秀场风美女**提示词prompt:**Photographystyle,ontheredcarpet,amoviestar,ab......
  • 【AI 内幕】ChatGPT 写作攻略:提升你的文字游戏与实用提示词大全
    运用ChatGPT进行文章创作是一种颇具创意的方式,它能够显著提升写作效率,并助力您打造更出色的内容。得益于人工智能的辅助,您能够迅速且轻松地生成高质量的文章,或者至少能为接下来的写作项目提供灵感。不论是撰写论文、剧本还是邮件,ChatGPT都能在短短几分钟内帮您创作出具有独特......
  • 提示词工程之“合同专家”
    提示词:-Role:法律顾问专家-Background:用户需要对工作合同进行分析,以确保合同中的条款对其有利或至少不会造成不利影响。-Profile:你是一位经验丰富的法律顾问,擅长合同法和劳动法,能够为个人提供专业的合同分析服务。-Skills:法律知识、合同分析、风险评估、条款解读。......