ImaGetCha

Shellmiao 5800128b75 Update example output 1 year ago
config 5800128b75 Update example output 1 year ago
modules 0c35700634 Update example, README 1 year ago
non_module_old bf6f7825e6 Imagetcha modolar version 1 year ago
old bf6f7825e6 Imagetcha modolar version 1 year ago
utils b15d68489a Update README,Optimize format prompt,judge module 1 year ago
.gitignore 30f23c4caf command line interaction 1 year ago
README.md 0c35700634 Update example, README 1 year ago
main_module.py b15d68489a Update README,Optimize format prompt,judge module 1 year ago
requirement.txt 58745042ac update readme 1 year ago

README.md

ImaGetCha

环境配置

  • 安装python=3.9

  • 安装python依赖库

  pip install -r requirement.txt

使用

python .\main_module.py -h
---usage: main_module.py [-h] -n NAME -t TYPE -c CLASSNAME -i ID

---生成文字游戏

---optional arguments:
---  -h, --help            show this help message and exit
---  -n NAME, --name NAME  config文件夹中配置的文件夹名称
---  -t TYPE, --type TYPE  开始的第一个组件的类型
---  -c CLASSNAME, --classname CLASSNAME
---                        开始的第一个组件的classname
---  -i ID, --id ID        开始的第一个组件的id

python .\main_module.py -n example -t Chat -c input -i 1

重新开始需要删除游戏配置文件中的module_output文件夹中的内容,其中保存了游戏进度; 若不删除,建议根据进度自行修改启动命令中的-t Chat -c input -i 1部分,使之在正确的组件上启动

配置介绍

通过配置config中的json文件来配置模型结构,下面使用一个例子来介绍如何配置运行结构

组件类型

Chat

需要配置的属性及其作用:

  1. class: 标识符,代表完成同类功能,且维护同一输出文件的chat组件

  2. id: 标识符,每个组件在同一class中的标识符,用于区分同class内不同组件

  3. input: 输入的数据来源,为{key:value}格式。程序会在运行过程中将value路径对应数据读取,并保存为key=json.load(value)的形式,可在后续过程中被调用

使用示例( json文件均可用此种方法读取到程序运行过程中 ):

   "input": {
       "user_input": "user_input.json",
       "input_module_prompt":"prompt/input.json",
       "json_declare":"prompt/json_declare.json"
   }
  1. prompt: 输入chat模型的template模板,结构固定如下
   //prompt/input.json
   {
       "system_template": "系统消息,可以在其中加入{占位符},必须填写;会被自动填入prompt中",
       "human_template": "用户消息,可以在其中加入{占位符},必须填写;会被自动填入prompt中",
       "其他key": "其他value, 不会被自动当成prompt加入到chat_module中,但可以被当成正常的input读取"
   }
  1. prompt_input: 定义输入到prompt中的占位符变量,为{key:value}格式。遵循python语法

使用示例:

  1. "style":"user_input['style']":user_input.json(在input属性中定义的user_input对应的json文件)中"style"对应的value。在这里定义后即可在template中使用{style}占位符表示
  2. "interact":"interact['class_output_data'][-1]":module_output/Interact_choose_output.json(在input属性中定义的interact对应的json文件)中"class_output_data"对应的数组的最后一个元素
  3. "event":"event['class_output_data'][-4:]":module_output/GPT_event_output.json(在input属性中定义的event对应的json文件)中"class_output_data"对应的数组的最后四个元素组成的数组

  4. output: 从以下选择填入(目前支持这些,后续再更新)

    1. "{class_output_data}":默认输出到moduleoutput/chat{classname}_output.json中,并且每次在输出时采取覆盖操作,不保留之前的数据;输出得到的json格式与chat_module输出相同
      {chat_module_output}
    
    1. "{class_output_data_append}":默认输出到moduleoutput/chat{classname}_output.json中,并且每次在输出时采取添加操作;输出得到的json格式为
      {
        "class_output_data": [
            ...previous_data,{chat_module_output}
        ]
      }
    
  5. next_module: 运行此组件完成后的下一个组件,固定格式如下

   "next_module": {
   	"type": "Chat"/"Judgement"/"Interact",
       "class": "classname",
       "id": id
   }

