| 51 | pass |
| 52 | # define a side bar for the setting of the chatbot, such as the max token length, temperature, api_key, base_url, system prompt, etc. |
| 53 | def side_bar(): |
| 54 | st.sidebar.title("设置") |
| 55 | state = st.session_state |
| 56 | # Set a form of the settings |
| 57 | with st.sidebar.form(key="settings"): |
| 58 | # Set the max token length for the chatbot |
| 59 | max_tokens = st.number_input("最大token长度", min_value=0, max_value=2048, value=100, step=1) |
| 60 | # Set the temperature for the chatbot |
| 61 | temperature = st.number_input("Temperature", min_value=0.0, max_value=1.0, value=0.0, step=0.01) |
| 62 | # Set the api key for the OpenAI API |
| 63 | api_key = st.text_input("API Key", value="internlm2") |
| 64 | # Set the base url for the OpenAI API |
| 65 | base_url = st.text_input("Base URL",value="http://0.0.0.0:23333/v1") |
| 66 | # Set the system prompt for the chatbot |
| 67 | system_prompt = st.text_area("系统提示", value="") |
| 68 | # Add a submit button to the form |
| 69 | submit = st.form_submit_button("保存设置") |
| 70 | # If the submit button is pressed, save the settings |
| 71 | if submit: |
| 72 | if max_tokens != 0: |
| 73 | state.max_tokens = max_tokens |
| 74 | state.temperature = temperature |
| 75 | state.api_key = api_key |
| 76 | state.base_url = base_url |
| 77 | state.message_history = [] |
| 78 | if system_prompt != "": |
| 79 | state.system_prompt = system_prompt |
| 80 | state.message_history.append({"role": "system", "content": system_prompt}) |
| 81 | state.client = OpenAI(api_key=state.api_key, base_url=state.base_url) |
| 82 | pass |
| 83 | if st.sidebar.button("开启新对话"): |
| 84 | if not os.path.exists("chat_history"): |
| 85 | os.mkdir("chat_history") |
| 86 | pass |
| 87 | with open(f"chat_history/{time.time()}.json", "w") as f: |
| 88 | json.dump(state.message_history, f, ensure_ascii=False) |
| 89 | pass |
| 90 | state.message_history = [] |
| 91 | st.rerun() |
| 92 | |
| 93 | pass |
| 94 | |
| 95 | if __name__ == "__main__": |
| 96 | side_bar() |