Error getting a new session... If this error persists, your country may not be supported yet. If your country was the issue, please consider using a U.S. VPN or a U.S. residential proxy.
建议是找一台可以访问 OpenAI 服务的服务器,最好是美国地区的服务器,在上面部署服务。
使用 ChatGPT 写小说
有了免费的 ChatGPT API 后,我们就可以使用 ChatGPT 来做很多事情了,AI 现在最擅长的是文字生成,我们可以使用 ChatGPT 来写小说。
gpt-author 的仓库主要是一个 ipynb 文件,里面包含了如何使用 ChatGPT 来写小说的代码,可以直接在本地运行,也可以在 Google Colab 上运行,下面我们来看下里面的代码并介绍其作用。
代码中包含了如何使用 Anthropic 的 API,但我们只需要调用我们部署的 ChatGPT API 即可,所以涉及到 Anuthropic 的代码这里就不做介绍。
1 2 3 4 5 6 7 8 9 10
import openai import os from ebooklib import epub import base64 import os import requests
openai.api_key = "YOUR OPENAI KEY"# You can enter any string here openai.api_base = "http://localhost:3040/v1" stability_api_key = "YOUR STABILITY KEY"# get it at https://beta.dreamstudio.ai/
这个方法罗列了编写小说的主要步骤,方法参数有提示词、章节数、写作风格,最后那个是否使用 Claude 模型的参数我们可以忽略
首先是生成多个小说情节
然后从这些小说情节中选择最吸引人的情节
再对选择的情节进行改进
根据最精彩的情节生成小说标题
接下来生成小说的故事线,并将其转换成 Python 数组,形成章节标题
使用第一个章节标题写小说第一章的内容
创建novel对象保存小说内容,创建chapters数组来保存每一章的内容
生成每一章节的内容,如果章节的内容较少(小于 100)则重新生成
最后将每一章的内容添加到novel和chapters对象中
我们再看生成小说章节的方法generate_plots:
1 2 3 4 5 6 7 8 9 10 11 12 13
import openai
defgenerate_plots(prompt): response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a creative assistant that generates engaging fantasy novel plots."}, {"role": "user", "content": f"Generate 10 fantasy novel plots based on this prompt: {prompt}"} ] )
这里的 model 参数即使填gpt-4也没有关系,我们的 API 服务始终只能调用 GPT3.5 模型
print_step_costs方法是计算 API 的花费金额,因为我们是用免费的 ChatGPT API 服务,所以这里不用关心费用
接下来是选择最吸引人的情节的方法select_most_engaging:
1 2 3 4 5 6 7 8 9 10 11
defselect_most_engaging(plots): response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are an expert in writing fantastic fantasy novel plots."}, {"role": "user", "content": f"Here are a number of possible plots for a new novel: {plots}\n\n--\n\nNow, write the final plot that we will go with. It can be one of these, a mix of the best elements of multiple, or something completely new and better. The most important thing is the plot should be fantastic, unique, and engaging."} ] )
defimprove_plot(plot): response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are an expert in improving and refining story plots."}, {"role": "user", "content": f"Improve this plot: {plot}"} ] )
defget_title(plot): response = openai.ChatCompletion.create( model="gpt-3.5-turbo-16k", messages=[ {"role": "system", "content": "You are an expert writer."}, {"role": "user", "content": f"Here is the plot: {plot}\n\nWhat is the title of this book? Just respond with the title, do nothing else."} ] )
defgenerate_storyline(prompt, num_chapters): print("Generating storyline with chapters and high-level details...") json_format = """[{"Chapter CHAPTER_NUMBER_HERE - CHAPTER_TITLE_GOES_HERE": "CHAPTER_OVERVIEW_AND_DETAILS_GOES_HERE"}, ...]""" response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a world-class fantasy writer. Your job is to write a detailed storyline, complete with chapters, for a fantasy novel. Don't be flowery -- you want to get the message across in as few words as possible. But those words should contain lots of information."}, {"role": "user", "content": f'Write a fantastic storyline with {num_chapters} chapters and high-level details based on this plot: {prompt}.\n\nDo it in this list of dictionaries format {json_format}'} ] )
print_step_costs(response, "gpt-4") improved_response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a world-class fantasy writer. Your job is to take your student's rough initial draft of the storyline of a fantasy novel, and rewrite it to be significantly better."}, {"role": "user", "content": f"Here is the draft storyline they wrote: {response['choices'][0]['message']['content']}\n\nNow, rewrite the storyline, in a way that is far superior to your student's version. It should have the same number of chapters, but it should be much improved in as many ways as possible. Remember to do it in this list of dictionaries format {json_format}"} ] )
defwrite_chapter(previous_chapters, plot, chapter_title, claude=True): try: response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a world-class fantasy writer."}, {"role": "user", "content": f"Plot: {plot}, Previous Chapters: {previous_chapters}\n\n--\n\nWrite the next chapter of this novel, following the plot and taking in the previous chapters as context. Here is the plan for this chapter: {chapter_title}\n\nWrite it beautifully. Include only the chapter text. There is no need to rewrite the chapter name."} ] )
print_step_costs(response, "gpt-4") return response['choices'][0]['message']['content'] except: response = openai.ChatCompletion.create( model="gpt-4-32k", messages=[ {"role": "system", "content": "You are a world-class fantasy writer."}, {"role": "user", "content": f"Plot: {plot}, Previous Chapters: {previous_chapters}\n\n--\n\nWrite the next chapter of this novel, following the plot and taking in the previous chapters as context. Here is the plan for this chapter: {chapter_title}\n\nWrite it beautifully. Include only the chapter text. There is no need to rewrite the chapter name."} ] )
# Example usage: prompt = "A kingdom hidden deep in the forest, where every tree is a portal to another world." num_chapters = 10 writing_style = "Clear and easily understandable, similar to a young adult novel. Lots of dialogue." novel, title, chapters, chapter_titles = write_fantasy_novel(prompt, num_chapters, writing_style, claude_true)
# Replace chapter descriptions with body text in chapter_titles for i, chapter inenumerate(chapters): chapter_number_and_title = list(chapter_titles[i].keys())[0] chapter_titles[i] = {chapter_number_and_title: chapter}
# Create the cover create_cover_image(str(chapter_titles))
# Create the EPUB file create_epub(title, 'AI', chapter_titles, '/content/cover.png')