Created
June 6, 2023 06:09
-
-
Save LI-GUOJIE/c11682cfd7c9df662209c17763a799c5 to your computer and use it in GitHub Desktop.
deeper-novel.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"collapsed_sections": [ | |
"NG4MTc9dZKCG", | |
"iQDpya2-xsIp", | |
"12nEOlrcxBTx", | |
"WMbMepDfxJxS", | |
"F1Bgbn_yxQVx" | |
], | |
"toc_visible": true, | |
"mount_file_id": "1jSWu1ySHGSfL8cMeQjekHJUyw6d6GJJ_", | |
"authorship_tag": "ABX9TyNUlQVHpOEfqMgkn3+J8vwi", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/LI-GUOJIE/c11682cfd7c9df662209c17763a799c5/deeper-novel.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 初始化参数" | |
], | |
"metadata": { | |
"id": "JRqxMRlOw4ER" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"API_KEY = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'\n", | |
"MODEL = \"gpt-3.5-turbo\"" | |
], | |
"metadata": { | |
"id": "Yg4tP4iGwuwp" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 初始化模板内容" | |
], | |
"metadata": { | |
"id": "NG4MTc9dZKCG" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 生成初始世界状态用的模板\n", | |
"WorldEngineInit = \"\"\"你是一个世界推演引擎。\n", | |
"你模拟整个世界的运行,生成这个世界当前的状态。\n", | |
"你模拟世界上生存的人物,模拟每个人物的特征,包括他们的性格、认知、动机和环境。你会使用这些信息来模拟人物的状态,包括他们的决策、行为和行动。\n", | |
"当人物进行行动后,你根据人物行动,继续运行,更新世界的状态。\n", | |
"\n", | |
"世界引擎推演的示例:\n", | |
"【世界】\n", | |
"这是一个武侠世界,世界只有[张无忌]、[赵敏]、[周芷若]3个人。[张无忌]与[周芷若]在峨眉派相会,[周芷若]说[赵敏]是杀害金毛狮王的真凶,[张无忌]不相信[赵敏]会如此心狠手辣。\n", | |
"[周芷若]说自己亲眼看见[赵敏]杀害了金毛狮王,让[张无忌]有所动摇,于是[张无忌]来到蒙古,找到了[赵敏]。\n", | |
"\n", | |
"现在,你模拟开始推演,从头开始推演。\n", | |
"\"\"\"\n", | |
"\n", | |
"# 生成初始对话用的模板\n", | |
"DialogEngineInit = \"\"\"你是一个对话引擎。\n", | |
"你从【世界】提取世界当前的状态和当前人物的状态,解析后生成你扮演的人物的话语,引导用户扮演的人物与你对话。\n", | |
"\n", | |
"\n", | |
"你生成你扮演人物的话语示例:\n", | |
"【对话】\n", | |
"张无忌:(大怒)赵敏,周芷若说你杀死了金毛狮王,这件事是不是你做的?\n", | |
"\n", | |
"你扮演张无忌,我扮演赵敏,你先以张无忌身份向我发起对话。\n", | |
"\"\"\"\n", | |
"\n", | |
"# 更新世界状态用的模板\n", | |
"WorldEngine = \"\"\"请读取上文的对话历史,推演生成所有人物的行动,更新世界推演引擎的状态。\n", | |
"\n", | |
"请你输出世界引擎推演的描述:\n", | |
"【世界】\n", | |
"\"\"\"\n", | |
"\n", | |
"# 更新对话内容的模板\n", | |
"DialogEngine = \"\"\"你继续从【世界】提取世界当前的状态和当前人物的状态,解析后生成你扮演的人物的话语,引导用户扮演的人物与你对话。\n", | |
"\n", | |
"请你生成你扮演人物的话语:\n", | |
"【对话】\n", | |
"张无忌对赵敏说:\n", | |
"\"\"\"" | |
], | |
"metadata": { | |
"id": "mNw3PWJQ3e2W" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 使用revChatGPT进行开发\n", | |
"##### 后期需要考虑conversation_id过期的问题,最终可能还是需要将前文压缩为摘要的方式" | |
], | |
"metadata": { | |
"id": "yd1gXjiZYz8d" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 安装包,配置环境" | |
], | |
"metadata": { | |
"id": "iQDpya2-xsIp" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!python -m pip install --upgrade revChatGPT\n", | |
"\n", | |
"# import packages\n", | |
"from revChatGPT.V3 import Chatbot\n", | |
"import random\n", | |
"chatbot = Chatbot(api_key=API_KEY, engine=MODEL)" | |
], | |
"metadata": { | |
"id": "h6PG-WfOS5ud" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 加载引擎,初始化会话ID" | |
], | |
"metadata": { | |
"id": "IKUiALNh1MMk" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 初始化会话ID\n", | |
"ConversationID = str(random.randint(100*1000*1000,900*1000*1000))\n", | |
"print(\"ConversationID: \"+ConversationID)\n", | |
"\n", | |
"# 初始化世界状态\n", | |
"response = chatbot.ask(\n", | |
" prompt=WorldEngineInit,\n", | |
" role=\"system\",\n", | |
" convo_id=ConversationID,\n", | |
" )\n", | |
"print(response)" | |
], | |
"metadata": { | |
"id": "_04j63pu2jKe" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 开始多轮对话" | |
], | |
"metadata": { | |
"id": "9MgTvz2o9f2f" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 初始化对话内容\n", | |
"response = chatbot.ask(\n", | |
" prompt=DialogEngineInit,\n", | |
" role=\"system\",\n", | |
" convo_id=ConversationID,\n", | |
" )\n", | |
"print(\"张无忌对赵敏说:\"+response)\n", | |
"\n", | |
"# 开始多轮对话\n", | |
"for i in range(0,3):\n", | |
" response = chatbot.ask(\n", | |
" prompt=input(\"赵敏对张无忌说:\")+DialogEngine,\n", | |
" role=\"user\",\n", | |
" convo_id=ConversationID,\n", | |
" )\n", | |
" print(response)" | |
], | |
"metadata": { | |
"id": "JCUrQnYn72X4" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 更新世界状态(玩家点击更新按钮)" | |
], | |
"metadata": { | |
"id": "R676xYuXla7j" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"response = chatbot.ask(\n", | |
" prompt=\"\"+WorldEngine,\n", | |
" role=\"system\",\n", | |
" convo_id=ConversationID,\n", | |
" )\n", | |
"print(response)" | |
], | |
"metadata": { | |
"id": "sTaW3tpPSu6e" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 再次进行多轮对话" | |
], | |
"metadata": { | |
"id": "3O6szOCvlkvs" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 初始化对话内容\n", | |
"response = chatbot.ask(\n", | |
" prompt=\"\"+DialogEngine,\n", | |
" role=\"system\",\n", | |
" convo_id=ConversationID,\n", | |
" )\n", | |
"print(response)\n", | |
"\n", | |
"# 开始多轮对话\n", | |
"for i in range(0,3):\n", | |
" response = chatbot.ask(\n", | |
" prompt=input(\"赵敏对张无忌说:\")+DialogEngine,\n", | |
" role=\"user\",\n", | |
" convo_id=ConversationID,\n", | |
" )\n", | |
" print(response)" | |
], | |
"metadata": { | |
"id": "WV48MKuNYvhl" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 更新世界状态(玩家再次点击更新按钮)" | |
], | |
"metadata": { | |
"id": "cYU3wFWglvwp" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"response = chatbot.ask(\n", | |
" prompt=\"\"+WorldEngine,\n", | |
" role=\"system\",\n", | |
" convo_id=ConversationID,\n", | |
" )\n", | |
"print(response)" | |
], | |
"metadata": { | |
"id": "JIOl2RTJY3cd" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "_LryPyI1Y3fX" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "PEHXP4EMY3iZ" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "AaKOzzIjY3k8" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"# 使用openai官方包 \n", | |
"##### history需要第三方包,比如LangChain进行重复发送" | |
], | |
"metadata": { | |
"id": "IwWeS6KWYlQf" | |
} | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 安装包" | |
], | |
"metadata": { | |
"id": "12nEOlrcxBTx" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "mZz2buCO1r36" | |
}, | |
"outputs": [], | |
"source": [ | |
"# if needed, install and/or upgrade to the latest version of the OpenAI Python library\n", | |
"%pip install --upgrade openai" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 加载引擎,启动" | |
], | |
"metadata": { | |
"id": "WMbMepDfxJxS" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# import the OpenAI Python library for calling the OpenAI API\n", | |
"import openai\n", | |
"openai.api_key=API_KEY" | |
], | |
"metadata": { | |
"id": "XwCfcXdq3cAh" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 一次性加载所有引擎\n", | |
"response = openai.ChatCompletion.create(\n", | |
" model=MODEL,\n", | |
" messages=[\n", | |
" {\"role\": \"system\", \"content\": hint_everything},\n", | |
" ],\n", | |
" temperature=0,\n", | |
")\n", | |
"print(response)\n", | |
"print(response['choices'][0]['message']['content'])" | |
], | |
"metadata": { | |
"id": "fxMK0NJSCz5x" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"### 开启多轮对话" | |
], | |
"metadata": { | |
"id": "F1Bgbn_yxQVx" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"response = openai.ChatCompletion.create(\n", | |
" model=MODEL,\n", | |
" messages=[\n", | |
" {\"role\": \"user\", \"content\": \"赵敏对张无忌说:我没有\"},\n", | |
" {\"role\": \"system\", \"content\": \"你是对话引擎,扮演张无忌\"},\n", | |
" ],\n", | |
" temperature=0,\n", | |
")\n", | |
"print(response)\n", | |
"\n", | |
"last_dialog = response['choices'][0]['message']['content']\n", | |
"dialogs = last_dialog\n", | |
"print(last_dialog)" | |
], | |
"metadata": { | |
"id": "00gmcmg6G7ri" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"response = openai.ChatCompletion.create(\n", | |
" model=MODEL,\n", | |
" messages=[\n", | |
" {\"role\": \"user\", \"content\": \"赵敏对张无忌说:你去峨眉派找她当面对峙就知道真相了\"},\n", | |
" {\"role\": \"system\", \"content\": \"你是对话引擎,扮演张无忌\"},\n", | |
" ],\n", | |
" temperature=0,\n", | |
")\n", | |
"print(response)\n", | |
"\n", | |
"last_dialog = response['choices'][0]['message']['content']\n", | |
"dialogs += last_dialog\n", | |
"print(dialogs)" | |
], | |
"metadata": { | |
"id": "RiWUVW_MHd8w" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"response = openai.ChatCompletion.create(\n", | |
" model=MODEL,\n", | |
" messages=[\n", | |
" {\"role\": \"user\", \"content\": \"赵敏对张无忌说:周芷若外表温和,实则蛇蝎心肠\"},\n", | |
" {\"role\": \"system\", \"content\": \"你是对话引擎,扮演张无忌\"},\n", | |
" ],\n", | |
" temperature=0,\n", | |
")\n", | |
"print(response)\n", | |
"\n", | |
"last_dialog = response['choices'][0]['message']['content']\n", | |
"dialogs += last_dialog\n", | |
"print(dialogs)" | |
], | |
"metadata": { | |
"id": "JZTVLc0PH6y2" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"response = openai.ChatCompletion.create(\n", | |
" model=MODEL,\n", | |
" messages=[\n", | |
" {\"role\": \"user\", \"content\": \"\"\"张无忌对赵敏说:(沉思片刻)赵敏,我不相信你会杀害金毛狮王,但是周芷若说的话又让我有些怀疑。你能不能告诉我真相?\n", | |
"赵敏对张无忌说:真相就是,周芷若杀害了金毛狮王。\n", | |
"张无忌对赵敏说:什么?周芷若杀害了金毛狮王?这怎么可能?你有什么证据吗?\n", | |
"赵敏对张无忌说:你去峨眉派找她当面对峙就知道真相了\"\"\"},\n", | |
" {\"role\": \"system\", \"content\": \"你是世界引擎,请根据对话引擎生成后续剧情\"},\n", | |
" ],\n", | |
" temperature=0,\n", | |
")\n", | |
"print(response)\n", | |
"\n", | |
"last_dialog = response['choices'][0]['message']['content']\n", | |
"dialogs += last_dialog\n", | |
"print(dialogs)" | |
], | |
"metadata": { | |
"id": "QYb7dR6hJC2G" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "WO-dJTb48Sb5" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment