File size: 6,770 Bytes
74cb225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import sys
import os
abs_path = os.getcwd()
sys.path.append(abs_path) # Adds higher directory to python modules path.

import streamlit as st
from modules.get_modules import get_modules
from modules.background import gen_background
from modules.command import gen_command
from modules.constraints import gen_constraints
from modules.goal import gen_goal
from modules.initialization import gen_initialization
from modules.output_format import gen_output_format
from modules.skills import gen_skills
from modules.suggestion import gen_suggestion
from modules.workflow import gen_workflow

module_name_dict = {
    "background": "背景",
    "command": "命令",
    "suggesstion": "建议",
    "goal": "目标",
    "examples": "任务样例",
    "constraints": "约束",
    "workflow": "工作流程",
    "output_format": "输出格式",
    "skills": "技能",
    "style": "风格",
    "initialization": "初始化"
}

module_func_dict = {
    "background": gen_background,
    "command": gen_command,
    "suggesstion": gen_suggestion,
    "goal": gen_goal,
    "examples": None,
    "constraints": gen_constraints,
    "workflow": gen_workflow,
    "output_format": gen_output_format,
    "skills": gen_skills,
    "style": None,
    "initialization": gen_initialization
}

## The page to generate the LangGPT prompt
def generate():
    state = st.session_state
    ## A text input for the user to input the basic description of the task
    col1, col2 = st.columns([8, 2])
    with col1:
        task = st.text_input("任务描述","撰写科幻小说",label_visibility="collapsed")
        pass
    ## A button to analyze the task and generate the modules
    with col2:
        if st.button("分析任务",type="primary"):
            ## Get the modules
            state.module_messages = [{"role": "user", "content": f"我希望LLM帮我执行的任务是:{task}"}]
            state.modules = get_modules(state.generator, state.module_messages)
            pass
    with st.sidebar:
        st.subheader("基本信息")
        state.role_name = st.text_input("助手名称","",help="例如:大模型、助手等")
        state.author = st.text_input("作者","LangGPT")
        state.version = st.number_input("版本",min_value=0.1,value=0.1,step=0.1)
        state.description = st.text_area("描述","这是一个LangGPT生成的助手",height=100)
        st.subheader("模块控制")
        if "modules" not in state:
            state.modules = {
                "background": False,
                "command": False,
                "suggesstion": False,
                "goal": False,
                "examples": False,
                "constraints": False,
                "workflow": False,
                "output_format": False,
                "skills": False,
                "style": False,
                "initialization": False
            }
        ## Some toggles to show the modules
        if "on_modules" not in state:
            state.on_modules = {}
            pass
        for key in state.modules.keys():
            if key in module_name_dict:
                state.on_modules[key] = st.toggle(module_name_dict[key],state.modules[key])
                pass
            pass
        pass
    if "modules" in state:
        if state.on_modules["examples"]:
            st.subheader("请提供任务样例:")
            input_area, output_area = st.columns(2)
            with input_area:
                input_example = st.text_area("样例输入","")
                pass
            with output_area:
                output_example = st.text_area("样例输出","")
                pass
            state.examples = {
                "input": input_example,
                "output": output_example
            }
            pass
        if state.on_modules["style"]:
            st.subheader("请指定回复的风格:")
            style = st.text_input("风格","",help="例如:正式、幽默、严肃等",label_visibility="collapsed")
            state.style = style
            pass
        ## A button to control the generation of the modules
        for key in state.modules.keys():
            if key in state:
                if state.on_modules[key]:
                    with st.expander(module_name_dict[key]):
                        st.text_area(module_name_dict[key],state[key],label_visibility="collapsed")
                        pass
            pass
        g,c = st.columns([1,1])
        with g:
            generate_button = st.button("生成模块")
            pass
        with c:
            compose_button = st.button("合成提示")
            pass
        if generate_button:
            for key in state.modules.keys():
                if key == "examples" or key == "style":
                    continue
                else:
                    if state.on_modules[key]:
                        if key not in state:
                            state[key] = module_func_dict[key](state.generator,state.module_messages)
                    pass
                pass
            st.rerun()
            pass
        if compose_button:
            if "prompt" not in state:
                state.prompt = ""
                pass
            if state.role_name:
                state.prompt += f"# Role: {state.role_name}\n"
                pass
            state.prompt += f"## profile\n"
            if state.author:
                state.prompt += f"- Author: {state.author}\n"
                pass
            if state.version:
                state.prompt += f"- Version: {state.version}\n"
                pass
            if state.description:
                state.prompt += f"- Description: {state.description}\n"
                pass
            ## Check if all the checked modules are generated
            for key in state.modules.keys():
                if state.on_modules[key]:
                    if key not in state:
                        st.error(f"请先生成{module_name_dict[key]}")
                        return
                    else:
                        if key == "examples":
                            state.prompt += f"## {module_name_dict[key]}\n"
                            state.prompt += f"### 输入\n"
                            state.prompt += state.examples["input"]
                            state.prompt += "\n"
                            state.prompt += f"### 输出\n"
                            state.prompt += state.examples["output"]
                            state.prompt += "\n\n"
                        state.prompt += f"## {key}\n"
                        state.prompt += state[key]
                        state.prompt += "\n\n"
                        state.page = "test"
                pass
            st.rerun()