Skip to content

Commit 4994bfa

Browse files
committed
add reset key for chatgpt demo
1 parent 6bed925 commit 4994bfa

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

demos/chatgpt.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ def messages(self) -> List[Dict]:
127127
return self._messages
128128

129129

130+
def get_openai_config():
131+
openai_config = json.loads(pywebio_battery.get_localstorage('openai_config') or '{}')
132+
if not openai_config:
133+
openai_config = input_group('OpenAI API Config', [
134+
input('API Key', name='api_key', type=TEXT, required=True,
135+
help_text='Get your API key from https://platform.openai.com/account/api-keys'),
136+
input('API Server', name='api_base', type=TEXT, value='https://api.openai.com', required=True),
137+
])
138+
openai_config['api_base'] = openai_config['api_base'].removesuffix('/v1').strip('/') + '/v1'
139+
pywebio_battery.set_localstorage('openai_config', json.dumps(openai_config))
140+
141+
put_button('Reset OpenAI API Key', reset_openai_config, link_style=True)
142+
return openai_config
143+
144+
145+
def reset_openai_config():
146+
pywebio_battery.set_localstorage('openai_config', json.dumps(None))
147+
toast("Please refresh the page to take effect")
148+
149+
130150
def main():
131151
""""""
132152
set_env(input_panel_fixed=False, output_animation=False)
@@ -135,20 +155,11 @@ def main():
135155
A ChatGPT client implemented with PyWebIO. [Source Code](https://github.com/pywebio/PyWebIO/blob/dev/demos/chatgpt.py)
136156
TIPS: refresh page to open a new chat.
137157
""")
138-
139158
put_select('model', ['gpt-3.5-turbo', 'gpt-4'], label='Model')
140159

141-
openai_config = json.loads(pywebio_battery.get_localstorage('openai_config') or '{}')
142-
if not openai_config:
143-
openai_config = input_group('OpenAI API Config', [
144-
input('API Key', name='api_key', type=TEXT, required=True,
145-
help_text='Get your API key from https://platform.openai.com/account/api-keys'),
146-
input('API Server', name='api_base', type=TEXT, value='https://api.openai.com', required=True),
147-
])
148-
pywebio_battery.set_localstorage('openai_config', json.dumps(openai_config))
160+
openai_config = get_openai_config()
149161

150-
api_base = openai_config['api_base'].removesuffix('/v1').strip('/') + '/v1'
151-
bot = ChatGPT(api_key=openai_config['api_key'], api_base=api_base, model=pin.model)
162+
bot = ChatGPT(api_key=openai_config['api_key'], api_base=openai_config['api_base'], model=pin.model)
152163
while True:
153164
form = input_group('', [
154165
input(name='msg', placeholder='Ask ChatGPT'),
@@ -194,4 +205,4 @@ def main():
194205
if __name__ == '__main__':
195206
from pywebio import start_server
196207

197-
start_server(main, port=8085, debug=True, cdn=False)
208+
start_server(main, port=8080, debug=True, cdn=False)

demos/index.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pywebio.session import info as session_info
55

66
index_md = r"""### Basic demo
7+
The source code of the demos can be found [here](https://github.com/pywebio/PyWebIO/tree/dev/demos).
78
89
- [BMI calculation](./bmi): Calculating Body Mass Index based on height and weight
910
- [Online chat room](./chat_room): Chat with everyone currently online (using less than 90 lines of code)
@@ -50,6 +51,8 @@
5051

5152
index_md_zh = r"""### 基本demo
5253
54+
Demo源码[链接](https://github.com/pywebio/PyWebIO/tree/dev/demos)
55+
5356
- [BMI计算](./bmi): 根据身高体重计算BMI指数
5457
- [聊天室](./chat_room): 和当前所有在线的人聊天 (不到90行代码实现)
5558
- [Markdown实时预览](./markdown_previewer): 可以实时预览的在线Markdown编辑器 (不到40行代码实现)

0 commit comments

Comments
 (0)