ChatGPT – 用于内容创建
自推出以来,ChatGPT 以超出预期的速度吸引了内容创建者的注意力。在本章中,我们将了解使用 ChatGPT 进行内容创建的各种方法。除此之外,我们还将看到使用 OpenAI API 的 Python 实现。
从 OpenAI 获取 API 密钥
首先,您需要在 OpenAI 平台上注册并获取 API 密钥。获得 API 密钥后,您可以按如下方式安装 OpenAI Python 库 −
pip install openai
现在,您已准备好将 ChatGPT 的创意功能融入您的内容创建项目中。
使用 ChatGPT 生成文本
作为语言模型,ChatGPT 擅长根据用户提示制作文本。
例如,您可以使用 ChatGPT 生成故事,如下所示 −
import openai # 设置您的 OpenAI API 密钥 openai.api_key = 'your-api-key-goes-here' # 定义文本生成提示 prompt = "Write a short story about a detective solving a mysterious case." # 指定 OpenAI 引擎并发出请求 response = openai.Completion.create( engine=" gpt-3.5-turbo-instruct", prompt=prompt, max_tokens=100 ) # 从 API 响应中提取生成的文本 generated_text = response['choices'][0]['text'] # 根据需要打印或使用生成的文本 print(generated_text)
注意 − 将"your-api-key-goes-here"替换为您的实际 OpenAI API 密钥。上面的示例提示模型生成一个关于侦探的短篇故事,您可以根据您的具体用例自定义提示和其他参数。
在这种情况下,我们得到了以下输出 −
Detective Mark Reynolds had been on the force for over a decade. He had seen his share of mysteries and solved his fair share of cases. But the one he was currently working on was unlike any he had encountered before.
请注意,当您将相同的代码与 OpenAI 密钥一起使用时,系统可能会在您的系统上产生不同的响应。
使用 ChatGPT 生成视频脚本
众所周知,生成视频内容需要编写脚本,而 ChatGPT 可以帮助您创建视频脚本。您可以利用生成的文本作为启动视频内容创建之旅的基础。让我们看看下面的示例 −
import openai # 设置您的 OpenAI API 密钥 openai.api_key = 'your-api-key-goes-here' # 定义生成视频脚本的提示 prompt = "Create a script for a promotional video showcasing our new AI product." # 指定 OpenAI 引擎并发出请求 response = openai.Completion.create( engine="gpt-3.5-turbo-instruct", prompt=prompt, max_tokens=100 ) # 从 API 响应中提取生成的脚本 generated_script = response['choices'][0]['text'] # 根据需要打印或使用生成的脚本 print(generated_script)
在本例中,我们得到了以下输出 −
[Opening shot of a modern office space with employees working at their desks] Voiceover: Are you tired of mundane tasks taking up valuable time at work? Do you wish there was a way to streamline your workflow and increase productivity? [Cut to employees looking stressed and overwhelmed] Voiceover: Well, look no further. Our company is proud to introduce our latest innovation – our revolutionary AI product. [Cut to a sleek and futuristic AI device on a desk]
使用 ChatGPT 创作音乐
ChatGPT 可通过提供音乐提示或请求来创作音乐。生成的音乐创意或歌词可用作创作灵感。
这是一个简单的示例,演示了 ChatGPT 如何根据给定的提示生成短钢琴旋律 −
import openai # 设置您的 OpenAI API 密钥 openai.api_key = 'your-api-key-goes-here' # 定义音乐创作提示 prompt = "Compose a short piano melody in the key of C major." # 指定 OpenAI 引擎并发出请求 response = openai.Completion.create( engine="gpt-3.5-turbo-instruct", prompt=prompt, max_tokens=300 ) # 从 API 响应中提取生成的音乐作品 generated_music = response['choices'][0]['text'] # 根据需要打印或使用生成的音乐 print(generated_music)
在本例中,我们得到了以下输出 −
Here is a simple piano melody in the key of C major: C D E F G A G F E D C B A C The melody begins and ends on C, the tonic note of the C major scale. It then moves up and down the scale, primarily using steps and occasionally skipping a note up or down. This gives the melody a smooth and pleasant flow.
请注意,您可以自定义提示以指导您想要创建的音乐的风格、流派或特定元素。
使用 ChatGPT 生成交互式内容
您还可以使用 ChatGPT 生成动态对话、测验或选择您自己的冒险叙述。让我们看一个示例,其中我们使用 ChatGPT 为主题为"机器人与社会"的学校戏剧生成动态对话。
import openai # 设置您的 OpenAI API 密钥 openai.api_key = 'your-api-key-goes-here' # 定义生成对话的提示 prompt = "Write dynamic dialogues for a school play on the topic 'Robotics and society'." # 指定 OpenAI 引擎并发出请求 response = openai.Completion.create( engine="gpt-3.5-turbo-instruct", prompt=prompt, max_tokens=100 ) # 从 API 响应中提取生成的对话 generated_dialogues = response['choices'][0]['text'] # 根据需要打印或使用生成的对话 print(generated_dialogues)
以下生成的文本可以作为为剧本创建引人入胜且动态的对话的起点 −
(Scene opens with a group of students gathered around a table, discussing about a robotics project) Student 1: Okay everyone, let's finalize our project idea for the robotics competition. Student 2: How about a robot that assists elderly people in their daily tasks? Student 3: That's a great idea, but I think we can do something more impactful for society.
使用 ChatGPT 增强内容
ChatGPT 可用于提供创意建议、增强甚至总结,方法是为其提供改进或扩展现有内容的具体说明。在下面的示例中,要求模型增强所提供的内容 −
import openai # 设置您的 OpenAI API 密钥 openai.api_key = 'your-api-key-goes-here' # 定义需要增强的内容 input_content = "The importance of renewable energy sources cannot be overstated. They play a crucial role in reducing our reliance on non-renewable resources." # 创建内容增强的提示 prompt = f"增强以下文本: {input_content}" # 指定 OpenAI 引擎并发出请求 response = openai.Completion.create( engine="gpt-3.5-turbo-instruct", prompt=prompt, max_tokens=100 ) # 从 API 响应中提取增强的内容 enhanced_content = response['choices'][0]['text'] # 根据需要打印或使用增强的内容 print(enhanced_content)
模型将根据需要给出增强的内容 −
The significance of renewable energy sources cannot be overstated. In today's world, where concerns about climate change and resource depletion are at an all-time high, these sources of energy have become essential. They not only offer a cleaner and more sustainable alternative to traditional, non-renewable resources, but also play a crucial role in reducing our carbon footprint and mitigating the adverse effects of global warming. Renewable energy sources, such as solar, wind, hydro, geothermal, and biomass, are constantly replenished and therefore do not deplete as traditional fossil fuels do. This makes them highly valuable in promoting a more secure and sustainable energy future.
请记住,结果取决于模型的理解和创造力,您可能需要迭代或试验才能达到所需的增强水平。
使用 ChatGPT 进行内容个性化
ChatGPT 可用于通过针对特定个人或受众定制文本来实现内容个性化。以下示例展示了如何利用用户数据来个性化生成的文本,使其更具相关性和吸引力 −
import openai # 设置您的 OpenAI API 密钥 openai.api_key = 'your-api-key-goes-here' # 用户特定信息 user_name = "Gaurav" user_interest = "ChatGPT" # 定义个性化内容的提示 prompt = f"Generate personalized content for {user_name}. Focus on {user_interest}." # 指定 OpenAI 引擎并发出请求 response = openai.Completion.create( engine="gpt-3.5-turbo-instruct", prompt=prompt, max_tokens=200 ) # 从 API 响应中提取个性化内容 personalized_content = response['choices'][0]['text'] # 根据需要打印或使用个性化内容 print(personalized_content)
模型将给出如下所示的个性化内容 −
Hello Gaurav! Have you heard about ChatGPT? ChatGPT is an innovative chatbot that uses advanced artificial intelligence to have human-like conversations with you. This means that you can talk to ChatGPT just like you would talk to a friend or a real person. One of the most amazing things about ChatGPT is its ability to understand natural language. This means that you don't have to use specific keywords or phrases to communicate with it. You can simply chat with ChatGPT in your own words, and it will understand and respond to you just like a human would.
结论
在本章中,我们了解了 ChatGPT 如何帮助制作文本、创建视频脚本、创作音乐,甚至使交互式内容变得更好。我们展示了 ChatGPT 如何在不同的创意任务中像一个乐于助人的朋友。