赞
踩
最终效果如图:
代码运行后会在桌面自动生成小说文件:
如下为小说创作脚本软件,大小10.79MB,使用方法:apikey使用英文逗号隔开,可直接运行!还有配置文件可记录参数,无需反复填写!生成的小说会显示在桌面的文件夹里:
小说创作脚本plushttps://pan.baidu.com/s/1FDfYB_vypa0lYozxMtVMKQ?pwd=6666
效果如图:
源码如下,这里免费提供一个测试apikey:"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",(12.30已更新5$key)余额有限,先到先用哦!可自行设置具体参数:
- import os
- import openai #pip install openai==0.28.0
- import random
- openai.api_base = "https://api.openai-proxy.com/v1" #代理网址
- # -*- coding: utf-8 -*-
-
- # 将你的多个 API 密钥放在这个列表中
- api_keys = [
- "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",
- "",
-
- ]
-
- # 小说信息
- novel_name = "熊出没之我是光头强" #小说名称
- total_chapters = 5 # 总章节数
- tokens_per_chapter = 100 #每章字数
- def write_novel_chapter(api_key, chapter_content, chapter_number, conversation_history):
- """使用OpenAI的API模型来编写小说章节"""
- openai.api_key = api_key # 设置当前使用的 API 密钥
- response = openai.ChatCompletion.create(
- model="gpt-3.5-turbo-0613", #输入使用的模型
- temperature=0.1,
- messages=conversation_history + [
- {
- "role": "system",
- "content": f"你是一名专业的小说家,你的任务是编写小说《{novel_name}》,共有{total_chapters}章,每章{tokens_per_chapter}字,请自行构思每章章的标题和内容,开始编写第 {chapter_number}章:"
- },
- {"role": "user", "content": chapter_content},
- ],
- max_tokens=1500, #gpt输出的tokens
- )
- generated_text = response['choices'][0]['message']['content']
- return generated_text, response['choices'][0]['message']['role'], response['choices'][0]['index']
-
- def write_novel(api_keys, output_folder):
- """编写小说的所有章节"""
- chapter_number = 1
- conversation_history = [] # 存储对话历史
- used_api_keys = [] # 用于存储已经使用过的 API 密钥
-
- while chapter_number <= total_chapters:
- available_keys = [key for key in api_keys if key not in used_api_keys]
-
- if not available_keys:
- # 所有密钥已使用过,随机不重复循环使用每个apikey,重新开始循环
- used_api_keys = []
- available_keys = api_keys[:]
-
- selected_api_key = random.choice(available_keys)
- used_api_keys.append(selected_api_key)
-
- print(f"正在编写《{novel_name}》第 {chapter_number} 章,使用的 API 密钥:{selected_api_key}")
-
- try:
- # 模拟章节内容,你可以根据实际需要修改
- chapter_content = f""
-
- # 调用章节编写函数
- generated_text, role, index = write_novel_chapter(selected_api_key, chapter_content, chapter_number, conversation_history)
-
- # 获取章节名称(文本内容的第一行)
- chapter_name = generated_text.split('\n', 1)[0]
-
- if "第" in chapter_name and "章" in chapter_name:
- chapter_file_name = f"{chapter_name}.txt"
- else:
- chapter_file_name = f"第{chapter_number}章:{chapter_name}.txt"
-
- # 去掉包含小说名称的部分
- chapter_file_name = chapter_file_name.replace(novel_name, "")
-
- # 去掉包含《》的部分
- if "《" in chapter_file_name and "》" in chapter_file_name:
- chapter_file_name = chapter_file_name.replace("《", "").replace("》", "")
-
- # 去掉多余的空格
- chapter_file_name = chapter_file_name.strip()
- chapter_file_path = os.path.join(output_folder, chapter_file_name)
- with open(chapter_file_path, 'w', encoding='utf-8') as chapter_file:
- chapter_file.write(generated_text)
-
- print(f"《{novel_name}》 {chapter_file_name.strip('.txt')} 编写成功,已保存至 {chapter_file_path}")
- print("-------------------------------------------------------------------")
-
- # 清空对话历史
- conversation_history = []
-
- # 如果不是最后一章,将生成的文本和回复加入对话历史
- if chapter_number < total_chapters:
- conversation_history.append({"role": "system", "content": generated_text})
-
- except Exception as e:
- print(f"《{novel_name}》 第{chapter_number}章 编写失败,错误信息: {e}")
- print("-------------------------------------------------------------------")
-
- chapter_number += 1
-
- # 程序的入口点
- if __name__ == "__main__":
- output_folder = fr"C:\Users\c\Desktop\{novel_name}" #保存到文件夹
- try:
- if not os.path.exists(output_folder):
- os.makedirs(output_folder)
- write_novel(api_keys, output_folder)
- except Exception as e:
- print(f"程序执行时出现错误: {e}")
- finally:
- print("程序执行完毕。")
感谢朋友们阅读,下期再见!!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。