main.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. from typing import List, Tuple
  3. from langchain.chains import ConversationChain
  4. from langchain.chat_models import ChatOpenAI
  5. from log_callback_handler import NiceGuiLogElementCallbackHandler
  6. from nicegui import context, ui
  7. OPENAI_API_KEY = 'not-set' # TODO: set your OpenAI API key here
  8. @ui.page('/')
  9. def main():
  10. llm = ConversationChain(llm=ChatOpenAI(model_name='gpt-3.5-turbo', openai_api_key=OPENAI_API_KEY))
  11. messages: List[Tuple[str, str]] = []
  12. thinking: bool = False
  13. @ui.refreshable
  14. def chat_messages() -> None:
  15. for name, text in messages:
  16. ui.chat_message(text=text, name=name, sent=name == 'You')
  17. if thinking:
  18. ui.spinner(size='3rem').classes('self-center')
  19. if context.get_client().has_socket_connection:
  20. ui.run_javascript('window.scrollTo(0, document.body.scrollHeight)')
  21. async def send() -> None:
  22. nonlocal thinking
  23. message = text.value
  24. messages.append(('You', text.value))
  25. thinking = True
  26. text.value = ''
  27. chat_messages.refresh()
  28. response = await llm.arun(message, callbacks=[NiceGuiLogElementCallbackHandler(log)])
  29. messages.append(('Bot', response))
  30. thinking = False
  31. chat_messages.refresh()
  32. anchor_style = r'a:link, a:visited {color: inherit !important; text-decoration: none; font-weight: 500}'
  33. ui.add_head_html(f'<style>{anchor_style}</style>')
  34. # the queries below are used to expand the contend down to the footer (content can then use flex-grow to expand)
  35. ui.query('.q-page').classes('flex')
  36. ui.query('.nicegui-content').classes('w-full')
  37. with ui.tabs().classes('w-full') as tabs:
  38. chat_tab = ui.tab('Chat')
  39. logs_tab = ui.tab('Logs')
  40. with ui.tab_panels(tabs, value=chat_tab).classes('w-full max-w-2xl mx-auto flex-grow items-stretch'):
  41. with ui.tab_panel(chat_tab).classes('items-stretch'):
  42. chat_messages()
  43. with ui.tab_panel(logs_tab):
  44. log = ui.log().classes('w-full h-full')
  45. with ui.footer().classes('bg-white'), ui.column().classes('w-full max-w-3xl mx-auto my-6'):
  46. with ui.row().classes('w-full no-wrap items-center'):
  47. placeholder = 'message' if OPENAI_API_KEY != 'not-set' else \
  48. 'Please provide your OPENAI key in the Python script first!'
  49. text = ui.input(placeholder=placeholder).props('rounded outlined input-class=mx-3') \
  50. .classes('w-full self-center').on('keydown.enter', send)
  51. ui.markdown('simple chat app built with [NiceGUI](https://nicegui.io)') \
  52. .classes('text-xs self-end mr-8 m-[-1em] text-primary')
  53. ui.run(title='Chat with GPT-3 (example)')