若无下一个组件,则填入

   "next_module": {
   	"type": "End"
   }

Judgement

需要配置的属性及其作用:

  1. class: 标识符,代表完成同类功能,且维护同一输出文件的chat组件
  2. id: 标识符,每个组件在同一class中的标识符,用于区分同class内不同组件
  3. input: 输入的数据来源,为{key:value}格式。 与Chat中的input作用相同
  4. judgement_input: 定义输入到judgement中的变量,为{key:value}格式。遵循python语法,与prompt_input使用方法相同
  5. value: 判断标准,经过python运行后输出为Boolean值的表达式字符串,示例:
    1. "event['class_output_data'][-1]['是否需要玩家选择下一步操作']": 此变量本身为Boolean值
    2. "value['value']['生命值']<10": 判断语句
  6. if_true_next: 格式与next_module相同
  7. if_false_next: 格式与next_module相同

Interact

需要配置的属性及其作用:

  1. class: 标识符,代表完成同类功能,且维护同一输出文件的chat组件
  2. id: 标识符,每个组件在同一class中的标识符,用于区分同class内不同组件
  3. input: 输入的数据来源,为{key:value}格式。 与Chat中的input作用相同
  4. choices: 非{key:value}格式,仅指向单个变量,需要读取后为数组。示例: "event['class_output_data'][-1]['玩家可以做出的选择']"
  5. output: 与Chat中的output相同
  6. next_module: 与Chat中的next_module相同

Example

{
    "Chat": [
        {
            "class": "input",
            "id": 1,
            "input": {
                "user_input": "user_input.json",
                "input_module_prompt":"prompt/input.json",
                "json_declare":"prompt/json_declare.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "declare":"input_module_prompt['declare']",
                "json_declare":"json_declare['json_declare']"
            },
            "prompt": "prompt/input.json",
            "output": "{class_output_data}",
            "next_module": {
                "type": "Chat",
                "class": "value",
                "id": 1
            }
        },
        {
            "class": "value",
            "id": 1,
            "input": {
                "intro": "module_output/Chat_input_output.json",
                "value_1_module_prompt":"prompt/value_1.json"
            },
            "prompt_input":{
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "output_type":"value_1_module_prompt['output_type']"
            },
            "prompt": "prompt/value_1.json",
            "output": "{class_output_data}",
            "next_module": {
                "type": "Chat",
                "class": "event",
                "id": 1
            }
        },
        {
            "class": "event",
            "id": 1,
            "input": {
                "user_input": "user_input.json",
                "event_module_prompt":"prompt/event_1.json",
                "intro": "module_output/Chat_input_output.json",
                "value": "module_output/Chat_value_output.json",
                "json_declare":"prompt/json_declare.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "output_type":"event_module_prompt['output_type']",
                "json_declare":"json_declare['json_declare']",
                "value":"value['value']"
            },
            "prompt": "prompt/event_1.json",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Chat",
                "class": "value",
                "id": 2
            }
        },
        {
            "class": "value",
            "id": 2,
            "input": {
                "intro": "module_output/Chat_input_output.json",
                "value_1_module_prompt":"prompt/value_1.json",
                "value": "module_output/Chat_value_output.json",
                "event": "module_output/Chat_event_output.json"
            },
            "prompt": "prompt/value_2.json",
            "prompt_input":{
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "event":"event['class_output_data'][-4:]",
                "value":"value['value']",
                "output_type":"value_1_module_prompt['output_type']"
            },
            "output": "{class_output_data}",
            "next_module": {
                "type": "Judgement",
                "class": "Boolean",
                "id": 1
            }
        },
        {
            "class": "event",
            "id": 2,
            "input": {
                "user_input": "user_input.json",
                "event_module_prompt":"prompt/event_2.json",
                "intro": "module_output/Chat_input_output.json",
                "value": "module_output/Chat_value_output.json",
                "json_declare":"prompt/json_declare.json",
                "event": "module_output/Chat_event_output.json",
                "interact":"module_output/Interact_choose_output.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "output_type":"event_module_prompt['output_type']",
                "json_declare":"json_declare['json_declare']",
                "value":"value['value']",
                "event":"event['class_output_data'][-4:]",
                "interact":"interact['class_output_data'][-1]"
            },
            "prompt": "prompt/event_2.json",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Judgement",
                "class": "Boolean",
                "id": 2
            }
        },
        {
            "class": "event",
            "id": 3,
            "input": {
                "user_input": "user_input.json",
                "event_module_prompt":"prompt/event_3.json",
                "intro": "module_output/Chat_input_output.json",
                "value": "module_output/Chat_value_output.json",
                "json_declare":"prompt/json_declare.json",
                "event": "module_output/Chat_event_output.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "output_type":"event_module_prompt['output_type']",
                "json_declare":"json_declare['json_declare']",
                "value":"value['value']",
                "event":"event['class_output_data'][-4:]"
            },
            "prompt": "prompt/event_3.json",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Judgement",
                "class": "Boolean",
                "id": 2
            }
        }
    ],
    "Judgement": [
        {
            "class": "Boolean",
            "id": 1,
            "input": {
                "event": "module_output/Chat_event_output.json"
            },
            "judgement_input":{
                "last_event":"event['class_output_data'][-1]"
            },
            "value":"last_event['是否需要玩家选择下一步操作']",
            "if_true_next": {
                "type": "Interact",
                "class": "choose",
                "id": 1
            },
            "if_false_next": {
                "type": "Chat",
                "class": "event",
                "id": 3
            }
        },
        {
            "class": "Boolean",
            "id": 2,
            "input": {
                "event": "module_output/Chat_event_output.json"
            },
            "judgement_input":{
                "last_event":"event['class_output_data'][-1]"
            },
            "value":"last_event['游戏是否结束']",
            "if_true_next": {"type":"End"},
            "if_false_next": {
                "type": "Chat",
                "class": "value",
                "id": 2
            }
        }
    ],
    "Interact": [
        {
            "class": "choose",
            "id": 1,
            "input": {
                "event": "module_output/Chat_event_output.json"
            },
            "choices": "event['class_output_data'][-1]['玩家可以做出的选择']",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Chat",
                "class": "event",
                "id": 2
            }
        }
    ]
}

