Skip to content

ChatGPT - 用于内容创作

自发布以来,ChatGPT 对内容创作产生了深远影响。本章将探讨利用 ChatGPT 进行内容创作的各种方法,并提供使用最新 OpenAI API 实践的 Python 示例。

首先,您需要在 OpenAI 平台注册并获取一个 API key。强烈建议将您的 API key 设置为环境变量(environment variable),以提高安全性(例如,OPENAI_API_KEY)。获取 API key 后,安装 OpenAI Python 库(library):

pip install openai

现在,您就可以利用 ChatGPT 的创造力来丰富您的内容创作项目了。我们将使用该库(library)中的 OpenAI client。

作为一种先进的语言模型(language model),ChatGPT 擅长根据用户提示(prompt)创作文本。现代方法是使用 Chat Completions API,它更加灵活和强大。

例如,您可以使用 ChatGPT 生成一个故事:

from openai import OpenAI
import os
# 使用环境变量来设置您的 API key 是最佳实践
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# 如果您需要硬编码(不建议用于生产环境):
# client = OpenAI(api_key='your-api-key-goes-here')
# 定义用于文本生成的提示词,使用基于聊天的消息格式
messages = [
{"role": "system", "content": "You are a creative storyteller."},
{"role": "user", "content": "Write a short story about a detective solving a mysterious case in a futuristic city."}
]
# 指定模型并发出请求
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150 # 根据需要调整
)
# 从 API 响应中提取生成的文本
generated_text = response.choices[0].message.content
# 打印或按需使用生成的文本
print(generated_text)

注意:如果您不使用环境变量,请将 “‘your-api-key-goes-here’” 替换为您实际的 OpenAI API key。上面的示例提示模型(model)生成一个短故事。您可以根据您的具体用例自定义系统消息(system message)、用户提示(user prompt)以及 temperature 或 max_tokens 等其他参数(parameters)。出于安全原因,强烈建议使用环境变量来设置 API key。

输出结果可能如下所示:

Detective Kaito stared at the rain-slicked neon streets of Neo-Kyoto from his hover-car. The case of the missing data-chip, 'Project Chimera,' was as elusive as a shadow in the city's underbelly. His cybernetic eye scanned the area, searching for clues others might miss...

请注意,由于模型的内在创造力,即使使用相同的提示(prompt),系统每次生成的响应(response)也可能不同。您可以通过 temperature 参数来影响这一点。

生成引人入胜的视频内容需要精心编写的脚本。ChatGPT 可以极大地帮助起草初始视频脚本。您可以使用生成的文本作为视频内容的基础。

我们来看一个示例:

from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# 定义用于生成视频脚本的提示词
messages = [
{"role": "system", "content": "You are a professional scriptwriter for promotional videos."},
{"role": "user", "content": "Create a short script for a 30-second promotional video showcasing a new eco-friendly coffee cup."}
]
# 指定模型并发出请求
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=200
)
# 从 API 响应中提取生成的脚本
generated_script = response.choices[0].message.content
# 打印或按需使用生成的脚本
print(generated_script)

输出示例可能如下所示:

[SCENE START]
VISUAL: Opens on a vibrant shot of someone enjoying coffee in a bustling park, using the sleek, eco-friendly coffee cup.
VOICEOVER (Warm, friendly): Tired of disposable cups? Make a change with the new AuraCup!
VISUAL: Quick cuts showing the cup's features: durable material, stylish design, easy to clean.
VOICEOVER: Sip sustainably. Beautifully designed, planet-friendly. Your coffee, your impact.
VISUAL: Final shot of the cup with website URL and logo overlay.
VOICEOVER: Get your AuraCup today! Link in bio.
[SCENE END]

使用 ChatGPT 进行音乐创意和歌词头脑风暴

Section titled “使用 ChatGPT 进行音乐创意和歌词头脑风暴”

虽然 ChatGPT 不能直接创作音频文件,但它可以是进行音乐创意头脑风暴、生成歌词(lyrics),甚至以文本格式建议和弦进行(chord progressions)的绝佳工具。这些可以作为您创作的灵感来源。

这是一个生成歌曲歌词的简单示例:

from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# 定义用于音乐创意生成的提示词
messages = [
{"role": "system", "content": "You are a creative lyricist."},
{"role": "user", "content": "Write a verse and chorus for a pop song about a hopeful journey."}
]
# 指定模型并发出请求
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150
)
# 从 API 响应中提取生成的音乐创意
generated_lyrics = response.choices[0].message.content
# 打印或按需使用生成的歌词
print(generated_lyrics)

输出结果可能如下所示:

(Verse 1)
Sunrise paints the canvas wide,
A dusty road, nowhere to hide.
With every step, a fear subsides,
Got my dreams and hopeful strides.
(Chorus)
Oh, this journey's just begun,
Chasing horizons, towards the sun.
With a hopeful heart, I'll run, I'll run,
'Til my new day has been won.

您可以自定义提示(prompt)来指导您想要创作的音乐创意或歌词的风格(style)、流派(genre)、主题(theme)或特定元素。对于实际的音乐生成,您通常会将这些文本输出与专门的音乐软件或 AI 音乐生成工具结合使用。

您还可以使用 ChatGPT 为交互式故事、测验或“选择您自己的冒险”叙事生成动态对话(dialogues)。我们来看一个示例,使用 ChatGPT 为一个关于“AI 在教育领域的未来”的学校戏剧生成动态对话。

from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# 定义用于生成对话的提示词
messages = [
{"role": "system", "content": "You are a playwright specializing in educational themes."},
{"role": "user", "content": "Write a short, dynamic dialogue scene for a school play on 'The Future of AI in Education'. Include two students, ANNA and BEN, discussing its pros and cons."}
]
# 指定模型并发出请求
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=250
)
# 从 API 响应中提取生成的对话
generated_dialogues = response.choices[0].message.content
# 打印或按需使用生成的对话
print(generated_dialogues)

生成的文本可以作为为戏剧创作引人入胜的动态对话的起点:

ANNA: (Excitedly) Ben, have you seen the new AI tutor program? It's amazing! It adapts to your learning speed!
BEN: (Skeptical) I don't know, Anna. Personalized learning sounds great, but what about the human touch? Can an AI really replace a teacher's encouragement?
ANNA: Maybe not replace, but assist! Think about getting instant help on homework, anytime. That's a huge pro!
BEN: True, but what if it makes us too reliant on technology? And what about data privacy? There are cons we need to consider.

可以通过向 ChatGPT 提供具体的指令来改进或扩展现有内容,从而利用它进行创意建议、内容增强(enhancement)甚至内容摘要。在下面的示例中,我们要求模型(model)增强一段提供的文本:

from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# 定义需要增强的内容
input_content = "Renewable energy is important. It helps reduce fossil fuel use."
# 创建用于内容增强的提示词
messages = [
{"role": "system", "content": "You are an expert editor and content enhancer."},
{"role": "user", "content": f"Enhance the following text to be more impactful and informative, targeting a general audience:\n\n{input_content}"}
]
# 指定模型并发出请求
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=200
)
# 从 API 响应中提取增强后的内容
enhanced_content = response.choices[0].message.content
# 打印或按需使用增强后的内容
print(enhanced_content)

模型(model)可能会提供一个像这样的增强版本:

The transition to renewable energy sources is not merely important; it's a critical imperative for a sustainable future. By harnessing the power of sun, wind, and water, we can significantly diminish our global reliance on finite fossil fuels. This shift not only combats climate change by reducing greenhouse gas emissions but also fosters energy independence and can drive innovation in green technologies, paving the way for a cleaner, healthier planet for generations to come.

请记住,结果取决于模型的理解和创造力。您可能需要反复修改提示(prompt)或提供更具体的指令,才能达到所需的内容增强(enhancement)水平。考虑尝试不同的系统消息(system message)来引导语气(tone)和风格(style)。

ChatGPT 可以通过根据特定个体或受众群体定制文本来实现内容个性化(personalization)。下面的示例展示了如何利用用户数据(user data)(如兴趣或姓名)来个性化生成的文本,使其更具相关性和吸引力:

from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# 用户特定信息
user_name = "Alex"
user_interest = "learning about AI-powered productivity tools"
# 创建用于个性化内容的提示词
messages = [
{"role": "system", "content": "You are an assistant that provides personalized information."},
{"role": "user", "content": f"Generate a short, engaging paragraph for {user_name}, who is interested in {user_interest}. Suggest how ChatGPT could help them."}
]
# 指定模型并发出请求
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150
)
# 从 API 响应中提取个性化后的内容
personalized_content = response.choices[0].message.content
# 打印或按需使用个性化后的内容
print(personalized_content)

模型(model)可能会生成像这样的个性化内容:

Hello Alex! Since you're keen on AI-powered productivity tools, you'll find ChatGPT to be an invaluable ally. Imagine effortlessly drafting emails, summarizing long documents, brainstorming ideas for projects, or even getting coding assistance – all through simple conversation. ChatGPT can help streamline your workflow and free up your time for more strategic tasks, truly boosting your productivity with the power of AI!

在本章中,我们学习了如何利用通过现代 Python 实践访问的 OpenAI API 来使用 ChatGPT,它可以帮助生成各种形式的文本、为脚本和音乐进行创意头脑风暴、增强现有材料以及实现内容个性化。它是各种内容创作任务的通用 AI 伙伴。