做网站编辑累不累,seo推广软件下载,企业所得税优惠政策有哪些,深圳做企业网站的公司文章目录 一. 提示词怎么写二. 完整代码三. 基于事实的prompt GPT-4和ChatGPT等LLM专用于生成文本。我们可以使用GPT-4和ChatGPT在各种场景中生成文本#xff0c;举例如下。 电子邮件合同或正式文档创意写作逐步行动计划头脑风暴广告职位描述 对于本项目#xff0c;我们将创建… 文章目录 一. 提示词怎么写二. 完整代码三. 基于事实的prompt GPT-4和ChatGPT等LLM专用于生成文本。我们可以使用GPT-4和ChatGPT在各种场景中生成文本举例如下。 电子邮件合同或正式文档创意写作逐步行动计划头脑风暴广告职位描述 对于本项目我们将创建一个工具它可以根据一系列事实生成新闻稿。我们可以根据目标媒体和受众选择新闻稿的篇幅、语调和风格。
一. 提示词怎么写
这里主要描述prompt提示词的构建逻辑因为大模型可以根据prompt的规定生成符合要求的文档。
给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其他规定 prompt_role角色的描述以便大模型能够按照角色回答FACTS基于给定的事实数据来回答TONE回答风格这里是informalLENGTH回答的单词数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( modelgpt-3.5-turbo, messagesmessages ) 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, its something we see every day and probably take for granted,
but its 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 Earths 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. Thats why the sky looks
blue most of the time! As for the grass being green... thats 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.Its pretty cool how science explains these things we take for granted,
dont you think? Next time youre 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 OReilly.,],toneexcited,length_words50,stylenews flash,)
)
结果如下
Exciting news for tech enthusiasts! OReilly 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. Dont miss out on this
opportunity to sharpen your skills!