Skip to content

Commit

Permalink
add reset key for chatgpt demo
Browse files Browse the repository at this point in the history
  • Loading branch information
wang0618 committed Aug 3, 2023
1 parent 6bed925 commit 4994bfa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
35 changes: 23 additions & 12 deletions demos/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ def messages(self) -> List[Dict]:
return self._messages


def get_openai_config():
openai_config = json.loads(pywebio_battery.get_localstorage('openai_config') or '{}')
if not openai_config:
openai_config = input_group('OpenAI API Config', [
input('API Key', name='api_key', type=TEXT, required=True,
help_text='Get your API key from https://platform.openai.com/account/api-keys'),
input('API Server', name='api_base', type=TEXT, value='https://api.openai.com', required=True),
])
openai_config['api_base'] = openai_config['api_base'].removesuffix('/v1').strip('/') + '/v1'
pywebio_battery.set_localstorage('openai_config', json.dumps(openai_config))

put_button('Reset OpenAI API Key', reset_openai_config, link_style=True)
return openai_config


def reset_openai_config():
pywebio_battery.set_localstorage('openai_config', json.dumps(None))
toast("Please refresh the page to take effect")


def main():
""""""
set_env(input_panel_fixed=False, output_animation=False)
Expand All @@ -135,20 +155,11 @@ def main():
A ChatGPT client implemented with PyWebIO. [Source Code](https://github.com/pywebio/PyWebIO/blob/dev/demos/chatgpt.py)
TIPS: refresh page to open a new chat.
""")

put_select('model', ['gpt-3.5-turbo', 'gpt-4'], label='Model')

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

api_base = openai_config['api_base'].removesuffix('/v1').strip('/') + '/v1'
bot = ChatGPT(api_key=openai_config['api_key'], api_base=api_base, model=pin.model)
bot = ChatGPT(api_key=openai_config['api_key'], api_base=openai_config['api_base'], model=pin.model)
while True:
form = input_group('', [
input(name='msg', placeholder='Ask ChatGPT'),
Expand Down Expand Up @@ -194,4 +205,4 @@ def main():
if __name__ == '__main__':
from pywebio import start_server

start_server(main, port=8085, debug=True, cdn=False)
start_server(main, port=8080, debug=True, cdn=False)
3 changes: 3 additions & 0 deletions demos/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pywebio.session import info as session_info

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

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

0 comments on commit 4994bfa

Please sign in to comment.