example

Example_with_outline

{
    "Chat": [
        {
            "class": "input",
            "id": 1,
            "input": {
                "user_input": "user_input.json",
                "input_module_prompt":"prompt/input.json",
                "json_declare":"prompt/json_declare.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "declare":"input_module_prompt['declare']",
                "json_declare":"json_declare['json_declare']"
            },
            "prompt": "prompt/input.json",
            "output": "{class_output_data}",
            "next_module": {
                "type": "Chat",
                "class": "outline",
                "id": 1
            }
        },
        {
            "class": "outline",
            "id": 1,
            "input": {
                "user_input": "user_input.json",
                "intro": "module_output/Chat_input_output.json",
                "outline_module_prompt":"prompt/outline.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "output_type":"outline_module_prompt['output_type']"
            },
            "prompt": "prompt/outline.json",
            "output": "{class_output_data}",
            "next_module": {
                "type": "Chat",
                "class": "value",
                "id": 1
            }
        },
        {
            "class": "value",
            "id": 1,
            "input": {
                "intro": "module_output/Chat_input_output.json",
                "value_1_module_prompt":"prompt/value_1.json"
            },
            "prompt_input":{
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "output_type":"value_1_module_prompt['output_type']"
            },
            "prompt": "prompt/value_1.json",
            "output": "{class_output_data}",
            "next_module": {
                "type": "Chat",
                "class": "event",
                "id": 1
            }
        },
        {
            "class": "event",
            "id": 1,
            "input": {
                "user_input": "user_input.json",
                "event_module_prompt":"prompt/event_1.json",
                "intro": "module_output/Chat_input_output.json",
                "value": "module_output/Chat_value_output.json",
                "outline":"module_output/Chat_outline_output.json",
                "json_declare":"prompt/json_declare.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "outline":"outline['outline']",
                "output_type":"event_module_prompt['output_type']",
                "json_declare":"json_declare['json_declare']",
                "value":"value['value']"
            },
            "prompt": "prompt/event_1.json",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Chat",
                "class": "value",
                "id": 2
            }
        },
        {
            "class": "value",
            "id": 2,
            "input": {
                "intro": "module_output/Chat_input_output.json",
                "value_1_module_prompt":"prompt/value_1.json",
                "value": "module_output/Chat_value_output.json",
                "event": "module_output/Chat_event_output.json"
            },
            "prompt": "prompt/value_2.json",
            "prompt_input":{
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "event":"event['class_output_data'][-4:]",
                "value":"value['value']",
                "output_type":"value_1_module_prompt['output_type']"
            },
            "output": "{class_output_data}",
            "next_module": {
                "type": "Judgement",
                "class": "Boolean",
                "id": 1
            }
        },
        {
            "class": "event",
            "id": 2,
            "input": {
                "user_input": "user_input.json",
                "event_module_prompt":"prompt/event_2.json",
                "intro": "module_output/Chat_input_output.json",
                "value": "module_output/Chat_value_output.json",
                "json_declare":"prompt/json_declare.json",
                "event": "module_output/Chat_event_output.json",
                "outline":"module_output/Chat_outline_output.json",
                "interact":"module_output/Interact_choose_output.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "outline":"outline['outline']",
                "output_type":"event_module_prompt['output_type']",
                "json_declare":"json_declare['json_declare']",
                "value":"value['value']",
                "event":"event['class_output_data'][-4:]",
                "interact":"interact['class_output_data'][-1]"
            },
            "prompt": "prompt/event_2.json",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Judgement",
                "class": "Boolean",
                "id": 2
            }
        },
        {
            "class": "event",
            "id": 3,
            "input": {
                "user_input": "user_input.json",
                "event_module_prompt":"prompt/event_3.json",
                "intro": "module_output/Chat_input_output.json",
                "value": "module_output/Chat_value_output.json",
                "json_declare":"prompt/json_declare.json",
                "event": "module_output/Chat_event_output.json",
                "outline":"module_output/Chat_outline_output.json"
            },
            "prompt_input":{
                "style":"user_input['style']",
                "story":"user_input['story']",
                "intro":"intro['故事简介']",
                "npc":"intro['角色设定']",
                "value_system":"intro['数值系统']",
                "target":"intro['游戏通关所需条件']",
                "outline":"outline['outline']",
                "output_type":"event_module_prompt['output_type']",
                "json_declare":"json_declare['json_declare']",
                "value":"value['value']",
                "event":"event['class_output_data'][-4:]"
            },
            "prompt": "prompt/event_3.json",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Judgement",
                "class": "Boolean",
                "id": 2
            }
        }
    ],
    "Judgement": [
        {
            "class": "Boolean",
            "id": 1,
            "input": {
                "event": "module_output/Chat_event_output.json"
            },
            "judgement_input":{
                "last_event":"event['class_output_data'][-1]"
            },
            "value":"last_event['是否需要玩家选择下一步操作']",
            "if_true_next": {
                "type": "Interact",
                "class": "choose",
                "id": 1
            },
            "if_false_next": {
                "type": "Chat",
                "class": "event",
                "id": 3
            }
        },
        {
            "class": "Boolean",
            "id": 2,
            "input": {
                "event": "module_output/Chat_event_output.json"
            },
            "judgement_input":{
                "last_event":"event['class_output_data'][-1]"
            },
            "value":"last_event['游戏是否结束']",
            "if_true_next": {"type":"End"},
            "if_false_next": {
                "type": "Chat",
                "class": "value",
                "id": 2
            }
        }
    ],
    "Interact": [
        {
            "class": "choose",
            "id": 1,
            "input": {
                "event": "module_output/Chat_event_output.json"
            },
            "choices": "event['class_output_data'][-1]['玩家可以做出的选择']",
            "output": "{class_output_data_append}",
            "next_module": {
                "type": "Chat",
                "class": "event",
                "id": 2
            }
        }
    ]
}

未命名文件 (3)

TODO

  • 支持从上次中断处继续
  • 主程序交互优化
  • 数据处理module
  • 更多演示case
  • Debug/Info logger